22# SPDX-License-Identifier: GPL-3.0-or-later
33# ---------------------------------------------------------------
44# File Name : installer.ps1
5- # File Authors : xuan <wick.dynex@qq.com>
6- # | ChatGPT <https://chatgpt.com/>
7- # Contributors : Aoran Zeng <ccmywish@qq.com>
5+ # File Authors : xuan <wick.dynex@qq.com>
6+ # | ChatGPT <https://chatgpt.com/>
7+ # | 曾奥然 <ccmywish@qq.com>
8+ # Contributors : GitHub Copilot <https://github.com/copilot>
89# |
910# Created On : <2024-10-26>
1011# Last Modified : <2026-01-06>
1112#
1213# chsrc installer for Windows
1314# ---------------------------------------------------------------
1415
15- # 定义参数
1616param (
1717 [switch ]
1818 $Help ,
@@ -21,38 +21,20 @@ param(
2121)
2222
2323
24- $binary_name = " chsrc"
25- $platform = " Windows"
24+ $script : binary_name = " chsrc"
25+ $script : platform = " Windows"
2626
27- $global :install_dir = " "
28- $global :arch = " "
29- $global :version = " "
30- $global :url = " "
31- $global :flag = 0
27+ $script :install_dir = " "
28+ $script :arch = " "
29+ $script :target_version = " "
30+ $script :url = " "
31+ $script :create_dir_flag = $false
3232
3333
34- $installInstructions = @"
35- Hey friend
36-
37- This installer is only available for ${platform} .
38- If you're looking for installation instructions for your operating system,
39- please visit the following link:
40- "@
41-
42- # 检查当前操作系统是否为 macOS 或 Linux
43- if ($IsMacOS -or $IsLinux ) {
44- Write-Host @"
45- $installInstructions
46-
47- https://github.com/RubyMetric/chsrc
48- "@
49- exit
50- }
51-
52- function help {
34+ function Help {
5335 Write-Host
5436@"
55- chsrc-installer: Install chsrc on ${platform} .
37+ chsrc-installer: Install chsrc on ${ script: platform } .
5638
5739Usage: install.ps1 [options]
5840Options:
@@ -63,39 +45,53 @@ Options:
6345"@
6446}
6547
66- # 执行帮助函数
48+ # 检查当前操作系统是否为 macOS 或 Linux
49+ if ($IsMacOS -or $IsLinux ) {
50+ Write-Host @"
51+ Sorry,
52+
53+ This installer is only available for ${ script:platform } .
54+ If you're looking for installation instructions for your operating system,
55+ please visit the following link:
56+
57+ https://github.com/RubyMetric/chsrc
58+ "@
59+ exit
60+ }
61+
6762if ($Help ) {
68- help
63+ Help
6964 exit
7065}
7166
72- function output_info () {
67+ function Output_Info () {
7368 Write-Host " [INFO] $args "
7469}
7570
76- function output_error () {
71+ function Output_Error () {
7772 Write-Host " [ERROR] $args "
7873 exit 1
7974}
8075
8176
82- function Get_Downloads_Dir {
77+ # https://github.com/RubyMetric/chsrc/issues/332
78+ function Get_System_Downloads_Dir {
8379 # 尝试从注册表获取实际的 Downloads 文件夹位置
8480 try {
8581 $shellFoldersKey = " HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
8682 $downloadsGuid = " {374DE290-123F-4565-9164-39C4925E467B}"
8783 $downloadsPath = (Get-ItemProperty - Path $shellFoldersKey - Name $downloadsGuid - ErrorAction Stop).$downloadsGuid
88-
84+
8985 # 展开环境变量 (例如 %USERPROFILE% -> C:\Users\xxx)
9086 $downloadsPath = [System.Environment ]::ExpandEnvironmentVariables($downloadsPath )
91-
87+
9288 if (Test-Path - Path $downloadsPath - PathType Container) {
9389 return $downloadsPath
9490 }
9591 } catch {
9692 # 如果注册表读取失败,不输出错误信息,继续使用后备方案
9793 }
98-
94+
9995 # 后备方案:返回 null,稍后使用当前目录
10096 return $null
10197}
@@ -104,43 +100,43 @@ function Set_Install_Dir {
104100 # 如果用户未指定目录,则自动检测
105101 if ($null -eq $Directory ) {
106102 # 尝试获取实际的 Downloads 目录
107- $detectedDownloads = Get_Downloads_Dir
103+ $detectedDownloads = Get_System_Downloads_Dir
108104 if ($null -ne $detectedDownloads ) {
109105 $Directory = $detectedDownloads
110- output_info " Detected Downloads directory: $Directory "
106+ Output_Info " Detected Downloads directory: $Directory "
111107 } else {
112108 # 使用当前目录作为默认值
113109 $Directory = $PWD.Path
114- output_info " Using current directory: $Directory "
110+ Output_Info " Using current directory: $Directory "
115111 }
116112 }
117-
113+
118114 # 检查目录是否存在
119115 if (-not (Test-Path - Path $Directory - PathType Container)) {
120116 # 如果目录不存在,执行下面的代码块
121117 try {
122118 New-Item - Path $Directory - ItemType Directory - Force | Out-Null
123- output_info " Directory created: $Directory "
124- $global :flag = 1
119+ Output_Info " Directory created: $Directory "
120+ $script :create_dir_flag = $true
125121 } catch {
126- output_error " Failed to create directory: $_ "
122+ Output_Error " Failed to create directory: $_ "
127123 }
128124 }
129- $global :install_dir = $Directory
125+ $script :install_dir = $Directory
130126 # 输出最终路径
131- output_info " Set install dir to: $global :install_dir "
127+ Output_Info " Set install dir to: $script :install_dir "
132128}
133129
134130function Set_Version {
135131 $pattern = ' ^(0\.[1-9]\.[0-9]|pre)$'
136132
137133 if ($Version -notmatch $pattern ) {
138- output_error " Invalid version '$Version '. Please provide a valid version: 0.x.y (>=0.1.4) or 'pre'"
134+ Output_Error " Invalid version '$Version '. Please provide a valid version: 0.x.y (>=0.1.4) or 'pre'"
139135 }
140136
141137 # 设置版本号
142- $global :version = $Version
143- output_info " Set chsrc version: $global :version "
138+ $script :target_version = $Version
139+ Output_Info " Set chsrc version: $script :target_version "
144140}
145141
146142function Set_URL {
@@ -149,33 +145,33 @@ function Set_URL {
149145 | Select-Object - First 1 - ExpandProperty Architecture
150146
151147 switch ($cpuArchitecture ) {
152- 0 { $global :arch = ' x86' }
148+ 0 { $script :arch = ' x86' }
153149 9 {
154150 # 如果是 64 位操作系统,选择 x64 安装包,否则选择 x86
155151 if ([Environment ]::Is64BitOperatingSystem) {
156- $global :arch = " x64"
152+ $script :arch = " x64"
157153 }
158154 else {
159- $global :arch = " x86"
155+ $script :arch = " x86"
160156 }
161157 }
162158 default {
163- output_error " Unsupported architecture '$cpuArchitecture '. Only x86 or x64 architectures are supported."
159+ Output_Error " Unsupported architecture '$cpuArchitecture '. Only x86 or x64 architectures are supported."
164160 }
165161 }
166- output_info " Get my CPU architecture: $global :arch "
162+ Output_Info " Get my CPU architecture: $script :arch "
167163
168164 # Set URL
169- if ($version -eq " pre" ) {
170- $global :url = " https://gitee.com/RubyMetric/chsrc/releases/download/" + `
171- " ${ global: version } /chsrc-${ global :arch } -windows.exe"
165+ if ($script :target_version -eq " pre" ) {
166+ $script :url = " https://gitee.com/RubyMetric/chsrc/releases/download/" + `
167+ " ${ script: target_version } /chsrc-${ script :arch } -windows.exe"
172168 }
173169 else {
174- $global :url = " https://gitee.com/RubyMetric/chsrc/releases/download/" + `
175- " v" + " ${ global: version } /chsrc-${ global :arch } -windows.exe"
170+ $script :url = " https://gitee.com/RubyMetric/chsrc/releases/download/" + `
171+ " v" + " ${ script: target_version } /chsrc-${ script :arch } -windows.exe"
176172 }
177173
178- output_info " Set download URL: $global :url "
174+ Output_Info " Set download URL: $script :url "
179175}
180176
181177function Install {
@@ -184,40 +180,40 @@ function Install {
184180 [Net.ServicePointManager ]::SecurityProtocol = [Net.SecurityProtocolType ]::Tls12
185181
186182 # 检查 URL 是否可访问
187- $response = Invoke-WebRequest - Uri $global :url - Method Head - ErrorAction Stop
183+ $response = Invoke-WebRequest - Uri $script :url - Method Head - ErrorAction Stop
188184
189185 if ($response.StatusCode -ne 200 ) {
190- output_error " Unable to access $global :url . Status code: $ ( $response.StatusCode ) "
186+ Output_Error " Unable to access $script :url . Status code: $ ( $response.StatusCode ) "
191187 }
192188 }
193189 catch {
194- Write-Host " Unable to download ${binary_name} . Please check your internet connection."
190+ Write-Host " Unable to download ${ script: binary_name } . Please check your internet connection."
195191 exit 1
196192 }
197193
198194 try {
199- $outfile = " \${binary_name} .exe"
200- output_info " Downloading $binary_name (architecture: $global :arch , platform: $platform , version: $global :version ) to $global :install_dir "
201- Invoke-WebRequest - OutFile ($global :install_dir + $outfile ) - Uri $global :url - ErrorAction Stop
195+ $outfile = " \${ script: binary_name } .exe"
196+ Output_Info " Downloading $script : binary_name (architecture: $script :arch , platform: $script : platform , version: $script :target_version ) to $script :install_dir "
197+ Invoke-WebRequest - OutFile ($script :install_dir + $outfile ) - Uri $script :url - ErrorAction Stop
202198 # 🎉 这个符号会变成 ??? 不要添加
203- output_info " Installation completed, destination:" ($global :install_dir + $outfile )
199+ Output_Info " Installation completed, destination:" ($script :install_dir + $outfile )
204200 } catch {
205- output_error " Unable to download $binary_name . Error: $_ "
201+ Output_Error " Unable to download $script : binary_name . Error: $_ "
206202 }
207203}
208204
209205
210- function cleanup {
211- if ($flag -eq 1 ) {
212- if (Test-Path - Path $global :install_dir ) {
213- Remove-Item - Path $global :install_dir - Recurse - Force # 删除路径及其内容
214- output_info " Deleted the directory: $global :install_dir "
206+ function Cleanup {
207+ if ($script :create_dir_flag -eq $true ) {
208+ if (Test-Path - Path $script :install_dir ) {
209+ Remove-Item - Path $script :install_dir - Recurse - Force # 删除路径及其内容
210+ Output_Info " Deleted the directory: $script :install_dir "
215211 }
216212 }
217213}
218214
219215
220- $null = Register-EngineEvent PowerShell.Exiting - Action { cleanup }
216+ $null = Register-EngineEvent PowerShell.Exiting - Action { Cleanup }
221217
222218
223219# main
0 commit comments