gpt4 book ai didi

c# - 在 MSBuild 的解决方案中循环项目

转载 作者:行者123 更新时间:2023-12-05 05:26:48 24 4
gpt4 key购买 nike

在我的工作地点,我们有一个“核心”解决方案,其中包含许多提供不同实用功能的项目,这些功能通过 nuget 打包并发布到内部 nuget 服务器。为了从我们的 CI 中实现 nuget 打包,我们目前有一个如下所示的 MSBuild 脚本:

<Exec Command="nuget pack &quot;$(ProjectRoot)\Domain\Domain.csproj&quot; -Properties Configuration=$(Configuration)" />
<Exec Command="nuget pack &quot;$(ProjectRoot)\Logging\Logging.csproj&quot; -Properties Configuration=$(Configuration)" />
<Exec Command="nuget pack &quot;$(ProjectRoot)\Logging.NLog\Logging.NLog.csproj&quot; -Properties Configuration=$(Configuration)" />
<Exec Command="nuget pack &quot;$(ProjectRoot)\Persistence\Persistence.csproj&quot; -Properties Configuration=$(Configuration)" />
<Exec Command="nuget pack &quot;$(ProjectRoot)\Persistence.NHibernate\Persistence.NHibernate.csproj&quot; -Properties Configuration=$(Configuration)" />

我们有大约 20 个项目以这种方式打包,每次引入要打包的新项目时,我们都必须在 MSBuild 脚本中添加另一行。是否可以使用 MSbuild 枚举解决方案中的项目,从而将此脚本压缩成类似...

<foreach project in solution>
<Exec Command="nuget pack project -Properties Configuration=$(Configuration)" />
</foreach>

此外,我们需要能够询问循环中的每个项目以排除测试项目(这些都在 Tests 目录下,所以应该是微不足道的?)

编辑: Other questions处理在循环中执行 msbuild 任务的问题,但解决方案涉及手动枚举要在 ItemGroup 中循环的项目,因此会导致稍微简单但仍然非常冗长的代码。如果可能,我想避免手动枚举项目。

最佳答案

您可以使用属性 SolutionDirSolutionName 来定位解决方案文件,然后编写自定义目标以查找包含在您的解决方案中的所有项目。我是这样做的:

<Target Name="AfterBuild" DependsOnTargets="PublishNuGetPackages" />
<Target Name="PublishNuGetPackages">
<CollectNuGetProjects SolutionDir="$(SolutionDir)" SolutionName="$(SolutionName)">
<Output TaskParameter="NuGetProjects" ItemName="NuGetProjects" />
</CollectNuGetProjects>
<Message Text="Collected the following nuget projects: @(NuGetProjects)" Importance="high" />
<Exec Command="nuget pack &quot;$(SolutionDir)\%(NuGetProjects.Identity)&quot; -Properties Configuration=$(Configuration)" />
<!-- publish packages etc... -->
</Target>

<UsingTask TaskName="CollectNuGetProjects"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<SolutionName ParameterType="System.String" Required="true" />
<SolutionDir ParameterType="System.String" Required="true" />
<NuGetProjects ParameterType="System.String[]" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Linq" />
<Using Namespace="System" />
<Using Namespace="System.Linq" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var solutionFile = Path.Combine(SolutionDir, SolutionName + ".sln");
var solutionText = File.ReadAllText(solutionFile);
NuGetProjects = Regex.Matches(solutionText, @"""(?<projectFile>[^""]+?\.(csproj|wixproj|vcxproj))""")
.Cast<Match>()
.Select(m => m.Groups["projectFile"].Value)
.Where(p => !p.Contains(@"\Tests\"))
.ToArray();
]]>
</Code>
</Task>
</UsingTask>

自定义任务还会根据需要过滤掉位于文件夹 Tests 中的项目。

关于c# - 在 MSBuild 的解决方案中循环项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23733998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com