forked from PowerShell/PowerShellEditorServices
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPowerShellEditorServices.build.ps1
465 lines (382 loc) · 17.7 KB
/
PowerShellEditorServices.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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
#
param(
[ValidateSet("Debug", "Release")]
[string]$Configuration = "Debug",
[string]$PsesSubmodulePath = "$PSScriptRoot/module",
[string]$ModulesJsonPath = "$PSScriptRoot/modules.json",
[string]$DefaultModuleRepository = "PSGallery"
)
#Requires -Modules @{ModuleName="InvokeBuild";ModuleVersion="3.2.1"}
$script:IsCIBuild = $env:APPVEYOR -ne $null
$script:IsUnix = $PSVersionTable.PSEdition -and $PSVersionTable.PSEdition -eq "Core" -and !$IsWindows
$script:TargetPlatform = "netstandard2.0"
$script:TargetFrameworksParam = "/p:TargetFrameworks=`"$script:TargetPlatform`""
$script:RequiredSdkVersion = "2.1.402"
$script:NugetApiUriBase = 'https://www.nuget.org/api/v2/package'
$script:ModuleBinPath = "$PSScriptRoot/module/PowerShellEditorServices/bin/"
$script:VSCodeModuleBinPath = "$PSScriptRoot/module/PowerShellEditorServices.VSCode/bin/"
$script:WindowsPowerShellFrameworkTarget = 'net461'
$script:NetFrameworkPlatformId = 'win'
$script:NetCoreTestingFrameworkVersion = '2.1.4'
$script:PSCoreModulePath = $null
$script:TestRuntime = @{
'Core' = 'netcoreapp2.1'
'Desktop' = 'net461'
}
<#
Declarative specification of binary assets produced
in the build that need to be binplaced in the module.
Schema is:
{
<Output Path>: {
<Project Name>: [
<FilePath From Project Build Folder>
]
}
}
#>
$script:RequiredBuildAssets = @{
$script:ModuleBinPath = @{
'PowerShellEditorServices' = @(
'publish/Serilog.dll',
'publish/Serilog.Sinks.Async.dll',
'publish/Serilog.Sinks.Console.dll',
'publish/Serilog.Sinks.File.dll',
'Microsoft.PowerShell.EditorServices.dll',
'Microsoft.PowerShell.EditorServices.pdb'
)
'PowerShellEditorServices.Host' = @(
'publish/UnixConsoleEcho.dll',
'publish/runtimes/osx-64/native/libdisablekeyecho.dylib',
'publish/runtimes/linux-64/native/libdisablekeyecho.so',
'publish/Newtonsoft.Json.dll',
'Microsoft.PowerShell.EditorServices.Host.dll',
'Microsoft.PowerShell.EditorServices.Host.pdb'
)
'PowerShellEditorServices.Protocol' = @(
'Microsoft.PowerShell.EditorServices.Protocol.dll',
'Microsoft.PowerShell.EditorServices.Protocol.pdb'
)
}
$script:VSCodeModuleBinPath = @{
'PowerShellEditorServices.VSCode' = @(
'Microsoft.PowerShell.EditorServices.VSCode.dll',
'Microsoft.PowerShell.EditorServices.VSCode.pdb'
)
}
}
<#
Declares the binary shims we need to make the netstandard DLLs hook into .NET Framework.
Schema is:
{
<Destination Bin Directory>: [{
'PackageName': <Package Name>,
'PackageVersion': <Package Version>,
'TargetRuntime': <Target .NET Runtime>,
'DllName'?: <Name of DLL to extract>
}]
}
#>
$script:RequiredNugetBinaries = @{
'Desktop' = @(
@{ PackageName = 'System.Security.Principal.Windows'; PackageVersion = '4.5.0'; TargetRuntime = 'net461' },
@{ PackageName = 'System.Security.AccessControl'; PackageVersion = '4.5.0'; TargetRuntime = 'net461' },
@{ PackageName = 'System.IO.Pipes.AccessControl'; PackageVersion = '4.5.1'; TargetRuntime = 'net461' }
)
'6.0' = @(
@{ PackageName = 'System.Security.Principal.Windows'; PackageVersion = '4.5.0'; TargetRuntime = 'netcoreapp2.0' },
@{ PackageName = 'System.Security.AccessControl'; PackageVersion = '4.5.0'; TargetRuntime = 'netcoreapp2.0' },
@{ PackageName = 'System.IO.Pipes.AccessControl'; PackageVersion = '4.5.1'; TargetRuntime = 'netstandard2.0' }
)
}
if ($PSVersionTable.PSEdition -ne "Core") {
Add-Type -Assembly System.IO.Compression.FileSystem
}
function Get-NugetAsmForRuntime {
param(
[ValidateNotNull()][string]$PackageName,
[ValidateNotNull()][string]$PackageVersion,
[string]$DllName,
[string]$DestinationPath,
[string]$TargetPlatform = $script:NetFrameworkPlatformId,
[string]$TargetRuntime = $script:WindowsPowerShellFrameworkTarget
)
$tmpDir = [System.IO.Path]::GetTempPath()
if (-not $DllName) {
$DllName = "$PackageName.dll"
}
if ($DestinationPath -eq $null) {
$DestinationPath = Join-Path $tmpDir $DllName
} elseif (Test-Path $DestinationPath -PathType Container) {
$DestinationPath = Join-Path $DestinationPath $DllName
}
$packageDirPath = Join-Path $tmpDir "$PackageName.$PackageVersion"
if (-not (Test-Path $packageDirPath)) {
$tmpNupkgPath = Join-Path $tmpDir 'tmp.zip'
if (Test-Path $tmpNupkgPath) {
Remove-Item -Force $tmpNupkgPath
}
$packageUri = "$script:NugetApiUriBase/$PackageName/$PackageVersion"
Invoke-WebRequest -Uri $packageUri -OutFile $tmpNupkgPath
Expand-Archive -Path $tmpNupkgPath -DestinationPath $packageDirPath
}
$internalPath = [System.IO.Path]::Combine($packageDirPath, 'runtimes', $TargetPlatform, 'lib', $TargetRuntime, $DllName)
Copy-Item -Path $internalPath -Destination $DestinationPath -Force
return $DestinationPath
}
function Invoke-WithCreateDefaultHook {
param([scriptblock]$ScriptBlock)
try
{
$env:PSES_TEST_USE_CREATE_DEFAULT = 1
& $ScriptBlock
} finally {
Remove-Item env:PSES_TEST_USE_CREATE_DEFAULT
}
}
task SetupDotNet -Before Clean, Build, TestHost, TestServer, TestProtocol, PackageNuGet {
$dotnetPath = "$PSScriptRoot/.dotnet"
$dotnetExePath = if ($script:IsUnix) { "$dotnetPath/dotnet" } else { "$dotnetPath/dotnet.exe" }
$originalDotNetExePath = $dotnetExePath
if (!(Test-Path $dotnetExePath)) {
$installedDotnet = Get-Command dotnet -ErrorAction Ignore
if ($installedDotnet) {
$dotnetExePath = $installedDotnet.Source
}
else {
$dotnetExePath = $null
}
}
# Make sure the dotnet we found is the right version
if ($dotnetExePath) {
# dotnet --version can return a semver that System.Version can't handle
# e.g.: 2.1.300-preview-01. The replace operator is used to remove any build suffix.
$version = (& $dotnetExePath --version) -replace '[+-].*$',''
if ([version]$version -ge [version]$script:RequiredSdkVersion) {
$script:dotnetExe = $dotnetExePath
}
else {
# Clear the path so that we invoke installation
$script:dotnetExe = $null
}
}
else {
# Clear the path so that we invoke installation
$script:dotnetExe = $null
}
if ($script:dotnetExe -eq $null) {
Write-Host "`n### Installing .NET CLI $script:RequiredSdkVersion...`n" -ForegroundColor Green
# The install script is platform-specific
$installScriptExt = if ($script:IsUnix) { "sh" } else { "ps1" }
# Download the official installation script and run it
$installScriptPath = "$([System.IO.Path]::GetTempPath())dotnet-install.$installScriptExt"
Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/v$script:RequiredSdkVersion/scripts/obtain/dotnet-install.$installScriptExt" -OutFile $installScriptPath
$env:DOTNET_INSTALL_DIR = "$PSScriptRoot/.dotnet"
if (!$script:IsUnix) {
& $installScriptPath -Version $script:RequiredSdkVersion -InstallDir "$env:DOTNET_INSTALL_DIR"
}
else {
& /bin/bash $installScriptPath -Version $script:RequiredSdkVersion -InstallDir "$env:DOTNET_INSTALL_DIR"
$env:PATH = $dotnetExeDir + [System.IO.Path]::PathSeparator + $env:PATH
}
Write-Host "`n### Installation complete." -ForegroundColor Green
$script:dotnetExe = $originalDotnetExePath
}
# This variable is used internally by 'dotnet' to know where it's installed
$script:dotnetExe = Resolve-Path $script:dotnetExe
if (!$env:DOTNET_INSTALL_DIR)
{
$dotnetExeDir = [System.IO.Path]::GetDirectoryName($script:dotnetExe)
$env:PATH = $dotnetExeDir + [System.IO.Path]::PathSeparator + $env:PATH
$env:DOTNET_INSTALL_DIR = $dotnetExeDir
}
Write-Host "`n### Using dotnet v$(& $script:dotnetExe --version) at path $script:dotnetExe`n" -ForegroundColor Green
}
task Clean {
exec { & $script:dotnetExe restore }
exec { & $script:dotnetExe clean }
Remove-Item $PSScriptRoot\module\PowerShellEditorServices\bin -Recurse -Force -ErrorAction Ignore
Remove-Item $PSScriptRoot\module\PowerShellEditorServices.VSCode\bin -Recurse -Force -ErrorAction Ignore
Get-ChildItem -Recurse $PSScriptRoot\src\*.nupkg | Remove-Item -Force -ErrorAction Ignore
Get-ChildItem $PSScriptRoot\PowerShellEditorServices*.zip | Remove-Item -Force -ErrorAction Ignore
Get-ChildItem $PSScriptRoot\module\PowerShellEditorServices\Commands\en-US\*-help.xml | Remove-Item -Force -ErrorAction Ignore
}
task GetProductVersion -Before PackageNuGet, PackageModule, UploadArtifacts {
[xml]$props = Get-Content .\PowerShellEditorServices.Common.props
$script:BuildNumber = 9999
$script:VersionSuffix = $props.Project.PropertyGroup.VersionSuffix
if ($env:APPVEYOR) {
$script:BuildNumber = $env:APPVEYOR_BUILD_NUMBER
}
if ($script:VersionSuffix -ne $null) {
$script:VersionSuffix = "$script:VersionSuffix-$script:BuildNumber"
}
else {
$script:VersionSuffix = "$script:BuildNumber"
}
$script:FullVersion = "$($props.Project.PropertyGroup.VersionPrefix)-$script:VersionSuffix"
Write-Host "`n### Product Version: $script:FullVersion`n" -ForegroundColor Green
}
task Build {
exec { & $script:dotnetExe publish -c $Configuration .\src\PowerShellEditorServices.Host\PowerShellEditorServices.Host.csproj -f $script:TargetPlatform }
exec { & $script:dotnetExe build -c $Configuration .\src\PowerShellEditorServices.VSCode\PowerShellEditorServices.VSCode.csproj $script:TargetFrameworksParam }
exec { & $script:dotnetExe publish -c $Configuration .\src\PowerShellEditorServices\PowerShellEditorServices.csproj -f $script:TargetPlatform }
}
function UploadTestLogs {
if ($script:IsCIBuild) {
$testLogsPath = "$PSScriptRoot/test/PowerShellEditorServices.Test.Host/bin/$Configuration/net452/logs"
$testLogsZipPath = "$PSScriptRoot/TestLogs.zip"
if (Test-Path $testLogsPath) {
[System.IO.Compression.ZipFile]::CreateFromDirectory(
$testLogsPath,
$testLogsZipPath)
Push-AppveyorArtifact $testLogsZipPath
}
else {
Write-Host "`n### WARNING: Test logs could not be found!`n" -ForegroundColor Yellow
}
}
}
task Test TestServer,TestProtocol
task TestServer {
Set-Location .\test\PowerShellEditorServices.Test\
if (-not $script:IsUnix) {
exec { & $script:dotnetExe build -f $script:TestRuntime.Desktop }
exec { & $script:dotnetExe xunit -f $script:TestRuntime.Desktop }
}
Invoke-WithCreateDefaultHook -NewModulePath $script:PSCoreModulePath {
exec { & $script:dotnetExe build -c $Configuration -f $script:TestRuntime.Core }
exec { & $script:dotnetExe xunit -f $script:TestRuntime.Core --fx-version $script:NetCoreTestingFrameworkVersion }
}
}
task TestProtocol {
Set-Location .\test\PowerShellEditorServices.Test.Protocol\
if (-not $script:IsUnix) {
exec { & $script:dotnetExe build -f $script:TestRuntime.Desktop }
exec { & $script:dotnetExe xunit -f $script:TestRuntime.Desktop }
}
Invoke-WithCreateDefaultHook {
exec { & $script:dotnetExe build -c $Configuration -f $script:TestRuntime.Core }
exec { & $script:dotnetExe xunit -f $script:TestRuntime.Core --fx-version $script:NetCoreTestingFrameworkVersion }
}
}
task TestHost -If {
Set-Location .\test\PowerShellEditorServices.Test.Host\
if (-not $script:IsUnix) {
exec { & $script:dotnetExe build -f $script:TestRuntime.Desktop }
exec { & $script:dotnetExe test -f $script:TestRuntime.Desktop }
}
exec { & $script:dotnetExe build -c $Configuration -f $script:TestRuntime.Core }
exec { & $script:dotnetExe test -f $script:TestRuntime.Core }
}
task CITest ?Test, {
# This task is used to ensure we have a chance to upload
# test logs as a CI artifact when the tests fail
if (error Test) {
UploadTestLogs
Write-Error "Failing build due to test failure."
}
}
task LayoutModule -After Build {
# Copy Third Party Notices.txt to module folder
Copy-Item -Force -Path "$PSScriptRoot\Third Party Notices.txt" -Destination $PSScriptRoot\module\PowerShellEditorServices
# Lay out the PowerShellEditorServices module's binaries
# For each binplace destination
foreach ($destDir in $script:RequiredBuildAssets.Keys) {
# Create the destination dir
$null = New-Item -Force $destDir -Type Directory
# For each PSES subproject
foreach ($projectName in $script:RequiredBuildAssets[$destDir].Keys) {
# Get the project build dir path
$basePath = [System.IO.Path]::Combine($PSScriptRoot, 'src', $projectName, 'bin', $Configuration, $script:TargetPlatform)
# For each asset in the subproject
foreach ($bin in $script:RequiredBuildAssets[$destDir][$projectName]) {
# Get the asset path
$binPath = Join-Path $basePath $bin
# Binplace the asset
Copy-Item -Force -Verbose $binPath $destDir
}
}
}
# Get and place the shim bins for net461
foreach ($binDestinationDir in $script:RequiredNugetBinaries.Keys) {
$binDestPath = Join-Path $script:ModuleBinPath $binDestinationDir
if (-not (Test-Path $binDestPath)) {
New-Item -Path $binDestPath -ItemType Directory
}
foreach ($packageDetails in $script:RequiredNugetBinaries[$binDestinationDir]) {
Get-NugetAsmForRuntime -DestinationPath $binDestPath @packageDetails
}
}
}
task RestorePsesModules -After Build {
$submodulePath = (Resolve-Path $PsesSubmodulePath).Path + [IO.Path]::DirectorySeparatorChar
Write-Host "`nRestoring EditorServices modules..."
# Read in the modules.json file as a hashtable so it can be splatted
$moduleInfos = @{}
(Get-Content -Raw $ModulesJsonPath | ConvertFrom-Json).PSObject.Properties | ForEach-Object {
$name = $_.Name
$body = @{
Name = $name
MinimumVersion = $_.Value.MinimumVersion
MaximumVersion = $_.Value.MaximumVersion
AllowPrerelease = $_.Value.AllowPrerelease
Repository = if ($_.Value.Repository) { $_.Value.Repository } else { $DefaultModuleRepository }
Path = $submodulePath
}
if (-not $name)
{
throw "EditorServices module listed without name in '$ModulesJsonPath'"
}
$moduleInfos.Add($name, $body)
}
# Save each module in the modules.json file
foreach ($moduleName in $moduleInfos.Keys)
{
if (Test-Path -Path (Join-Path -Path $submodulePath -ChildPath $moduleName))
{
Write-Host "`tModule '${moduleName}' already detected. Skipping"
continue
}
$moduleInstallDetails = $moduleInfos[$moduleName]
$splatParameters = @{
Name = $moduleName
MinimumVersion = $moduleInstallDetails.MinimumVersion
MaximumVersion = $moduleInstallDetails.MaximumVersion
AllowPrerelease = $moduleInstallDetails.AllowPrerelease
Repository = if ($moduleInstallDetails.Repository) { $moduleInstallDetails.Repository } else { $DefaultModuleRepository }
Path = $submodulePath
}
Write-Host "`tInstalling module: ${moduleName} with arguments $(ConvertTo-Json $splatParameters)"
Save-Module @splatParameters
}
Write-Host "`n"
}
task BuildCmdletHelp {
New-ExternalHelp -Path $PSScriptRoot\module\docs -OutputPath $PSScriptRoot\module\PowerShellEditorServices\Commands\en-US -Force
}
task PackageNuGet {
exec { & $script:dotnetExe pack -c $Configuration --version-suffix $script:VersionSuffix .\src\PowerShellEditorServices\PowerShellEditorServices.csproj $script:TargetFrameworksParam }
exec { & $script:dotnetExe pack -c $Configuration --version-suffix $script:VersionSuffix .\src\PowerShellEditorServices.Protocol\PowerShellEditorServices.Protocol.csproj $script:TargetFrameworksParam }
exec { & $script:dotnetExe pack -c $Configuration --version-suffix $script:VersionSuffix .\src\PowerShellEditorServices.Host\PowerShellEditorServices.Host.csproj $script:TargetFrameworksParam }
}
task PackageModule {
[System.IO.Compression.ZipFile]::CreateFromDirectory(
"$PSScriptRoot/module/",
"$PSScriptRoot/PowerShellEditorServices-$($script:FullVersion).zip",
[System.IO.Compression.CompressionLevel]::Optimal,
$false)
}
task UploadArtifacts -If ($script:IsCIBuild) {
if ($env:APPVEYOR) {
Push-AppveyorArtifact .\src\PowerShellEditorServices\bin\$Configuration\Microsoft.PowerShell.EditorServices.$($script:FullVersion).nupkg
Push-AppveyorArtifact .\src\PowerShellEditorServices.Protocol\bin\$Configuration\Microsoft.PowerShell.EditorServices.Protocol.$($script:FullVersion).nupkg
Push-AppveyorArtifact .\src\PowerShellEditorServices.Host\bin\$Configuration\Microsoft.PowerShell.EditorServices.Host.$($script:FullVersion).nupkg
Push-AppveyorArtifact .\PowerShellEditorServices-$($script:FullVersion).zip
}
}
# The default task is to run the entire CI build
task . GetProductVersion, Clean, Build, CITest, BuildCmdletHelp, PackageNuGet, PackageModule, UploadArtifacts