Better maintainability 2 for install.ps1 [GitHub #98 #107]

This commit is contained in:
Aoran Zeng 2024-10-27 20:55:16 +08:00
parent 303f9cad14
commit dd4a9efd52
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98

View File

@ -13,17 +13,17 @@
# 定义参数 # 定义参数
param( param(
[switch] [switch]
$h, $Help,
$d = "${HOME}\Downloads", $Directory = "${HOME}\Downloads",
$v = "pre" $Version = "pre"
) )
$fileName = "\chsrc.exe"
$default_path = "${HOME}\Downloads"
$binary_name = "chsrc" $binary_name = "chsrc"
$default_install_dir = "${HOME}\Downloads"
$platform = "Windows" $platform = "Windows"
$global:path = "" $global:install_dir = ""
$global:arch = "" $global:arch = ""
$global:version = "" $global:version = ""
$global:url = "" $global:url = ""
@ -48,23 +48,23 @@ https://github.com/RubyMetric/chsrc
exit exit
} }
function Help { function help {
Write-Host Write-Host
@" @"
chsrc-installer: Install chsrc on ${platform}. chsrc-installer: Install chsrc on ${platform}.
Usage: install.sh [options] Usage: install.ps1 [options]
Options: Options:
-h Print this help information -h Print this help information
-d <dir> Specify installation directory, default is $default_path -d <dir> Specify installation directory, default is $default_install_dir
-v <version> Specify chsrc version -v <version> Specify chsrc version
"@ "@
} }
# 执行帮助函数 # 执行帮助函数
if ($h) { if ($Help) {
Help help
exit exit
} }
@ -80,30 +80,30 @@ function output_error () {
function Set_Install_Dir { function Set_Install_Dir {
# 检查目录是否存在 # 检查目录是否存在
if (-not (Test-Path -Path $d -PathType Container)) { if (-not (Test-Path -Path $Directory -PathType Container)) {
# 如果目录不存在,执行下面的代码块 # 如果目录不存在,执行下面的代码块
try { try {
New-Item -Path $d -ItemType Directory -Force | Out-Null New-Item -Path $Directory -ItemType Directory -Force | Out-Null
output_info "Directory created: $d" output_info "Directory created: $Directory"
$global:flag = 1 $global:flag = 1
} catch { } catch {
output_error "Failed to create directory: $_" output_error "Failed to create directory: $_"
} }
} }
$global:path=$d $global:install_dir=$Directory
# 输出最终路径 # 输出最终路径
output_info "Set install dir to: $global:path" output_info "Set install dir to: $global:install_dir"
} }
function Set_Version { function Set_Version {
$pattern = '^(0\.1\.[4-9]|pre)$' $pattern = '^(0\.1\.[4-9]|pre)$'
if ($v -notmatch $pattern) { if ($Version -notmatch $pattern) {
output_error "Invalid version '$v'. Please provide a valid version (0.1.4 - 0.1.9 or 'pre')." output_error "Invalid version '$Version'. Please provide a valid version (0.1.4 - 0.1.9 or 'pre')."
} }
# 设置版本号 # 设置版本号
$global:version=$v $global:version=$Version
output_info "Set chsrc version: $global:version" output_info "Set chsrc version: $global:version"
} }
@ -160,9 +160,9 @@ function Install {
} }
try { try {
output_info "Downloading $binary_name ($global:arch architecture, $platform platform, version $global:version) to $global:path ..." output_info "Downloading $binary_name ($global:arch architecture, $platform platform, version $global:version) to $global:install_dir ..."
Invoke-WebRequest -OutFile ($global:path + $fileName) -Uri $global:url -ErrorAction Stop Invoke-WebRequest -OutFile ($global:install_dir + "\$binary_name") -Uri $global:url -ErrorAction Stop
output_info "🎉 Installation completed, path: $global:path" output_info "🎉 Installation completed, destination dir: $global:install_dir"
} catch { } catch {
output_error "Unable to download $binary_name. Error: $_" output_error "Unable to download $binary_name. Error: $_"
} }
@ -171,9 +171,9 @@ function Install {
function cleanup { function cleanup {
if ($flag -eq 1) { if ($flag -eq 1) {
if (Test-Path -Path $path) { if (Test-Path -Path $global:install_dir) {
Remove-Item -Path $path -Recurse -Force # 删除路径及其内容 Remove-Item -Path $global:install_dir -Recurse -Force # 删除路径及其内容
output_info "Deleted the path: $path" output_info "Deleted the directory: $global:install_dir"
} }
} }
} }