如何自动将版本号插入到AssemblyName中 [英] How to automatically insert version number into AssemblyName

查看:220
本文介绍了如何自动将版本号插入到AssemblyName中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力建立这个问题:



从MSBuild中的文件读取单个值



我的目标是拥有一个地方把几个项目中使用的版本号,我也想要一个项目的DLL文件名中的一部分版本号。



根据上面的问题,我已经得到了第一部分,但是我在第二部分遇到困难,并希望有一些指导。



在我的解决方案中,我设置了一个简单的名为Version.txt的文本文件仅包含完整版本号:



1.1.0.0



在我的两个项目中,我打开了AssemblyInfo.cs文件,并删除了AssemblyVersion和AssemblyFileVersion项目,然后修改了这两个项目,以在上述问题中所述的单独文件中生成。

 &升吨;&的ItemGroup GT; 
< VersionFile Include =.. \Version.txt/>
< / ItemGroup>
< Target Name =BeforeBuild>
< ReadLinesFromFile File =@(VersionFile)>
< Output TaskParameter =LinesPropertyName =VersionNumber/>
< / ReadLinesFromFile>
< Delete Files =Properties\Version.cs/>
< WriteLinesToFile File =Properties\Version.csLines =using System.Reflection%3B& #xD;& #xA;& #xD;& #xA; [assembly:AssemblyVersion $(VersionNumber))]& #xD;& #xA; [assembly:AssemblyFileVersion($(VersionNumber))]/>
< / Target>

现在,当我构建时,我会为每个项目生成一个Properties \Version.cs文件,其中用于构建EXE / DLL,并在其文件属性中显示为1.1.0.0。这就是我想要的。



对于DLL,我想命名程序集filename.v1.1.dll,其中1.1来自上述Version.txt中的前两个组件。只要可以在DLL文件名中的EXE / DLL属性和1.1中获得完整的1.1.0.0,就可以对Version.txt格式灵活。



要尝试这一点,我修改了DLL的csproj文件,以具有:

 < RootNamespace> dllfile< / RootNamespace> 
< AssemblyName> dllfile.v $(VersionNumber)< / AssemblyName>

当然,这将插入完整的版本号在文件名,我不想



有没有人有任何提示如何继续?



谢谢。



编辑:我已经能够通过在.csproj BeforeBuild目标中添加以下内容来提取版本号的主要/次要组件:

 < ReadLinesFromFile File =@(VersionFile)> 
<输出TaskParameter =LinesPropertyName =VersionNumber/>
< / ReadLinesFromFile>
< PropertyGroup>
< VersionNumberFirstDotIndex> $(VersionNumber.IndexOf('。'))< / VersionNumberFirstDotIndex>
< VersionNumberMajorStart> 0< / VersionNumberMajorStart>
< VersionNumberMajorLen> $(VersionNumberFirstDotIndex)< / VersionNumberMajorLen>
< VersionNumberMinorStart> $([MsBuild] :: Add(1,$(VersionNumberFirstDotIndex)))< / VersionNumberMinorStart>
< VersionNumberSecondDotIndex> $(VersionNumber.IndexOf('。',$(VersionNumberMinorStart)))< / VersionNumberSecondDotIndex>
< VersionNumberMinorLen> $([MSBuild] :: Subtract($([MSBuild] :: Subtract($(VersionNumberSecondDotIndex),$(VersionNumberFirstDotIndex))),1))< / VersionNumberMinorLen>
< VersionNumberMajor> $(VersionNumber.Substring($(VersionNumberMajorStart),$(VersionNumberMajorLen)))< / VersionNumberMajor>
< VersionNumberMinor> $(VersionNumber.Substring($(VersionNumberMinorStart),$(VersionNumberMinorLen)))< / VersionNumberMinor>
< VersionNumberShort> $(VersionNumberMajor)$(VersionNumberMinor)< / VersionNumberShort>
< / PropertyGroup>
< Message Text =DEBUG1 VersionNumberFull = $(VersionNumber)Importance =High/>
< Message Text =DEBUG2 VersionNumberAbbrev = $(VersionNumberShort)Importance =High/>
< Delete Files =Properties\Version.cs/>
< WriteLinesToFile File =Properties\Version.csLines =using System.Reflection%3B& #xD;& #xA;& #xD;& #xA; [assembly:AssemblyVersion & quot; $(VersionNumber)& quot;)]& #xD;& #xA; [assembly:AssemblyFileVersion(& quot; $(VersionNumber)& quot))]/>

现在我唯一缺少的是如何将此VersionNumberShort设置为DLL文件名。除非有一个更好的主意,我可以采取彼得的建议,并使用移动任务:

 <目标名称=AfterBuild> ; 
