-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.build.ps1
More file actions
445 lines (363 loc) · 15.2 KB
/
Copy path.build.ps1
File metadata and controls
445 lines (363 loc) · 15.2 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<#
.Synopsis
Build script
.Description
TASKS AND REQUIREMENTS
Initialize and Clean repository
Restore packages, workflows, tools
Format code
Build projects and the solution
Run Tests
Pack
Publish
#>
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '', Justification = 'Parameter is used actually.')]
param(
# Build Version
[Parameter()]
[string]
$Version,
# Build Instance
[Parameter()]
[string]
$Instance,
# Fast mode
[Parameter()]
[switch]
$Fast
)
Set-StrictMode -Version Latest
function Update-FableNullableReferenceTypes {
param(
[Parameter(Mandatory = $true)]
[string]
$ProjectPath,
[Parameter(Mandatory = $true)]
[string]
$FableOutputPath
)
$projectFolder = Split-Path -Path $ProjectPath -Parent
[xml]$projectXml = Get-Content -Path $ProjectPath -Raw
$nullableFieldsByType = @{}
foreach ($compileItem in $projectXml.SelectNodes('//Compile')) {
$sourcePath = Join-Path -Path $projectFolder -ChildPath $compileItem.Include
if (-not (Test-Path -Path $sourcePath)) {
continue
}
$currentType = $null
foreach ($line in Get-Content -Path $sourcePath) {
if ($line -match '^\s*type\s+([A-Za-z_][A-Za-z0-9_''`]*)') {
$currentType = $Matches[1] -replace '`.*$', ''
}
if ($currentType -and ($line -match '^\s*\{?\s*([A-Za-z_][A-Za-z0-9_]*)\s*:\s*(?!Nullable<)(.+?)\s*\|\s*null\b')) {
if (-not $nullableFieldsByType.ContainsKey($currentType)) {
$nullableFieldsByType[$currentType] = [System.Collections.Generic.HashSet[string]]::new()
}
[void]$nullableFieldsByType[$currentType].Add($Matches[1])
}
}
}
if ($nullableFieldsByType.Count -eq 0) {
return
}
foreach ($typescriptFile in Get-ChildItem -Path $FableOutputPath -Filter '*.ts' -Recurse -File) {
$currentType = $null
$fileChanged = $false
$usesNullable = $false
$usesNonNullValue = $false
$updatedLines = foreach ($line in Get-Content -Path $typescriptFile.FullName) {
$updatedLine = $line
if ($updatedLine -match '^\s*export\s+class\s+([A-Za-z_][A-Za-z0-9_$]*)\b') {
$currentType = $Matches[1] -replace '\$.*$', ''
}
if ($currentType -and $nullableFieldsByType.ContainsKey($currentType)) {
foreach ($fieldName in $nullableFieldsByType[$currentType]) {
$escapedFieldName = [regex]::Escape($fieldName)
$fieldPattern = "^(\s*readonly\s+$escapedFieldName\s*:\s*)(?!Nullable<)([^;]+)(;.*)$"
if ($updatedLine -match $fieldPattern) {
$updatedLine = [regex]::Replace($updatedLine, $fieldPattern, '$1Nullable<$2>$3')
$fileChanged = $true
$usesNullable = $true
}
if ($updatedLine -match '^\s*constructor\(') {
$parameterPattern = "(\b$escapedFieldName\s*:\s*)(?!Nullable<)([^,)]+)"
$nextLine = [regex]::Replace($updatedLine, $parameterPattern, '$1Nullable<$2>')
if ($nextLine -ne $updatedLine) {
$updatedLine = $nextLine
$fileChanged = $true
$usesNullable = $true
}
}
}
}
$nonNullValuePattern = 'UrlPart_op_Implicit_Z721C83C5\((?!nonNullValue\()([A-Za-z_][A-Za-z0-9_]*\.[A-Za-z_][A-Za-z0-9_]*)\)'
$nextLine = [regex]::Replace($updatedLine, $nonNullValuePattern, 'UrlPart_op_Implicit_Z721C83C5(nonNullValue($1))')
if ($nextLine -ne $updatedLine) {
$updatedLine = $nextLine
$fileChanged = $true
$usesNonNullValue = $true
}
$updatedLine
}
if ($fileChanged) {
if ($usesNullable -and -not ($updatedLines -match 'import\s+\{[^}]*\bNullable\b[^}]*\}\s+from\s+"@fable-org/fable-library-ts/Util\.ts"')) {
$updatedLines = foreach ($line in $updatedLines) {
if ($line -match '^(import\s+\{)([^}]+)(\}\s+from\s+"@fable-org/fable-library-ts/Util\.ts";)$') {
"$($Matches[1])$($Matches[2]), Nullable $($Matches[3])"
}
else {
$line
}
}
}
if ($usesNonNullValue -and -not ($updatedLines -match 'import\s+\{[^}]*\bnonNullValue\b[^}]*\}\s+from\s+"@fable-org/fable-library-ts/Option\.ts"')) {
$updatedLines = foreach ($line in $updatedLines) {
if ($line -match '^(import\s+\{)([^}]+)(\}\s+from\s+"@fable-org/fable-library-ts/Option\.ts";)$') {
"$($Matches[1])$($Matches[2]), nonNullValue $($Matches[3])"
}
else {
$line
}
}
}
Set-Content -Path $typescriptFile.FullName -Value $updatedLines
}
}
}
# Synopsis: Initialize folders and variables
Task Init {
$trashFolder = Join-Path -Path . -ChildPath '.trash'
$trashFolder = Join-Path -Path $trashFolder -ChildPath $Instance
New-Item -Path $trashFolder -ItemType Directory | Out-Null
$trashFolder = Resolve-Path -Path $trashFolder
$buildArtifactsFolder = Join-Path -Path $trashFolder -ChildPath 'artifacts'
New-Item -Path $buildArtifactsFolder -ItemType Directory | Out-Null
$distArtifactsFolder = Join-Path -Path $buildArtifactsFolder -ChildPath 'dist'
New-Item -Path $distArtifactsFolder -ItemType Directory | Out-Null
$fableOutputArtifactsFolder = Join-Path -Path $buildArtifactsFolder -ChildPath 'fable_output'
New-Item -Path $fableOutputArtifactsFolder -ItemType Directory | Out-Null
$state = [PSCustomObject]@{
NextVersion = $null
TrashFolder = $trashFolder
BuildArtifactsFolder = $buildArtifactsFolder
DistArtifactsFolder = $distArtifactsFolder
FableOutputArtifactsFolder = $fableOutputArtifactsFolder
NuGetPackagePath = $null
NPMPackagePath = $null
}
$state | Export-Clixml -Path ".\.trash\$Instance\state.clixml"
Write-Output $state
}
# Synopsis: Clean previous build leftovers
Task Clean Init, {
Get-ChildItem -Directory
| Where-Object { -not $_.Name.StartsWith('.') }
| ForEach-Object { Get-ChildItem -Path $_ -Recurse -Directory }
| Where-Object { ( $_.Name -eq 'bin') -or ( $_.Name -eq 'obj') }
| ForEach-Object { Remove-Item -Path $_ -Recurse -Force }
}
# Synopsis: Ensure Central Package Versions compliance
Task EnsureCentralPackageVersions Clean, {
$projectFiles = Get-ChildItem -Path . `
-Recurse `
-Include *.csproj, *.fsproj, *.vbproj `
-File
$violations = @()
foreach ($projectFile in $projectFiles) {
try {
[xml]$xml = Get-Content $projectFile.FullName -Raw
}
catch {
throw "Failed to parse XML: $($projectFile.FullName)"
}
$ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$ns.AddNamespace('msb', $xml.DocumentElement.NamespaceURI)
$nodes = $xml.SelectNodes('//*[@VersionOverride]', $ns)
foreach ($node in $nodes) {
$violations += [PSCustomObject]@{
File = $projectFile.FullName
Node = $node.Name
Value = $node.GetAttribute('VersionOverride')
}
}
}
if ($violations.Count -gt 0) {
throw "VersionOverride attributes are not allowed. File: $($violations[0].File) Node: <$($violations[0].Node)>"
}
}
# Synopsis: Restore workloads
Task RestoreWorkloads -If { -not $Fast } Clean, {
Exec { dotnet workload restore }
}
# Synopsis: Restore tools
Task RestoreTools Clean, {
Exec { dotnet tool restore }
}
# Synopsis: Restore NuGet packages
Task RestoreNuGetPackages Clean, EnsureCentralPackageVersions, {
$solution = Resolve-Path -Path 'Bridge.slnx'
Exec { dotnet restore $solution }
}
# Synopsis: Restore NPM packages
Task RestoreNPMPackages Clean, {
Exec { npm ci }
}
# Synopsis: Restore
Task Restore RestoreWorkloads, RestoreTools, RestoreNuGetPackages, RestoreNPMPackages
# Synopsis: Scan with DevSkim for security issues
Task DevSkim Restore, {
$state = Import-Clixml -Path ".\.trash\$Instance\state.clixml"
$trashFolder = $state.TrashFolder
$sarifFile = Join-Path -Path $trashFolder -ChildPath 'DevSkim.sarif'
Exec { dotnet tool run devskim analyze --source-code . --output-file $sarifFile }
Exec { dotnet tool run devskim fix --source-code . --sarif-result $sarifFile --all }
}
# Synopsis: Format XML Files
Task FormatXmlFiles Clean, {
Get-ChildItem -Include *.xml, *.config, *.props, *.targets, *.nuspec, *.resx, *.ruleset, *.vsixmanifest, *.vsct, *.xlf, *.csproj, *.fsproj, *.vbproj, *.slnx -Recurse -File
| Where-Object { -not (git check-ignore $PSItem) }
| ForEach-Object {
Write-Output "Formatting XML File: $PSItem"
$content = Get-Content -Path $PSItem -Raw
$xml = [xml]$content
$xml.Save($PSItem)
}
}
# Synopsis: Format Fantomas
Task FormatFantomas Restore, {
$solution = Resolve-Path -Path 'Bridge.slnx'
Exec { dotnet fantomas . }
}
# Synopsis: Format
Task Format -If { -not $Fast } Restore, FormatXmlFiles, FormatFantomas
# Synopsis: Estimate Next Version
Task EstimateVersion Restore, {
$state = Import-Clixml -Path ".\.trash\$Instance\state.clixml"
if ($Version) {
$state.NextVersion = [System.Management.Automation.SemanticVersion]$Version
}
else {
$gitversion = Exec { dotnet tool run dotnet-gitversion } | ConvertFrom-Json
$state.NextVersion = [System.Management.Automation.SemanticVersion]::Parse($gitversion.SemVer)
}
$state | Export-Clixml -Path ".\.trash\$Instance\state.clixml"
Write-Output "Next version estimated to be $($state.NextVersion)"
Write-Output $state
}
# Synopsis: Build Project
Task BuildProject EstimateVersion, {
$state = Import-Clixml -Path ".\.trash\$Instance\state.clixml"
$project = Resolve-Path -Path 'src/Bridge/Bridge.fsproj'
$nextVersion = $state.NextVersion
Exec { dotnet build $project /v:m --configuration Release /p:version=$nextVersion }
}
# Synopsis: Build
Task Build Format, BuildProject, {
$solution = Resolve-Path -Path 'Bridge.slnx'
Exec { dotnet build $solution --configuration Release }
}
# Synopsis: Unit Test
Task UnitTest Build, {
$project = Resolve-Path -Path 'tests/Bridge.Tests/Bridge.Tests.fsproj'
Exec { dotnet run --project $project --configuration Release }
}
# Synopsis: Functional Test
Task FunctionalTest Build, {
}
# Synopsis: Integration Test
Task IntegrationTest Build, {
if (-not $env:CI) {
}
}
# Synopsis: Test
Task Test UnitTest, FunctionalTest, IntegrationTest
# Synopsis: Pack NuGet package
Task PackNuGet Build, Test, {
$state = Import-Clixml -Path ".\.trash\$Instance\state.clixml"
$buildArtifactsFolder = $state.BuildArtifactsFolder
$nextVersion = $state.NextVersion
$projectPath = Resolve-Path -Path 'src/Bridge/Bridge.fsproj'
Exec { dotnet pack $projectPath /v:m /p:Configuration=Release /p:version=$nextVersion --output $buildArtifactsFolder }
$nugetPackage = Get-ChildItem -Path $buildArtifactsFolder -Filter '*.nupkg' | Select-Object -First 1
$state.NuGetPackagePath = $nugetPackage.FullName
$state | Export-Clixml -Path ".\.trash\$Instance\state.clixml"
Write-Output $state
}
# Synopsis: Pack NPM package
Task PackNPM Build, Test, {
$state = Import-Clixml -Path ".\.trash\$Instance\state.clixml"
$buildArtifactsFolder = $state.BuildArtifactsFolder
$distArtifactsFolder = $state.DistArtifactsFolder
$fableOutputArtifactsFolder = $state.FableOutputArtifactsFolder
$nextVersion = $state.NextVersion
$projectPath = Resolve-Path -Path 'src/Bridge/Bridge.fsproj'
Exec { dotnet fable $projectPath --outDir $fableOutputArtifactsFolder --language typescript --fableLib @fable-org/fable-library-ts }
Update-FableNullableReferenceTypes -ProjectPath $projectPath -FableOutputPath $fableOutputArtifactsFolder
Exec { npm version $nextVersion --no-git-tag-version --allow-same-version }
Exec { npm install }
$baseTsConfig = (Resolve-Path .\tsconfig.json).Path.Replace('\', '/')
$tempTsConfigPath = Join-Path -Path $buildArtifactsFolder -ChildPath 'tsconfig.build.json'
$fablePattern = "$($fableOutputArtifactsFolder.Replace('\', '/'))/**/*"
$distPath = $distArtifactsFolder.Replace('\', '/')
$fablePath = $fableOutputArtifactsFolder.Replace('\', '/')
$tempTsConfig = @"
{
`"extends`": `"$baseTsConfig`",
"compilerOptions": {
"outDir": "$distPath",
"rootDir": "$fablePath",
"noEmit": false,
"allowImportingTsExtensions": false,
"rewriteRelativeImportExtensions": true
},
`"include`": [
`"$fablePattern`"
],
`"exclude`": []
}
"@
Set-Content -Path $tempTsConfigPath -Value $tempTsConfig
Exec { npm run build -- --project $tempTsConfigPath }
$packageJsonPath = Resolve-Path -Path 'package.json'
$packageJsonContent = Get-Content -Path $packageJsonPath -Raw | ConvertFrom-Json
$packageJsonContent.version = $nextVersion.ToString()
$packageJsonContent | ConvertTo-Json -Depth 10 | Set-Content -Path (Join-Path -Path $distArtifactsFolder -ChildPath 'package.json')
Get-ChildItem -Path 'README.md', 'LICENSE' -File | Copy-Item -Destination $distArtifactsFolder
Exec { npm version '1.0.0' --no-git-tag-version --allow-same-version }
Exec { npm pack $distArtifactsFolder --pack-destination $buildArtifactsFolder }
$npmPackage = Get-ChildItem -Path $buildArtifactsFolder -Filter '*.tgz' | Select-Object -First 1
$state.NPMPackagePath = $npmPackage.FullName
$state | Export-Clixml -Path ".\.trash\$Instance\state.clixml"
Write-Output $state
}
# Synopsis: Pack
Task Pack PackNuGet, PackNPM
# Synopsis: Publish NuGet package
Task PublishNuGet PackNuGet, {
$state = Import-Clixml -Path ".\.trash\$Instance\state.clixml"
$nugetPackagePath = $state.NuGetPackagePath
if ($null -eq $env:NUGET_API_KEY) {
Import-Module -Name Microsoft.PowerShell.SecretManagement
$apiKey = Get-Secret -Name 'Fossa-Bridge-NuGet-API-Key' -AsPlainText
}
else {
$apiKey = $env:NUGET_API_KEY
}
Exec { dotnet nuget push $nugetPackagePath --source https://api.nuget.org/v3/index.json --api-key $apiKey }
}
# Synopsis: Publish NPM package
Task PublishNPM PackNPM, {
$state = Import-Clixml -Path ".\.trash\$Instance\state.clixml"
$npmPackagePath = $state.NPMPackagePath
if ($null -eq $env:NPM_TOKEN) {
Import-Module -Name Microsoft.PowerShell.SecretManagement
$token = Get-Secret -Name 'Fossa-Bridge-NPM-Token' -AsPlainText
}
else {
$token = $env:NPM_TOKEN
}
Exec { npm config set //registry.npmjs.org/:_authToken $token }
Exec { npm publish $npmPackagePath --access public }
}
# Synopsis: Publish
Task Publish PublishNuGet, PublishNPM