Skip to content

Commit 303f9ca

Browse files
committed
Better maintainability for install.ps1 [GitHub #98 #107]
1 parent b089172 commit 303f9ca

File tree

1 file changed

+58
-60
lines changed

1 file changed

+58
-60
lines changed

tool/install.ps1

+58-60
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
# ---------------------------------------------------------------
22
# 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+
# |
57
# Created On : <2024-10-26>
68
# Last Modified : <2024-10-27>
79
#
8-
#
910
# chsrc installer for Windows
10-
#
1111
# ---------------------------------------------------------------
12+
1213
# 定义参数
1314
param(
1415
[switch]
1516
$h,
16-
$d = "${HOME}\Downloads",
17+
$d = "${HOME}\Downloads",
1718
$v = "pre"
1819
)
20+
1921
$fileName = "\chsrc.exe"
20-
$default_path = "${HOME}\Downloads"
22+
$default_path = "${HOME}\Downloads"
2123
$binary_name = "chsrc"
2224
$platform = "Windows"
2325

24-
$global:path = ""
25-
$global:arch = ""
26-
$global:version = ""
27-
$global:url = ""
26+
$global:path = ""
27+
$global:arch = ""
28+
$global:version = ""
29+
$global:url = ""
2830
$global:flag = 0
2931

30-
# 安装说明的多行字符串
32+
3133
$installInstructions = @"
3234
Hey friend
3335
@@ -43,19 +45,19 @@ $installInstructions
4345
4446
https://github.com/RubyMetric/chsrc
4547
"@
46-
exit # 退出脚本
48+
exit
4749
}
4850

4951
function Help {
50-
Write-Host
52+
Write-Host
5153
@"
5254
chsrc-installer: Install chsrc on ${platform}.
5355
5456
Usage: install.sh [options]
5557
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
5961
6062
"@
6163
}
@@ -66,51 +68,53 @@ if ($h) {
6668
exit
6769
}
6870

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 {
7082
# 检查目录是否存在
7183
if (-not (Test-Path -Path $d -PathType Container)) {
7284
# 如果目录不存在,执行下面的代码块
7385
try {
7486
New-Item -Path $d -ItemType Directory -Force | Out-Null
75-
Write-Host "Directory created: $d"
87+
output_info "Directory created: $d"
7688
$global:flag = 1
7789
} catch {
78-
# 捕获异常并输出错误信息
79-
Write-Host "Failed to create directory: $_"
80-
exit 1
90+
output_error "Failed to create directory: $_"
8191
}
8292
}
8393
$global:path=$d
8494
# 输出最终路径
85-
Write-Output "The path is set to: $global:path"
95+
output_info "Set install dir to: $global:path"
8696
}
8797

88-
function Get_Version {
89-
# 定义有效的版本
98+
function Set_Version {
9099
$pattern = '^(0\.1\.[4-9]|pre)$'
91100

92-
# 检查版本号是否符合
93101
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')."
98103
}
99104

100105
# 设置版本号
101106
$global:version=$v
102-
Write-Host "Version: $global:version"
107+
output_info "Set chsrc version: $global:version"
103108
}
104109

105-
function Get_Url {
110+
function Set_URL {
106111
# 获取 CPU 型号
107112
$cpuArchitecture = Get-WmiObject Win32_Processor `
108113
| Select-Object -First 1 -ExpandProperty Architecture
109114

110-
# 将 CPU 型号转换为 x64 或 x86
111115
switch ($cpuArchitecture) {
112116
0 { $global:arch = 'x86' }
113-
9 {
117+
9 {
114118
# 如果是 64 位操作系统,选择 x64 安装包,否则选择 x86
115119
if ([Environment]::Is64BitOperatingSystem) {
116120
$global:arch = "x64"
@@ -120,12 +124,10 @@ function Get_Url {
120124
}
121125
}
122126
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."
126128
}
127129
}
128-
Write-Host "CPU Architecture: $global:arch"
130+
output_info "My CPU architecture: $global:arch"
129131

130132
# Set URL
131133
if ($version -eq "pre") {
@@ -137,55 +139,51 @@ function Get_Url {
137139
"v" + "${global:version}/chsrc-${global:arch}-windows.exe"
138140
}
139141

140-
Write-Host "DownLoad URL: $global:url."
142+
output_info "Set downLoad URL: $global:url"
141143
}
142144

143-
function DownLoad {
145+
function Install {
144146
try {
145147
# 设置安全协议为 TLS 1.2
146148
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
147-
149+
148150
# 检查 URL 是否可访问
149151
$response = Invoke-WebRequest -Uri $global:url -Method Head -ErrorAction Stop
150-
151-
# 检查状态码是否为 200
152+
152153
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)"
155155
}
156156
}
157157
catch {
158158
Write-Host "Unable to download ${binary_name}. Please check your internet connection."
159-
exit 1 # 下载失败,输出错误信息并退出
159+
exit 1
160160
}
161161

162-
# 执行下载
163162
try {
163+
output_info "Downloading $binary_name ($global:arch architecture, $platform platform, version $global:version) to $global:path ..."
164164
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"
167166
} catch {
168-
Write-Host "Error: Unable to download $binary_name. Error: $_"
169-
exit 1 # 下载失败,输出错误信息并退出
167+
output_error "Unable to download $binary_name. Error: $_"
170168
}
171169
}
172170

173-
# 定义清理函数
174-
function Cleanup {
171+
172+
function cleanup {
175173
if ($flag -eq 1) {
176174
if (Test-Path -Path $path) {
177175
Remove-Item -Path $path -Recurse -Force # 删除路径及其内容
178-
Write-Host "Deleted the path: $path"
176+
output_info "Deleted the path: $path"
179177
}
180178
}
181179
}
182180

183-
# 注册退出事件
184-
$null = Register-EngineEvent PowerShell.Exiting -Action { Cleanup }
185181

186-
# 下载chsrc
182+
$null = Register-EngineEvent PowerShell.Exiting -Action { cleanup }
183+
187184

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

Comments
 (0)