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

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

问题描述

我试图建立在这样一个问题:

I'm trying to build upon this question:

中的MSBuild

我的目标是在一个地方把该公司在多个项目中使用的版本号,我也想在DLL文件名的版本号的一个项目的一部分。

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.

在我的解决方案,我成立了一个简单的所谓Version.txt包含文本文件我的完整版本号只:

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

1.1.0.0

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

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>

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

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.

有关的DLL,我想命名大会filename.v1.1.dll,其中1.1来自在Version.txt的前两个组分的上方。我在Version.txt的格式灵活,只要我能在EXE / DLL性能得到充分的1.1.0.0,并在DLL文件名1.1。

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.

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

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?

感谢。

编辑:我已经能够通过增加提取版本号的主要/次要成分下面我的.csproj BeforeBuild目标:

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;)]" />



我现在唯一缺少的部分是如何得到这个VersionNumberShort到DLL文件的名称。除非有人有更好的主意,我可以把彼得的建议,并使用移动任务:

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.

感谢

编辑:以下jdlugosz的回答,我尝试设置我的任务里面的AssemblyName 。不幸的是,这仍然似乎并没有基于在顶部列出原来的例子有任何影响:

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>



我试着用的MSBuild从Visual Studio开发人员命令提示符下运行这样的:

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

在此之前,我改名为我的csproj文件的顶部,并在重新定义,以独特的东西,可以很容易地搜索(如dllfileoriginal与dllfilemodified)通过输出日志。

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").

看,我找不到任何参照修改后的文本; 。它仍然dllfileoriginal无处不在输出

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

继WriteLinesToFile任务,它看起来像以下目标分别建:

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


  • IncrementalClean(完)

  • PostBuildEvent

  • CoreBuild

  • AfterBuild

  • 构建

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

有对里面这些要么DLL名称没有提及。

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

它看起来像目前是我最好的选择还是。

It looks like the is currently my best bet still.

推荐答案

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

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)

呵呵,请查看 FormatVersion 任务,这可能会有帮助。我认为有操纵一个版本装配类似于大家展示一下,过了一些预制任务。

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.

我在做什么的版本是通过#define语句传递条为 / D 命令行参数。我猜你没有,在C#虽然,IIRC。

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天全站免登陆