Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 637d127

Browse files
committed
In Progress refactoring
1 parent 1a217d7 commit 637d127

File tree

2 files changed

+194
-116
lines changed

2 files changed

+194
-116
lines changed

build.ps1

Lines changed: 135 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,152 @@
11
#!/usr/bin/env pwsh
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
4-
5-
[CmdletBinding(DefaultParameterSetName = "Build")]
4+
<#
5+
.SYNOPSIS
6+
A bootstrap for Invoke-Build which will perform the rest of the process. This is mostly compatability for programs that still use it, normally you should use Invoke-Build directly unless you don't have invoke-build installed. For noninteractive use, be sure to specify -Confirm:$false to avoid prompts
7+
#>
8+
#requires -version 5
9+
using namespace System.Management.Automation
10+
using namespace System.Collections.Generic
11+
12+
[CmdletBinding(DefaultParameterSetName = 'Build')]
613
param(
7-
[Parameter(ParameterSetName="Bootstrap")]
14+
[Parameter(ParameterSetName = 'Bootstrap')]
815
[switch]
916
$Bootstrap,
1017

11-
[Parameter(ParameterSetName="Build")]
18+
[Parameter(ParameterSetName = 'Build')]
1219
[switch]
1320
$Clean,
1421

15-
[Parameter(ParameterSetName="Build")]
22+
[Parameter(ParameterSetName = 'Build')]
1623
[switch]
1724
$Test
1825
)
19-
20-
$NeededTools = @{
21-
VSCode = "Visual Studio Code"
22-
NodeJS = "Node.js 6.0 or higher"
23-
PowerShellGet = "PowerShellGet latest"
24-
InvokeBuild = "InvokeBuild latest"
25-
}
26-
27-
function needsVSCode () {
28-
try {
29-
$vscodeVersion = (code -v)
30-
if (-not $vscodeVersion) {
31-
Throw
32-
}
33-
} catch {
34-
try {
35-
$vscodeInsidersVersion = (code-insiders -v)
36-
if (-not $vscodeInsidersVersion) {
37-
Throw
38-
}
39-
} catch {
40-
return $true
41-
}
42-
}
43-
return $false
44-
}
45-
46-
function needsNodeJS () {
47-
try {
48-
$nodeJSVersion = node -v
49-
} catch {
50-
return $true
51-
}
52-
53-
if ($nodeJSVersion -notmatch 'v(\d+\.\d+\.\d+)') {
54-
return $true
55-
}
56-
57-
$nodeVer = [System.Version]$matches[1]
58-
return ($nodeVer.Major -lt 6)
26+
$SCRIPT:ErrorActionPreference = 'Stop'
27+
# Pin the InvokeBuild version to avoid possible supply chain attacks or breaking changes.
28+
$InvokeBuildVersion = '5.10.3'
29+
30+
# Get unique non-common parameters specified to pass to Invoke-Build
31+
[hashset[string]]$commonParameters = ([PSCmdlet]::CommonParameters, [PSCmdlet]::OptionalCommonParameters) | ForEach-Object { $_ }
32+
$ibParams = @{}
33+
$ibParams.Task = ($PSBoundParameters.Keys | Where-Object { $_ -notin $commonParameters }) -join ','
34+
$commonParams = @{}
35+
$PSBoundParameters.GetEnumerator() | Where-Object Key -In $commonParameters | ForEach-Object {
36+
$commonParams[$PSItem.Key] = $PSItem.Value
37+
$ibParams[$PSItem.Key] = $PSItem.Value
5938
}
6039

61-
function needsPowerShellGet () {
62-
if (Get-Module -ListAvailable -Name PowerShellGet) {
63-
return $false
64-
}
65-
return $true
40+
try {
41+
$invokeBuildCommand = Get-Command Invoke-Build -FullyQualifiedModule @{ModuleName = 'InvokeBuild'; RequiredVersion = $InvokeBuildVersion }
42+
Write-Verbose "Bootstrap: Invoke-Build $InvokeBuildVersion detected."
43+
} catch {
44+
Write-Warning "Invoke-Build $InvokeBuildVersion not detected. Installing..."
45+
Install-Module -Name InvokeBuild -RequiredVersion $InvokeBuildVersion -Scope CurrentUser @commonParams
6646
}
6747

68-
function needsInvokeBuild () {
69-
if (Get-Module -ListAvailable -Name InvokeBuild) {
70-
return $false
71-
}
72-
return $true
73-
}
74-
75-
function getMissingTools () {
76-
$missingTools = @()
77-
78-
if (needsVSCode) {
79-
$missingTools += $NeededTools.VSCode
80-
}
81-
if (needsNodeJS) {
82-
$missingTools += $NeededTools.NodeJS
83-
}
84-
if (needsPowerShellGet) {
85-
$missingTools += $NeededTools.PowerShellGet
86-
}
87-
if (needsInvokeBuild) {
88-
$missingTools += $NeededTools.InvokeBuild
89-
}
90-
91-
return $missingTools
92-
}
93-
94-
function hasMissingTools () {
95-
return ((getMissingTools).Count -gt 0)
96-
}
97-
98-
if ($Bootstrap) {
99-
$string = "Here is what your environment is missing:`n"
100-
$missingTools = getMissingTools
101-
if (($missingTools).Count -eq 0) {
102-
$string += "* nothing!`n`n Run this script without a flag to build or a -Clean to clean."
103-
} else {
104-
$missingTools | ForEach-Object {$string += "* $_`n"}
105-
$string += "`nAll instructions for installing these tools can be found on VSCode PowerShell's Github:`n" `
106-
+ "https://github.com/PowerShell/vscode-powershell/blob/main/docs/development.md"
107-
}
108-
Write-Host "`n$string`n"
109-
} elseif(hasMissingTools) {
110-
Write-Host "You are missing needed tools. Run './build.ps1 -Bootstrap' to see what they are."
111-
} else {
112-
if($Clean) {
113-
Invoke-Build Clean
114-
}
115-
116-
Invoke-Build Build
117-
118-
if($Test) {
119-
Invoke-Build Test
120-
}
121-
}
48+
Write-Verbose "Starting Invoke-Build $($ibParams.Task -join ', ')"
49+
& $invokeBuildCommand @ibParams
50+
51+
# $NeededTools = @{
52+
# VSCode = "Visual Studio Code"
53+
# NodeJS = "Node.js 6.0 or higher"
54+
# PowerShellGet = "PowerShellGet latest"
55+
# InvokeBuild = "InvokeBuild latest"
56+
# }
57+
58+
# function needsVSCode () {
59+
# try {
60+
# $vscodeVersion = (code -v)
61+
# if (-not $vscodeVersion) {
62+
# Throw
63+
# }
64+
# } catch {
65+
# try {
66+
# $vscodeInsidersVersion = (code-insiders -v)
67+
# if (-not $vscodeInsidersVersion) {
68+
# Throw
69+
# }
70+
# } catch {
71+
# return $true
72+
# }
73+
# }
74+
# return $false
75+
# }
76+
77+
# function needsNodeJS () {
78+
# try {
79+
# $nodeJSVersion = node -v
80+
# } catch {
81+
# return $true
82+
# }
83+
84+
# if ($nodeJSVersion -notmatch 'v(\d+\.\d+\.\d+)') {
85+
# return $true
86+
# }
87+
88+
# $nodeVer = [System.Version]$matches[1]
89+
# return ($nodeVer.Major -lt 6)
90+
# }
91+
92+
# function needsPowerShellGet () {
93+
# if (Get-Module -ListAvailable -Name PowerShellGet) {
94+
# return $false
95+
# }
96+
# return $true
97+
# }
98+
99+
# function needsInvokeBuild () {
100+
# if (Get-Module -ListAvailable -Name InvokeBuild) {
101+
# return $false
102+
# }
103+
# return $true
104+
# }
105+
106+
# function getMissingTools () {
107+
# $missingTools = @()
108+
109+
# if (needsVSCode) {
110+
# $missingTools += $NeededTools.VSCode
111+
# }
112+
# if (needsNodeJS) {
113+
# $missingTools += $NeededTools.NodeJS
114+
# }
115+
# if (needsPowerShellGet) {
116+
# $missingTools += $NeededTools.PowerShellGet
117+
# }
118+
# if (needsInvokeBuild) {
119+
# $missingTools += $NeededTools.InvokeBuild
120+
# }
121+
122+
# return $missingTools
123+
# }
124+
125+
# function hasMissingTools () {
126+
# return ((getMissingTools).Count -gt 0)
127+
# }
128+
129+
# if ($Bootstrap) {
130+
# $string = "Here is what your environment is missing:`n"
131+
# $missingTools = getMissingTools
132+
# if (($missingTools).Count -eq 0) {
133+
# $string += "* nothing!`n`n Run this script without a flag to build or a -Clean to clean."
134+
# } else {
135+
# $missingTools | ForEach-Object {$string += "* $_`n"}
136+
# $string += "`nAll instructions for installing these tools can be found on VSCode PowerShell's Github:`n" `
137+
# + "https://github.com/PowerShell/vscode-powershell/blob/main/docs/development.md"
138+
# }
139+
# Write-Host "`n$string`n"
140+
# } elseif(hasMissingTools) {
141+
# Write-Host "You are missing needed tools. Run './build.ps1 -Bootstrap' to see what they are."
142+
# } else {
143+
# if($Clean) {
144+
# Invoke-Build Clean
145+
# }
146+
147+
# Invoke-Build Build
148+
149+
# if($Test) {
150+
# Invoke-Build Test
151+
# }
152+
# }

vscode-powershell.build.ps1

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,57 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4+
using namespace Microsoft.PowerShell.Commands
5+
46
param(
5-
[ValidateSet("Debug", "Release")]
6-
[string]$Configuration = "Debug",
7-
[string]$EditorServicesRepoPath = $null
7+
[ValidateSet('Debug', 'Release')]
8+
[string]$Configuration = 'Debug',
9+
[string]$EditorServicesRepoPath = $null,
10+
# Keep this up to date with the version that vscode stable uses (see vscode help -> about)
11+
[Version]$RequiredNodeVersion = '16.14.2',
12+
[PowerShell.Commands.ModuleSpecification]$RequiredModules = @(
13+
@{ ModuleName = 'InvokeBuild'; ModuleVersion = '5.0.0' }
14+
@{ ModuleName = 'Pester'; ModuleVersion = '5.3.0' } #For PSES Build. TODO: Remove once we hook into PSES build script
15+
@{ ModuleName = 'PSScriptAnalyzer'; ModuleVersion = '1.21.0' } #For PSES Build
16+
@{ ModuleName = 'platyPS'; ModuleVersion = '0.14.0' } #For PSES Build
17+
)
818
)
19+
$ErrorActionPreference = 'Stop'
20+
21+
#Requires -Modules @{ ModuleName = "InvokeBuild"; ModuleVersion = "5.0.0" }
22+
23+
#region Prerequisites
24+
25+
task Prerequisites {
26+
Assert-NodeVersion $RequiredNodeVersion -ErrorAction Continue
27+
Assert-Module $RequiredModules -ErrorAction Continue
28+
}
29+
30+
function Assert-NodeVersion ($RequiredNodeVersion) {
31+
[version]$nodeVersion = (& node -v).Substring(1)
32+
if ($nodeVersion -lt $RequiredNodeVersion) {
33+
Write-Error "Node.js version $nodeVersion is not supported. Please install Node.js $RequiredNodeVersion or higher"
34+
return
35+
}
36+
Write-Debug "PREREQUISITE: Detected supported Node.js version $nodeVersion at or above minimum $RequiredNodeVersion"
37+
}
938

10-
#Requires -Modules @{ ModuleName = "InvokeBuild"; ModuleVersion = "3.0.0" }
39+
function Assert-Module ([ModuleSpecification[]]$RequiredModules) {
40+
foreach ($moduleSpec in $RequiredModules) {
41+
$moduleMatch = Get-Module -ListAvailable -FullyQualifiedName $moduleSpec |
42+
Sort-Object Version -Descending |
43+
Select-Object -First 1
44+
45+
if (-not $moduleMatch) {
46+
Write-Error "Module $($moduleSpec.Name) $($moduleSpec.Version) is not installed. Please install it."
47+
return
48+
}
49+
50+
Write-Debug "PREREQUISITE: Detected supported module $($moduleMatch.Name) $($moduleMatch.Version) at or above minimum $($moduleSpec.Version)"
51+
}
52+
}
53+
54+
#endregion Prerequisites
1155

1256
function Get-EditorServicesPath {
1357
$psesRepoPath = if ($EditorServicesRepoPath) {
@@ -35,10 +79,10 @@ task RestoreNodeModules -If { !(Test-Path ./node_modules) } {
3579

3680
task RestoreEditorServices -If (Get-EditorServicesPath) {
3781
switch ($Configuration) {
38-
"Debug" {
82+
'Debug' {
3983
# When debugging, we always rebuild PSES and ensure its symlinked so
4084
# that developers always have the latest local bits.
41-
if ((Get-Item ./modules -ErrorAction SilentlyContinue).LinkType -ne "SymbolicLink") {
85+
if ((Get-Item ./modules -ErrorAction SilentlyContinue).LinkType -ne 'SymbolicLink') {
4286
Write-Host "`n### Creating symbolic link to PSES" -ForegroundColor Green
4387
Remove-BuildItem ./modules
4488
New-Item -ItemType SymbolicLink -Path ./modules -Target "$(Split-Path (Get-EditorServicesPath))/module"
@@ -47,10 +91,10 @@ task RestoreEditorServices -If (Get-EditorServicesPath) {
4791
Write-Host "`n### Building PSES`n" -ForegroundColor Green
4892
Invoke-Build Build (Get-EditorServicesPath) -Configuration $Configuration
4993
}
50-
"Release" {
94+
'Release' {
5195
# When releasing, we ensure the bits are not symlinked but copied,
5296
# and only if they don't already exist.
53-
if ((Get-Item ./modules -ErrorAction SilentlyContinue).LinkType -eq "SymbolicLink") {
97+
if ((Get-Item ./modules -ErrorAction SilentlyContinue).LinkType -eq 'SymbolicLink') {
5498
Write-Host "`n### Deleting PSES symbolic link" -ForegroundColor Green
5599
Remove-BuildItem ./modules
56100
}
@@ -89,7 +133,7 @@ task CleanEditorServices -If (Get-EditorServicesPath) {
89133

90134
task Build Restore, {
91135
Write-Host "`n### Building vscode-powershell`n" -ForegroundColor Green
92-
Assert-Build (Test-Path ./modules/PowerShellEditorServices/bin) "Extension requires PSES"
136+
Assert-Build (Test-Path ./modules/PowerShellEditorServices/bin) 'Extension requires PSES'
93137

94138
Write-Host "`n### Linting TypeScript`n" -ForegroundColor Green
95139
Invoke-BuildExec { & npm run lint }
@@ -101,8 +145,8 @@ task Build Restore, {
101145
# Unfortunately `esbuild` doesn't support emitting 1:1 files (yet).
102146
# https://github.com/evanw/esbuild/issues/944
103147
switch ($Configuration) {
104-
"Debug" { Invoke-BuildExec { & npm run build -- --sourcemap } }
105-
"Release" { Invoke-BuildExec { & npm run build -- --minify } }
148+
'Debug' { Invoke-BuildExec { & npm run build -- --sourcemap } }
149+
'Release' { Invoke-BuildExec { & npm run build -- --minify } }
106150
}
107151
}
108152

@@ -133,7 +177,7 @@ task Package Build, {
133177
Assert-Build ($packageJson.version -eq $packageVersion)
134178

135179
Write-Host "`n### Packaging powershell-$packageVersion.vsix`n" -ForegroundColor Green
136-
Assert-Build ((Get-Item ./modules).LinkType -ne "SymbolicLink") "Packaging requires a copy of PSES, not a symlink!"
180+
Assert-Build ((Get-Item ./modules).LinkType -ne 'SymbolicLink') 'Packaging requires a copy of PSES, not a symlink!'
137181
if (Test-IsPreRelease) {
138182
Write-Host "`n### This is a pre-release!`n" -ForegroundColor Green
139183
Invoke-BuildExec { & npm run package -- --pre-release }
@@ -144,4 +188,7 @@ task Package Build, {
144188

145189
#endregion
146190

191+
192+
# High Level Tasks
193+
task Bootstrap Prerequisites
147194
task . Build, Test, Package

0 commit comments

Comments
 (0)