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

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

View File

@ -1,14 +1,15 @@
# --------------------------------------------------------------- # ---------------------------------------------------------------
# File Name : install.ps1 # File Name : install.ps1
# File Authors : Aoran Zeng <ccmywish@qq.com> # File Authors : xuan <wick.dynex@qq.com>
# | ChatGPT <https://chatgpt.com/> # | ChatGPT <https://chatgpt.com/>
# Contributors : Aoran Zeng <ccmywish@qq.com>
# |
# Created On : <2024-10-26> # Created On : <2024-10-26>
# Last Modified : <2024-10-27> # Last Modified : <2024-10-27>
# #
#
# chsrc installer for Windows # chsrc installer for Windows
#
# --------------------------------------------------------------- # ---------------------------------------------------------------
# 定义参数 # 定义参数
param( param(
[switch] [switch]
@ -16,6 +17,7 @@ param(
$d = "${HOME}\Downloads", $d = "${HOME}\Downloads",
$v = "pre" $v = "pre"
) )
$fileName = "\chsrc.exe" $fileName = "\chsrc.exe"
$default_path = "${HOME}\Downloads" $default_path = "${HOME}\Downloads"
$binary_name = "chsrc" $binary_name = "chsrc"
@ -27,7 +29,7 @@ $global:version = ""
$global:url = "" $global:url = ""
$global:flag = 0 $global:flag = 0
# 安装说明的多行字符串
$installInstructions = @" $installInstructions = @"
Hey friend Hey friend
@ -43,7 +45,7 @@ $installInstructions
https://github.com/RubyMetric/chsrc https://github.com/RubyMetric/chsrc
"@ "@
exit # 退出脚本 exit
} }
function Help { function Help {
@ -53,9 +55,9 @@ chsrc-installer: Install chsrc on ${platform}.
Usage: install.sh [options] Usage: install.sh [options]
Options: Options:
-h Print this help information. -h Print this help information
-d <directory> Specify installation directory, default is $default_path. -d <dir> Specify installation directory, default is $default_path
-v <version> Specify chsrc version. -v <version> Specify chsrc version
"@ "@
} }
@ -66,48 +68,50 @@ if ($h) {
exit exit
} }
function Get_Path { function output_info () {
Write-Host "[INFO] $args"
}
function output_error () {
Write-Host "[ERROR] $args"
exit 1
}
function Set_Install_Dir {
# 检查目录是否存在 # 检查目录是否存在
if (-not (Test-Path -Path $d -PathType Container)) { if (-not (Test-Path -Path $d -PathType Container)) {
# 如果目录不存在,执行下面的代码块 # 如果目录不存在,执行下面的代码块
try { try {
New-Item -Path $d -ItemType Directory -Force | Out-Null New-Item -Path $d -ItemType Directory -Force | Out-Null
Write-Host "Directory created: $d" output_info "Directory created: $d"
$global:flag = 1 $global:flag = 1
} catch { } catch {
# 捕获异常并输出错误信息 output_error "Failed to create directory: $_"
Write-Host "Failed to create directory: $_"
exit 1
} }
} }
$global:path=$d $global:path=$d
# 输出最终路径 # 输出最终路径
Write-Output "The path is set to: $global:path" output_info "Set install dir to: $global:path"
} }
function Get_Version { function Set_Version {
# 定义有效的版本
$pattern = '^(0\.1\.[4-9]|pre)$' $pattern = '^(0\.1\.[4-9]|pre)$'
# 检查版本号是否符合
if ($v -notmatch $pattern) { if ($v -notmatch $pattern) {
# 输出错误信息并结束程序 output_error "Invalid version '$v'. Please provide a valid version (0.1.4 - 0.1.9 or 'pre')."
Write-Host "Error: Invalid version '$v'."
Write-Host "Please provide a valid version (0.1.4 - 0.1.9 or 'pre')."
exit 1
} }
# 设置版本号 # 设置版本号
$global:version=$v $global:version=$v
Write-Host "Version: $global:version" output_info "Set chsrc version: $global:version"
} }
function Get_Url { function Set_URL {
# 获取 CPU 型号 # 获取 CPU 型号
$cpuArchitecture = Get-WmiObject Win32_Processor ` $cpuArchitecture = Get-WmiObject Win32_Processor `
| Select-Object -First 1 -ExpandProperty Architecture | Select-Object -First 1 -ExpandProperty Architecture
# 将 CPU 型号转换为 x64 或 x86
switch ($cpuArchitecture) { switch ($cpuArchitecture) {
0 { $global:arch = 'x86' } 0 { $global:arch = 'x86' }
9 { 9 {
@ -120,12 +124,10 @@ function Get_Url {
} }
} }
default { default {
Write-Host "Error: Unsupported architecture '$cpuArchitecture'." output_error "Unsupported architecture '$cpuArchitecture'. Only x86 or x64 architectures are supported."
Write-Host "Only x86 or x64 architectures are supported."
exit 1
} }
} }
Write-Host "CPU Architecture: $global:arch" output_info "My CPU architecture: $global:arch"
# Set URL # Set URL
if ($version -eq "pre") { if ($version -eq "pre") {
@ -137,10 +139,10 @@ function Get_Url {
"v" + "${global:version}/chsrc-${global:arch}-windows.exe" "v" + "${global:version}/chsrc-${global:arch}-windows.exe"
} }
Write-Host "DownLoad URL: $global:url." output_info "Set downLoad URL: $global:url"
} }
function DownLoad { function Install {
try { try {
# 设置安全协议为 TLS 1.2 # 设置安全协议为 TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
@ -148,44 +150,40 @@ function DownLoad {
# 检查 URL 是否可访问 # 检查 URL 是否可访问
$response = Invoke-WebRequest -Uri $global:url -Method Head -ErrorAction Stop $response = Invoke-WebRequest -Uri $global:url -Method Head -ErrorAction Stop
# 检查状态码是否为 200
if ($response.StatusCode -ne 200) { if ($response.StatusCode -ne 200) {
Write-Host "Error: Unable to access $global:url. Status code: $($response.StatusCode)" output_error "Unable to access $global:url. Status code: $($response.StatusCode)"
exit 1 # 状态码不为 200退出
} }
} }
catch { catch {
Write-Host "Unable to download ${binary_name}. Please check your internet connection." Write-Host "Unable to download ${binary_name}. Please check your internet connection."
exit 1 # 下载失败,输出错误信息并退出 exit 1
} }
# 执行下载
try { try {
output_info "Downloading $binary_name ($global:arch architecture, $platform platform, version $global:version) to $global:path ..."
Invoke-WebRequest -OutFile ($global:path + $fileName) -Uri $global:url -ErrorAction Stop Invoke-WebRequest -OutFile ($global:path + $fileName) -Uri $global:url -ErrorAction Stop
Write-Host "Downloading $binary_name ($global:arch architecture, $platform platform, version $global:version) to $global:path" output_info "🎉 Installation completed, path: $global:path"
Write-Host "🎉 Installation completed, path: $global:path"
} catch { } catch {
Write-Host "Error: Unable to download $binary_name. Error: $_" output_error "Unable to download $binary_name. Error: $_"
exit 1 # 下载失败,输出错误信息并退出
} }
} }
# 定义清理函数
function Cleanup { function cleanup {
if ($flag -eq 1) { if ($flag -eq 1) {
if (Test-Path -Path $path) { if (Test-Path -Path $path) {
Remove-Item -Path $path -Recurse -Force # 删除路径及其内容 Remove-Item -Path $path -Recurse -Force # 删除路径及其内容
Write-Host "Deleted the path: $path" output_info "Deleted the path: $path"
} }
} }
} }
# 注册退出事件
$null = Register-EngineEvent PowerShell.Exiting -Action { Cleanup }
# 下载chsrc $null = Register-EngineEvent PowerShell.Exiting -Action { cleanup }
Get_Path
Get_Version # main
Get_Url Set_Install_Dir
DownLoad Set_Version
Set_URL
Install