在 Windows 10/11 上自动配置简单的开发环境
通过下载 standalone build 并写入环境变量实现
目前配置的环境:

脚本如下

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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# Stop execution on any error
$ErrorActionPreference = "Stop"
# Disable the Invoke-WebRequest progress bar
$ProgressPreference = 'SilentlyContinue'
# Configure proxy settings (optional).
$PSDefaultParameterValues = @{ "Invoke-WebRequest:Proxy" = "http://127.0.0.1:10000" }
# Modify $RootDir as work directory
$RootDir = "D:\cli-programs"
$TmpDir = Join-Path $RootDir "tmp"
$SevenZipExe = "C:\Program Files\7-Zip\7z.exe"

function Write-Log {
param (
[Parameter(Mandatory = $true)]
[string]$Message,
[Parameter(Mandatory = $true)]
[ValidateSet("Info", "Success", "Warning", "Error")]
[string]$Level
)

switch ($Level) {
"Info" { Write-Host "[-] $Message" -ForegroundColor Cyan }
"Success" { Write-Host "[+] $Message" -ForegroundColor Green }
"Warning" { Write-Host "[!] $Message" -ForegroundColor Yellow }
"Error" { Write-Host "[X] $Message" -ForegroundColor Red }
}
}

function New-Directory {
param (
[Parameter(Mandatory = $true)]
[string]$Path
)

if (-not (Test-Path $Path)) {
New-Item -ItemType Directory -Path $Path | Out-Null
}
}

# Reference: https://stackoverflow.com/questions/76871462/set-itemproperty-is-failing-to-add-item-to-path/76876384#76876384
function Add-UserPath {
param (
[Parameter(Mandatory = $true)]
[string]$Path
)

$RegPath = "registry::HKEY_CURRENT_USER\Environment"
$CurrentDirs = (Get-Item -LiteralPath $RegPath).GetValue("Path", "", "DoNotExpandEnvironmentNames") -split ";" -ne ""

if ($Path -in $CurrentDirs) {
Write-Log "$Path already on User Path, skip." -Level Info
return
}

$NewUserPath = ($CurrentDirs + $Path) -join ";"
Set-ItemProperty -Type ExpandString -LiteralPath $RegPath Path $NewUserPath

# Broadcast WM_SETTINGCHANGE
$DummyName = New-Guid
[System.Environment]::SetEnvironmentVariable($DummyName, 'dummy', 'User')
[System.Environment]::SetEnvironmentVariable($DummyName, $null, 'User')
}

function Initialize-Installation {
Write-Log "Verifying 7-Zip installation..." -Level Info
if (-not (Test-Path -Path $SevenZipExe)) {
throw "7-Zip not found at $SevenZipExe. Please install it at https://www.7-zip.org/ first."
}
Write-Log "Done." -Level Success

foreach ($Path in $RootDir, $TmpDir) {
Write-Log "Checking and creating directory: $Path." -Level Info
New-Directory -Path $Path
Write-Log "Done." -Level Success
}
}

function Install-MinGW64 {
Write-Log "Configuring MinGW64..." -Level Info

Write-Log "Fetching lastest MinGW64 download link at https://winlibs.com/..." -Level Info
$WinlibsResponse = Invoke-WebRequest -Uri "https://winlibs.com/" -UseBasicParsing
$MinGW64DownloadLink = ($WinlibsResponse.Links | Where-Object {
$_.href -like "*winlibs-x86_64-posix-seh-gcc-*-mingw-w64ucrt-*.zip"
} | Select-Object -First 1).href

if (-not $MinGW64DownloadLink) {
throw "Could not find a valid MinGW64 download link on WinLibs."
}
Write-Log "Done." -Level Success

$MinGW64ZipFile = Join-Path $TmpDir "mingw64.zip"
Write-Log "Downloading MinGW64 to $MinGW64ZipFile at $MinGW64DownloadLink..." -Level Info
Invoke-WebRequest -Uri $MinGW64DownloadLink -UseBasicParsing -OutFile $MinGW64ZipFile
Write-Log "Done." -Level Success

Write-Log "Extracting MinGW64 to $RootDir\mingw64..." -Level Info
& $SevenZipExe x $MinGW64ZipFile -o"$RootDir" -y
Write-Log "Done." -Level Success

Write-Log "Configuring environment variables..." -Level Info
$MinGW64BinDir = Join-Path $RootDir "mingw64\bin"
do {
$Confirm = Read-Host "Add $MinGW64BinDir to User Path? (Y/N)"
$Confirm = $Confirm.ToUpper()
} while ($Confirm -notin @("Y", "N"))

if ($Confirm -eq "Y") {
Write-Log "Adding $MinGW64BinDir to User Path..." -Level Warning
Add-UserPath -Path $MinGW64BinDir
Write-Log "Done." -Level Success
}
else {
Write-Log "Adding was canceled. You may need to manually configure User Path." -Level Warning
}

Write-Log "Successfully configure MinGW64." -Level Success
}

