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

Commit e36e067

Browse files
committed
Add requirements file and replace existing prereq installation steps
1 parent 983dca2 commit e36e067

File tree

3 files changed

+72
-18
lines changed

3 files changed

+72
-18
lines changed

.vsts-ci/templates/ci-general.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ steps:
6161
inputs:
6262
targetType: inline
6363
script: |
64-
Install-Module InvokeBuild -Scope CurrentUser -Force -PassThru
65-
Install-Module platyPS -Scope CurrentUser -Force -Verbose -Passthru
66-
Invoke-Build -Configuration Release Package
64+
Invoke-Build -InstallPrerequisites -Configuration Release Package
6765
$PackageJson = Get-Content -Raw package.json | ConvertFrom-Json
6866
Write-Host "##vso[task.setvariable variable=vsixPath]$(Resolve-Path powershell-$($PackageJson.version).vsix)"
6967
workingDirectory: $(Build.SourcesDirectory)/vscode-powershell
@@ -81,11 +79,7 @@ steps:
8179
inputs:
8280
targetType: inline
8381
script: |
84-
$PSVersionTable
85-
Get-ChildItem env:
86-
Get-Module -ListAvailable Pester
87-
Install-Module InvokeBuild -Scope CurrentUser -Force
88-
Invoke-Build -Configuration Release Test
82+
Invoke-Build -InstallPrerequisites -Configuration Release Test
8983
workingDirectory: $(Build.SourcesDirectory)/vscode-powershell
9084
pwsh: ${{ parameters.pwsh }}
9185
env:

requirements.psd1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@{
2+
Pwsh = '7.2.0'
3+
Node = '16.14.2'
4+
Modules = @(
5+
@{ ModuleName = 'InvokeBuild'; RequiredVersion = '5.0.0' }
6+
@{ ModuleName = 'Pester'; RequiredVersion = '5.3.0' } #For PSES Build. TODO: Remove once we hook into PSES build script
7+
@{ ModuleName = 'PSScriptAnalyzer'; RequiredVersion = '1.21.0' } #For PSES Build
8+
@{ ModuleName = 'platyPS'; RequiredVersion = '0.14.2' } #For PSES Build
9+
)
10+
}

vscode-powershell.build.ps1

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

4+
#requires -modules @{ ModuleName = "InvokeBuild"; ModuleVersion = "5.0.0" }
5+
46
using namespace Microsoft.PowerShell.Commands
57
using namespace System.Management.Automation
68

79
param(
810
[ValidateSet('Debug', 'Release')]
911
[string]$Configuration = 'Debug',
12+
1013
[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
2028
)
2129
$SCRIPT:ErrorActionPreference = 'Stop'
2230

23-
#Requires -Modules @{ ModuleName = "InvokeBuild"; ModuleVersion = "5.0.0" }
2431

2532
#region Prerequisites
2633

2734
task Prerequisites {
2835
# 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.
2936
[ErrorRecord[]]$preRequisiteIssues = & {
37+
Assert-Pwsh $RequiredPowerShellVersion -ErrorAction Continue
3038
Assert-NodeVersion $RequiredNodeVersion -ErrorAction Continue
3139
Assert-Module $RequiredModules -ErrorAction Continue
3240
} 2>&1
@@ -40,9 +48,32 @@ task Prerequisites {
4048

4149
}
4250

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+
4371
function Assert-NodeVersion ($RequiredNodeVersion) {
4472
[version]$nodeVersion = (& node -v).Substring(1)
4573
if ($nodeVersion -lt $RequiredNodeVersion) {
74+
if ($InstallPrerequisites) {
75+
throw [NotImplementedException]'Automatic installation of Node.js is not yet supported.'
76+
}
4677
Write-Error "Node.js version $nodeVersion is not supported. Please install Node.js $RequiredNodeVersion or higher"
4778
return
4879
}
@@ -56,6 +87,25 @@ function Assert-Module ([ModuleSpecification[]]$RequiredModules) {
5687
Select-Object -First 1
5788

5889
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+
}
59109
Write-Error "Module $($moduleSpec.Name) $($moduleSpec.Version) is not installed. Please install it."
60110
return
61111
}

0 commit comments

Comments
 (0)