Skip to content

Commit

Permalink
Merge pull request #21 from zhilbug/master
Browse files Browse the repository at this point in the history
Adding ability to have as many command outputs go to meta data as needed.
  • Loading branch information
MarkPflug authored Apr 5, 2020
2 parents c72ef9c + 72ea033 commit 60bf854
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 90 deletions.
186 changes: 96 additions & 90 deletions MSBuildGitHash/build/MSBuildGitHash.targets
Original file line number Diff line number Diff line change
Expand Up @@ -4,132 +4,139 @@
<IncludeMSBuildGitHashMetadata Condition="'$(IncludeMSBuildGitHashMetadata)' == ''">true</IncludeMSBuildGitHashMetadata>
<IncludeMSBuildGitHashInfoVersion Condition="'$(IncludeMSBuildGitHashInfoVersion)' == ''">true</IncludeMSBuildGitHashInfoVersion>
<MSBuildGitHashValidateSuccess Condition="$(MSBuildGitHashValidateSuccess) == ''">$(MSBuildGitHashValidate)</MSBuildGitHashValidateSuccess>
<MSBuildGitRepository Condition="'$(MSBuildGitRepository)' == '' And '$(RepositoryType)' == 'git' And '$(RepositoryUrl)' != ''">$(RepositoryUrl)</MSBuildGitRepository>
</PropertyGroup>

<!-- Existing commands to be backwards compatible -->
<ItemGroup>
<AssemblyEmbed Include="GitHash">
<Command>$(MSBuildGitHashCommand)</Command>
<ValidationRegex Condition="'$(MSBuildGitHashValidate)' == 'True'">$(MSBuildGitHashValidateRegex)</ValidationRegex>
</AssemblyEmbed>
<AssemblyEmbed Include="GitRepository">
<Command>$(MSBuildGitRepository)</Command>
</AssemblyEmbed>
</ItemGroup>

<!-- Execute custom commands for assembly meta data -->
<Target
Name="GetGitHash"
BeforeTargets="WriteGitHash;GenerateAssemblyInfo">

<PropertyGroup>
<MSBuildGitRepository
Condition="'$(MSBuildGitRepository)' == '' And '$(RepositoryType)' == 'git' And '$(RepositoryUrl)' != ''"
>$(RepositoryUrl)</MSBuildGitRepository>
</PropertyGroup>

Name="CommandBatch"
Inputs="@(AssemblyEmbed)"
Outputs="%(Identity)"
BeforeTargets="WriteGitHash;GenerateAssemblyInfo"
>
<Exec
Command="$(MSBuildGitHashCommand)"
IgnoreExitCode="true"
Condition="'%(AssemblyEmbed.Command)' != '' And '%(AssemblyEmbed.Identity)' != 'GitRepository'"
Command="%(AssemblyEmbed.Command)"
ConsoleToMsBuild="true"
>
<Output TaskParameter="ExitCode" PropertyName="MSBuildGitHashExitCode" />
<Output TaskParameter="ConsoleOutput" ItemName="GitVersion" />
IgnoreExitCode="true"
>
<Output PropertyName="ConsoleOutput" TaskParameter="ConsoleOutput"/>
<Output PropertyName="ExitCode" TaskParameter="ExitCode"/>
</Exec>


<ItemGroup>
<AssemblyEmbed Update="%(AssemblyEmbed.Identity)">
<ValidationRegex Condition="'%(AssemblyEmbed.ValidationRegex)' == ''"></ValidationRegex>
<ConsoleOutput>$(ConsoleOutput)</ConsoleOutput>
<ExitCode>$(ExitCode)</ExitCode>
</AssemblyEmbed>
</ItemGroup>

<PropertyGroup>
<GitVersion>@(GitVersion)</GitVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(MSBuildGitHashExitCode)' == '0'">
<MSBuildGitHashSuccess>True</MSBuildGitHashSuccess>
<MSBuildGitHashVersionAttribute>$(Version)+$(ConsoleOutput)</MSBuildGitHashVersionAttribute>
<MSBuildGitHashVersionAttribute Condition="'$(MSBuildGitHashReplaceInfoVersion)' == 'True'">$(ConsoleOutput)</MSBuildGitHashVersionAttribute>
</PropertyGroup>

<PropertyGroup Condition="'$(MSBuildGitHashExitCode)' != '0'">
<MSBuildGitHashSuccess>False</MSBuildGitHashSuccess>
<!-- in the event of failure, this would contain an error message, so clear it out. -->
<GitVersion></GitVersion>
<MSBuildGitHashValidate>false</MSBuildGitHashValidate>
<PropertyGroup Condition="'$(UsingMicrosoftNETSdk)' == 'true'">
<Filtered Condition="'%(AssemblyEmbed.Identity)' == 'GitHash'"/>
<!--
SourceRevisionId is used by the Microsoft.NET.GenerateAssemblyInfo to populate
the AssemblyInformationalVersion, this only happens in SDK builds.
-->
<SourceRevisionId Condition="'$(SourceRevisionId)' == '' And '$(IncludeMSBuildGitHashInfoVersion)' == 'true'">
$(ConsoleOutput)
</SourceRevisionId>

<!-- suppress the sdk handling of informational version when MSBuildGitHashReplaceInfoVersion is set -->
<IncludeSourceRevisionInInformationalVersion Condition="'$(MSBuildGitHashReplaceInfoVersion)' == 'True'">
false
</IncludeSourceRevisionInInformationalVersion>
<InformationalVersion Condition="'$(MSBuildGitHashReplaceInfoVersion)' == 'True'">
$(ConsoleOutput)
</InformationalVersion>
</PropertyGroup>
</Target>

<!-- Validate outputs from target command batch -->
<Target
Name="ValidateOutput"
DependsOnTargets="CommandBatch"
>
<Error
Condition="'$(MSBuildGitHashValidateSuccess)' == 'True' And '$(MSBuildGitHashSuccess)' != 'True'"
Text="MSBuildGitHash error executing command $(MSBuildGitHashCommand) returned error code $(MSBuildGitHashExitCode)"
Condition="'%(AssemblyEmbed.ValidationRegex)' != '' And '%(AssemblyEmbed.ExitCode)' != '0'"
Text="%(AssemblyEmbed.Identity) error executing command %(Command) returned error code %(ExitCode)"
/>

<Warning
Condition="'$(MSBuildGitHashValidateSuccess)' != 'True' And '$(MSBuildGitHashSuccess)' != 'True'"
Text="MSBuildGitHash error executing command $(MSBuildGitHashCommand) returned error code $(MSBuildGitHashExitCode)"
Condition="'%(AssemblyEmbed.ValidationRegex)' == '' And '%(AssemblyEmbed.ExitCode)' != '0' And '%(AssemblyEmbed.Identity)' != 'GitRepository'"
Text="%(AssemblyEmbed.Identity) error executing command %(AssemblyEmbed.Command) returned error code %(ExitCode)"
/>

<PropertyGroup>
<MSBuildGitHashValue Condition="'$(MSBuildGitHashValue)' == ''">$(GitVersion)</MSBuildGitHashValue>
<MSBuildGitHashValueLength>$(MSBuildGitHashValue.Length)</MSBuildGitHashValueLength>
<MSBuildGitHashValueStart Condition="$(MSBuildGitHashValue.Length) &lt;= 32">$(MSBuildGitHashValue)</MSBuildGitHashValueStart>
<MSBuildGitHashValueStart Condition="$(MSBuildGitHashValue.Length) &gt; 32">$(MSBuildGitHashValue.Substring(0,32))</MSBuildGitHashValueStart>
<MSBuildGitHashRegexMatch>$([System.Text.RegularExpressions.Regex]::IsMatch($(MSBuildGitHashValue), $(MSBuildGitHashValidateRegex)))</MSBuildGitHashRegexMatch>
<MSBuildGitHashIsDirty>$([System.Text.RegularExpressions.Regex]::IsMatch($(MSBuildGitHashValue), $(MSBuildGitHashDirtyRegex)))</MSBuildGitHashIsDirty>
</PropertyGroup>

<PropertyGroup Condition="'$(UsingMicrosoftNETSdk)' == 'true'">
<!--
SourceRevisionId is used by the Microsoft.NET.GenerateAssemblyInfo to populate
the AssemblyInformationalVersion, this only happens in SDK builds.
-->
<SourceRevisionId
Condition="'$(SourceRevisionId)' == '' And '$(IncludeMSBuildGitHashInfoVersion)' == 'true'"
>$(MSBuildGitHashValue)</SourceRevisionId>
</PropertyGroup>

<PropertyGroup Condition="'$(UsingMicrosoftNETSdk)' == 'true' And '$(MSBuildGitHashReplaceInfoVersion)' == 'True'">
<!-- suppress the sdk handling of informational version when MSBuildGitHashReplaceInfoVersion is set -->
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<InformationalVersion>$(MSBuildGitHashValue)</InformationalVersion>
<Filtered Condition="'%(AssemblyEmbed.ConsoleOutput)' != '' And '%(AssemblyEmbed.ExitCode)' == '0' And '%(AssemblyEmbed.ValidationRegex)' != ''"/>
<OutputValue>%(AssemblyEmbed.ConsoleOutput)</OutputValue>
<OutputLength>$(OutputValue.Length)</OutputLength>
<Regex>%(AssemblyEmbed.ValidationRegex)</Regex>
<OutputValueStart Condition="'%(AssemblyEmbed.Identity)' == 'GitHash' And $(OutputLength) &lt;= 32">$(OutputValue)</OutputValueStart>
<OutputValueStart Condition="'%(AssemblyEmbed.Identity)' == 'GitHash' And $(OutputLength) &gt; 32">$(OutputValue.Substring(0,32))</OutputValueStart>
<MSBuildGitHashIsDirty Condition="'%(AssemblyEmbed.Identity)' == 'GitHash'">$([System.Text.RegularExpressions.Regex]::IsMatch($(OutputValue), $(MSBuildGitHashDirtyRegex)))</MSBuildGitHashIsDirty>
<OutputRegexMatch>$([System.Text.RegularExpressions.Regex]::IsMatch($(OutputValue), $(Regex)))</OutputRegexMatch>
</PropertyGroup>

<Error
Condition="'$(MSBuildGitHashExitCode)' == '0' And '$(MSBuildGitHashValidate)' == 'True' And ($(MSBuildGitHashValueLength) &gt; $(MSBuildGitHashValidateLength) Or '$(MSBuildGitHashRegexMatch)' != 'True')"
Text="MSBuildGitHash hash value starting with '$(MSBuildGitHashValueStart)' was invalid."
Condition="'%(AssemblyEmbed.ValidationRegex)' != '' And '%(AssemblyEmbed.Identity)' == 'GitHash' And ($(OutputLength) &gt; $(OutputLength) Or '$(OutputRegexMatch)' != 'True')"
Text="%(AssemblyEmbed.Identity) output value starting with '$(OutputValueStart)' was invalid."
/>

<Warning
Condition="'$(MSBuildGitHashSuppressDirtyWarning)' != 'True' And '$(MSBuildGitHashIsDirty)' == 'True'"
Text="MSBuildGitHash: the git version is dirty. You should commit all changes before building, or the assembly versions will be inconsistent."
<Warning
Condition="'%(AssemblyEmbed.Identity)' == 'GitHash' And '$(MSBuildGitHashSuppressDirtyWarning)' != 'True' And '$(MSBuildGitHashIsDirty)' == 'True'"
Text="%(AssemblyEmbed.Identity): the git version is dirty. You should commit all changes before building, or the assembly versions will be inconsistent."
/>

</Target>

<Target
Name="GenerateAssemblyVersionAttributes"
Condition="'$(UsingMicrosoftNETSdk)' != 'True'"
DependsOnTargets="GetGitHash"
>
<PropertyGroup>
<MSBuildGitHashVersionAttribute>$(Version)+$(MSBuildGitHashValue)</MSBuildGitHashVersionAttribute>
<MSBuildGitHashVersionAttribute Condition="'$(MSBuildGitHashReplaceInfoVersion)' == 'True'">$(MSBuildGitHashValue)</MSBuildGitHashVersionAttribute>
</PropertyGroup>
<Error
Condition="'%(AssemblyEmbed.ValidationRegex)' != '' And '%(AssemblyEmbed.Identity)' != 'GitHash' And '$(OutputRegexMatch)' != 'True'"
Text="%(AssemblyEmbed.Identity) output value failed regular expression comparison [%(AssemblyEmbed.ValidationRegex)]."
/>

