-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.ps1
137 lines (125 loc) · 5.64 KB
/
bootstrap.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<#
.DESCRIPTION
Script to install python and start the bootstrap.py.
This file was generated by yanga.
VERSION: 0.6.1
#>
$ErrorActionPreference = "Stop"
###################################################################################################
# Configuration
###################################################################################################
$bootstrapJsonPath = Join-Path $PSScriptRoot "bootstrap.json"
if (Test-Path $bootstrapJsonPath) {
$json = Get-Content $bootstrapJsonPath | ConvertFrom-Json
$config = @{
pythonVersion = $json.python_version
scoopInstaller = $json.scoop_installer
scoopMainBucketBaseUrl = $json.scoop_main_bucket_base_url
scoopPythonBucketBaseUrl = $json.scoop_python_bucket_base_url
}
} else {
$config = @{
pythonVersion = "3.10"
scoopInstaller = "https://raw.githubusercontent.com/ScoopInstaller/Install/master/install.ps1"
scoopMainBucketBaseUrl = "https://raw.githubusercontent.com/ScoopInstaller/Main/master/bucket"
scoopPythonBucketBaseUrl = "https://raw.githubusercontent.com/ScoopInstaller/Versions/master/bucket"
}
}
###################################################################################################
# Utility functions
###################################################################################################
# Update/Reload current environment variable PATH with settings from registry
Function Initialize-EnvPath {
# workaround for system-wide installations
if ($Env:USER_PATH_FIRST) {
$Env:Path = [System.Environment]::GetEnvironmentVariable("Path", "User") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "Machine")
}
else {
$Env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
}
}
Function Invoke-CommandLine {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingInvokeExpression', '', Justification = 'Usually this statement must be avoided (https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/avoid-using-invoke-expression?view=powershell-7.3), here it is OK as it does not execute unknown code.')]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$CommandLine,
[Parameter(Mandatory = $false, Position = 1)]
[bool]$StopAtError = $true,
[Parameter(Mandatory = $false, Position = 2)]
[bool]$Silent = $false
)
if (-Not $Silent) {
Write-Output "Executing: $CommandLine"
}
$global:LASTEXITCODE = 0
Invoke-Expression $CommandLine
if ($global:LASTEXITCODE -ne 0) {
if ($StopAtError) {
Write-Error "Command line call `"$CommandLine`" failed with exit code $global:LASTEXITCODE"
}
else {
if (-Not $Silent) {
Write-Output "Command line call `"$CommandLine`" failed with exit code $global:LASTEXITCODE, continuing ..."
}
}
}
}
Function Install-Scoop {
# Initial Scoop installation
if (-Not (Get-Command 'scoop' -ErrorAction SilentlyContinue)) {
$tempDir = [System.IO.Path]::GetTempPath()
$tempFile = "$tempDir\install.ps1"
Invoke-RestMethod $config.scoopInstaller -OutFile $tempFile
if ((New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
& $tempFile -RunAsAdmin
}
else {
& $tempFile
}
Initialize-EnvPath
Remove-Item $tempFile
}
# Install needed tools
Invoke-CommandLine "scoop update"
# Avoid deadlocks while updating scoop buckets
Invoke-CommandLine "scoop config autostash_on_conflict $true" -Silent $true
Initialize-EnvPath
}
###################################################################################################
# Main
###################################################################################################
# python executable name
$python = "python" + $config.pythonVersion.Replace(".", "")
# Check if scoop is installed
$scoopPath = (Get-Command scoop -ErrorAction SilentlyContinue).Source
if ($scoopPath -eq $null) {
Write-Output "Scoop not found. Trying to install scoop ..."
Install-Scoop
}
else {
Write-Output "Found scoop under $scoopPath."
}
# Check if python is installed
$pythonPath = (Get-Command $python -ErrorAction SilentlyContinue).Source
if ($pythonPath -eq $null) {
Write-Output "$python not found. Try to install $python via scoop ..."
# Install Python installer dependencies
Invoke-CommandLine "scoop install $($config.scoopMainBucketBaseUrl)/dark.json"
Invoke-CommandLine "scoop install $($config.scoopMainBucketBaseUrl)/lessmsi.json"
# Install python
Invoke-CommandLine "scoop install $($config.scoopPythonBucketBaseUrl)/$python.json"
}
else {
Write-Output "$python found in $pythonPath"
# Extract the directory of python exe file and add it to PATH. It needs to be the first entry in PATH
# such that this version is used when the user calls python and not python311
$pythonDir = [System.IO.Path]::GetDirectoryName($pythonPath)
Write-Output "Adding $pythonDir to PATH"
$Env:Path += ";$pythonDir"
}
# Call the bootstrap.py if it exists with all provided arguments
$buildScript = Join-Path $PSScriptRoot "bootstrap.py"
if (Test-Path $buildScript) {
Write-Output "Calling $buildScript ..."
& $python $buildScript $args
}