1
1
# ---------------------------------------------------------------
2
2
# File Name : install.ps1
3
- # File Authors : Aoran Zeng <ccmywish@qq.com>
4
- # | ChatGPT <https://chatgpt.com/>
3
+ # File Authors : xuan <wick.dynex@qq.com>
4
+ # | ChatGPT <https://chatgpt.com/>
5
+ # Contributors : Aoran Zeng <ccmywish@qq.com>
6
+ # |
5
7
# Created On : <2024-10-26>
6
8
# Last Modified : <2024-10-27>
7
9
#
8
- #
9
10
# chsrc installer for Windows
10
- #
11
11
# ---------------------------------------------------------------
12
+
12
13
# 定义参数
13
14
param (
14
15
[switch ]
15
16
$h ,
16
- $d = " ${HOME} \Downloads" ,
17
+ $d = " ${HOME} \Downloads" ,
17
18
$v = " pre"
18
19
)
20
+
19
21
$fileName = " \chsrc.exe"
20
- $default_path = " ${HOME} \Downloads"
22
+ $default_path = " ${HOME} \Downloads"
21
23
$binary_name = " chsrc"
22
24
$platform = " Windows"
23
25
24
- $global :path = " "
25
- $global :arch = " "
26
- $global :version = " "
27
- $global :url = " "
26
+ $global :path = " "
27
+ $global :arch = " "
28
+ $global :version = " "
29
+ $global :url = " "
28
30
$global :flag = 0
29
31
30
- # 安装说明的多行字符串
32
+
31
33
$installInstructions = @"
32
34
Hey friend
33
35
@@ -43,19 +45,19 @@ $installInstructions
43
45
44
46
https://github.com/RubyMetric/chsrc
45
47
"@
46
- exit # 退出脚本
48
+ exit
47
49
}
48
50
49
51
function Help {
50
- Write-Host
52
+ Write-Host
51
53
@"
52
54
chsrc-installer: Install chsrc on ${platform} .
53
55
54
56
Usage: install.sh [options]
55
57
Options:
56
- -h Print this help information.
57
- -d <directory > Specify installation directory, default is $default_path .
58
- -v <version> Specify chsrc version.
58
+ -h Print this help information
59
+ -d <dir > Specify installation directory, default is $default_path
60
+ -v <version> Specify chsrc version
59
61
60
62
"@
61
63
}
@@ -66,51 +68,53 @@ if ($h) {
66
68
exit
67
69
}
68
70
69
- function Get_Path {
71
+ function output_info () {
72
+ Write-Host " [INFO] $args "
73
+ }
74
+
75
+ function output_error () {
76
+ Write-Host " [ERROR] $args "
77
+ exit 1
78
+ }
79
+
80
+
81
+ function Set_Install_Dir {
70
82
# 检查目录是否存在
71
83
if (-not (Test-Path - Path $d - PathType Container)) {
72
84
# 如果目录不存在,执行下面的代码块
73
85
try {
74
86
New-Item - Path $d - ItemType Directory - Force | Out-Null
75
- Write-Host " Directory created: $d "
87
+ output_info " Directory created: $d "
76
88
$global :flag = 1
77
89
} catch {
78
- # 捕获异常并输出错误信息
79
- Write-Host " Failed to create directory: $_ "
80
- exit 1
90
+ output_error " Failed to create directory: $_ "
81
91
}
82
92
}
83
93
$global :path = $d
84
94
# 输出最终路径
85
- Write-Output " The path is set to: $global :path "
95
+ output_info " Set install dir to: $global :path "
86
96
}
87
97
88
- function Get_Version {
89
- # 定义有效的版本
98
+ function Set_Version {
90
99
$pattern = ' ^(0\.1\.[4-9]|pre)$'
91
100
92
- # 检查版本号是否符合
93
101
if ($v -notmatch $pattern ) {
94
- # 输出错误信息并结束程序
95
- Write-Host " Error: Invalid version '$v '."
96
- Write-Host " Please provide a valid version (0.1.4 - 0.1.9 or 'pre')."
97
- exit 1
102
+ output_error " Invalid version '$v '. Please provide a valid version (0.1.4 - 0.1.9 or 'pre')."
98
103
}
99
104
100
105
# 设置版本号
101
106
$global :version = $v
102
- Write-Host " Version : $global :version "
107
+ output_info " Set chsrc version : $global :version "
103
108
}
104
109
105
- function Get_Url {
110
+ function Set_URL {
106
111
# 获取 CPU 型号
107
112
$cpuArchitecture = Get-WmiObject Win32_Processor `
108
113
| Select-Object - First 1 - ExpandProperty Architecture
109
114
110
- # 将 CPU 型号转换为 x64 或 x86
111
115
switch ($cpuArchitecture ) {
112
116
0 { $global :arch = ' x86' }
113
- 9 {
117
+ 9 {
114
118
# 如果是 64 位操作系统,选择 x64 安装包,否则选择 x86
115
119
if ([Environment ]::Is64BitOperatingSystem) {
116
120
$global :arch = " x64"
@@ -120,12 +124,10 @@ function Get_Url {
120
124
}
121
125
}
122
126
default {
123
- Write-Host " Error: Unsupported architecture '$cpuArchitecture '."
124
- Write-Host " Only x86 or x64 architectures are supported."
125
- exit 1
127
+ output_error " Unsupported architecture '$cpuArchitecture '. Only x86 or x64 architectures are supported."
126
128
}
127
129
}
128
- Write-Host " CPU Architecture : $global :arch "
130
+ output_info " My CPU architecture : $global :arch "
129
131
130
132
# Set URL
131
133
if ($version -eq " pre" ) {
@@ -137,55 +139,51 @@ function Get_Url {
137
139
" v" + " ${ global:version } /chsrc-${ global:arch } -windows.exe"
138
140
}
139
141
140
- Write-Host " DownLoad URL: $global :url . "
142
+ output_info " Set downLoad URL: $global :url "
141
143
}
142
144
143
- function DownLoad {
145
+ function Install {
144
146
try {
145
147
# 设置安全协议为 TLS 1.2
146
148
[Net.ServicePointManager ]::SecurityProtocol = [Net.SecurityProtocolType ]::Tls12
147
-
149
+
148
150
# 检查 URL 是否可访问
149
151
$response = Invoke-WebRequest - Uri $global :url - Method Head - ErrorAction Stop
150
-
151
- # 检查状态码是否为 200
152
+
152
153
if ($response.StatusCode -ne 200 ) {
153
- Write-Host " Error: Unable to access $global :url . Status code: $ ( $response.StatusCode ) "
154
- exit 1 # 状态码不为 200,退出
154
+ output_error " Unable to access $global :url . Status code: $ ( $response.StatusCode ) "
155
155
}
156
156
}
157
157
catch {
158
158
Write-Host " Unable to download ${binary_name} . Please check your internet connection."
159
- exit 1 # 下载失败,输出错误信息并退出
159
+ exit 1
160
160
}
161
161
162
- # 执行下载
163
162
try {
163
+ output_info " Downloading $binary_name ($global :arch architecture, $platform platform, version $global :version ) to $global :path ..."
164
164
Invoke-WebRequest - OutFile ($global :path + $fileName ) - Uri $global :url - ErrorAction Stop
165
- Write-Host " Downloading $binary_name ($global :arch architecture, $platform platform, version $global :version ) to $global :path "
166
- Write-Host " 🎉 Installation completed, path: $global :path "
165
+ output_info " 🎉 Installation completed, path: $global :path "
167
166
} catch {
168
- Write-Host " Error: Unable to download $binary_name . Error: $_ "
169
- exit 1 # 下载失败,输出错误信息并退出
167
+ output_error " Unable to download $binary_name . Error: $_ "
170
168
}
171
169
}
172
170
173
- # 定义清理函数
174
- function Cleanup {
171
+
172
+ function cleanup {
175
173
if ($flag -eq 1 ) {
176
174
if (Test-Path - Path $path ) {
177
175
Remove-Item - Path $path - Recurse - Force # 删除路径及其内容
178
- Write-Host " Deleted the path: $path "
176
+ output_info " Deleted the path: $path "
179
177
}
180
178
}
181
179
}
182
180
183
- # 注册退出事件
184
- $null = Register-EngineEvent PowerShell.Exiting - Action { Cleanup }
185
181
186
- # 下载chsrc
182
+ $null = Register-EngineEvent PowerShell.Exiting - Action { cleanup }
183
+
187
184
188
- Get_Path
189
- Get_Version
190
- Get_Url
191
- DownLoad
185
+ # main
186
+ Set_Install_Dir
187
+ Set_Version
188
+ Set_URL
189
+ Install
0 commit comments