2024-10-26 00:16:35 +08:00
|
|
|
# ---------------------------------------------------------------
|
2024-10-27 20:56:34 +08:00
|
|
|
# File Name : installer.ps1
|
2024-10-27 20:43:20 +08:00
|
|
|
# File Authors : xuan <wick.dynex@qq.com>
|
|
|
|
# | ChatGPT <https://chatgpt.com/>
|
|
|
|
# Contributors : Aoran Zeng <ccmywish@qq.com>
|
|
|
|
# |
|
2024-10-26 00:16:35 +08:00
|
|
|
# Created On : <2024-10-26>
|
2024-10-27 15:43:09 +08:00
|
|
|
# Last Modified : <2024-10-27>
|
2024-10-26 00:16:35 +08:00
|
|
|
#
|
|
|
|
# chsrc installer for Windows
|
|
|
|
# ---------------------------------------------------------------
|
2024-10-27 20:43:20 +08:00
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
# 定义参数
|
|
|
|
param(
|
|
|
|
[switch]
|
2024-10-27 20:55:16 +08:00
|
|
|
$Help,
|
|
|
|
$Directory = "${HOME}\Downloads",
|
|
|
|
$Version = "pre"
|
2024-10-27 15:25:14 +08:00
|
|
|
)
|
2024-10-27 20:43:20 +08:00
|
|
|
|
2024-10-27 20:55:16 +08:00
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
$binary_name = "chsrc"
|
2024-10-27 20:55:16 +08:00
|
|
|
$default_install_dir = "${HOME}\Downloads"
|
2024-10-27 15:25:14 +08:00
|
|
|
$platform = "Windows"
|
2024-10-26 00:16:35 +08:00
|
|
|
|
2024-10-27 20:55:16 +08:00
|
|
|
$global:install_dir = ""
|
2024-10-27 20:43:20 +08:00
|
|
|
$global:arch = ""
|
|
|
|
$global:version = ""
|
|
|
|
$global:url = ""
|
2024-10-27 15:25:14 +08:00
|
|
|
$global:flag = 0
|
2024-10-27 15:43:09 +08:00
|
|
|
|
2024-10-27 20:43:20 +08:00
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
$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
|
|
|
|
"@
|
2024-10-27 20:43:20 +08:00
|
|
|
exit
|
2024-10-27 15:25:14 +08:00
|
|
|
}
|
|
|
|
|
2024-10-27 20:55:16 +08:00
|
|
|
function help {
|
2024-10-27 20:43:20 +08:00
|
|
|
Write-Host
|
2024-10-27 15:25:14 +08:00
|
|
|
@"
|
2024-10-27 15:27:51 +08:00
|
|
|
chsrc-installer: Install chsrc on ${platform}.
|
2024-10-27 15:25:14 +08:00
|
|
|
|
2024-10-27 20:55:16 +08:00
|
|
|
Usage: install.ps1 [options]
|
2024-10-27 15:25:14 +08:00
|
|
|
Options:
|
2024-10-27 20:43:20 +08:00
|
|
|
-h Print this help information
|
2024-10-27 20:55:16 +08:00
|
|
|
-d <dir> Specify installation directory, default is $default_install_dir
|
2024-10-27 20:43:20 +08:00
|
|
|
-v <version> Specify chsrc version
|
2024-10-27 15:25:14 +08:00
|
|
|
|
|
|
|
"@
|
|
|
|
}
|
|
|
|
|
|
|
|
# 执行帮助函数
|
2024-10-27 20:55:16 +08:00
|
|
|
if ($Help) {
|
|
|
|
help
|
2024-10-27 15:25:14 +08:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2024-10-27 20:43:20 +08:00
|
|
|
function output_info () {
|
|
|
|
Write-Host "[INFO] $args"
|
|
|
|
}
|
|
|
|
|
|
|
|
function output_error () {
|
|
|
|
Write-Host "[ERROR] $args"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function Set_Install_Dir {
|
2024-10-27 15:25:14 +08:00
|
|
|
# 检查目录是否存在
|
2024-10-27 20:55:16 +08:00
|
|
|
if (-not (Test-Path -Path $Directory -PathType Container)) {
|
2024-10-27 15:25:14 +08:00
|
|
|
# 如果目录不存在,执行下面的代码块
|
|
|
|
try {
|
2024-10-27 20:55:16 +08:00
|
|
|
New-Item -Path $Directory -ItemType Directory -Force | Out-Null
|
|
|
|
output_info "Directory created: $Directory"
|
2024-10-27 15:25:14 +08:00
|
|
|
$global:flag = 1
|
|
|
|
} catch {
|
2024-10-27 20:43:20 +08:00
|
|
|
output_error "Failed to create directory: $_"
|
2024-10-27 15:25:14 +08:00
|
|
|
}
|
|
|
|
}
|
2024-10-27 20:55:16 +08:00
|
|
|
$global:install_dir=$Directory
|
2024-10-27 15:25:14 +08:00
|
|
|
# 输出最终路径
|
2024-10-27 20:55:16 +08:00
|
|
|
output_info "Set install dir to: $global:install_dir"
|
2024-10-27 15:25:14 +08:00
|
|
|
}
|
|
|
|
|
2024-10-27 20:43:20 +08:00
|
|
|
function Set_Version {
|
2024-10-27 15:25:14 +08:00
|
|
|
$pattern = '^(0\.1\.[4-9]|pre)$'
|
|
|
|
|
2024-10-27 20:55:16 +08:00
|
|
|
if ($Version -notmatch $pattern) {
|
|
|
|
output_error "Invalid version '$Version'. Please provide a valid version (0.1.4 - 0.1.9 or 'pre')."
|
2024-10-27 15:25:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# 设置版本号
|
2024-10-27 20:55:16 +08:00
|
|
|
$global:version=$Version
|
2024-10-27 20:43:20 +08:00
|
|
|
output_info "Set chsrc version: $global:version"
|
2024-10-27 15:25:14 +08:00
|
|
|
}
|
|
|
|
|
2024-10-27 20:43:20 +08:00
|
|
|
function Set_URL {
|
2024-10-27 15:25:14 +08:00
|
|
|
# 获取 CPU 型号
|
|
|
|
$cpuArchitecture = Get-WmiObject Win32_Processor `
|
|
|
|
| Select-Object -First 1 -ExpandProperty Architecture
|
|
|
|
|
|
|
|
switch ($cpuArchitecture) {
|
|
|
|
0 { $global:arch = 'x86' }
|
2024-10-27 20:43:20 +08:00
|
|
|
9 {
|
2024-10-27 15:25:14 +08:00
|
|
|
# 如果是 64 位操作系统,选择 x64 安装包,否则选择 x86
|
|
|
|
if ([Environment]::Is64BitOperatingSystem) {
|
|
|
|
$global:arch = "x64"
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$global:arch = "x86"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default {
|
2024-10-27 20:43:20 +08:00
|
|
|
output_error "Unsupported architecture '$cpuArchitecture'. Only x86 or x64 architectures are supported."
|
2024-10-27 15:25:14 +08:00
|
|
|
}
|
|
|
|
}
|
2024-10-27 20:43:20 +08:00
|
|
|
output_info "My CPU architecture: $global:arch"
|
2024-10-27 15:25:14 +08:00
|
|
|
|
|
|
|
# Set URL
|
2024-10-27 17:25:43 +08:00
|
|
|
if ($version -eq "pre") {
|
|
|
|
$global:url = "https://gitee.com/RubyMetric/chsrc/releases/download/" + `
|
|
|
|
"${global:version}/chsrc-${global:arch}-windows.exe"
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$global:url = "https://gitee.com/RubyMetric/chsrc/releases/download/" + `
|
|
|
|
"v" + "${global:version}/chsrc-${global:arch}-windows.exe"
|
|
|
|
}
|
2024-10-27 15:25:14 +08:00
|
|
|
|
2024-10-27 20:43:20 +08:00
|
|
|
output_info "Set downLoad URL: $global:url"
|
2024-10-27 15:25:14 +08:00
|
|
|
}
|
2024-10-26 00:16:35 +08:00
|
|
|
|
2024-10-27 20:43:20 +08:00
|
|
|
function Install {
|
2024-10-27 15:25:14 +08:00
|
|
|
try {
|
|
|
|
# 设置安全协议为 TLS 1.2
|
|
|
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
2024-10-27 20:43:20 +08:00
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
# 检查 URL 是否可访问
|
|
|
|
$response = Invoke-WebRequest -Uri $global:url -Method Head -ErrorAction Stop
|
2024-10-27 20:43:20 +08:00
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
if ($response.StatusCode -ne 200) {
|
2024-10-27 20:43:20 +08:00
|
|
|
output_error "Unable to access $global:url. Status code: $($response.StatusCode)"
|
2024-10-27 15:25:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
Write-Host "Unable to download ${binary_name}. Please check your internet connection."
|
2024-10-27 20:43:20 +08:00
|
|
|
exit 1
|
2024-10-27 15:25:14 +08:00
|
|
|
}
|
2024-10-26 00:16:35 +08:00
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
try {
|
2024-10-27 20:55:16 +08:00
|
|
|
output_info "Downloading $binary_name ($global:arch architecture, $platform platform, version $global:version) to $global:install_dir ..."
|
|
|
|
Invoke-WebRequest -OutFile ($global:install_dir + "\$binary_name") -Uri $global:url -ErrorAction Stop
|
|
|
|
output_info "🎉 Installation completed, destination dir: $global:install_dir"
|
2024-10-27 15:25:14 +08:00
|
|
|
} catch {
|
2024-10-27 20:43:20 +08:00
|
|
|
output_error "Unable to download $binary_name. Error: $_"
|
2024-10-27 15:25:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-27 20:43:20 +08:00
|
|
|
|
|
|
|
function cleanup {
|
2024-10-27 15:25:14 +08:00
|
|
|
if ($flag -eq 1) {
|
2024-10-27 20:55:16 +08:00
|
|
|
if (Test-Path -Path $global:install_dir) {
|
|
|
|
Remove-Item -Path $global:install_dir -Recurse -Force # 删除路径及其内容
|
|
|
|
output_info "Deleted the directory: $global:install_dir"
|
2024-10-27 15:25:14 +08:00
|
|
|
}
|
|
|
|
}
|
2024-10-26 00:16:35 +08:00
|
|
|
}
|
|
|
|
|
2024-10-27 15:25:14 +08:00
|
|
|
|
2024-10-27 20:43:20 +08:00
|
|
|
$null = Register-EngineEvent PowerShell.Exiting -Action { cleanup }
|
|
|
|
|
2024-10-26 00:16:35 +08:00
|
|
|
|
2024-10-27 20:43:20 +08:00
|
|
|
# main
|
|
|
|
Set_Install_Dir
|
|
|
|
Set_Version
|
|
|
|
Set_URL
|
|
|
|
Install
|