Skip to content

Commit d7785d6

Browse files
author
Nate McMaster
committed
Add tag repos script
1 parent 7235918 commit d7785d6

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

scripts/TagRepos.ps1

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env pwsh
2+
3+
<#
4+
.SYNOPSIS
5+
Tags each repo according to VersionPrefix in version.props of that repo
6+
.PARAMETER Push
7+
Push all updated tags
8+
.PARAMETER ForceUpdateTag
9+
This will call git tag --force
10+
#>
11+
[cmdletbinding(SupportsShouldProcess = $true)]
12+
param(
13+
[switch]$Push = $false,
14+
[switch]$ForceUpdateTag = $false
15+
)
16+
17+
Set-StrictMode -Version 2
18+
$ErrorActionPreference = 'Stop'
19+
20+
Import-Module -Scope Local -Force "$PSScriptRoot/common.psm1"
21+
22+
Assert-Git
23+
24+
$RepoRoot = Resolve-Path "$PSScriptRoot/../"
25+
26+
Get-Submodules $RepoRoot -Shipping | % {
27+
Push-Location $_.path | Out-Null
28+
try {
29+
30+
if (-not $_.versionPrefix) {
31+
Write-Warning "Could not determine tag version for $(_.path)"
32+
}
33+
else {
34+
$tag = $_.versionPrefix
35+
Write-Host "$($_.module) => $tag"
36+
37+
$gitTagArgs = @()
38+
if ($ForceUpdateTag) {
39+
$gitTagArgs += '--force'
40+
}
41+
42+
Invoke-Block { & git tag @gitTagArgs $tag }
43+
44+
if ($Push) {
45+
$gitPushArgs = @()
46+
if ($WhatIfPreference) {
47+
$gitPushArgs += '--dry-run'
48+
}
49+
Invoke-Block { & git push --dry-run @gitPushArgs origin "refs/tags/${tag}" }
50+
}
51+
52+
if ($WhatIfPreference) {
53+
Invoke-Block { & git tag -d $tag } | Out-Null
54+
}
55+
}
56+
}
57+
catch {
58+
Write-Host -ForegroundColor Red "Could not update $_"
59+
throw
60+
}
61+
finally {
62+
Pop-Location
63+
}
64+
}
65+
66+

scripts/common.psm1

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
function Assert-Git {
2+
if (!(Get-Command git -ErrorAction Ignore)) {
3+
Write-Error 'git is required to execute this script'
4+
exit 1
5+
}
6+
}
7+
8+
function Invoke-Block([scriptblock]$cmd) {
9+
$cmd | Out-String | Write-Verbose
10+
& $cmd
11+
12+
# Need to check both of these cases for errors as they represent different items
13+
# - $?: did the powershell script block throw an error
14+
# - $lastexitcode: did a windows command executed by the script block end in error
15+
if ((-not $?) -or ($lastexitcode -ne 0)) {
16+
Write-Warning $error[0]
17+
throw "Command failed to execute: $cmd"
18+
}
19+
}
20+
21+
function Get-Submodules {
22+
param(
23+
[Parameter(Mandatory = $true)]
24+
[string]$RepoRoot,
25+
[switch]$Shipping
26+
)
27+
28+
$moduleConfigFile = Join-Path $RepoRoot ".gitmodules"
29+
$submodules = @()
30+
31+
[xml] $submoduleConfig = Get-Content "$RepoRoot/build/submodules.props"
32+
$repos = $submoduleConfig.Project.ItemGroup.Repository | % { $_.Include }
33+
34+
Get-ChildItem "$RepoRoot/modules/*" -Directory `
35+
| ? { (-not $Shipping) -or $($repos -contains $($_.Name)) -or $_.Name -eq 'Templating' } `
36+
| % {
37+
Push-Location $_ | Out-Null
38+
Write-Verbose "Attempting to get submodule info for $_"
39+
40+
if (Test-Path 'version.props') {
41+
[xml] $versionXml = Get-Content 'version.props'
42+
$versionPrefix = $versionXml.Project.PropertyGroup.VersionPrefix
43+
} else {
44+
$versionPrefix = ''
45+
}
46+
47+
try {
48+
$data = @{
49+
path = $_
50+
module = $_.Name
51+
commit = $(git rev-parse HEAD)
52+
newCommit = $null
53+
changed = $false
54+
branch = $(git config -f $moduleConfigFile --get submodule.modules/$($_.Name).branch )
55+
versionPrefix = $versionPrefix
56+
}
57+
58+
$submodules += $data
59+
}
60+
finally {
61+
Pop-Location | Out-Null
62+
}
63+
}
64+
65+
return $submodules
66+
}

0 commit comments

Comments
 (0)