|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Licensed to the Ed-Fi Alliance under one or more agreements. |
| 3 | +# The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0. |
| 4 | +# See the LICENSE and NOTICES files in the project root for more information. |
| 5 | +function Add-Path { |
| 6 | + param( |
| 7 | + [Parameter(Mandatory, Position=0)] |
| 8 | + [string] $LiteralPath, |
| 9 | + [ValidateSet('User', 'CurrentUser', 'Machine', 'LocalMachine')] |
| 10 | + [string] $Scope |
| 11 | + ) |
| 12 | + Set-StrictMode -Version 1; $ErrorActionPreference = 'Stop' |
| 13 | + $isMachineLevel = $Scope -in 'Machine', 'LocalMachine' |
| 14 | + if ($isMachineLevel -and -not $($ErrorActionPreference = 'Continue'; net session 2>$null)) { throw "You must run AS ADMIN to update the machine-level Path environment variable." } |
| 15 | + $regPath = 'registry::' + ('HKEY_CURRENT_USER\Environment', 'HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment')[$isMachineLevel] |
| 16 | + $currDirs = (Get-Item -LiteralPath $regPath).GetValue('Path', '', 'DoNotExpandEnvironmentNames') -split ';' -ne '' |
| 17 | + if ($LiteralPath -in $currDirs) { |
| 18 | + Write-Verbose "Already present in the persistent $(('user', 'machine')[$isMachineLevel])-level Path: $LiteralPath" |
| 19 | + return |
| 20 | + } |
| 21 | + $newValue = ($currDirs + $LiteralPath) -join ';' |
| 22 | + Set-ItemProperty -Type ExpandString -LiteralPath $regPath Path $newValue |
| 23 | + $dummyName = [guid]::NewGuid().ToString() |
| 24 | + [Environment]::SetEnvironmentVariable($dummyName, 'foo', 'User') |
| 25 | + [Environment]::SetEnvironmentVariable($dummyName, [NullString]::value, 'User') |
| 26 | + $env:Path = ($env:Path -replace ';$') + ';' + $LiteralPath |
| 27 | + Write-Verbose "`"$LiteralPath`" successfully appended to the persistent $(('user', 'machine')[$isMachineLevel])-level Path and also the current-process value." |
| 28 | + } |
| 29 | +function Test-ExitCode { |
| 30 | + if ($LASTEXITCODE -ne 0) { |
| 31 | + throw @" |
| 32 | +The last task failed with exit code $LASTEXITCODE |
| 33 | +$(Get-PSCallStack) |
| 34 | +"@ |
| 35 | + } |
| 36 | +} |
| 37 | +####### Configure-Windows.psm1 |
| 38 | +function Set-TLS12Support { |
| 39 | + Write-Host "Enabling TLS 1.2" |
| 40 | + |
| 41 | + if (-not [Net.ServicePointManager]::SecurityProtocol.HasFlag([Net.SecurityProtocolType]::Tls12)) { |
| 42 | + [Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::Tls12 |
| 43 | + } |
| 44 | +} |
| 45 | +function Invoke-RefreshPath { |
| 46 | + # Some of the installs in this process do not set the immediate path correctly. |
| 47 | + # This function simply reads the global path settings and reloads them. Useful |
| 48 | + # when you can't even get to chocolatey's `refreshenv` command. |
| 49 | + $env:Path=( |
| 50 | + [System.Environment]::GetEnvironmentVariable("Path","Machine"), |
| 51 | + [System.Environment]::GetEnvironmentVariable("Path","User") |
| 52 | + ) -match '.' -join ';' |
| 53 | +} |
| 54 | +function Enable-LongFileNames { |
| 55 | + Write-Host "Enabling long file name support" |
| 56 | + if (Test-Path 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem') { |
| 57 | + Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' -name "LongPathsEnabled" -Value 1 -Verbose -Force |
| 58 | + } |
| 59 | +} |
| 60 | +function Install-PowerShellTools { |
| 61 | + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 |
| 62 | + Install-PackageProvider -Name NuGet -Force |
| 63 | + Install-Module CredentialManager -AllowClobber -Force |
| 64 | + Install-Module SqlServer -AllowClobber -Force |
| 65 | +} |
| 66 | +function Update-Path { |
| 67 | + $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + |
| 68 | + ";" + |
| 69 | + [System.Environment]::GetEnvironmentVariable("Path","User") |
| 70 | +} |
| 71 | +###### Install-Applications.psm1 |
| 72 | +$common_args = @( |
| 73 | + "--execution-timeout=$installTimeout", |
| 74 | + "-y", |
| 75 | + "--ignore-pending-reboot" |
| 76 | +) |
| 77 | +$installTimeout = 14400 # Set to 0 for infinite |
| 78 | +function Install-Chocolatey { |
| 79 | + if (! (Get-Command choco.exe -ErrorAction SilentlyContinue )) { |
| 80 | + Set-ExecutionPolicy Bypass -Scope Process -Force |
| 81 | + Invoke-WebRequest https://chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression |
| 82 | + $env:ChocolateyInstall = Convert-Path "$((Get-Command choco).path)\..\.." |
| 83 | + Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1" |
| 84 | + refreshenv |
| 85 | + } |
| 86 | +} |
| 87 | +function Install-Pyenv { |
| 88 | + #$pyenvVersion = cmd /c pyenv --version |
| 89 | + if(!(Test-Path "C:\.pyenv\pyenv-win\bin")){ |
| 90 | + &choco install pyenv-win @common_args |
| 91 | + refreshenv |
| 92 | + # refreshenv doesn't appear to be sufficient to recognize user environment variable changes |
| 93 | + Update-Path |
| 94 | + Copy-Item "C:\Windows\System32\config\systemprofile\.pyenv" "C:\.pyenv" -Recurse |
| 95 | + Add-Path "C:\.pyenv\pyenv-win\bin" -Scope Machine |
| 96 | + Add-Path "C:\.pyenv\pyenv-win\shims" -Scope Machine |
| 97 | + } |
| 98 | +} |
| 99 | +function Install-Python { |
| 100 | + pyenv install 3.9.4 |
| 101 | + pyenv rehash |
| 102 | + pyenv local 3.9.4 |
| 103 | + pyenv global 3.9.4 |
| 104 | +} |
| 105 | +function Install-Poetry { |
| 106 | + # Ensure pip is on the latest version |
| 107 | + python -m pip install --upgrade pip |
| 108 | + # Update local and global PATH variables |
| 109 | + $addition = "C:\.pyenv\pyenv-win\versions\3.9.4\Scripts" |
| 110 | + Add-Path $addition -Scope Machine |
| 111 | + refreshenv |
| 112 | + # Install poetry |
| 113 | + # Poetry's native installation process encounters SSL errors |
| 114 | + # in some environments. `pip install` is a reasonable alternative |
| 115 | + # that has been shown to work in our situation. |
| 116 | + pip install poetry |
| 117 | +} |
| 118 | +Set-NetFirewallProfile -Enabled False |
| 119 | +$ConfirmPreference="high" |
| 120 | +$ErrorActionPreference = "Stop" |
| 121 | +Set-TLS12Support |
| 122 | +Invoke-RefreshPath |
| 123 | +Enable-LongFileNames |
| 124 | +Install-PowerShellTools |
| 125 | +Install-Chocolatey |
| 126 | +Install-Pyenv |
| 127 | +Invoke-RefreshPath |
| 128 | +Install-Python |
| 129 | +Install-Poetry |
0 commit comments