< Move SourceFiles =$(OutputPath)$(AssemblyName).pdbDestinationFiles =$(OutputPath)$(AssemblyName).v $(VersionNumberShort).pdb/>
< Move SourceFiles =$(OutputPath)$(AssemblyName).dllDestinationFiles =$(OutputPath)$(AssemblyName).v $(VersionNumberShort).dll/>
< / Target>
< Target Name =AfterCleanDependsOnTargets =Common>
< Delete Files =$(OutputPath)$(AssemblyName).v $(VersionNumberShort).pdbContinueOnError =true/>
< Delete Files =$(OutputPath)$(AssemblyName).v $(VersionNumberShort).dllContinueOnError =true/>
< / Target>

由于我需要与以前一样的属性定义,我将上面的代码段移动到常用目标并将其引用到这里显示的构建和清理任务。



彼得 - 如果你想把你的评论作为答案,我会接受。 >

谢谢!



编辑:按照jdlugosz的回答,我尝试在我的任务中设置AssemblyName 。不幸的是,根据顶部的原始示例,这仍然没有任何效果:

 <目标名称= BeforeBuild > 
...
< WriteLinesToFile ... />
< PropertyGroup>
< AssemblyName> dllfile.v $(VersionNumber)< / AssemblyName>
< / PropertyGroup>
< / Target>

我从Visual Studio Developer命令提示符中尝试使用MSBuild运行此代码:

  msbuild / target:clean projfile.csproj 
msbuild / verbosity:diag projfile.csproj> out.txt

在此之前,我更名为我的csproj文件的顶部,并在重新定义到独特的东西,使其易于搜索(例如dllfileoriginal对dllfilemodified)。



查看输出日志,我找不到任何参考修改后的文字;在输出中,它仍然是dllfileoriginal无处不在。



在WriteLinesToFile任务之后,它看起来像以下目标被构建:




  • IncrementalClean(finished)

  • PostBuildEvent

  • CoreBuild

  • AfterBuild

  • 构建



没有引用这些内容中的任何DLL名称。



目前看来,现在是我最好的选择。

解决方案

目标名称是显示在IDE属性页编辑器的配置属性选项卡的常规页面上。我没有一个方便自己来查找你的名字,但你可以通过将IDE中的空白更改为XXXX并保存。然后查看版本控制提交审阅者中的差异,并查看该属性的名称。在这种情况下,然后编辑行将XXXX更改为 $(OutputPath)$(AssemblyName).v $(VersionNumberShort)





哦,请查看 FormatVersion 任务,这可能有帮助。我认为还有一些预处理的任务也可以操作类似于你显示的版本组件。



我正在为版本做什么,通过#defines作为 / D 命令行参数。我猜你在C#中没有,IIRC。


I'm trying to build upon this question:

Reading a single value from a file in MSBuild

My goal is to have a single place to put the version number that's used in several projects, and I also want a portion of the version number in the DLL file name for one of the projects.

Based on the question above, I already got the first part, but I'm having difficulty with the second part and would appreciate some guidance.

In my solution, I set up a plain text file called Version.txt containing my full version number only:

1.1.0.0

In both of my projects, I opened their AssemblyInfo.cs files and removed the AssemblyVersion and AssemblyFileVersion items, then modified both projects to generate them in a separate file as described in the question above.

<ItemGroup>
    <VersionFile Include="..\Version.txt" />
</ItemGroup>
<Target Name="BeforeBuild">
    <ReadLinesFromFile File="@(VersionFile)">
        <Output TaskParameter="Lines" PropertyName="VersionNumber" />
    </ReadLinesFromFile>
    <Delete Files="Properties\Version.cs" />
    <WriteLinesToFile File="Properties\Version.cs" Lines="using System.Reflection%3B&#xD;&#xA;&#xD;&#xA;[assembly: AssemblyVersion("$(VersionNumber)")]&#xD;&#xA;[assembly: AssemblyFileVersion("$(VersionNumber)")]" />
</Target>

Now when I build, I get a generated Properties\Version.cs file for each project, which is used to build the EXE/DLL and shows up as "1.1.0.0" in their file properties. This is exactly what I want.

For the DLL, I would like to name the assembly "filename.v1.1.dll", where the "1.1" comes from the first two components in Version.txt above. I'm flexible on the format of Version.txt as long as I can get the full "1.1.0.0" in the EXE/DLL properties and "1.1" in the DLL file name.

To try this out, I modified the DLL's csproj file to have:

<RootNamespace>dllfile</RootNamespace>
<AssemblyName>dllfile.v$(VersionNumber)</AssemblyName>

Of course, this will insert the full version number in the file name, which I don't want.

Does anyone have any tips on how to proceed?

Thanks.

EDIT: I have been able to extract the major/minor components of the version number by adding the following to my .csproj BeforeBuild target:

<ReadLinesFromFile File="@(VersionFile)">
    <Output TaskParameter="Lines" PropertyName="VersionNumber" />
