3

C# 如何部分加载“超大”解决方案中的部分项目

 1 year ago
source link: https://www.newbe.pro/Others/0x020-csharp-how-to-partially-load-some-items-in-a-mega-solution/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

C# 如何部分加载 “超大” 解决方案中的部分项目

2023-02-09 2023-02-10 8

在有的特有的项目环境下,团队会将所有的项目使用同一个解决方案进行管理。这种方式方面了管理,但是却会导致解决方案变得非常庞大,导致加载时间过长。那么,如何部分加载解决方案中的部分项目呢?就让我们来借用微软退出的
slngen 工具来体验一下部分加载解决方案中的部分项目吧。

slngen 从根项目生成临时解决方案

SlnGen 是一个 Visual Studio 解决方案文件生成器。Visual Studio 解决方案对于大型项目树来说通常不能很好地扩展。SlnGen 读取一个给定项目的项目引用,按需创建一个 Visual Studio 解决方案。例如,你可以针对一个单元测试项目运行 SlnGen,并呈现一个包含单元测试项目及其所有项目引用的 Visual Studio 解决方案。你也可以针对一个有根的文件夹中的遍历项目运行 SlnGen,打开一个包含你的项目树的那个视图的 Visual Studio 解决方案。

安装 slngen

dotnet tool install --global Microsoft.VisualStudio.SlnGen.Tool --add-source https://api.nuget.org/v3/index.json --ignore-failed-sources

运行以上命令,你就可以在全局安装 slngen 工具了。然后,你就可以在任何地方使用 slngen 命令了。

slngen --help

为所有的项目引入 Microsoft.VisualStudio.SlnGen

在你的项目树中,你需要为所有的项目引入 Microsoft.VisualStudio.SlnGen 包。可以通过 Directory.Build.props 来轻松实现。

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.SlnGen" Version="9.5.2" />
</ItemGroup>

准备一个临时的测试项目

为了方便演示,我们创建三个项目,分别是 slngen-demo、slngen-demo-a、slngen-demo-b。

其中,slngen-demo-a 和 slngen-demo-b 项目都引用了 slngen-demo 项目。

mkdir slngen-demo
cd slngen-demo
dotnet new classlib -o slngen-demo
dotnet new console -o slngen-demo-a
dotnet new console -o slngen-demo-b
cd slngen-demo-a
dotnet add reference ../slngen-demo/slngen-demo.csproj
cd ../slngen-demo-b
dotnet add reference ../slngen-demo/slngen-demo.csproj

文件夹结构大致如下:

C:\REPOS\SLNGEN-DEMO
│ Directory.Build.props

├─slngen-demo
│ │ Class1.cs
│ │ slngen-demo.csproj
│ │
│ ├─bin
│ └─obj

├─slngen-demo-a
│ │ Program.cs
│ │ slngen-demo-a.csproj
│ │
│ ├─bin
│ └─obj

└─slngen-demo-b
│ Program.cs
│ slngen-demo-b.csproj
└─obj

使用 slngen 生成临时解决方案

注意 slngen 是通过驱动 Visual Studio 来生成解决方案的。因此需要在命令行中具备 MSBuild.exe 的路径。

因此我们需要使用 Developer Command Prompt for VS 2022 来运行 slngen 命令。

我们来使用 slngen 加载 slngen-demo-a 项目。

slngen slngen-demo-a/slngen-demo-a.csproj

通过以上命令,我们就使用 slngen 加载了 slngen-demo-a 项目。

这种方式可以加载 slngen-demo-a 项目和 slngen-demo,但是 slngen-demo-b 项目并没有被加载。

运行结果大致如下:

C:\Repos\slngen-demo>slngen slngen-demo-a/slngen-demo-a.csproj
SlnGen version 9.5.2+b19739dfbc for .NET Framework
Copyright (c) Microsoft Corporation. Licensed under the MIT license.

Build started 2/9/2023 8:29:24 PM.
Generating solution for project "C:\Repos\slngen-demo\slngen-demo-a\slngen-demo-a.csproj"
Loading project references...
Loaded 2 project(s) in 840ms
Generating Visual Studio solution "C:\Repos\slngen-demo\slngen-demo-a\slngen-demo-a.sln" ...
Updating existing solution file and reusing Visual Studio cache
Launching Visual Studio...

Success
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:01.33

通过 slngen,我们可以很方便地加载一个项目及其所有的项目引用。这对于我们在 Visual Studio 中打开一个项目树的视图非常有用。可惜 Rider 不得行。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK