-
Notifications
You must be signed in to change notification settings - Fork 3
/
bootstrap.ps1
202 lines (170 loc) · 6.78 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<#
.DESCRIPTION
Wrapper for installing dependencies of a project
#>
function Convert-CustomObjectToHashtable {
param (
[Parameter(Mandatory = $true, Position = 0)]
[AllowEmptyString()]
[PSCustomObject]$CustomObject
)
# Create an empty hashtable
$hashtable = @{}
# Iterate through the properties of the PSCustomObject
$CustomObject.psobject.properties | ForEach-Object {
$hashtable[$_.Name] = $_.Value
}
# Return the hashtable
return $hashtable
}
# Load configuration from bootstrap.json or use default values
function Get-BootstrapConfig {
$bootstrapConfig = @{
python_version = "3.11"
python_package_manager = "poetry"
scoop_installer = "https://raw.githubusercontent.com/xxthunder/ScoopInstall/v1.1.0/install.ps1"
scoop_installer_with_repo_arg = $false
scoop_default_bucket_base_url = "https://raw.githubusercontent.com/ScoopInstaller/Main/master/bucket"
scoop_python_bucket_base_url = "https://raw.githubusercontent.com/ScoopInstaller/Versions/master/bucket"
scoop_ignore_scoopfile = $false
scoop_config = @{
autostash_on_conflict = "true"
use_lessmsi = "true"
scoop_repo = "https://github.com/xxthunder/Scoop.git"
scoop_branch = "develop"
}
}
$bootstrapJsonPath = "bootstrap.json"
if (Test-Path $bootstrapJsonPath) {
$JsonString = Get-Content $bootstrapJsonPath | Out-String
$custom_config = Convert-CustomObjectToHashtable -CustomObject (ConvertFrom-Json $JsonString)
if ($custom_config.scoop_config) {
$custom_config.scoop_config = Convert-CustomObjectToHashtable -CustomObject $custom_config.scoop_config
}
else {
$custom_config.scoop_config = @{}
}
}
# Merge the default and custom configuration
if ($custom_config) {
$custom_config.GetEnumerator() | ForEach-Object {
# Handle nested configuration
# Overwrite every key in the default configuration with the custom configuration if it exists
if ($bootstrapConfig[$_.Key] -is [Hashtable] -and $_.Value -is [Hashtable]) {
$hashtableValue = $_.Key
$_.Value.GetEnumerator() | ForEach-Object {
$bootstrapConfig[$hashtableValue][$_.Key] = $_.Value
}
}
else {
$bootstrapConfig[$_.Key] = $_.Value
}
}
}
return $bootstrapConfig
}
function Install-Scoop {
if (-Not (Get-Command 'scoop' -ErrorAction SilentlyContinue)) {
$tempDir = [System.IO.Path]::GetTempPath()
$tempFile = Join-Path $tempDir "install.ps1"
Invoke-RestMethod -Uri $config.scoop_installer -OutFile $tempFile
$installCmd = @("$tempFile")
if ($config.scoop_installer_with_repo_arg) {
$installCmd += "-ScoopAppRepoGit"
$installCmd += $config.scoop_config.scoop_repo
}
if ((New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
$installCmd += "-RunAsAdmin"
}
Invoke-CommandLine "& $($installCmd)"
Remove-Item $tempFile
Initialize-EnvPath
}
Write-Output "Applying scoop configuration"
foreach ($item in $config.scoop_config.GetEnumerator()) {
Invoke-CommandLine ("scoop config " + $item.Key + " " + $item.Value) -Silent $true -PrintCommand $false
}
# Install any installer dependencies
# CAUTION: the order is important and shall not be changed!
# - 7zip needs lessmsi for installation
# - innounp needs 7zip
$manifests = @(
"lessmsi.json",
"7zip.json",
"innounp.json",
"dark.json"
)
$manifests | ForEach-Object {
Invoke-CommandLine "scoop install $($config.scoop_default_bucket_base_url)/$_" -Silent $true -PrintCommand $false
}
# Import scoopfile.json
if ((-Not $config.scoop_ignore_scoopfile) -and (Test-Path -Path 'scoopfile.json')) {
Write-Output "File 'scoopfile.json' found, running 'scoop import' ..."
Invoke-CommandLine "scoop update"
# TODO: scoop's import feature is not working properly, do it with our ScoopWrapper in Pypeline
Invoke-CommandLine "scoop import scoopfile.json --reset"
Initialize-EnvPath
}
}
# Prepare virtual Python environment
function Install-PythonEnvironment {
if ((Test-Path -Path 'pyproject.toml') -or (Test-Path -Path 'Pipfile')) {
if ($clean) {
# Start with a fresh virtual environment
Remove-Path '.venv'
}
New-Directory '.venv'
$bootstrapPy = Join-Path $PSScriptRoot "bootstrap.py"
Invoke-CommandLine "$python $bootstrapPy"
}
else {
Write-Output "No Python config file found, skipping Python setup."
}
}
function Install-Python {
# Check if python is installed
$pythonPath = (Get-Command $python -ErrorAction SilentlyContinue).Source
if ($null -eq $pythonPath) {
Write-Output "$python not found. Try to install $python via scoop ..."
# Install python
Invoke-CommandLine "scoop install $($config.scoop_python_bucket_base_url)/$python.json"
Initialize-EnvPath
}
else {
Write-Output "$python found in $pythonPath, skipping installation."
}
}
function Get-PythonExecutableName {
param (
[string]$pythonVersion
)
# Split the version and handle varying segment lengths
$version_parts = $pythonVersion.Split(".")
$major_minor_version = $version_parts[0] # Always include the major version
# Append the minor version if it exists
if ($version_parts.Count -ge 2) {
$major_minor_version += $version_parts[1]
}
return "python" + $major_minor_version
}
# Main function needed for testing (will be mocked)
function Main {
Install-Scoop
Install-Python
Install-PythonEnvironment
}
## start of script
# Always set the $InformationPreference variable to "Continue" globally,
# this way it gets printed on execution and continues execution afterwards.
$InformationPreference = "Continue"
# Stop on first error
$ErrorActionPreference = "Stop"
# Load functions from utils.ps1
. "$PSScriptRoot\utils.ps1"
# Load config
$config = Get-BootstrapConfig
# python executable name
$python = Get-PythonExecutableName -pythonVersion $config.python_version
Write-Output "Python executable: $python"
Main
## end of script