<ItemGroup>
<AssemblyAttributes
Condition="'$(MSBuildGitHashVersionAttribute)' != '' And '$(IncludeMSBuildGitHashInfoVersion)' == 'true'"
Include="System.Reflection.AssemblyInformationalVersionAttribute">
<_Parameter1>$(MSBuildGitHashVersionAttribute)</_Parameter1>
</AssemblyAttributes>
</ItemGroup>
</Target>


<!-- Apply commands output to assembly meta data -->
<Target
Name="GenerateAssemblyAttributes"
DependsOnTargets="GetGitHash"
DependsOnTargets="ValidateOutput"
>
<ItemGroup
Condition="'$(IncludeMSBuildGitHashMetadata)' == 'true'"
>
<ItemGroup>
<AssemblyAttributes Include="AssemblyMetadata">
<_Parameter1>GitHash</_Parameter1>
<_Parameter2>$(MSBuildGitHashValue)</_Parameter2>
<_Parameter1>%(AssemblyEmbed.Identity)</_Parameter1>
<_Parameter2>%(AssemblyEmbed.ConsoleOutput)</_Parameter2>
</AssemblyAttributes>

<AssemblyAttributes Include="AssemblyMetadata" Condition="'$(MSBuildGitRepository)' != ''">
<_Parameter1>GitRepository</_Parameter1>
<_Parameter2>$(MSBuildGitRepository)</_Parameter2>
<AssemblyAttributes
Condition="'$(MSBuildGitHashVersionAttribute)' != '' And '$(IncludeMSBuildGitHashInfoVersion)' == 'true'"
Include="System.Reflection.AssemblyInformationalVersionAttribute"
>
<_Parameter1>$(MSBuildGitHashVersionAttribute)</_Parameter1>
</AssemblyAttributes>
</ItemGroup>
</Target>

<!-- Write the git hash to assembly info file -->
<Target
Name="WriteGitHash"
DependsOnTargets="GenerateAssemblyAttributes;GenerateAssemblyVersionAttributes"
BeforeTargets="CoreCompile">

DependsOnTargets="GenerateAssemblyAttributes"
BeforeTargets="CoreCompile"
>
<PropertyGroup>
<LanguageExt Condition="'$(Language)' == 'C#'">.cs</LanguageExt>
<LanguageExt Condition="'$(Language)' == 'VB'">.vb</LanguageExt>
Expand All @@ -138,8 +145,8 @@
<MSBuildGitHashAssemblyInfoFile>$(IntermediateOutputPath)MSBuildGitHashAssemblyInfo$(LanguageExt)</MSBuildGitHashAssemblyInfoFile>
</PropertyGroup>

<WriteCodeFragment
Language="$(CodeGenLanguage)"
<WriteCodeFragment
Language="$(CodeGenLanguage)"
OutputFile="$(MSBuildGitHashAssemblyInfoFile)"
AssemblyAttributes="@(AssemblyAttributes)" />

Expand All @@ -148,6 +155,5 @@
Include="$(MSBuildGitHashAssemblyInfoFile)"
Condition="Exists('$(MSBuildGitHashAssemblyInfoFile)')" />
</ItemGroup>

</Target>
</Project>
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ Alternatively, the git hash can replace the Informational Version attribute valu
<MSBuildGitHashReplaceInfoVersion>True</MSBuildGitHashReplaceInfoVersion>
</PropertyGroup>
```
### Command Output to Assembly Metadata

If there are additional commands and output that are needed to be included in the meta data, you can do so by adding them using `AssemblyEmbed`. Examples:

```xml
<AssemblyEmbed Include="KeyNameForOutput">
<Command>your command</Command>
<ValidationRegex>optional regex to validate output</ValidationRegex>
</AssemblyEmbed>
```

```xml
<ItemGroup>
<AssemblyEmbed Include="GitDescription">
<Command>git describe</Command>
</AssemblyEmbed>
<AssemblyEmbed Include="GitTag">
<Command>git describe --tag --abbrev=0</Command>
</AssemblyEmbed>
</ItemGroup>
```

## Version History
_1.0.2_
Expand Down

0 comments on commit 60bf854

Please sign in to comment.