forked from cuinixam/python-app-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.ps1
More file actions
129 lines (115 loc) · 5.28 KB
/
bootstrap.ps1
File metadata and controls
129 lines (115 loc) · 5.28 KB
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
<#
.DESCRIPTION
Script to install python and start the build.py.
#>
# About preference variables: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variable
# Stop execution on first non-terminating error (an error that doesn't stop the cmdlet processing)
$ErrorActionPreference = "Stop"
###################################################################################################
# Configuration
###################################################################################################
$config = @{
# Required Python version - major.minor (e.g. 3.11)
pythonVersion = "3.10"
# Scoop install script
scoopInstaller = "https://raw.githubusercontent.com/ScoopInstaller/Install/master/install.ps1"
}
###################################################################################################
# Utility functions
###################################################################################################
Function Edit-Env {
# workaround for GithubActions
if ($Env:USER_PATH_FIRST -eq "true") {
$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)) {
$tempFile = New-TemporaryFile
Invoke-RestMethod $config.scoopInstaller -OutFile $tempFile.FullName
if ((New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
& $tempFile.FullName -RunAsAdmin
}
else {
& $tempFile.FullName
}
Edit-Env
Remove-Item $tempFile.FullName
}
# Install needed tools
Invoke-CommandLine "scoop update"
Invoke-CommandLine "scoop install lessmsi" -Silent $true
# Some old tweak to get 7zip installed correctly
Invoke-CommandLine "scoop config use_lessmsi $true" -Silent $true
# Avoid deadlocks while updating scoop buckets
Invoke-CommandLine "scoop config autostash_on_conflict $true" -Silent $true
# Some prerequisites to install other packages
Invoke-CommandLine "scoop install 7zip" -Silent $true
Invoke-CommandLine "scoop install innounp" -StopAtError $false -Silent $true
Invoke-CommandLine "scoop install dark" -Silent $true
Edit-Env
}
###################################################################################################
# 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
Invoke-CommandLine "scoop install https://raw.githubusercontent.com/ScoopInstaller/Versions/master/bucket/$python.json"
# Check if python is installed
} 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 build.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
}