1
1
# Copyright (c) Microsoft Corporation.
2
2
# Licensed under the MIT License.
3
3
4
+ # requires -modules @ { ModuleName = " InvokeBuild" ; ModuleVersion = " 5.0.0" }
5
+
4
6
using namespace Microsoft.PowerShell.Commands
5
7
using namespace System.Management.Automation
6
8
7
9
param (
8
10
[ValidateSet (' Debug' , ' Release' )]
9
11
[string ]$Configuration = ' Debug' ,
12
+
10
13
[string ]$PSESBuildScriptPath ,
11
- # Keep this up to date with the version that vscode stable uses (see vscode help -> about)
12
- [Version ]$RequiredNodeVersion = ' 16.14.2' ,
13
- # List of modules that are required for the build
14
- [Microsoft.PowerShell.Commands.ModuleSpecification []]$RequiredModules = @ (
15
- @ { ModuleName = ' InvokeBuild' ; ModuleVersion = ' 5.0.0' }
16
- @ { ModuleName = ' Pester' ; ModuleVersion = ' 5.3.0' } # For PSES Build. TODO: Remove once we hook into PSES build script
17
- @ { ModuleName = ' PSScriptAnalyzer' ; ModuleVersion = ' 1.21.0' } # For PSES Build
18
- @ { ModuleName = ' platyPS' ; ModuleVersion = ' 0.14.0' } # For PSES Build
19
- )
14
+
15
+ [ValidateNotNullOrEmpty ()]
16
+ [string ]$RequirementsManifest = $ (Join-Path $PSScriptRoot ' requirements.psd1' ),
17
+
18
+ [ValidateNotNullOrEmpty ()]
19
+ [string ]$RequiredNodeVersion = $RequirementsManifest.Node ,
20
+
21
+ [ValidateNotNullOrEmpty ()]
22
+ [Microsoft.PowerShell.Commands.ModuleSpecification ]$RequiredModules = $RequirementsManifest.Node ,
23
+
24
+ [ValidateNotNullOrEmpty ()]
25
+ [Version ]$RequiredPowerShellVersion = $RequirementsManifest.Pwsh ,
26
+
27
+ [Switch ]$InstallPrerequisites
20
28
)
21
29
$SCRIPT :ErrorActionPreference = ' Stop'
22
30
23
- # Requires -Modules @ { ModuleName = " InvokeBuild" ; ModuleVersion = " 5.0.0" }
24
31
25
32
# region Prerequisites
26
33
27
34
task Prerequisites {
28
35
# We want all prereqs to run so we can tell the user everything they are missing, rather than them having to fix/check/fix/check/etc.
29
36
[ErrorRecord []]$preRequisiteIssues = & {
37
+ Assert-Pwsh $RequiredPowerShellVersion - ErrorAction Continue
30
38
Assert-NodeVersion $RequiredNodeVersion - ErrorAction Continue
31
39
Assert-Module $RequiredModules - ErrorAction Continue
32
40
} 2>&1
@@ -40,9 +48,32 @@ task Prerequisites {
40
48
41
49
}
42
50
51
+ function Assert-Pwsh ([string ]$RequiredPowerShellVersion ) {
52
+ try {
53
+ [Version ]$pwshVersion = (Get-Command - Name pwsh - CommandType Application).Version
54
+ } catch {
55
+ if ($InstallPrerequisites ) {
56
+ throw [NotImplementedException ]' Automatic installation of Pwsh is not yet supported.'
57
+ }
58
+ Write-Error " PowerShell (pwsh) not found on your system. Please install PowerShell $RequiredPowerShellVersion or higher and ensure it is available in your `$ env:PATH environment variable"
59
+ return
60
+ }
61
+ if ($pwshVersion -lt $RequiredPowerShellVersion ) {
62
+ if ($InstallPrerequisites ) {
63
+ throw [NotImplementedException ]' Automatic installation of Pwsh is not yet supported.'
64
+ }
65
+ Write-Error " PowerShell version $pwshVersion is not or no longer supported. Please install PowerShell $RequiredPowerShellVersion or higher"
66
+ return
67
+ }
68
+ Write-Debug " PREREQUISITE: Detected supported PowerShell version $psVersion at or above minimum $RequiredPowerShellVersion "
69
+ }
70
+
43
71
function Assert-NodeVersion ($RequiredNodeVersion ) {
44
72
[version ]$nodeVersion = (& node - v).Substring(1 )
45
73
if ($nodeVersion -lt $RequiredNodeVersion ) {
74
+ if ($InstallPrerequisites ) {
75
+ throw [NotImplementedException ]' Automatic installation of Node.js is not yet supported.'
76
+ }
46
77
Write-Error " Node.js version $nodeVersion is not supported. Please install Node.js $RequiredNodeVersion or higher"
47
78
return
48
79
}
@@ -56,6 +87,25 @@ function Assert-Module ([ModuleSpecification[]]$RequiredModules) {
56
87
Select-Object - First 1
57
88
58
89
if (-not $moduleMatch ) {
90
+ if ($InstallPrerequisites ) {
91
+ $otherPowershell = if ($PSVersionTable.PSVersion -lt ' 6.0.0' ) {
92
+ ' pwsh'
93
+ } else {
94
+ ' powershell'
95
+ }
96
+ Write-Verbose " PREREQUISITE: Installing Missing Module $ ( $moduleSpec.Name ) $ ( $moduleSpec.Version ) "
97
+ $installModuleParams = @ {
98
+ Name = $moduleSpec.Name
99
+ RequiredVersion = $moduleSpec.Version
100
+ Force = $true
101
+ Scope = ' CurrentUser'
102
+ }
103
+ Install-Module @installModuleParams
104
+
105
+ # We could do a symbolic link or point both instances to the same PSModulePath but there are some potential risks so we go slow in the name of safety.
106
+ Write-Verbose " PREREQUISITE: Installing Missing Module $ ( $moduleSpec.Name ) $ ( $moduleSpec.Version ) ($otherPowerShell )"
107
+ & $otherPowershell - noprofile - c " Install-Module -Name $ ( $moduleSpec.Name ) -RequiredVersion $ ( $moduleSpec.Version ) -Force -Scope CurrentUser -ErrorAction Stop"
108
+ }
59
109
Write-Error " Module $ ( $moduleSpec.Name ) $ ( $moduleSpec.Version ) is not installed. Please install it."
60
110
return
61
111
}
0 commit comments