-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
scripts.ps1
179 lines (136 loc) · 4.13 KB
/
scripts.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
[CmdletBinding()]
param (
[ValidateSet('info', 'build', 'test', 'pack', 'tag-version')]
[string]$Script = 'info',
[switch]$NoBuildInfo = $false
)
function WriteInfo([string] $text) {
Write-Host $text -ForegroundColor Blue
}
function WriteError([string] $text) {
Write-Host $text -ForegroundColor Red
}
function WriteHeader([string] $text) {
WriteInfo '#------------'
WriteInfo "# $text"
WriteInfo '#------------'
}
function ExitIfFailed() {
if ($LASTEXITCODE -ne 0) {
WriteError "Failed with code $LASTEXITCODE. Exiting..."
Exit 1
}
}
function CreateStamp() {
return Get-Date -UFormat '+%Y%m%dT%H%M%S'
}
function CleanArtifacts() {
if (Test-Path 'artifacts') {
Remove-Item -Recurse 'artifacts'
}
}
function CompoundVersionSuffix([string[]] $versionSuffixes) {
return (@($versionSuffixes) | Where-Object { $_ }) -join '.'
}
function LoadBuildInfo() {
# Resolve src/test projects
$srcProjects = @(Get-ChildItem -Path './src/*/*.csproj' -File)
$testProjects = @(Get-ChildItem -Path './test/*/*.csproj' -File)
$allProjects = $srcProjects + $testProjects
# git info
$sha = git rev-parse HEAD
$hasUncommittedChanges = !([string]::IsNullOrEmpty($(git status --porcelain)))
$tags = [string[]]@(git tag --points-at HEAD) | Where-Object { $_ -Match 'v[0-9]+\.[0-9]+\.[0-9]+.*' }
$tagged = !!$tags.Length
# ci info
$ci = Test-Path env:GITHUB_ACTIONS
$addCIPrerelease = !$tagged
# Load version info
$versionPrefix = Select-Xml -Path 'version.props' -XPath '/Project/PropertyGroup/VersionPrefix' | ForEach-Object { $_.Node.InnerXML }
$versionSuffix = Select-Xml -Path 'version.props' -XPath '/Project/PropertyGroup/VersionSuffix' | ForEach-Object { $_.Node.InnerXML }
$version = (@($versionPrefix, $versionSuffix) | Where-Object { $_ }) -join '-'
# ---
$stamp = CreateStamp
return [PSCustomObject]@{
SrcProjects = $srcProjects
TestProjects = $testProjects
AllProjects = $allProjects
Sha = $sha
HasUncommittedChanges = $hasUncommittedChanges
Tags = $tags
Tagged = $tagged
CI = $ci
AddCIPrerelease = $addCIPrerelease
Stamp = $stamp
VersionPrefix = $versionPrefix
VersionSuffix = $versionSuffix
Version = $version
}
}
function PathToName($path) {
return Split-Path $path -leaf
}
function PrintBuildInfo() {
$buildInfo |
Select-Object @{name = 'SrcProjects'; expression = { PathToName $_.SrcProjects } }, @{name = 'TestProjects'; expression = { PathToName $_.TestProjects } }, Sha, Tags, Tagged, CI, AddCIPrerelease, Stamp, VersionPrefix, VersionSuffix, Version |
Format-List
}
function TagVersion() {
if ($buildInfo.HasUncommittedChanges) {
WriteError "You have uncommitted changes. Tagging failed."
Exit 1
}
$version = (@($buildInfo.VersionPrefix, $buildInfo.VersionSuffix) | Where-Object { $_ }) -join '-'
$tag = "v$version"
WriteInfo "Tagging: $tag"
# Create an annotated tag.
git tag -m $tag $tag
}
function BuildProjects() {
WriteHeader 'Building'
foreach ($srcProject in $buildInfo.SrcProjects) {
dotnet build $srcProject -c Release
}
ExitIfFailed
}
function TestProjects() {
WriteHeader 'Testing'
$testLoggersArg = ''
if ($ci) {
$testLoggersArg = '--logger "GitHubActions;report-warnings=false"'
}
foreach ($testProject in $buildInfo.TestProjects) {
Invoke-Expression "dotnet test $testProject -c Release $testLoggersArg"
}
ExitIfFailed
}
function PackProjects {
WriteHeader 'Packing'
CleanArtifacts
$versionSuffixArg = ''
if ($buildInfo.AddCIPrerelease) {
$versionSuffix = CompoundVersionSuffix ($buildInfo.VersionSuffix, "ci.$($buildInfo.Stamp)+sha.$($buildInfo.Sha)")
$versionSuffixArg = "--version-suffix $versionSuffix"
}
foreach ($srcProject in $buildInfo.SrcProjects) {
Invoke-Expression "dotnet pack $srcProject -c Release -o artifacts/packages $versionSuffixArg"
}
ExitIfFailed
}
# ===
$buildInfo = LoadBuildInfo
if (!$NoBuildInfo) {
PrintBuildInfo
}
if ($Script -eq 'build') {
BuildProjects
}
if ($Script -eq 'test') {
TestProjects
}
if ($Script -eq 'pack') {
PackProjects
}
if ($Script -eq 'tag-version') {
TagVersion
}