</ReadLinesFromFile>
<PropertyGroup>
    <VersionNumberFirstDotIndex>$(VersionNumber.IndexOf('.'))</VersionNumberFirstDotIndex>
    <VersionNumberMajorStart>0</VersionNumberMajorStart>
    <VersionNumberMajorLen>$(VersionNumberFirstDotIndex)</VersionNumberMajorLen>
    <VersionNumberMinorStart>$([MsBuild]::Add(1, $(VersionNumberFirstDotIndex)))</VersionNumberMinorStart>
    <VersionNumberSecondDotIndex>$(VersionNumber.IndexOf('.', $(VersionNumberMinorStart)))</VersionNumberSecondDotIndex>
    <VersionNumberMinorLen>$([MSBuild]::Subtract($([MSBuild]::Subtract($(VersionNumberSecondDotIndex), $(VersionNumberFirstDotIndex))), 1))</VersionNumberMinorLen>
    <VersionNumberMajor>$(VersionNumber.Substring($(VersionNumberMajorStart), $(VersionNumberMajorLen)))</VersionNumberMajor>
    <VersionNumberMinor>$(VersionNumber.Substring($(VersionNumberMinorStart), $(VersionNumberMinorLen)))</VersionNumberMinor>
    <VersionNumberShort>$(VersionNumberMajor).$(VersionNumberMinor)</VersionNumberShort>
</PropertyGroup>
<Message Text="DEBUG1 VersionNumberFull=$(VersionNumber)" Importance="High" />
<Message Text="DEBUG2 VersionNumberAbbrev=$(VersionNumberShort)" Importance="High" />
<Delete Files="Properties\Version.cs" />
<WriteLinesToFile File="Properties\Version.cs" Lines="using System.Reflection%3B&#xD;&#xA;&#xD;&#xA;[assembly: AssemblyVersion(&quot;$(VersionNumber)&quot;)]&#xD;&#xA;[assembly: AssemblyFileVersion(&quot;$(VersionNumber)&quot;)]" />

The only piece I'm missing now is how to get this VersionNumberShort into the DLL file name. Unless someone has a better idea, I can take Peter's suggestion and use Move tasks:

<Target Name="AfterBuild">
    <Move SourceFiles="$(OutputPath)$(AssemblyName).pdb" DestinationFiles="$(OutputPath)$(AssemblyName).v$(VersionNumberShort).pdb" />
    <Move SourceFiles="$(OutputPath)$(AssemblyName).dll" DestinationFiles="$(OutputPath)$(AssemblyName).v$(VersionNumberShort).dll" />
</Target>
<Target Name="AfterClean" DependsOnTargets="Common">
    <Delete Files="$(OutputPath)$(AssemblyName).v$(VersionNumberShort).pdb" ContinueOnError="true" />
    <Delete Files="$(OutputPath)$(AssemblyName).v$(VersionNumberShort).dll" ContinueOnError="true" />
</Target>

Since I needed the same property definitions as before, I moved the snippet above into a "Common" target and referenced it in both the build and clean tasks shown here.

Peter - If you want to move your comment as an answer, I'll accept it.

Thanks!

EDIT: Following jdlugosz's answer, I tried setting the AssemblyName inside my task. Unfortunately, this still didn't seem to have any effect based on the original example listed at the top:

<Target Name="BeforeBuild">
    ...
    <WriteLinesToFile ... />
    <PropertyGroup>
      <AssemblyName>dllfile.v$(VersionNumber)</AssemblyName>
    </PropertyGroup>
</Target>

I tried running this with MSBuild from a Visual Studio Developer Command Prompt:

msbuild /target:clean projfile.csproj
msbuild /verbosity:diag projfile.csproj > out.txt

Prior to this, I renamed the at the top of my csproj file and in the "redefinition" to something unique to make it easy to search (e.g. "dllfileoriginal" vs. "dllfilemodified").

Looking through the output log, I can't find any reference to the modified text; it's still dllfileoriginal everywhere in the output.

Following the WriteLinesToFile task, it looks like the following targets were built:

  • IncrementalClean (finished)
  • PostBuildEvent
  • CoreBuild
  • AfterBuild
  • Build

There's no reference to either DLL name inside these.

It looks like the is currently my best bet still.

解决方案

The Target Name is is shown on the General page under the Configuration Properties tab in the IDE Property Page editor. I don't have one handy myself to look up the name for you, but you can do it by changing the blank in the IDE to something like XXXX and save. Then view the diff in the version control commit reviewer and see what the name of the Property is. In this case, then edit the line to change XXXX to $(OutputPath)$(AssemblyName).v$(VersionNumberShort)

Oh, check out the FormatVersion task, which might help. I think there are some premade tasks that manipulate a version assembly similar to what you show, too.

What I'm doing for versions is passing the pieces in via #defines as /D command line arguments. I guess you don't have that in C# though, IIRC.

这篇关于如何自动将版本号插入到AssemblyName中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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