Skip to content

Commit

Permalink
Vastly simplifies ap version metadata generation
Browse files Browse the repository at this point in the history
Now instead of trying to import a generated project after a task (which was always impossible), we instead parse the output of git_version.ps1 and dynamically batch create properties.

We also have better support for error cases which should be helpful.

Work done for #196
  • Loading branch information
atruskie committed Mar 6, 2020
1 parent a3bc20e commit c7c9d28
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
28 changes: 20 additions & 8 deletions src/AP.VersionBuild.targets
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Condition="'$(Configuration)' == 'Release' Or !Exists('Properties\AssemblyInfo.cs')" -->

<!-- Register our task that as something to run before standard build target -->
<Target Name="APVersionBeforeBuild" BeforeTargets="PrepareForBuild">
<Exec Command="pwsh -noprofile $(ProjectDir)../git_version.ps1 -configuration $(Configuration)" ConsoleToMSBuild="True" EchoOff="False">
<Output TaskParameter="ConsoleOutput" PropertyName="VersionMetadata" />
<Exec Command="pwsh –NonInteractive -noprofile $(ProjectDir)../git_version.ps1 -configuration $(Configuration)"
ConsoleToMSBuild="True"
EchoOff="false"
StandardOutputImportance="low" >
<Output TaskParameter="ConsoleOutput" ItemName="VersionMetadata" />
</Exec>
</Target>
<Import Project="$(MSBuildThisFileDirectory)AssemblyMetadata.Generated.targets" Condition="Exists('$(MSBuildThisFileDirectory)AssemblyMetadata.Generated.targets')"/>
<Target Name="APVersionMessage" AfterTargets="APVersionLoadProperties">
<Message Text="[APVersionBeforeBuild] Last Tag: $(LastTag)%0a[APVersionBeforeBuild] Hash: $(CommitHashShort)%0a[APVersionBeforeBuild] CommitCount: $(CommitsSinceLastTag)" Importance="High" />
<Message Text="[APVersionBeforeBuild] Updating assembly version with: $(Version)!" Importance="High" />
<!-- batch through returned metadata, then use ✨MAGIC✨ to convert subexpression to string, then split on key/value -->
<!-- and finally create each property -->
<CreateProperty Value="$([System.String]::Copy(`%(VersionMetadata.Identity)`).Split('=')[1].Trim())">
<Output TaskParameter="Value" PropertyName="$([System.String]::Copy(`%(VersionMetadata.Identity)`).Split('=')[0].Trim())" />
</CreateProperty>

<Message Text="[APVersionBeforeBuild] Generated metadata file: $(GeneratedMetadata)" Importance="High" />
<Message Text="[APVersionBeforeBuild] Last Tag: $(LastTag), Hash: $(CommitHashShort), CommitCount: $(CommitsSinceLastTag)" Importance="High" />
<Message Text="[APVersionBeforeBuild] Using assembly version with: $(Version)!" Importance="High" />
<Message Text="[APVersionBeforeBuild] InformationalVersion: $(InformationalVersion)" Importance="High" />
<Message Text="[APVersionBeforeBuild] BuildDate: $(BuildDate)" Importance="High" />
<Message Text="[APVersionBeforeBuild] git_version.ps1 stdout: %(VersionMetadata.Identity)" Importance="High" Condition="$(GeneratedMetadata) == ''"/>

<Error Text="AP Version metadata generation failed. Check you have PowerShell 6+ &amp; Git installed and available on the system PATH."
Condition="$(GeneratedMetadata) == ''"/>
</Target>
</Project>
42 changes: 14 additions & 28 deletions src/git_version.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Push-Location
Set-Location $PSScriptRoot

$metadata_file = "AssemblyMetadata.Generated.cs"
$props_file = "AssemblyMetadata.Generated.targets"
$now = [System.DateTimeOffset]::UtcNow

# cache files in debug release
Expand All @@ -28,15 +27,12 @@ $commit_hash = git show -s --format="%H"

$branch = git rev-parse --abbrev-ref HEAD



$describe = git describe --dirty --abbrev --long --always

$describe_tag, $describe_commit_count, $describe_hash, $describe_dirty = $describe.Split("-")
$is_dirty = if ($null -ne $describe_dirty) { 'true' } else { 'false' }
$dirty = if ($is_dirty) { 'DIRTY'} else { '' }


$year = $now.Year
$month = $now.Month
$short_year = $now.ToString("yy")
Expand All @@ -56,32 +52,22 @@ $templated = $ExecutionContext.InvokeCommand.ExpandString($content)
$templated | Out-File $metadata_file -Force -Encoding utf8NoBOM

$props = @"
<?xml version="1.0" encoding="utf-8"?>
<Project>
<Target Name="APVersionLoadProperties" AfterTargets="APVersionBeforeBuild">
<PropertyGroup>
<Year>$short_year</Year>
<Month>$short_month</Month>
<BuildDate>$build_date</BuildDate>
<BuildNumber>$build_number</BuildNumber>
<CommitHash>$commit_hash</CommitHash>
<CommitHashShort>$describe_hash</CommitHashShort>
<Branch>$branch</Branch>
<LastTag>$describe_tag</LastTag>
<CommitsSinceLastTag>$describe_commit_count</CommitsSinceLastTag>
<TagsThisMonth>$tag_count_this_month</TagsThisMonth>
<IsDirty>$is_dirty</IsDirty>
<Version>$version</Version>
<InformationalVersion>$informational_version</InformationalVersion>
<GeneratedMetadata>$metadata_file</GeneratedMetadata>
<GeneratedProps>$props_file</GeneratedProps>
</PropertyGroup>
</Target>
</Project>
Year=$short_year
Month=$short_month
BuildDate=$build_date
BuildNumber=$build_number
CommitHash=$commit_hash
CommitHashShort=$describe_hash
Branch=$branch
LastTag=$describe_tag
CommitsSinceLastTag=$describe_commit_count
TagsThisMonth=$tag_count_this_month
IsDirty=$is_dirty
Version=$version
InformationalVersion=$informational_version
GeneratedMetadata=$metadata_file
"@

$props | Out-File $props_file -Force -Encoding utf8NoBOM

Write-Output $props

Pop-Location

0 comments on commit c7c9d28

Please sign in to comment.