From 4dc2c17f04cb0fe082ac5b70dcf94fac52c7f8a2 Mon Sep 17 00:00:00 2001 From: Nate McMaster Date: Mon, 26 Feb 2018 16:32:02 -0800 Subject: [PATCH] Add script for setting and pushing tags (#919) --- scripts/GenerateTags.ps1 | 106 ++++++++++++++++++++++++++++++ scripts/GetPackageVersion.targets | 5 ++ scripts/common.psm1 | 24 ++++--- 3 files changed, 125 insertions(+), 10 deletions(-) create mode 100755 scripts/GenerateTags.ps1 create mode 100644 scripts/GetPackageVersion.targets diff --git a/scripts/GenerateTags.ps1 b/scripts/GenerateTags.ps1 new file mode 100755 index 0000000000..958ab36378 --- /dev/null +++ b/scripts/GenerateTags.ps1 @@ -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 +} diff --git a/scripts/GetPackageVersion.targets b/scripts/GetPackageVersion.targets new file mode 100644 index 0000000000..061c114027 --- /dev/null +++ b/scripts/GetPackageVersion.targets @@ -0,0 +1,5 @@ + + + + + diff --git a/scripts/common.psm1 b/scripts/common.psm1 index d1d493eaf0..87205a92d5 100644 --- a/scripts/common.psm1 +++ b/scripts/common.psm1 @@ -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