Refactor MSBuild project files to get PowerShell version from git tag (#4182)

Extract information about the release tag, number of commits since the tag and the hash of the latest commit within a MSBuild target, and bake that information into version properties of the assemblies appropriately.
This commit is contained in:
Ilya 2017-09-07 02:20:25 +04:00 committed by Dongbo Wang
parent 7faf76b9c9
commit e588a18c76
3 changed files with 120 additions and 22 deletions

View file

@ -1,10 +1,95 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--
The 'version' property is populated with the default value in 'Microsoft.NET.DefaultAssemblyInfo.targets'
*before* any targets get a chance to execute.
We need to *explicitly* re-assign the 'version' property and other version tags in this target.
In order for the versions assigned here to take effect, we need to execute this target at an early stage:
before 'Restore' target - '_GenerateRestoreProjectSpec'
before 'Pack' target - 'GenerateNuspec'
before 'Build' target - 'BeforeBuild'
-->
<Target Name="GetPSCoreVersionFromGit"
BeforeTargets="_GenerateRestoreProjectSpec;GenerateNuspec;BeforeBuild"
>
<Exec Command='git describe --abbrev=60 --long'
WorkingDirectory="$(MSBuildProjectDirectory)"
ConsoleToMSBuild="true"
StandardOutputImportance="Low">
<Output TaskParameter="ConsoleOutput" PropertyName="PowerShellVersion" />
</Exec>
<PropertyGroup Condition = "'$(ReleaseTag)' != ''">
<PSCoreBuildVersion>$(ReleaseTag)</PSCoreBuildVersion>
</PropertyGroup>
<PropertyGroup>
<RegexGitVersion>^v(.+)-(\d+)-g(.+)</RegexGitVersion>
<PSCoreBuildVersion Condition = "'$(PSCoreBuildVersion)' == ''">$([System.Text.RegularExpressions.Regex]::Match($(PowerShellVersion), $(RegexGitVersion)).Groups[1].Value)</PSCoreBuildVersion>
<PSCoreAdditionalCommits>$([System.Text.RegularExpressions.Regex]::Match($(PowerShellVersion), $(RegexGitVersion)).Groups[2].Value)</PSCoreAdditionalCommits>
<PSCoreCommitSHA>$([System.Text.RegularExpressions.Regex]::Match($(PowerShellVersion), $(RegexGitVersion)).Groups[3].Value)</PSCoreCommitSHA>
<PSCoreFormattedVersion Condition = "'$(ReleaseTag)' != '' or '$(PSCoreAdditionalCommits)' == '0'">$(PSCoreBuildVersion) SHA: $(PSCoreCommitSHA)</PSCoreFormattedVersion>
<PSCoreFormattedVersion Condition = "'$(PSCoreFormattedVersion)' == ''">$(PSCoreBuildVersion) Commits: $(PSCoreAdditionalCommits) SHA: $(PSCoreCommitSHA)</PSCoreFormattedVersion>
<!-- Extract the major, minor and patch version numbers, as well as the preview label.
They are currently not used anywhere, so we comment them out for now.
<RegexSymVer>^((\d+).(\d+).(\d+))(?:-(.+))?</RegexSymVer>
<PSCorePrefixVersion>$([System.Text.RegularExpressions.Regex]::Match($(PSCoreBuildVersion), $(RegexSymVer)).Groups[1].Value)</PSCorePrefixVersion>
<PSCoreMajorVersion>$([System.Text.RegularExpressions.Regex]::Match($(PSCoreBuildVersion), $(RegexSymVer)).Groups[2].Value)</PSCoreMajorVersion>
<PSCoreMinorVersion>$([System.Text.RegularExpressions.Regex]::Match($(PSCoreBuildVersion), $(RegexSymVer)).Groups[3].Value)</PSCoreMinorVersion>
<PSCorePatchVersion>$([System.Text.RegularExpressions.Regex]::Match($(PSCoreBuildVersion), $(RegexSymVer)).Groups[4].Value)</PSCorePatchVersion>
<PSCoreLabelVersion>$([System.Text.RegularExpressions.Regex]::Match($(PSCoreBuildVersion), $(RegexSymVer)).Groups[5].Value)</PSCoreLabelVersion>
-->
<!--
Here we define explicitly 'Version' to set 'FileVersion' and 'AssemblyVersion' by 'GetAssemblyVersion' target in 'Microsoft.NET.GenerateAssemblyInfo.targets'.
Here we define explicitly 'InformationalVersion' because by default it is defined as 'Version' by 'GetAssemblyVersion' target in 'Microsoft.NET.GenerateAssemblyInfo.targets'.
-->
<Version>$(PSCoreBuildVersion)</Version>
<InformationalVersion>$(PSCoreFormattedVersion)</InformationalVersion>
<ProductVersion>$(PSCoreFormattedVersion)</ProductVersion>
<!--
We have explicitly assign 'PackageVersion'
because there is a bug: 'PackageVersion' is correctly assigned as 'Version' in 'NuGet.targets'
but then immediately redefined as '1.0.0'.
Tracking Issue https://github.com/dotnet/sdk/issues/1557
-->
<PackageVersion>$(PSCoreBuildVersion)</PackageVersion>
</PropertyGroup>
<!-- Output For Debugging
<WriteLinesToFile File="targetfile1.txt"
Lines="ReleaseTag=$(ReleaseTag);
PowerShellVersion=$(PowerShellVersion);
PSCoreBuildVersion = $(PSCoreBuildVersion);
PSCoreAdditionalCommits = $(PSCoreAdditionalCommits);
PSCoreCommitSHA = $(PSCoreCommitSHA);
PSCoreMajorVersion = $(PSCoreMajorVersion);
PSCoreMinorVersion = $(PSCoreMinorVersion);
PSCorePatchVersion = $(PSCorePatchVersion);
PSCoreLabelVersion = $(PSCoreLabelVersion);
RegexGitVersion = $(RegexGitVersion);
PSCoreFormattedVersion = $(PSCoreFormattedVersion);
ProductVersion = $(ProductVersion);
Version = $(Version);
ProjectVersion = '!$(ProjectVersion)!'
InformationalVersion = $(InformationalVersion);
"
Overwrite="true" />
-->
</Target>
<PropertyGroup>
<Product>PowerShell Core</Product>
<Company>Microsoft Corporation</Company>
<Copyright>(c) Microsoft Corporation. All rights reserved.</Copyright>
<VersionPrefix>6.0.0</VersionPrefix>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
@ -17,4 +102,5 @@
<AssemblyOriginatorKeyFile>../signing/visualstudiopublic.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
</Project>

View file

@ -51,6 +51,8 @@ function Sync-PSTags
# Gets the latest tag for the current branch
function Get-PSLatestTag
{
[CmdletBinding()]
param()
# This function won't always return the correct value unless tags have been sync'ed
# So, Write a warning to run Sync-PSTags
if(!$tagsUpToDate)
@ -63,6 +65,7 @@ function Get-PSLatestTag
function Get-PSVersion
{
[CmdletBinding()]
param(
[switch]
$OmitCommitId
@ -79,6 +82,8 @@ function Get-PSVersion
function Get-PSCommitId
{
[CmdletBinding()]
param()
# This function won't always return the correct value unless tags have been sync'ed
# So, Write a warning to run Sync-PSTags
if(!$tagsUpToDate)
@ -339,7 +344,7 @@ function Start-PSBuild {
$gitCommitId = $ReleaseTag
if (-not $gitCommitId) {
# if ReleaseTag is not specified, use 'git describe' to get the commit id
$gitCommitId = Get-PSCommitId
$gitCommitId = Get-PSCommitId -WarningAction SilentlyContinue
}
$gitCommitId > "$psscriptroot/powershell.version"
@ -416,6 +421,11 @@ Fix steps:
$Arguments += "--runtime", $Options.Runtime
}
if ($ReleaseTag) {
$ReleaseTagToUse = $ReleaseTag -Replace '^v'
$Arguments += "/property:ReleaseTag=$ReleaseTagToUse"
}
# handle Restore
if ($Restore -or -not (Test-Path "$($Options.Top)/obj/project.assets.json")) {
log "Run dotnet restore"
@ -1392,20 +1402,21 @@ function Publish-NuGetFeed
{
param(
[string]$OutputPath = "$PSScriptRoot/nuget-artifacts",
[Parameter(Mandatory=$true)]
[string]$VersionSuffix
[ValidatePattern("^v\d+\.\d+\.\d+(-\w+\.\d+)?$")]
[ValidateNotNullOrEmpty()]
[string]$ReleaseTag
)
# Add .NET CLI tools to PATH
Find-Dotnet
if ($VersionSuffix) {
## NuGet/Home #3953, #4337 -- dotnet pack - version suffix missing from ProjectReference
## Workaround:
## dotnet restore /p:VersionSuffix=<suffix> # Bake the suffix into project.assets.json
## dotnet pack --version-suffix <suffix>
$TopProject = (New-PSOptions).Top
dotnet restore $TopProject "/p:VersionSuffix=$VersionSuffix"
## We update 'project.assets.json' files with new version tag value by 'GetPSCoreVersionFromGit' target.
$TopProject = (New-PSOptions).Top
if ($ReleaseTag) {
$ReleaseTagToUse = $ReleaseTag -Replace '^v'
dotnet restore $TopProject "/property:ReleaseTag=$ReleaseTagToUse"
} else {
dotnet restore $TopProject
}
try {
@ -1423,8 +1434,8 @@ function Publish-NuGetFeed
'Microsoft.WSMan.Runtime',
'Microsoft.PowerShell.SDK'
) | ForEach-Object {
if ($VersionSuffix) {
dotnet pack "src/$_" --output $OutputPath --version-suffix $VersionSuffix /p:IncludeSymbols=true
if ($ReleaseTag) {
dotnet pack "src/$_" --output $OutputPath "/property:IncludeSymbols=true;ReleaseTag=$ReleaseTagToUse"
} else {
dotnet pack "src/$_" --output $OutputPath
}

View file

@ -94,7 +94,7 @@ Function Test-DailyBuild
{
return $true
}
# if [Feature] is in the commit message,
# Run Daily tests
if($env:APPVEYOR_REPO_COMMIT_MESSAGE -match '\[feature\]')
@ -208,7 +208,6 @@ function Invoke-AppVeyorInstall
}
elseif($env:APPVEYOR_REPO_COMMIT_MESSAGE -notmatch '^\[Daily\].*$')
{
$buildName += $env:APPVEYOR_REPO_COMMIT_MESSAGE
}
else
@ -451,24 +450,26 @@ function Invoke-AppveyorFinish
if ($env:APPVEYOR_REPO_TAG_NAME)
{
# ignore the first part of semver, use the preview part
$preReleaseVersion = ($env:APPVEYOR_REPO_TAG_NAME).Split('-')[1]
$preReleaseVersion = $env:APPVEYOR_REPO_TAG_NAME
}
else
{
$previewLabel = (git describe --abbrev=0).Split('-')[1].replace('.','')
$previewVersion = (git describe --abbrev=0).Split('-')
$previewPrefix = $previewVersion[0]
$previewLabel = $previewVersion[1].replace('.','')
if(Test-DailyBuild)
{
$previewLabel= "daily-{0}" -f $previewLabel
$previewLabel= "daily{0}" -f $previewLabel
}
$preReleaseVersion = "$previewLabel-$($env:APPVEYOR_BUILD_NUMBER.replace('.','-'))"
$preReleaseVersion = "$previewPrefix-$previewLabel.$env:APPVEYOR_BUILD_NUMBER"
}
# only publish to nuget feed if it is a daily build and tests passed
if((Test-DailyBuild) -and $env:TestPassed -eq 'True')
{
Publish-NuGetFeed -OutputPath .\nuget-artifacts -VersionSuffix $preReleaseVersion
Publish-NuGetFeed -OutputPath .\nuget-artifacts -ReleaseTag $preReleaseVersion
}
$nugetArtifacts = Get-ChildItem .\nuget-artifacts -ErrorAction SilentlyContinue | ForEach-Object { $_.FullName }
@ -498,7 +499,7 @@ function Invoke-AppveyorFinish
{
log "pushing $_ to $env:NUGET_URL"
Start-NativeExecution -sb {dotnet nuget push $_ --api-key $env:NUGET_KEY --source "$env:NUGET_URL/api/v2/package"} -IgnoreExitcode
}
}
}
if(!$pushedAllArtifacts)
{