简体   繁体   English

dotnet core/csproj 中的 AssemblyInfo 与 Target 中的属性

[英]AssemblyInfo in dotnet core/csproj with Property from Target

I'm trying to setup a csproj with assembly info automatically generated from compilation datetime and mercurial revision.我正在尝试使用从编译日期时间和 mercurial 修订版自动生成的程序集信息设置一个 csproj。

  <!-- assembly info -->
  <PropertyGroup>
    <!-- versioning number: Maj.Min.TimeStamp.Rev -->
    <Major>2</Major>
    <Minor>90</Minor>
    <!-- define Build based on the compilation date: yyddd that sould be integer in [0..65535] -->
    <Build>$([System.DateTime]::Now.ToString(`yy`))$([System.DateTime]::Now.DayOfYear.ToString(`000`))</Build>
    <Revision>0</Revision>
    <Version>$(Major).$(Minor).$(Build)</Version>
    <AssemblyVersion>$(Major).$(Minor).$(Build).$(Revision)</AssemblyVersion>
    <FileVersion>$(Major).$(Minor).$(Build).$(Revision)</FileVersion>
  </PropertyGroup>

  <Import Project="..\packages\MSBuild.Mercurial.1.2.1\build\MSBuild.Mercurial.targets" Condition="Exists('..\packages\MSBuild.Mercurial.1.2.1\build\MSBuild.Mercurial.targets')" />
  <Target Name="GetHgRev" BeforeTargets="GenerateAssemblyInfo" DependsOnTargets="PrepareForBuild">
    <HgVersion LocalPath="$(SolutionDir)" Timeout="5000">
      <Output TaskParameter="Revision" PropertyName="Revision" />
    </HgVersion>
    <Message Text="Last revision from HG: $(Revision)" Importance="High" />
    <Message Text="$(Major).$(Minor).$(Build).$(Revision)" Importance="High" />
  </Target>

On console output, I get something consistent: 2.90.20160.239 But only $(Build) is properly used in tmp AssemblyInfo.cs, $(Revision) is basically 0.在控制台 output 上,我得到了一致的结果:2.90.20160.239 但在 tmp AssemblyInfo.cs 中仅正确使用了 $(Build),$(Revision) 基本上为 0。

In previous csproj format (2015), I got the job done with explicit cmd in BeforeBuild target:在以前的 csproj 格式(2015)中,我在 BeforeBuild 目标中使用显式 cmd 完成了工作:

  <PropertyGroup>
    <AssemblyCompany>AAA</AssemblyCompany>
    <MyAppName>BBB</MyAppName>
    <Major>2</Major>
    <Minor>90</Minor>
    <Build>0</Build>
    <Revision>0</Revision>
  </PropertyGroup>
  <Target Name="BeforeBuild">
    <!-- get the Mercurial revision no -->
    <HgVersion LocalPath="$(SolutionDir)" Timeout="5000">
      <Output TaskParameter="Revision" PropertyName="Revision" />
    </HgVersion>
    <Message Text="Last revision from HG: $(Revision), today date $([System.DateTime]::Now.ToString(`yyyy.MM.dd`)) -&gt; $([System.DateTime]::Now.ToString(`yy`))$([System.DateTime]::Now.DayOfYear.ToString(`000`))" Importance="High" />
    <!-- define Build based on the compilation date: yyddd that sould be integer in [0..65535] -->
    <CreateProperty Value="$([System.DateTime]::Now.ToString(`yy`))$([System.DateTime]::Now.DayOfYear.ToString(`000`))">
      <Output PropertyName="Build" TaskParameter="Value" />
    </CreateProperty>
    <!-- generate version file, i.e. AssemblyInfo.cs used later for compilation -->
    <Message Text="Creating Version File: $(Major).$(Minor).$(Build).$(Revision)" Importance="High" />
    <AssemblyInfo CodeLanguage="CS" OutputFile=".\Properties\AssemblyInfo.cs" AssemblyTitle="AAA" AssemblyDescription="BBB" AssemblyCompany="$(AssemblyCompany)" AssemblyProduct="CCC" AssemblyCopyright="Copyright © DDD $([System.DateTime]::Now.ToString(`yyyy`))" AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)" AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)" />
  </Target>

Would you see any fix to this issue?你会看到这个问题的任何解决方法吗?

New test:新测试:

  <Import Project="..\packages\MSBuild.Mercurial.1.2.1\build\MSBuild.Mercurial.targets" Condition="Exists('..\packages\MSBuild.Mercurial.1.2.1\build\MSBuild.Mercurial.targets')" />
  <Target Name="GetHgRev" BeforeTargets="GenerateAssemblyInfo" DependsOnTargets="PrepareForBuild">
    <HgVersion LocalPath="$(SolutionDir)" Timeout="5000">
      <Output TaskParameter="Revision" PropertyName="Revision" />
    </HgVersion>
    <Message Text="Proposed number: $(Major).$(Minor).$(Build).$(Revision)" Importance="High" />
    <Message Text="AssemblyVersion before: $(AssemblyVersion)" Importance="High" />
    <PropertyGroup>
      <AssemblyVersion>$(Major).$(Minor).$(Build).$(Revision)</AssemblyVersion>
    </PropertyGroup>
    <Message Text="AssemblyVersion after: $(AssemblyVersion)" Importance="High" />
  </Target>

Kind regards, Charles亲切的问候,查尔斯

The static portion of a project file (everything outside a <Target> ) is evaluated completely before beginning to run the actual build logic (which is made up by targets).项目文件的 static 部分( <Target>之外的所有内容)在开始运行实际构建逻辑(由目标组成)之前被完全评估。

Properties are like a string-to-string dictionary, so Version and others will be set to the string values, with $() replacements being evaluated to in the string but not logically preserved.属性就像一个字符串到字符串的字典,因此Version和其他将被设置为字符串值, $() 替换在字符串中被评估但不被逻辑保留。

This means that once the target runs, you will also need to update all the properties you now need to change again ( Version , AssemblyVersion , FileVersion etc.).这意味着一旦目标运行,您还需要更新您现在需要再次更改的所有属性( VersionAssemblyVersionFileVersion等)。 You can do so by adding a <PropertyGroup> inside the target after the HgVersion task invocation.您可以通过在HgVersion任务调用之后在目标内添加<PropertyGroup>来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM