-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinvoke.build.ps1
206 lines (171 loc) · 7.34 KB
/
invoke.build.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Install PowerShell Core:
# https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell
# Install Invoke-Build:
# https://github.com/nightroman/Invoke-Build
# (Optional) Add "Set-Alias ib Invoke-Build" to your PS profile.
# At a PS prompt, run any build task (optionally use "ib" alias):
# Invoke-Build build
# Invoke-Build ? # this lists available tasks
param (
$NuGetApiPushKey = ( property NuGetApiPushKey 'MISSING' ) ,
$LocalPackageDir = ( property LocalPackageDir 'C:/code/LocalPackages' ) ,
$Configuration = "Release"
)
[ System.Environment ]::CurrentDirectory = $BuildRoot
$baseProjectName = "TypedCssClasses"
$basePackageName = "Zanaptak.$baseProjectName"
$mainProjectFilePath = "src/$baseProjectName.fsproj"
function trimLeadingZero {
param ( $item )
$item = $item.TrimStart( '0' )
if ( $item -eq "" ) { "0" } else { $item }
}
function combinePrefixSuffix {
param ( $prefix , $suffix )
"$prefix-$suffix".TrimEnd( '-' )
}
function writeProjectFileProperty {
param ( $projectFile , $propertyName , $propertyValue )
$xml = New-Object System.Xml.XmlDocument
$xml.PreserveWhitespace = $true
$xml.Load( $projectFile )
$nodePath = '/Project/PropertyGroup/' + $propertyName
$node = $xml.SelectSingleNode( $nodePath )
$node.InnerText = $propertyValue
$settings = New-Object System.Xml.XmlWriterSettings
$settings.OmitXmlDeclaration = $true
$settings.Encoding = New-Object System.Text.UTF8Encoding( $true )
$writer = [ System.Xml.XmlWriter ]::Create( $projectFile , $settings )
try {
$xml.Save( $writer )
} finally {
$writer.Dispose()
}
}
function readProjectFileProperty {
param ( $projectFile , $propertyName )
$nodePath = '/Project/PropertyGroup/' + $propertyName
$propertyValue =
Select-Xml -Path $projectFile -XPath $nodePath |
Select-Object -First 1 |
& { process { $_.Node.InnerXml.Trim() } }
$propertyValue
}
function changelogTopVersionAndDate {
$topSectionVersion = ""
$topSectionDate = ""
foreach ( $line in ( Get-Content .\CHANGELOG.md ) ) {
$versionMatch = ( [ regex ] "^#+ (\d+(?:\.\d+){2,}(?:-\S+)?) \((\S+)\)" ).Match( $line )
if ( $versionMatch.Success ) {
$topSectionVersion , $topSectionDate = $versionMatch.Groups[ 1 ].Value , $versionMatch.Groups[ 2 ].Value
break
}
}
$topSectionVersion , $topSectionDate
}
task . Build
task Clean {
exec { dotnet clean ./src -c $Configuration }
}
task Build {
exec { dotnet build ./src -c $Configuration }
}
task Test {
$script:Configuration = "ReleaseTest"
} , Clean , Build , {
exec { dotnet test ./test/TypedCssClasses.Tests -c $Configuration }
}
task IncrementMajor LoadVersion , {
$version = [ System.Version ] $VersionPrefix
$newVersionPrefix = [ System.Version ]::new( ( $version.Major + 1 ) , 0 , 0 ).ToString( 3 )
writeProjectFileProperty $mainProjectFilePath "VersionPrefix" $newVersionPrefix
writeProjectFileProperty $mainProjectFilePath "VersionSuffix" ""
} , ReportProjectFileVersion
task IncrementMinor LoadVersion , {
$version = [ System.Version ] $VersionPrefix
$newVersionPrefix = [ System.Version ]::new( $version.Major , ( $version.Minor + 1 ) , 0 ).ToString( 3 )
writeProjectFileProperty $mainProjectFilePath "VersionPrefix" $newVersionPrefix
writeProjectFileProperty $mainProjectFilePath "VersionSuffix" ""
} , ReportProjectFileVersion
task IncrementPatch LoadVersion , {
$version = [ System.Version ] $VersionPrefix
$newVersionPrefix = [ System.Version ]::new( $version.Major , $version.Minor , ( $version.Build + 1 ) ).ToString( 3 )
writeProjectFileProperty $mainProjectFilePath "VersionPrefix" $newVersionPrefix
writeProjectFileProperty $mainProjectFilePath "VersionSuffix" ""
} , ReportProjectFileVersion
task IncrementPre LoadVersion , {
$now = Get-Date
$pre1 = $now.ToString( "yyyyMMdd" )
$pre2 = trimLeadingZero $now.ToString( "HHmmssff" )
$newVersionSuffix = "pre.{0}.{1}" -f $pre1 , $pre2
writeProjectFileProperty $mainProjectFilePath "VersionSuffix" $newVersionSuffix
} , ReportProjectFileVersion
task ClearPre LoadVersion , {
writeProjectFileProperty $mainProjectFilePath "VersionSuffix" ""
} , ReportProjectFileVersion
task ReportProjectFileVersion {
$actualVersionPrefix = readProjectFileProperty $mainProjectFilePath "VersionPrefix"
$actualVersionSuffix = readProjectFileProperty $mainProjectFilePath "VersionSuffix"
$actualFullVersion = combinePrefixSuffix $actualVersionPrefix $actualVersionSuffix
Write-Build Green "Version: $actualFullVersion"
}
task LoadVersion {
$script:VersionPrefix = readProjectFileProperty $mainProjectFilePath "VersionPrefix"
$script:VersionSuffix = readProjectFileProperty $mainProjectFilePath "VersionSuffix"
$script:FullVersion = combinePrefixSuffix $VersionPrefix $VersionSuffix
}
task Pack {
$script:Configuration = "Release"
} , Clean , Build , {
exec { dotnet pack ./src -c Release }
}
task PackInternal {
$script:Configuration = "Debug"
} , Clean , Build , LoadVersion , {
$yearStart = Get-Date -Year ( ( Get-Date ).Year ) -Month 1 -Day 1 -Hour 0 -Minute 0 -Second 0 -Millisecond 0
$now = Get-Date
$seconds = [ int ] ( $now - $yearStart ).TotalSeconds
if ( $VersionSuffix ) {
$internalVersionPrefix = $VersionPrefix
$internalVersionSuffix = "$VersionSuffix.$seconds"
}
else {
$internalVersionPrefix = "$VersionPrefix.$seconds"
$internalVersionSuffix = $VersionSuffix
}
exec { dotnet pack ./src -c $Configuration -p:VersionPrefix=$internalVersionPrefix -p:VersionSuffix=$internalVersionSuffix }
$internalFullVersion = combinePrefixSuffix $internalVersionPrefix $internalVersionSuffix
$filename = "$basePackageName.$internalFullVersion.nupkg"
Copy-Item ./src/bin/$Configuration/$filename $LocalPackageDir
Write-Build Green "Copied $filename to $LocalPackageDir"
}
task UploadNuGet EnsureCommitted , LoadVersion , {
if ( $NuGetApiPushKey -eq "MISSING" ) { throw "NuGet key not provided" }
Set-Location ./src/bin/Release
$filename = "$basePackageName.$FullVersion.nupkg"
if ( -not ( Test-Path $filename ) ) { throw "nupkg file not found" }
$lastHour = ( Get-Date ).AddHours( -1 )
if ( ( Get-ChildItem $filename ).LastWriteTime -lt $lastHour ) { throw "nupkg file too old" }
exec { dotnet nuget push $filename -k $NuGetApiPushKey -s https://api.nuget.org/v3/index.json }
}
task EnsureCommitted {
$gitoutput = exec { git status -s -uall }
if ( $gitoutput ) { throw "uncommitted changes exist in working directory" }
}
task UpdateProjectFromChangelog {
$version , $date = changelogTopVersionAndDate
if ( $version -match '-' ) {
$prefix , $suffix = $version -split '-'
}
else {
$prefix , $suffix = $version , ""
}
writeProjectFileProperty $mainProjectFilePath "VersionPrefix" $prefix
writeProjectFileProperty $mainProjectFilePath "VersionSuffix" $suffix
$anchor = ( $version -replace '\.','' ) + "-$date"
$url = "https://github.com/zanaptak/TypedCssClasses/blob/main/CHANGELOG.md#$anchor"
writeProjectFileProperty $mainProjectFilePath "PackageReleaseNotes" $url
Write-Build Green "****"
Write-Build Green "**** Assumed changelog URL (VERIFY): $url"
Write-Build Green "****"
} , ReportProjectFileVersion