共享 AssemblyInfo 以实现整个解决方案的统一版本控制 [英] Shared AssemblyInfo for uniform versioning across the solution

查看:16
本文介绍了共享 AssemblyInfo 以实现整个解决方案的统一版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关此技术的信息:VS 项目中的共享程序集信息 - JJameson 的博客

I've read about this technique: Shared assembly info in VS projects - JJameson's blog

基本上这意味着创建一个包含有关程序集的版本信息的 SharedAssemblyInfo.cs,并将此文件作为链接添加到解决方案的所有项目中,因此实际文件仅位于磁盘上的 1 个位置.

Basically it means to create a SharedAssemblyInfo.cs with versioning information about the assembly, and adding this file as Link to all projects of the solution, so the actual file resides only in 1 location on disk.

我的问题涉及两种情况:

My question deals with 2 scenarios:

  1. 不使用这种机制的现有解决方案:有没有办法轻松地将 ShareAssemblyInfo 添加到所有项目?(假设我有一个包含 50 个项目的解决方案).

  1. Existing solution that doesn't use this mechanism: Is there a way to easily add the ShareAssemblyInfo to all projects? (lets say i have a solution with 50 projects).

创建新项目时,默认情况下会创建一个新的 AssemblyInfo.cs.但是,我也想自动链接到 SharedAssemblyInfo.

When creating a new project, by default a new AssemblyInfo.cs is created. However i'd like to link automatically to the SharedAssemblyInfo as well.

有什么解决办法吗?通常的做法是什么?

Is there any solution for this? what is the common practice?

推荐答案

第一点可以通过简单的文本编辑器解决,该编辑器可以同时处理多个文件并查找/替换.只需在其中打开所有 csproj 并将字符串 <Compile Include="PropertiesAssemblyInfo.cs"/> 替换为

First point could be solved with simple text editor that could handle several files at once and find/replace. Just open all of your csproj in it and replace string <Compile Include="PropertiesAssemblyInfo.cs" /> with

<Compile Include="..SharedAssemblyInfo.cs">
  <Link>PropertiesSharedAssemblyInfo.cs</Link>
</Compile>

或者,您可以编写这样的实用程序:

Alternatively you could write a utility like that:

var files = Directory.GetFiles(yourSolutionDir, "*.csproj", SearchOption.AllDirectories);
foreach (var f in files) {
  string contents = File.ReadAllText(f);
  string result = contents.Replace("<Compile Include="Properties\AssemblyInfo.cs" />", putSecondStringHere_ItIsJustTooLong); // :)
  File.WriteAllText(f, contents);
}

至于第二个问题...你可以看看Visual Studio 自定义项目模板 ,但我不确定是否值得付出努力.你应该 IMO 编写测试来检查这个.它会简单得多,结果实际上几乎相同.

As for the second question... You could take a look at Visual Studio custom project templates , but I'm not sure it worth the efforts. You should IMO write test that will check this instead. It will be much simpler and outcome is actually almost the same.

UPD:关于编写测试以根据某些自定义规则检查解决方案/项目文件.基本上,sln/csproj 格式足够简单,无需太多努力即可解析.因此,如果您想将 SharedAssemblyInfo.cs 链接到每个项目中 - 只需解析 csproj 并检查.然后将该检查器放在您的构建服务器中并在每个构建上运行它.我们目前有这样的系统正在运行,编写大约需要两天时间,但为我们节省了更多时间(我们有更复杂的规则和多解决方案项目,因此值得付出努力).

UPD: About writing tests for checking solution/project files against some custom rules. Basically, sln/csproj format is simple enough to be parseable without much efforts. So if you want to have SharedAssemblyInfo.cs linked into every project - just parse csproj's and check that. Then put that checker in your build server and run it on each build. We have such system working currently and it costs something about two days to write but saved us many more (we have there more sophisticated rules and multi-solution project, so it was worth the efforts).

我现在不会在这里详细写下这个检查(它不是那么短),但我很快就会写一篇关于它的博客文章 - 很可能要到本周末.所以,如果您有兴趣 - 请尽快查看 我的博客 :)

I won't write about this checking in detail here right now (it is not that short), but I'm going to write blog post about it soon - most probably till the end of this week. So, if you're interested - just check my blog soon :)

UPD: 这里是.

这篇关于共享 AssemblyInfo 以实现整个解决方案的统一版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