123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?xml version="1.0" encoding="utf-8"?>
- <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
- <PropertyGroup>
- <CompileDependsOn>
- CommonBuildDefineModifiedAssemblyVersion;
- $(CompileDependsOn);
- </CompileDependsOn>
- </PropertyGroup>
- <Target Name="CommonBuildDefineModifiedAssemblyVersion" Condition="'$(GitCommit)' != ''">
- <!-- Find AssemblyInfo.cs or AssemblyInfo.vb in the "Compile" Items. Remove it from "Compile" Items because we will use a modified version instead. -->
- <ItemGroup>
- <OriginalAssemblyInfo Include="@(Compile)" Condition="%(Filename) == 'AssemblyInfo' And (%(Extension) == '.vb' Or %(Extension) == '.cs')" />
- <Compile Remove="**/AssemblyInfo.vb" />
- <Compile Remove="**/AssemblyInfo.cs" />
- </ItemGroup>
- <!-- Copy the original AssemblyInfo.cs/.vb to obj\ folder, i.e. $(IntermediateOutputPath). The copied filepath is saved into @(ModifiedAssemblyInfo) Item. -->
- <Copy SourceFiles="@(OriginalAssemblyInfo)"
- DestinationFiles="@(OriginalAssemblyInfo->'$(IntermediateOutputPath)%(Identity)')">
- <Output TaskParameter="DestinationFiles" ItemName="ModifiedAssemblyInfo"/>
- </Copy>
- <!-- Replace the version bit (in AssemblyVersion and AssemblyFileVersion attributes) using regular expression. Use the defined property: $(GitCommit). -->
- <Message Text="Setting GitCommit to $(GitCommit)" />
- <AppendFile Files="@(ModifiedAssemblyInfo)"
- GitHash="$(GitCommit)"
- />
- <!-- Include the modified AssemblyInfo.cs/.vb file in "Compile" items (instead of the original). -->
- <ItemGroup>
- <Compile Include="@(ModifiedAssemblyInfo)" />
- </ItemGroup>
- </Target>
- <UsingTask TaskName="AppendFile" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
- <ParameterGroup>
- <Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
- <GitHash ParameterType="System.String" Required="true" />
- </ParameterGroup>
- <Task>
- <Reference Include="System.Core" />
- <Using Namespace="System" />
- <Using Namespace="System.IO" />
- <Using Namespace="Microsoft.Build.Framework" />
- <Using Namespace="Microsoft.Build.Utilities" />
- <Code Type="Fragment" Language="cs">
- <![CDATA[
- try {
- for (int i = 0; i < Files.Length; ++i)
- {
- var path = Files[i].GetMetadata("FullPath");
- if (!File.Exists(path)) continue;
- using (StreamWriter sw = File.AppendText(path))
- {
- var text = $"[assembly: AssemblyGitCommit(\"{GitHash}\")]";
- Log.LogMessage(text);
- sw.WriteLine(text);
- }
- }
- return true;
- }
- catch (Exception ex) {
- Log.LogErrorFromException(ex);
- return false;
- }
- ]]>
- </Code>
- </Task>
- </UsingTask>
- </Project>
|