Skip to content

Commit dd4a9ef

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

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

tool/install.ps1

+25-25
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
# 定义参数
1414
param(
1515
[switch]
16-
$h,
17-
$d = "${HOME}\Downloads",
18-
$v = "pre"
16+
$Help,
17+
$Directory = "${HOME}\Downloads",
18+
$Version = "pre"
1919
)
2020

21-
$fileName = "\chsrc.exe"
22-
$default_path = "${HOME}\Downloads"
21+
2322
$binary_name = "chsrc"
23+
$default_install_dir = "${HOME}\Downloads"
2424
$platform = "Windows"
2525

26-
$global:path = ""
26+
$global:install_dir = ""
2727
$global:arch = ""
2828
$global:version = ""
2929
$global:url = ""
@@ -48,23 +48,23 @@ https://github.com/RubyMetric/chsrc
4848
exit
4949
}
5050

51-
function Help {
51+
function help {
5252
Write-Host
5353
@"
5454
chsrc-installer: Install chsrc on ${platform}.
5555
56-
Usage: install.sh [options]
56+
Usage: install.ps1 [options]
5757
Options:
5858
-h Print this help information
59-
-d <dir> Specify installation directory, default is $default_path
59+
-d <dir> Specify installation directory, default is $default_install_dir
6060
-v <version> Specify chsrc version
6161
6262
"@
6363
}
6464

6565
# 执行帮助函数
66-
if ($h) {
67-
Help
66+
if ($Help) {
67+
help
6868
exit
6969
}
7070

@@ -80,30 +80,30 @@ function output_error () {
8080

8181
function Set_Install_Dir {
8282
# 检查目录是否存在
83-
if (-not (Test-Path -Path $d -PathType Container)) {
83+
if (-not (Test-Path -Path $Directory -PathType Container)) {
8484
# 如果目录不存在,执行下面的代码块
8585
try {
86-
New-Item -Path $d -ItemType Directory -Force | Out-Null
87-
output_info "Directory created: $d"
86+
New-Item -Path $Directory -ItemType Directory -Force | Out-Null
87+
output_info "Directory created: $Directory"
8888
$global:flag = 1
8989
} catch {
9090
output_error "Failed to create directory: $_"
9191
}
9292
}
93-
$global:path=$d
93+
$global:install_dir=$Directory
9494
# 输出最终路径
95-
output_info "Set install dir to: $global:path"
95+
output_info "Set install dir to: $global:install_dir"
9696
}
9797

9898
function Set_Version {
9999
$pattern = '^(0\.1\.[4-9]|pre)$'
100100

101-
if ($v -notmatch $pattern) {
102-
output_error "Invalid version '$v'. Please provide a valid version (0.1.4 - 0.1.9 or 'pre')."
101+
if ($Version -notmatch $pattern) {
102+
output_error "Invalid version '$Version'. Please provide a valid version (0.1.4 - 0.1.9 or 'pre')."
103103
}
104104

105105
# 设置版本号
106-
$global:version=$v
106+
$global:version=$Version
107107
output_info "Set chsrc version: $global:version"
108108
}
109109

@@ -160,9 +160,9 @@ function Install {
160160
}
161161

162162
try {
163-
output_info "Downloading $binary_name ($global:arch architecture, $platform platform, version $global:version) to $global:path ..."
164-
Invoke-WebRequest -OutFile ($global:path + $fileName) -Uri $global:url -ErrorAction Stop
165-
output_info "🎉 Installation completed, path: $global:path"
163+
output_info "Downloading $binary_name ($global:arch architecture, $platform platform, version $global:version) to $global:install_dir ..."
164+
Invoke-WebRequest -OutFile ($global:install_dir + "\$binary_name") -Uri $global:url -ErrorAction Stop
165+
output_info "🎉 Installation completed, destination dir: $global:install_dir"
166166
} catch {
167167
output_error "Unable to download $binary_name. Error: $_"
168168
}
@@ -171,9 +171,9 @@ function Install {
171171

172172
function cleanup {
173173
if ($flag -eq 1) {
174-
if (Test-Path -Path $path) {
175-
Remove-Item -Path $path -Recurse -Force # 删除路径及其内容
176-
output_info "Deleted the path: $path"
174+
if (Test-Path -Path $global:install_dir) {
175+
Remove-Item -Path $global:install_dir -Recurse -Force # 删除路径及其内容
176+
output_info "Deleted the directory: $global:install_dir"
177177
}
178178
}
179179
}

0 commit comments

Comments
 (0)