Skip to content

Commit

Permalink
Better maintainability for install.ps1 [GitHub #98 #107]
Browse files Browse the repository at this point in the history
  • Loading branch information
ccmywish committed Oct 27, 2024
1 parent b089172 commit 303f9ca
Showing 1 changed file with 58 additions and 60 deletions.
118 changes: 58 additions & 60 deletions tool/install.ps1
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
# ---------------------------------------------------------------
# File Name : install.ps1
# File Authors : Aoran Zeng <[email protected]>
# | ChatGPT <https://chatgpt.com/>
# File Authors : xuan <[email protected]>
# | ChatGPT <https://chatgpt.com/>
# Contributors : Aoran Zeng <[email protected]>
# |
# Created On : <2024-10-26>
# Last Modified : <2024-10-27>
#
#
# chsrc installer for Windows
#
# ---------------------------------------------------------------

# 定义参数
param(
[switch]
$h,
$d = "${HOME}\Downloads",
$d = "${HOME}\Downloads",
$v = "pre"
)

$fileName = "\chsrc.exe"
$default_path = "${HOME}\Downloads"
$default_path = "${HOME}\Downloads"
$binary_name = "chsrc"
$platform = "Windows"

$global:path = ""
$global:arch = ""
$global:version = ""
$global:url = ""
$global:path = ""
$global:arch = ""
$global:version = ""
$global:url = ""
$global:flag = 0

# 安装说明的多行字符串

$installInstructions = @"
Hey friend
Expand All @@ -43,19 +45,19 @@ $installInstructions
https://github.com/RubyMetric/chsrc
"@
exit # 退出脚本
exit
}

function Help {
Write-Host
Write-Host
@"
chsrc-installer: Install chsrc on ${platform}.
Usage: install.sh [options]
Options:
-h Print this help information.
-d <directory> Specify installation directory, default is $default_path.
-v <version> Specify chsrc version.
-h Print this help information
-d <dir> Specify installation directory, default is $default_path
-v <version> Specify chsrc version
"@
}
Expand All @@ -66,51 +68,53 @@ if ($h) {
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)) {
# 如果目录不存在,执行下面的代码块
try {
New-Item -Path $d -ItemType Directory -Force | Out-Null
Write-Host "Directory created: $d"
output_info "Directory created: $d"
$global:flag = 1
} catch {
# 捕获异常并输出错误信息
Write-Host "Failed to create directory: $_"
exit 1
output_error "Failed to create directory: $_"
}
}
$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)$'

# 检查版本号是否符合
if ($v -notmatch $pattern) {
# 输出错误信息并结束程序
Write-Host "Error: Invalid version '$v'."
Write-Host "Please provide a valid version (0.1.4 - 0.1.9 or 'pre')."
exit 1
output_error "Invalid version '$v'. Please provide a valid version (0.1.4 - 0.1.9 or 'pre')."
}

# 设置版本号
$global:version=$v
Write-Host "Version: $global:version"
output_info "Set chsrc version: $global:version"
}

function Get_Url {
function Set_URL {
# 获取 CPU 型号
$cpuArchitecture = Get-WmiObject Win32_Processor `
| Select-Object -First 1 -ExpandProperty Architecture

# 将 CPU 型号转换为 x64 或 x86
switch ($cpuArchitecture) {
0 { $global:arch = 'x86' }
9 {
9 {
# 如果是 64 位操作系统,选择 x64 安装包,否则选择 x86
if ([Environment]::Is64BitOperatingSystem) {
$global:arch = "x64"
Expand All @@ -120,12 +124,10 @@ function Get_Url {
}
}
default {
Write-Host "Error: Unsupported architecture '$cpuArchitecture'."
Write-Host "Only x86 or x64 architectures are supported."
exit 1
output_error "Unsupported architecture '$cpuArchitecture'. Only x86 or x64 architectures are supported."
}
}
Write-Host "CPU Architecture: $global:arch"
output_info "My CPU architecture: $global:arch"

# Set URL
if ($version -eq "pre") {
Expand All @@ -137,55 +139,51 @@ function Get_Url {
"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 {
# 设置安全协议为 TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# 检查 URL 是否可访问
$response = Invoke-WebRequest -Uri $global:url -Method Head -ErrorAction Stop

# 检查状态码是否为 200

if ($response.StatusCode -ne 200) {
Write-Host "Error: Unable to access $global:url. Status code: $($response.StatusCode)"
exit 1 # 状态码不为 200,退出
output_error "Unable to access $global:url. Status code: $($response.StatusCode)"
}
}
catch {
Write-Host "Unable to download ${binary_name}. Please check your internet connection."
exit 1 # 下载失败,输出错误信息并退出
exit 1
}

# 执行下载
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
Write-Host "Downloading $binary_name ($global:arch architecture, $platform platform, version $global:version) to $global:path"
Write-Host "🎉 Installation completed, path: $global:path"
output_info "🎉 Installation completed, path: $global:path"
} catch {
Write-Host "Error: Unable to download $binary_name. Error: $_"
exit 1 # 下载失败,输出错误信息并退出
output_error "Unable to download $binary_name. Error: $_"
}
}

# 定义清理函数
function Cleanup {

function cleanup {
if ($flag -eq 1) {
if (Test-Path -Path $path) {
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
Get_Url
DownLoad
# main
Set_Install_Dir
Set_Version
Set_URL
Install

0 comments on commit 303f9ca

Please sign in to comment.