BuildCommon.targets 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  3. <PropertyGroup>
  4. <CompileDependsOn>
  5. CommonBuildDefineModifiedAssemblyVersion;
  6. $(CompileDependsOn);
  7. </CompileDependsOn>
  8. </PropertyGroup>
  9. <Target Name="CommonBuildDefineModifiedAssemblyVersion" Condition="'$(GitCommit)' != ''">
  10. <!-- Find AssemblyInfo.cs or AssemblyInfo.vb in the "Compile" Items. Remove it from "Compile" Items because we will use a modified version instead. -->
  11. <ItemGroup>
  12. <OriginalAssemblyInfo Include="@(Compile)" Condition="%(Filename) == 'AssemblyInfo' And (%(Extension) == '.vb' Or %(Extension) == '.cs')" />
  13. <Compile Remove="**/AssemblyInfo.vb" />
  14. <Compile Remove="**/AssemblyInfo.cs" />
  15. </ItemGroup>
  16. <!-- Copy the original AssemblyInfo.cs/.vb to obj\ folder, i.e. $(IntermediateOutputPath). The copied filepath is saved into @(ModifiedAssemblyInfo) Item. -->
  17. <Copy SourceFiles="@(OriginalAssemblyInfo)"
  18. DestinationFiles="@(OriginalAssemblyInfo->'$(IntermediateOutputPath)%(Identity)')">
  19. <Output TaskParameter="DestinationFiles" ItemName="ModifiedAssemblyInfo"/>
  20. </Copy>
  21. <!-- Replace the version bit (in AssemblyVersion and AssemblyFileVersion attributes) using regular expression. Use the defined property: $(GitCommit). -->
  22. <Message Text="Setting GitCommit to $(GitCommit)" />
  23. <AppendFile Files="@(ModifiedAssemblyInfo)"
  24. GitHash="$(GitCommit)"
  25. />
  26. <!-- Include the modified AssemblyInfo.cs/.vb file in "Compile" items (instead of the original). -->
  27. <ItemGroup>
  28. <Compile Include="@(ModifiedAssemblyInfo)" />
  29. </ItemGroup>
  30. </Target>
  31. <UsingTask TaskName="AppendFile" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
  32. <ParameterGroup>
  33. <Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
  34. <GitHash ParameterType="System.String" Required="true" />
  35. </ParameterGroup>
  36. <Task>
  37. <Reference Include="System.Core" />
  38. <Using Namespace="System" />
  39. <Using Namespace="System.IO" />
  40. <Using Namespace="Microsoft.Build.Framework" />
  41. <Using Namespace="Microsoft.Build.Utilities" />
  42. <Code Type="Fragment" Language="cs">
  43. <![CDATA[
  44. try {
  45. for (int i = 0; i < Files.Length; ++i)
  46. {
  47. var path = Files[i].GetMetadata("FullPath");
  48. if (!File.Exists(path)) continue;
  49. using (StreamWriter sw = File.AppendText(path))
  50. {
  51. var text = $"[assembly: AssemblyGitCommit(\"{GitHash}\")]";
  52. Log.LogMessage(text);
  53. sw.WriteLine(text);
  54. }
  55. }
  56. return true;
  57. }
  58. catch (Exception ex) {
  59. Log.LogErrorFromException(ex);
  60. return false;
  61. }
  62. ]]>
  63. </Code>
  64. </Task>
  65. </UsingTask>
  66. </Project>