2024-10-26 00:16:35 +08:00
|
|
|
|
# ---------------------------------------------------------------
|
|
|
|
|
# File Name : install.ps1
|
|
|
|
|
# File Authors : Aoran Zeng <ccmywish@qq.com>
|
|
|
|
|
# | ChatGPT <https://chatgpt.com/>
|
|
|
|
|
# Created On : <2024-10-26>
|
|
|
|
|
# Last Modified : <2024-10-26>
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
# chsrc installer for Windows
|
|
|
|
|
#
|
|
|
|
|
# ---------------------------------------------------------------
|
2024-10-27 15:25:14 +08:00
|
|
|
|
# 定义参数
|
|
|
|
|
param(
|
|
|
|
|
[switch]
|
|
|
|
|
$h,
|
|
|
|
|
$d = "${Home}\.chsrc\bin",
|
|
|
|
|
$v = "pre"
|
|
|
|
|
)
|
|
|
|
|
$fileName = "\chsrc.exe"
|
|
|
|
|
$default_path = "${Home}\.chsrc\bin"
|
|
|
|
|
$binary_name = "chsrc"
|
|
|
|
|
$platform = "Windows"
|
2024-10-26 00:16:35 +08:00
|
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
|
$global:path = ""
|
|
|
|
|
$global:arch = ""
|
|
|
|
|
$global:version = ""
|
|
|
|
|
$global:url = ""
|
|
|
|
|
$global:flag = 0
|
|
|
|
|
# 安装说明的多行字符串
|
|
|
|
|
$installInstructions = @"
|
|
|
|
|
Hey friend
|
2024-10-26 00:16:35 +08:00
|
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
|
This installer is only available for ${platform}.
|
|
|
|
|
If you're looking for installation instructions for your operating system,
|
|
|
|
|
please visit the following link:
|
|
|
|
|
"@
|
2024-10-26 00:16:35 +08:00
|
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
|
# 检查当前操作系统是否为 macOS 或 Linux
|
|
|
|
|
if ($IsMacOS -or $IsLinux) {
|
|
|
|
|
Write-Host @"
|
|
|
|
|
$installInstructions
|
2024-10-26 00:16:35 +08:00
|
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
|
https://github.com/RubyMetric/chsrc
|
|
|
|
|
"@
|
|
|
|
|
exit # 退出脚本
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Help {
|
|
|
|
|
Write-Host
|
|
|
|
|
@"
|
2024-10-27 15:27:51 +08:00
|
|
|
|
chsrc-installer: Install chsrc on ${platform}.
|
2024-10-27 15:25:14 +08:00
|
|
|
|
|
|
|
|
|
Usage: install.sh [options]
|
|
|
|
|
Options:
|
|
|
|
|
-h Print this help information.
|
|
|
|
|
-d <directory> Specify installation directory, default is $default_path.
|
|
|
|
|
-v <version> Specify chsrc version.
|
|
|
|
|
|
|
|
|
|
"@
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 执行帮助函数
|
|
|
|
|
if ($h) {
|
|
|
|
|
Help
|
|
|
|
|
exit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Get_Path {
|
|
|
|
|
# 检查目录是否存在
|
|
|
|
|
if (-not (Test-Path -Path $d -PathType Container)) {
|
|
|
|
|
# 如果目录不存在,执行下面的代码块
|
|
|
|
|
try {
|
|
|
|
|
New-Item -Path $d -ItemType Directory -Force | Out-Null
|
|
|
|
|
Write-Host "Directory created: $d"
|
|
|
|
|
$global:flag = 1
|
|
|
|
|
} catch {
|
|
|
|
|
# 捕获异常并输出错误信息
|
|
|
|
|
Write-Host "Failed to create directory: $_"
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$global:path=$d
|
|
|
|
|
# 输出最终路径
|
|
|
|
|
Write-Output "The path is set to: $global:path"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Get_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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 设置版本号
|
|
|
|
|
$global:version=$v
|
|
|
|
|
Write-Host "Version: $global:version"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Get_Url {
|
|
|
|
|
# 获取 CPU 型号
|
|
|
|
|
$cpuArchitecture = Get-WmiObject Win32_Processor `
|
|
|
|
|
| Select-Object -First 1 -ExpandProperty Architecture
|
|
|
|
|
|
|
|
|
|
# 将 CPU 型号转换为 x64 或 x86
|
|
|
|
|
switch ($cpuArchitecture) {
|
|
|
|
|
0 { $global:arch = 'x86' }
|
|
|
|
|
9 {
|
|
|
|
|
# 如果是 64 位操作系统,选择 x64 安装包,否则选择 x86
|
|
|
|
|
if ([Environment]::Is64BitOperatingSystem) {
|
|
|
|
|
$global:arch = "x64"
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$global:arch = "x86"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
default {
|
|
|
|
|
Write-Host "Error: Unsupported architecture '$cpuArchitecture'."
|
|
|
|
|
Write-Host "Only x86 or x64 architectures are supported."
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Write-Host "CPU Architecture: $global:arch"
|
|
|
|
|
|
|
|
|
|
# Set URL
|
|
|
|
|
$global:url = "https://gitee.com/RubyMetric/chsrc/releases/download/" + `
|
|
|
|
|
"${global:version}/chsrc-${global:arch}-windows.exe"
|
|
|
|
|
|
|
|
|
|
Write-Host "DownLoad URL: $global:url."
|
|
|
|
|
}
|
2024-10-26 00:16:35 +08:00
|
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
|
function DownLoad {
|
|
|
|
|
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,退出
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
Write-Host "Unable to download ${binary_name}. Please check your internet connection."
|
|
|
|
|
exit 1 # 下载失败,输出错误信息并退出
|
|
|
|
|
}
|
2024-10-26 00:16:35 +08:00
|
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
|
# 执行下载
|
|
|
|
|
try {
|
|
|
|
|
Invoke-WebRequest -OutFile ($global:path + $fileName) -Uri $global:url -ErrorAction Stop
|
2024-10-26 00:16:35 +08:00
|
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
|
# curl.exe -Lo $global:path $global:url
|
|
|
|
|
|
|
|
|
|
Write-Host "Downloading $binary_name ($global:arch architecture, $platform platform, version $global:version) to $global:path"
|
|
|
|
|
Write-Host "🎉 Installation completed, path: $global:path"
|
|
|
|
|
} catch {
|
|
|
|
|
Write-Host "Error: Unable to download $binary_name. Error: $_"
|
|
|
|
|
exit 1 # 下载失败,输出错误信息并退出
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# function DownLoad {
|
|
|
|
|
|
|
|
|
|
# try {
|
|
|
|
|
# [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
|
|
|
|
# Invoke-WebRequest -Uri $url -Method Head | `
|
|
|
|
|
# Where-Object -FilterScript { $_.StatusCode -ne 200 } # 检查状态码是否为 200
|
|
|
|
|
# }
|
|
|
|
|
# catch {
|
|
|
|
|
# Write-Host "Unable to download ${binary_name}. Please check your internet connection."
|
|
|
|
|
# exit 1 # 下载失败,输出错误信息并退出
|
|
|
|
|
|
|
|
|
|
# }
|
|
|
|
|
# Invoke-WebRequest -OutFile $path $url
|
|
|
|
|
# Write-Host "Downloading $binary_name ($arch architecture, $platform platform, version $version) to $path"
|
|
|
|
|
# Write-Host "🎉 Installation completed, path: $path_to_executable"
|
|
|
|
|
# }
|
|
|
|
|
|
|
|
|
|
# 定义清理函数
|
|
|
|
|
function Cleanup {
|
|
|
|
|
if ($flag -eq 1) {
|
|
|
|
|
if (Test-Path -Path $path) {
|
|
|
|
|
Remove-Item -Path $path -Recurse -Force # 删除路径及其内容
|
|
|
|
|
Write-Host "Deleted the path: $path"
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-26 00:16:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
|
# 注册退出事件
|
|
|
|
|
Register-EngineEvent PowerShell.Exiting -Action { Cleanup }
|
|
|
|
|
|
|
|
|
|
# 下载chsrc
|
2024-10-26 00:16:35 +08:00
|
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
|
Get_Path
|
|
|
|
|
Get_Version
|
|
|
|
|
Get_Url
|
|
|
|
|
DownLoad
|