forked from PowerShell/PowerShellEditorServices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerShellEditorServices.build.ps1
456 lines (365 loc) · 16.8 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
#
# 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",
[string]$TestFilter = ''
)
#Requires -Modules @{ModuleName="InvokeBuild";ModuleVersion="3.2.1"}
$script:IsUnix = $PSVersionTable.PSEdition -and $PSVersionTable.PSEdition -eq "Core" -and !$IsWindows
$script:BuildInfoPath = [System.IO.Path]::Combine($PSScriptRoot, "src", "PowerShellEditorServices.Hosting", "BuildInfo.cs")
$script:PsesCommonProps = [xml](Get-Content -Raw "$PSScriptRoot/PowerShellEditorServices.Common.props")
$script:IsPreview = [bool]($script:PsesCommonProps.Project.PropertyGroup.VersionSuffix)
$script:NetRuntime = @{
PS62 = 'netcoreapp2.1'
PS7 = 'netcoreapp3.1'
PS71 = 'net5.0'
Desktop = 'net461'
Standard = 'netstandard2.0'
}
$script:HostCoreOutput = "$PSScriptRoot/src/PowerShellEditorServices.Hosting/bin/$Configuration/$($script:NetRuntime.PS62)/publish"
$script:HostDeskOutput = "$PSScriptRoot/src/PowerShellEditorServices.Hosting/bin/$Configuration/$($script:NetRuntime.Desktop)/publish"
$script:PsesOutput = "$PSScriptRoot/src/PowerShellEditorServices/bin/$Configuration/$($script:NetRuntime.Standard)/publish"
$script:VSCodeOutput = "$PSScriptRoot/src/PowerShellEditorServices.VSCode/bin/$Configuration/$($script:NetRuntime.Standard)/publish"
if (Get-Command git -ErrorAction SilentlyContinue) {
# ignore changes to this file
git update-index --assume-unchanged "$PSScriptRoot/src/PowerShellEditorServices.Hosting/BuildInfo.cs"
}
function Invoke-WithCreateDefaultHook {
param([scriptblock]$ScriptBlock)
try
{
$env:PSES_TEST_USE_CREATE_DEFAULT = 1
& $ScriptBlock
} finally {
Remove-Item env:PSES_TEST_USE_CREATE_DEFAULT
}
}
function Install-Dotnet {
param (
[string[]]$Channel
)
$env:DOTNET_INSTALL_DIR = "$PSScriptRoot/.dotnet"
Write-Host "Installing .NET channels $Channel" -ForegroundColor Green
# The install script is platform-specific
$installScriptExt = if ($script:IsUnix) { "sh" } else { "ps1" }
$installScript = "dotnet-install.$installScriptExt"
# Download the official installation script and run it
$installScriptPath = Join-Path ([System.IO.Path]::GetTempPath()) $installScript
Invoke-WebRequest "https://dot.net/v1/$installScript" -OutFile $installScriptPath
# Download and install the different .NET channels
foreach ($dotnetChannel in $Channel)
{
Write-Host "`n### Installing .NET CLI $Version...`n"
if ($script:IsUnix) {
chmod +x $installScriptPath
}
$params = if ($script:IsUnix)
{
@('-Channel', $dotnetChannel, '-InstallDir', $env:DOTNET_INSTALL_DIR, '-NoPath', '-Verbose')
}
else
{
@{
Channel = $dotnetChannel
InstallDir = $env:DOTNET_INSTALL_DIR
NoPath = $true
Verbose = $true
}
}
& $installScriptPath @params
Write-Host "`n### Installation complete for version $Version."
}
$env:PATH = $env:DOTNET_INSTALL_DIR + [System.IO.Path]::PathSeparator + $env:PATH
Write-Host '.NET installation complete' -ForegroundColor Green
}
task SetupDotNet -Before Clean, Build, TestHost, TestServerWinPS, TestServerPS7, TestServerPS71, TestE2E {
$dotnetPath = "$PSScriptRoot/.dotnet"
$dotnetExePath = if ($script:IsUnix) { "$dotnetPath/dotnet" } else { "$dotnetPath/dotnet.exe" }
if (!(Test-Path $dotnetExePath)) {
Install-Dotnet -Channel '2.1','3.1','release/5.0.1xx-preview6'
}
# This variable is used internally by 'dotnet' to know where it's installed
$script:dotnetExe = Resolve-Path $dotnetExePath
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 BinClean {
Remove-Item $PSScriptRoot\.tmp -Recurse -Force -ErrorAction Ignore
Remove-Item $PSScriptRoot\module\PowerShellEditorServices\bin -Recurse -Force -ErrorAction Ignore
Remove-Item $PSScriptRoot\module\PowerShellEditorServices.VSCode\bin -Recurse -Force -ErrorAction Ignore
}
task Clean BinClean,{
exec { & $script:dotnetExe restore }
exec { & $script:dotnetExe clean }
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
# Remove bundled component modules
$moduleJsonPath = "$PSScriptRoot\modules.json"
if (Test-Path $moduleJsonPath) {
Get-Content -Raw $moduleJsonPath |
ConvertFrom-Json |
ForEach-Object { $_.PSObject.Properties.Name } |
ForEach-Object { Remove-Item -Path "$PSScriptRoot/module/$_" -Recurse -Force -ErrorAction Ignore }
}
}
task GetProductVersion -Before PackageModule, UploadArtifacts {
[xml]$props = Get-Content .\PowerShellEditorServices.Common.props
$script:BuildNumber = 9999
$script:VersionSuffix = $props.Project.PropertyGroup.VersionSuffix
if ($env:TF_BUILD) {
# SYSTEM_PHASENAME is the Job name.
# Job names can only include `_` but that's not a valid character for versions.
$jobname = $env:SYSTEM_PHASENAME -replace '_', ''
$script:BuildNumber = "$jobname-$env:BUILD_BUILDID"
}
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 CreateBuildInfo -Before Build {
$buildVersion = "<development-build>"
$buildOrigin = "Development"
# Set build info fields on build platforms
if ($env:TF_BUILD) {
if ($env:BUILD_BUILDNUMBER -like "PR-*") {
$buildOrigin = "PR"
} elseif ($env:BUILD_DEFINITIONNAME -like "*-CI") {
$buildOrigin = "CI"
} else {
$buildOrigin = "Release"
}
$propsXml = [xml](Get-Content -Raw -LiteralPath "$PSScriptRoot/PowerShellEditorServices.Common.props")
$propsBody = $propsXml.Project.PropertyGroup
$buildVersion = $propsBody.VersionPrefix
if ($propsBody.VersionSuffix)
{
$buildVersion += '-' + $propsBody.VersionSuffix
}
}
# Allow override of build info fields (except date)
if ($env:PSES_BUILD_VERSION) {
$buildVersion = $env:PSES_BUILD_VERSION
}
if ($env:PSES_BUILD_ORIGIN) {
$buildOrigin = $env:PSES_BUILD_ORIGIN
}
[string]$buildTime = [datetime]::Now.ToString("s", [System.Globalization.CultureInfo]::InvariantCulture)
$buildInfoContents = @"
using System.Globalization;
namespace Microsoft.PowerShell.EditorServices.Hosting
{
public static class BuildInfo
{
public static readonly string BuildVersion = "$buildVersion";
public static readonly string BuildOrigin = "$buildOrigin";
public static readonly System.DateTime? BuildTime = System.DateTime.Parse("$buildTime", CultureInfo.InvariantCulture.DateTimeFormat);
}
}
"@
Set-Content -LiteralPath $script:BuildInfoPath -Value $buildInfoContents -Force
}
task SetupHelpForTests -Before Test {
if (-not (Get-Help Write-Host).Examples) {
Update-Help -Module Microsoft.PowerShell.Utility -Force -Scope CurrentUser
}
}
task Build BinClean,{
exec { & $script:dotnetExe publish -c $Configuration .\src\PowerShellEditorServices\PowerShellEditorServices.csproj -f $script:NetRuntime.Standard }
exec { & $script:dotnetExe publish -c $Configuration .\src\PowerShellEditorServices.Hosting\PowerShellEditorServices.Hosting.csproj -f $script:NetRuntime.PS62 }
if (-not $script:IsUnix)
{
exec { & $script:dotnetExe publish -c $Configuration .\src\PowerShellEditorServices.Hosting\PowerShellEditorServices.Hosting.csproj -f $script:NetRuntime.Desktop }
}
# Build PowerShellEditorServices.VSCode module
exec { & $script:dotnetExe publish -c $Configuration .\src\PowerShellEditorServices.VSCode\PowerShellEditorServices.VSCode.csproj -f $script:NetRuntime.Standard }
}
function DotNetTestFilter {
# Reference https://docs.microsoft.com/en-us/dotnet/core/testing/selective-unit-tests
if ($TestFilter) { @("--filter",$TestFilter) } else { "" }
}
task Test TestServer,TestE2E
task TestServer TestServerWinPS,TestServerPS7,TestServerPS71
task TestServerWinPS -If (-not $script:IsUnix) {
Set-Location .\test\PowerShellEditorServices.Test\
exec { & $script:dotnetExe test --logger trx -f $script:NetRuntime.Desktop (DotNetTestFilter) }
}
task TestServerPS7 {
Set-Location .\test\PowerShellEditorServices.Test\
Invoke-WithCreateDefaultHook -NewModulePath $script:PSCoreModulePath {
exec { & $script:dotnetExe test --logger trx -f $script:NetRuntime.PS7 (DotNetTestFilter) }
}
}
task TestServerPS71 {
Set-Location .\test\PowerShellEditorServices.Test\
Invoke-WithCreateDefaultHook -NewModulePath $script:PSCoreModulePath {
exec { & $script:dotnetExe test --logger trx -f $script:NetRuntime.PS71 (DotNetTestFilter) }
}
}
task TestHost {
Set-Location .\test\PowerShellEditorServices.Test.Host\
if (-not $script:IsUnix) {
exec { & $script:dotnetExe build -f $script:NetRuntime.Desktop }
exec { & $script:dotnetExe test -f $script:NetRuntime.Desktop (DotNetTestFilter) }
}
exec { & $script:dotnetExe build -c $Configuration -f $script:NetRuntime.PS62 }
exec { & $script:dotnetExe test -f $script:NetRuntime.PS62 (DotNetTestFilter) }
}
task TestE2E {
Set-Location .\test\PowerShellEditorServices.Test.E2E\
$env:PWSH_EXE_NAME = if ($IsCoreCLR) { "pwsh" } else { "powershell" }
exec { & $script:dotnetExe test --logger trx -f $script:NetRuntime.PS62 (DotNetTestFilter) }
# Run E2E tests in ConstrainedLanguage mode.
if (!$script:IsUnix) {
try {
[System.Environment]::SetEnvironmentVariable("__PSLockdownPolicy", "0x80000007", [System.EnvironmentVariableTarget]::Machine);
exec { & $script:dotnetExe test --logger trx -f $script:NetRuntime.PS62 (DotNetTestFilter) }
} finally {
[System.Environment]::SetEnvironmentVariable("__PSLockdownPolicy", $null, [System.EnvironmentVariableTarget]::Machine);
}
}
}
task LayoutModule -After Build {
$modulesDir = "$PSScriptRoot/module"
$psesVSCodeBinOutputPath = "$modulesDir/PowerShellEditorServices.VSCode/bin"
$psesOutputPath = "$modulesDir/PowerShellEditorServices"
$psesBinOutputPath = "$PSScriptRoot/module/PowerShellEditorServices/bin"
$psesDepsPath = "$psesBinOutputPath/Common"
$psesCoreHostPath = "$psesBinOutputPath/Core"
$psesDeskHostPath = "$psesBinOutputPath/Desktop"
foreach ($dir in $psesDepsPath,$psesCoreHostPath,$psesDeskHostPath,$psesVSCodeBinOutputPath)
{
New-Item -Force -Path $dir -ItemType Directory
}
# Copy Third Party Notices.txt to module folder
Copy-Item -Force -Path "$PSScriptRoot\Third Party Notices.txt" -Destination $psesOutputPath
# Assemble PSES module
$includedDlls = [System.Collections.Generic.HashSet[string]]::new()
[void]$includedDlls.Add('System.Management.Automation.dll')
# PSES/bin/Common
foreach ($psesComponent in Get-ChildItem $script:PsesOutput)
{
if ($psesComponent.Name -eq 'System.Management.Automation.dll' -or
$psesComponent.Name -eq 'System.Runtime.InteropServices.RuntimeInformation.dll')
{
continue
}
if ($psesComponent.Extension)
{
[void]$includedDlls.Add($psesComponent.Name)
Copy-Item -Path $psesComponent.FullName -Destination $psesDepsPath -Force
}
}
# PSES/bin/Core
foreach ($hostComponent in Get-ChildItem $script:HostCoreOutput)
{
if (-not $includedDlls.Contains($hostComponent.Name))
{
Copy-Item -Path $hostComponent.FullName -Destination $psesCoreHostPath -Force
}
}
# PSES/bin/Desktop
if (-not $script:IsUnix)
{
foreach ($hostComponent in Get-ChildItem $script:HostDeskOutput)
{
if (-not $includedDlls.Contains($hostComponent.Name))
{
Copy-Item -Path $hostComponent.FullName -Destination $psesDeskHostPath -Force
}
}
}
# Assemble the PowerShellEditorServices.VSCode module
foreach ($vscodeComponent in Get-ChildItem $script:VSCodeOutput)
{
if (-not $includedDlls.Contains($vscodeComponent.Name))
{
Copy-Item -Path $vscodeComponent.FullName -Destination $psesVSCodeBinOutputPath -Force
}
}
}
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 = $script:IsPreview
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)
}
if ($moduleInfos.Keys.Count -gt 0) {
# `#Requires` doesn't display the version needed in the error message and `using module` doesn't work with InvokeBuild in Windows PowerShell
# so we'll just use Import-Module to check that PowerShellGet 1.6.0 or higher is installed.
# This is needed in order to use the `-AllowPrerelease` parameter
Import-Module -Name PowerShellGet -MinimumVersion 1.6.0 -ErrorAction Stop
}
# 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
AllowPrerelease = $moduleInstallDetails.AllowPrerelease
Repository = if ($moduleInstallDetails.Repository) { $moduleInstallDetails.Repository } else { $DefaultModuleRepository }
Path = $submodulePath
}
# Only add Min and Max version if we're doing a stable release.
# This is due to a PSGet issue with AllowPrerelease not installing the latest preview.
if (!$moduleInstallDetails.AllowPrerelease) {
$splatParameters.MinimumVersion = $moduleInstallDetails.MinimumVersion
$splatParameters.MaximumVersion = $moduleInstallDetails.MaximumVersion
}
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
New-ExternalHelp -Path $PSScriptRoot\module\PowerShellEditorServices.VSCode\docs -OutputPath $PSScriptRoot\module\PowerShellEditorServices.VSCode\en-US -Force
}
task PackageModule {
[System.IO.Compression.ZipFile]::CreateFromDirectory(
"$PSScriptRoot/module/",
"$PSScriptRoot/PowerShellEditorServices-$($script:FullVersion).zip",
[System.IO.Compression.CompressionLevel]::Optimal,
$false)
}
task UploadArtifacts -If ($null -ne $env:TF_BUILD) {
Copy-Item -Path .\PowerShellEditorServices-$($script:FullVersion).zip -Destination $env:BUILD_ARTIFACTSTAGINGDIRECTORY -Force
}
# The default task is to run the entire CI build
task . GetProductVersion, Clean, Build, Test, BuildCmdletHelp, PackageModule, UploadArtifacts