[Windows] Refactor PATH helpers (#8885)

This commit is contained in:
Vasilii Polikarpov
2023-11-29 13:00:16 +01:00
committed by GitHub
parent c73276d3f6
commit bfe32a2b12
14 changed files with 179 additions and 191 deletions

View File

@@ -6,11 +6,30 @@
Write-Host "Cleanup WinSxS"
Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase
# Sets the default install version to v1 for new distributions
# Set default version to 1 for WSL (aka LXSS - Linux Subsystem)
# The value should be set in the default user registry hive
# https://github.com/actions/runner-images/issues/5760
if (Test-IsWin22) {
Write-Host "Sets the default install version to v1 for new distributions"
Add-DefaultItem -DefaultVariable "DefaultVersion" -Value 1 -Name "DEFAULT\Software\Microsoft\Windows\CurrentVersion\Lxss" -Kind "DWord"
Write-Host "Setting WSL default version to 1"
Mount-RegistryHive `
-FileName "C:\Users\Default\NTUSER.DAT" `
-SubKey "HKLM\DEFAULT"
# Create the key if it doesn't exist
$keyPath = "DEFAULT\Software\Microsoft\Windows\CurrentVersion\Lxss"
if (-not (Test-Path $keyPath)) {
Write-Host "Creating $keyPath key"
New-Item -Path (Join-Path "HKLM:\" $keyPath) -Force | Out-Null
}
# Set the DefaultVersion value to 1
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($keyPath, $true)
$key.SetValue("DefaultVersion", "1", "DWord")
$key.Handle.Close()
[System.GC]::Collect()
Dismount-RegistryHive "HKLM\DEFAULT"
}
Write-Host "Clean up various directories"
@@ -70,9 +89,11 @@ $registrySettings = @(
)
$registrySettings | ForEach-Object {
$regPath = $PSItem.Path
New-ItemPath -Path $regPath
New-ItemProperty @PSItem -Force -ErrorAction Ignore
$regPath = $_.Path
if (-not (Test-Path $regPath)) {
New-Item -Path $regPath -Force -ErrorAction Ignore | Out-Null
}
New-ItemProperty @_ -Force -ErrorAction Ignore
} | Out-Null
# Disable Template Services / User Services added by Desktop Experience
@@ -86,7 +107,9 @@ $regUserServicesToDisables = @(
$regUserServicesToDisables | ForEach-Object {
$regPath = $_
New-ItemPath -Path $regPath
if (-not (Test-Path $regPath)) {
New-Item -Path $regPath -Force -ErrorAction Ignore | Out-Null
}
New-ItemProperty -Path $regPath -Name "Start" -Value 4 -PropertyType DWORD -Force -ErrorAction Ignore
New-ItemProperty -Path $regPath -Name "UserServiceFlags" -Value 0 -PropertyType DWORD -Force -ErrorAction Ignore
} | Out-Null

View File

@@ -3,7 +3,6 @@
## Desc: Install Git for Windows
## Supply chain security: Git - checksum validation, Hub CLI - managed by package manager
################################################################################
Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
# Install the latest version of Git for Windows
$repoURL = "https://api.github.com/repos/git-for-windows/git/releases/latest"
@@ -28,7 +27,7 @@ Install-Binary `
"/COMPONENTS=gitlfs") `
-ExpectedSHA256Sum $externalHash
Update-SessionEnvironment
Update-Environment
git config --system --add safe.directory "*"

View File

@@ -2,7 +2,6 @@
## File: Install-Haskell.ps1
## Desc: Install Haskell for Windows
################################################################################
Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
# install minimal ghcup, utilizing pre-installed msys2 at C:\msys64
Write-Host 'Installing ghcup...'
@@ -28,8 +27,7 @@ Start-DownloadWithRetry -Url $ghcupDownloadURL -Name "ghcup.exe" -DownloadPath "
[System.Environment]::SetEnvironmentVariable("CABAL_DIR", $cabalDir, "Machine")
Add-MachinePathItem "$ghcupPrefix\ghcup\bin"
Add-MachinePathItem "$cabalDir\bin"
Update-SessionEnvironment
Update-Environment
# Get 3 latest versions of GHC
$Versions = ghcup list -t ghc -r | Where-Object {$_ -notlike "prerelease"}

View File

@@ -24,7 +24,7 @@ function Set-JavaPath {
if ($Default) {
# Clean up any other Java folders from PATH to make sure that they won't conflict with each other
$currentPath = Get-MachinePath
$currentPath = [System.Environment]::GetEnvironmentVariable("PATH", "Machine")
$pathSegments = $currentPath.Split(';')
$newPathSegments = @()
@@ -39,7 +39,7 @@ function Set-JavaPath {
$newPath = $javaPath + '\bin;' + $newPath
Write-Host "Add $javaPath\bin to PATH"
Set-MachinePath -NewPath $newPath
[System.Environment]::SetEnvironmentVariable("PATH", $newPath, "Machine")
Write-Host "Set JAVA_HOME environmental variable as $javaPath"
[Environment]::SetEnvironmentVariable("JAVA_HOME", $javaPath, "Machine")
@@ -123,15 +123,14 @@ Install-ChocoPackage maven -ArgumentList "--version=$versionToInstall"
Install-ChocoPackage gradle
# Add maven env variables to Machine
[string]$m2 = (Get-MachinePath).Split(";") -match "maven"
$maven_opts = '-Xms256m'
[string]$m2 = ([Environment]::GetEnvironmentVariable("PATH", "Machine")).Split(";") -match "maven"
$m2_repo = 'C:\ProgramData\m2'
New-Item -Path $m2_repo -ItemType Directory -Force | Out-Null
[Environment]::SetEnvironmentVariable("M2", $m2, "Machine")
[Environment]::SetEnvironmentVariable("M2_REPO", $m2_repo, "Machine")
[Environment]::SetEnvironmentVariable("MAVEN_OPTS", $maven_opts, "Machine")
[Environment]::SetEnvironmentVariable("MAVEN_OPTS", "-Xms256m", "Machine")
# Download cobertura jars
$uri = 'https://repo1.maven.org/maven2/net/sourceforge/cobertura/cobertura/2.1.1/cobertura-2.1.1-bin.zip'

View File

@@ -5,8 +5,7 @@
Install-ChocoPackage hg -ArgumentList "--version", "5.0.0"
$hgPath = "${env:ProgramFiles}\Mercurial\"
Add-MachinePathItem $hgPath
$env:Path = Get-MachinePath
Add-MachinePathItem "${env:ProgramFiles}\Mercurial\"
Update-Environment
Invoke-PesterTests -TestFile "Tools" -TestName "Mercurial"

View File

@@ -8,8 +8,7 @@ $NsisVersion = (Get-ToolsetContent).nsis.version
Install-ChocoPackage nsis -ArgumentList "--version", "$NsisVersion"
$NsisPath = "${env:ProgramFiles(x86)}\NSIS\"
Add-MachinePathItem $NsisPath
$env:Path = Get-MachinePath
Add-MachinePathItem "${env:ProgramFiles(x86)}\NSIS\"
Update-Environment
Invoke-PesterTests -TestFile "Tools" -TestName "NSIS"

View File

@@ -16,7 +16,7 @@ $versionToInstall = Resolve-ChocoPackageVersion -PackageName "nodejs" -TargetVer
Install-ChocoPackage nodejs -ArgumentList "--version=$versionToInstall"
Add-MachinePathItem $PrefixPath
$env:Path = Get-MachinePath
Update-Environment
[Environment]::SetEnvironmentVariable("npm_config_prefix", $PrefixPath, "Machine")
$env:npm_config_prefix = $PrefixPath

View File

@@ -39,6 +39,6 @@ Install-Binary `
# Update PATH
Add-MachinePathItem "$installDir\bin"
$env:Path = Get-MachinePath
Update-Environment
Invoke-PesterTests -TestFile "Tools" -TestName "OpenSSL"

View File

@@ -15,7 +15,7 @@ Invoke-Expression "$InstallDir\$VcpkgExecPath integrate install"
# Add vcpkg to system environment
Add-MachinePathItem $InstallDir
$env:Path = Get-MachinePath
[Environment]::SetEnvironmentVariable("VCPKG_INSTALLATION_ROOT", $InstallDir, "Machine")
Update-Environment
Invoke-PesterTests -TestFile "Tools" -TestName "Vcpkg"