Add script for setting and pushing tags (#919)

This commit is contained in:
Nate McMaster 2018-02-26 16:32:02 -08:00 committed by GitHub
parent 9dc18f864a
commit 4dc2c17f04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 125 additions and 10 deletions

106
scripts/GenerateTags.ps1 Executable file
View File

@ -0,0 +1,106 @@
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Generates a tag on this repo and adds a tag for each submodule that corresponds
to the value in version.props
.PARAMETER Push
Push the tag to origin
.PARAMETER WhatIf
Dry run
#>
[cmdletbinding(PositionalBinding = $false, SupportsShouldProcess = $true)]
param(
[switch]$Push
)
$ErrorActionPreference = 'Stop'
Import-Module -Scope Local -Force "$PSScriptRoot/common.psm1"
Set-StrictMode -Version 1
function New-GitTag {
[cmdletbinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true)]
[string]$Repo,
[Parameter(Mandatory = $true)]
[string]$Tag
)
Push-Location $Repo
try {
git show-ref --tags --verify "refs/tags/$Tag" -q
$existingTag = $?
if ($existingTag) {
Write-Warning "${Repo}: Tag '$Tag' already exists. Skipped adding tag"
}
else {
if ($PSCmdlet.ShouldProcess($Repo, "Tag $Tag")) {
Invoke-Block { & git tag -m "v$Tag" $Tag HEAD }
Write-Host -f Magenta "${Repo}: added tag '$Tag'"
}
}
if ($Push -and $PSCmdlet.ShouldProcess($Repo, "Push tag $Tag to origin")) {
Invoke-Block { & git push origin refs/tags/$Tag }
}
}
finally {
Pop-Location
}
}
#
# Gets the package version by invoking KoreBuild on a repo with a custom target that spits out the package version
#
function Get-PackageVersion([string]$repoRoot) {
$buildScript = if (-not $IsCoreCLR -or $IsWindows) { 'build.ps1' } else { 'build.sh' }
$inspectTarget = "/p:CustomAfterKoreBuildTargets=$PSScriptRoot/GetPackageVersion.targets"
# Add the /t:Noop target which may be used by the bootstrapper to skip unimportant initialization
$output = & "$repoRoot/$buildScript" $inspectTarget /v:m /p:IsFinalBuild=true /t:Noop /t:GetPackageVersion
$output | out-string | Write-Verbose
if (-not $? -or $LASTEXITCODE -ne 0) {
throw "$buildScript failed on $repoRoot. Exit code $LASTEXITCODE"
}
$packageVersion = $output | where-object { $_ -like '*PackageVersion=*' } | select-object -first 1
$packageVersion = $packageVersion -replace 'PackageVersion=',''
if ($packageVersion) { $packageVersion = $packageVersion.Trim() }
if (-not $packageVersion) {
throw "Could not determine final package version for $repoRoot"
}
return $packageVersion.Trim()
}
$repoRoot = Resolve-Path "$PSScriptRoot/../"
Write-Warning "Make sure you have run ``git submodule update`` first to pin the submodules to the correct commit"
if (-not $PSCmdlet.ShouldContinue("Continue?", "This will apply tags to all submodules")) {
Write-Host "Exiting"
exit 1
}
$universeTag = Get-PackageVersion $repoRoot
New-GitTag $repoRoot $universeTag -WhatIf:$WhatIfPreference
Get-Submodules $repoRoot | ForEach-Object {
$modPath = $_.path
$module = $_.module
if (-not (Test-Path (Join-Path $_.path 'version.props'))) {
Write-Warning "$module does not have a version.props file. Skipping"
return
}
try {
$tag = Get-PackageVersion $_.path
if ($tag -ne $universeTag) {
Write-Warning "${module}: version ($tag) does not match universe ($universeTag)"
}
}
catch {
Write-Warning "${module}: Could not automatically determine tag for $modPath. Skipping"
return
}
New-GitTag $_.path $tag -WhatIf:$WhatIfPreference
}

View File

@ -0,0 +1,5 @@
<Project>
<Target Name="GetPackageVersion">
<Message Importance="high" Text="PackageVersion=$(PackageVersion)" />
</Target>
</Project>

View File

@ -32,27 +32,31 @@ function Get-Submodules {
$repos = $submoduleConfig.Project.ItemGroup.Repository | % { $_.Include }
Get-ChildItem "$RepoRoot/modules/*" -Directory `
| ? { (-not $Shipping) -or $($repos -contains $($_.Name)) -or $_.Name -eq 'Templating' } `
| % {
| ? { (-not $Shipping) -or $($repos -contains $($_.Name)) -or $_.Name -eq 'Templating' } `
| % {
Push-Location $_ | Out-Null
Write-Verbose "Attempting to get submodule info for $_"
if (Test-Path 'version.props') {
[xml] $versionXml = Get-Content 'version.props'
$versionPrefix = $versionXml.Project.PropertyGroup.VersionPrefix
} else {
$versionPrefix = $versionXml.Project.PropertyGroup.VersionPrefix | select-object -first 1
$versionSuffix = $versionXml.Project.PropertyGroup.VersionSuffix | select-object -first 1
}
else {
$versionPrefix = ''
$versionSuffix = ''
}
try {
$data = [PSCustomObject] @{
path = $_
module = $_.Name
commit = $(git rev-parse HEAD)
newCommit = $null
changed = $false
branch = $(git config -f $moduleConfigFile --get submodule.modules/$($_.Name).branch )
path = $_
module = $_.Name
commit = $(git rev-parse HEAD)
newCommit = $null
changed = $false
branch = $(git config -f $moduleConfigFile --get submodule.modules/$($_.Name).branch )
versionPrefix = $versionPrefix
versionSuffix = $versionSuffix
}
$submodules += $data