-
Notifications
You must be signed in to change notification settings - Fork 2
/
GitVersion.Task.ps1
132 lines (117 loc) · 6.28 KB
/
GitVersion.Task.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
$Script:GitVersionMessagePrefix ??= "semver"
$Script:GitVersionTagPrefix ??= "v"
Add-BuildTask GitVersion @{
Inputs = {
# Exclude generated source files in /obj/ folders
Get-ChildItem $BuildRoot -Recurse -File
}
Outputs = {
if ($script:BuildSystem -eq "None") {
# Locally, we can never skip versioning, because someone could have tagged git
$BuildRoot
} else {
# In the build system, run it ONCE PER BUILD PER PROJECT
# Use a $TempRoot the build cleans
$VersionFile = Join-Path $TempRoot -ChildPath "$GitSha.json"
if (Test-Path $VersionFile) {
$script:GitVersion = Get-Content $VersionFile | ConvertFrom-Json
}
$VersionFile
}
}
Jobs = {
$VersionFile = Join-Path $TempRoot -ChildPath "$GitSha.json"
$script:GitVersion = @{}
foreach ($Name in $PackageNames) {
if ($PackageNames.Count -gt 1) {
$GitVersionMessagePrefix = ($GitVersionMessagePrefix, $Name) -join "-"
$GitVersionTagPrefix = ($Name, $GitVersionTagPrefix) -join "-"
}
# Since we know the things we need to version, let's make *sure* that we version it:
# Write-Host git commit "--ammend" "-m" "$commitMessage`n$GitVersionMessagePrefix:patch"
# git commit --ammend -m "$commitMessage`n$GitVersionMessagePrefix:patch"
$GitVersionYaml = if (Test-Path (Join-Path $BuildRoot GitVersion.yml)) {
Join-Path $BuildRoot GitVersion.yml
} else {
Convert-Path (Join-Path $PSScriptRoot GitVersion.yml)
}
Write-Verbose "For ${Name}: Using GitVersion config $GitVersionYaml" -Verbose
$LogFile = Join-Path $TempRoot -ChildPath "$GitVersionTagPrefix$GitSha.log"
if (Test-Path $LogFile) {
Remove-Item $LogFile
}
if (Test-Path $VersionFile) {
Remove-Item $VersionFile
}
try {
# We can't splat because it's 5 copies of the same parameter, so, use line-wrapping escapes:
# Also, the no-bump-message has to stay at .* or else every commit to main will increment all components
# Write-Host dotnet gitversion -config $GitVersionYaml -output file -outputfile $VersionFile -verbosity verbose
<# -output file -outputfile $VersionFile #>
dotnet gitversion -verbosity diagnostic -config $GitVersionYaml `
-overrideconfig tag-prefix="$($GitVersionTagPrefix)" `
-overrideconfig major-version-bump-message="$($GitVersionMessagePrefix):\s*(breaking|major)" `
-overrideconfig minor-version-bump-message="$($GitVersionMessagePrefix):\s*(feature|minor)" `
-overrideconfig patch-version-bump-message="$($GitVersionMessagePrefix):\s*(fix|patch)" `
-overrideconfig no-bump-message="$($GitVersionMessagePrefix):\s*(skip|none)" > $VersionFile 2> $LogFile
if (Test-Path $LogFile) {
Write-Host $PSStyle.Formatting.Error ((Get-Content $LogFile) -join "`n") $PSStyle.Reset
}
if (!(Test-Path $VersionFile)) {
throw "GitVersion failed to produce a version file or a log file"
} else {
$VersionContent = Get-Content $VersionFile
if (!$VersionContent) {
throw "GitVersion produced an empty version file"
}
try {
$Version = $VersionContent | ConvertFrom-Json
} catch {
throw "GitVersion produced an invalid version file: $VersionContent"
}
}
} catch {
Write-Warning "GitVersion failed $($_.Exception.Message) trying with URL $GitUrl"
dotnet gitversion -url $GitUrl -b $BranchName -c $GitSha -config $GitVersionYaml `
-overrideconfig tag-prefix="$($GitVersionTagPrefix)" `
-overrideconfig major-version-bump-message="$($GitVersionMessagePrefix):\s*(breaking|major)" `
-overrideconfig minor-version-bump-message="$($GitVersionMessagePrefix):\s*(feature|minor)" `
-overrideconfig patch-version-bump-message="$($GitVersionMessagePrefix):\s*(fix|patch)" `
-overrideconfig no-bump-message="$($GitVersionMessagePrefix):\s*(skip|none)" > $VersionFile 2> $LogFile
if (Test-Path $LogFile) {
Write-Host $PSStyle.Formatting.Error ((Get-Content $LogFile) -join "`n") $PSStyle.Reset
}
if (!(Test-Path $VersionFile)) {
throw "GitVersion failed to produce a version file or a log file"
} else {
$VersionContent = Get-Content $VersionFile
if (!$VersionContent) {
throw "GitVersion produced an empty version file"
}
try {
$Version = $VersionContent | ConvertFrom-Json
} catch {
throw "GitVersion produced an invalid version file: $VersionContent"
}
}
}
$Version | Add-Member ScriptProperty Tag -Value { $GitVersionTagPrefix + $this.SemVer } -PassThru | Format-List | Out-Host
$GitVersion[$Name] = $Version
# Output for Azure DevOps
if ($ENV:SYSTEM_COLLECTIONURI) {
foreach ($envar in $Version.PSObject.Properties) {
$EnvVarName = if ($Name) {
@($Name, $Envar.Name) -join "."
} else {
$Envar.Name
}
Write-Host "INFO [task.setvariable variable=$EnvVarName;isOutput=true]$($envar.Value)"
Write-Host "##vso[task.setvariable variable=$EnvVarName;isOutput=true]$($envar.Value)"
}
} else {
Write-Host "GitVersion: $($Version.InformationalVersion)"
}
}
$GitVersion | ConvertTo-Json | Set-Content $VersionFile
}
}