13
13
# 定义参数
14
14
param (
15
15
[switch ]
16
- $h ,
17
- $d = " ${HOME} \Downloads" ,
18
- $v = " pre"
16
+ $Help ,
17
+ $Directory = " ${HOME} \Downloads" ,
18
+ $Version = " pre"
19
19
)
20
20
21
- $fileName = " \chsrc.exe"
22
- $default_path = " ${HOME} \Downloads"
21
+
23
22
$binary_name = " chsrc"
23
+ $default_install_dir = " ${HOME} \Downloads"
24
24
$platform = " Windows"
25
25
26
- $global :path = " "
26
+ $global :install_dir = " "
27
27
$global :arch = " "
28
28
$global :version = " "
29
29
$global :url = " "
@@ -48,23 +48,23 @@ https://github.com/RubyMetric/chsrc
48
48
exit
49
49
}
50
50
51
- function Help {
51
+ function help {
52
52
Write-Host
53
53
@"
54
54
chsrc-installer: Install chsrc on ${platform} .
55
55
56
- Usage: install.sh [options]
56
+ Usage: install.ps1 [options]
57
57
Options:
58
58
-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
60
60
-v <version> Specify chsrc version
61
61
62
62
"@
63
63
}
64
64
65
65
# 执行帮助函数
66
- if ($h ) {
67
- Help
66
+ if ($Help ) {
67
+ help
68
68
exit
69
69
}
70
70
@@ -80,30 +80,30 @@ function output_error () {
80
80
81
81
function Set_Install_Dir {
82
82
# 检查目录是否存在
83
- if (-not (Test-Path - Path $d - PathType Container)) {
83
+ if (-not (Test-Path - Path $Directory - PathType Container)) {
84
84
# 如果目录不存在,执行下面的代码块
85
85
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 "
88
88
$global :flag = 1
89
89
} catch {
90
90
output_error " Failed to create directory: $_ "
91
91
}
92
92
}
93
- $global :path = $d
93
+ $global :install_dir = $Directory
94
94
# 输出最终路径
95
- output_info " Set install dir to: $global :path "
95
+ output_info " Set install dir to: $global :install_dir "
96
96
}
97
97
98
98
function Set_Version {
99
99
$pattern = ' ^(0\.1\.[4-9]|pre)$'
100
100
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')."
103
103
}
104
104
105
105
# 设置版本号
106
- $global :version = $v
106
+ $global :version = $Version
107
107
output_info " Set chsrc version: $global :version "
108
108
}
109
109
@@ -160,9 +160,9 @@ function Install {
160
160
}
161
161
162
162
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 "
166
166
} catch {
167
167
output_error " Unable to download $binary_name . Error: $_ "
168
168
}
@@ -171,9 +171,9 @@ function Install {
171
171
172
172
function cleanup {
173
173
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 "
177
177
}
178
178
}
179
179
}
0 commit comments