function Install-UV {
$UVDir = "$RootDir\uv"
$UVBinDir = "$UVDir\bin"
$UVCacheDir = "$UVDir\cache" # UV_CACHE_DIR
$UVCredentialsDir = "$UVDir\credentials" # UV_CREDENTIALS_DIR
$UVInstallDir = "$UVDir\install" # UV_INSTALL_DIR
$UVPythonBinDir = "$UVDir\python-bin" # UV_PYTHON_BIN_DIR
$UVPythonCacheDir = "$UVDir\python-cache" # UV_PYTHON_CACHE_DIR
$UVPythonInstallDir = "$UVDir\python-install" # UV_PYTHON_INSTALL_DIR
$UVToolBinDir = "$UVDir\tool-bin" # UV_TOOL_BIN_DIR
$UVToolDir = "$UVDir\tool" # UV_TOOL_DIR

Write-Log "Configuring UV..." -Level Info

foreach ($Path in $UVDir, $UVBinDir, $UVCacheDir, $UVCredentialsDir, $UVInstallDir, $UVPythonBinDir, $UVPythonCacheDir, $UVPythonInstallDir, $UVToolBinDir, $UVToolDir) {
Write-Log "Checking and creating directory: $Path." -Level Info
New-Directory -Path $Path
Write-Log "Done." -Level Success
}

$UVZipFile = Join-Path $TmpDir "uv.zip"
$UVDownloadLink = "https://github.com/astral-sh/uv/releases/latest/download/uv-i686-pc-windows-msvc.zip"
Write-Log "Downloading UV to $UVZipFile at $UVDownloadLink..." -Level Info
Invoke-WebRequest -Uri $UVDownloadLink -UseBasicParsing -OutFile $UVZipFile
Write-Log "Done." -Level Success

Write-Log "Extracting UV to $UVBinDir..." -Level Info
& $SevenZipExe x $UVZipFile -o"$UVBinDir" -y
Write-Log "Done." -Level Success

Write-Log "Configuring environment variables..." -Level Info
$ModifiedEnvVars = @"
Following variables will add to User environment:
UV_CACHE_DIR=$UVCacheDir
UV_CREDENTIALS_DIR=$UVCredentialsDir
UV_INSTALL_DIR=$UVInstallDir
UV_PYTHON_BIN_DIR=$UVPythonBinDir
UV_PYTHON_CACHE_DIR=$UVPythonCacheDir
UV_PYTHON_INSTALL_DIR=$UVPythonInstallDir
UV_TOOL_BIN_DIR=$UVToolBinDir
UV_TOOL_DIR=$UVToolDir
Path+=Path;$UVBinDir;$UVPythonBinDir
"@
Write-Log $ModifiedEnvVars -Level Warning
do {
$Confirm = Read-Host "Confirm? (Y/N)"
$Confirm = $Confirm.ToUpper()
} while ($Confirm -notin @("Y", "N"))

if ($Confirm -eq "Y") {
foreach ($var in $UVBinDir, $UVPythonBinDir) {
Write-Log "Adding $var to User Path..." -Level Warning
Add-UserPath -Path $var
Write-Log "Done." -Level Success
}

$UVEnvVarsDict = [ordered]@{
"UV_CACHE_DIR" = $UVCacheDir
"UV_CREDENTIALS_DIR" = $UVCredentialsDir
"UV_INSTALL_DIR" = $UVInstallDir
"UV_PYTHON_BIN_DIR" = $UVPythonBinDir
"UV_PYTHON_CACHE_DIR" = $UVPythonCacheDir
"UV_PYTHON_INSTALL_DIR" = $UVPythonInstallDir
"UV_TOOL_BIN_DIR" = $UVToolBinDir
"UV_TOOL_DIR" = $UVToolDir
}
foreach ($item in $UVEnvVarsDict.Keys) {
$K = $item
$V = $UVEnvVarsDict[$item]
Write-Log "Adding $K=$V to User environments..." -Level Warning
[System.Environment]::SetEnvironmentVariable($K, $V, "User")
Write-Log "Done." -Level Success
}
} else {
Write-Log "Adding was canceled. You may need to manually configure User Path." -Level Warning
}

Write-Log "Successfully configure UV." -Level Success
}

function Install-Git {
Write-Log "Configuring Git..." -Level Info

Write-Log "Fetching lastest PortableGit download link at https://git-scm.com/install/windows..." -Level Info
$GitScmResponse = Invoke-WebRequest -Uri "https://git-scm.com/install/windows" -UseBasicParsing
$PortableGitDownloadLink = ($GitScmResponse.Links | Where-Object {
$_.href -like "*PortableGit-*-64-bit.7z.exe"
} | Select-Object -First 1).href

if (-not $PortableGitDownloadLink) {
throw "Could not find a valid PortableGit download link."
}
Write-Log "Done." -Level Success

$PortableGitExeFile = Join-Path $TmpDir "portable-git.exe"
Write-Log "Downloading PortableGit to $PortableGitExeFile at $PortableGitDownloadLink..." -Level Info
Invoke-WebRequest -Uri $PortableGitDownloadLink -UseBasicParsing -OutFile $PortableGitExeFile
Write-Log "Done." -Level Success

$GitDir = Join-Path $RootDir "git"
Write-Log "Extracting Git to $GitDir..." -Level Info
& $PortableGitExeFile -o $GitDir -y
Write-Log "Done." -Level Success
Write-Log "YOU NEED TO WAIT A FEW SECONDS AFTER EXTRACTION" -Level Warning

Write-Log "Configuring environment variables..." -Level Info
$GitBinDir = Join-Path $GitDir "cmd"
do {
$Confirm = Read-Host "Add $GitBinDir to User Path? (Y/N)"
$Confirm = $Confirm.ToUpper()
} while ($Confirm -notin @("Y", "N"))

if ($Confirm -eq "Y") {
Write-Log "Adding $GitBinDir to User Path..." -Level Warning
Add-UserPath -Path $GitBinDir
Write-Log "Done." -Level Success
}
else {
Write-Log "Adding was canceled. You may need to manually configure User Path." -Level Warning
}

Write-Log "Successfully configure Git." -Level Success
}

function Install-All {
Initialize-Installation
Install-MinGW64
Install-UV
Install-Git

Write-Log "Deleting $TmpDir..." -Level Info
Remove-Item $TmpDir -Recurse -Force
Write-Log "Done." -Level Success

Write-Log "If it doesn't work, please log out or reboot." -Level Info
}

try {
Install-All
} catch {
Write-Log $_ -Level Error
}