mirror of
https://github.com/RubyMetric/chsrc
synced 2026-02-02 15:50:29 +08:00
优化 installer.ps1
This commit is contained in:
@@ -2,9 +2,10 @@
|
|||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
# ---------------------------------------------------------------
|
# ---------------------------------------------------------------
|
||||||
# File Name : installer.ps1
|
# File Name : installer.ps1
|
||||||
# File Authors : xuan <wick.dynex@qq.com>
|
# File Authors : xuan <wick.dynex@qq.com>
|
||||||
# | ChatGPT <https://chatgpt.com/>
|
# | ChatGPT <https://chatgpt.com/>
|
||||||
# Contributors : Aoran Zeng <ccmywish@qq.com>
|
# | 曾奥然 <ccmywish@qq.com>
|
||||||
|
# Contributors : GitHub Copilot <https://github.com/copilot>
|
||||||
# |
|
# |
|
||||||
# Created On : <2024-10-26>
|
# Created On : <2024-10-26>
|
||||||
# Last Modified : <2026-01-06>
|
# Last Modified : <2026-01-06>
|
||||||
@@ -12,7 +13,6 @@
|
|||||||
# chsrc installer for Windows
|
# chsrc installer for Windows
|
||||||
# ---------------------------------------------------------------
|
# ---------------------------------------------------------------
|
||||||
|
|
||||||
# 定义参数
|
|
||||||
param(
|
param(
|
||||||
[switch]
|
[switch]
|
||||||
$Help,
|
$Help,
|
||||||
@@ -21,38 +21,20 @@ param(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
$binary_name = "chsrc"
|
$script:binary_name = "chsrc"
|
||||||
$platform = "Windows"
|
$script:platform = "Windows"
|
||||||
|
|
||||||
$global:install_dir = ""
|
$script:install_dir = ""
|
||||||
$global:arch = ""
|
$script:arch = ""
|
||||||
$global:version = ""
|
$script:target_version = ""
|
||||||
$global:url = ""
|
$script:url = ""
|
||||||
$global:flag = 0
|
$script:create_dir_flag = $false
|
||||||
|
|
||||||
|
|
||||||
$installInstructions = @"
|
function Help {
|
||||||
Hey friend
|
|
||||||
|
|
||||||
This installer is only available for ${platform}.
|
|
||||||
If you're looking for installation instructions for your operating system,
|
|
||||||
please visit the following link:
|
|
||||||
"@
|
|
||||||
|
|
||||||
# 检查当前操作系统是否为 macOS 或 Linux
|
|
||||||
if ($IsMacOS -or $IsLinux) {
|
|
||||||
Write-Host @"
|
|
||||||
$installInstructions
|
|
||||||
|
|
||||||
https://github.com/RubyMetric/chsrc
|
|
||||||
"@
|
|
||||||
exit
|
|
||||||
}
|
|
||||||
|
|
||||||
function help {
|
|
||||||
Write-Host
|
Write-Host
|
||||||
@"
|
@"
|
||||||
chsrc-installer: Install chsrc on ${platform}.
|
chsrc-installer: Install chsrc on ${script:platform}.
|
||||||
|
|
||||||
Usage: install.ps1 [options]
|
Usage: install.ps1 [options]
|
||||||
Options:
|
Options:
|
||||||
@@ -63,23 +45,37 @@ Options:
|
|||||||
"@
|
"@
|
||||||
}
|
}
|
||||||
|
|
||||||
# 执行帮助函数
|
# 检查当前操作系统是否为 macOS 或 Linux
|
||||||
if ($Help) {
|
if ($IsMacOS -or $IsLinux) {
|
||||||
help
|
Write-Host @"
|
||||||
|
Sorry,
|
||||||
|
|
||||||
|
This installer is only available for ${script:platform}.
|
||||||
|
If you're looking for installation instructions for your operating system,
|
||||||
|
please visit the following link:
|
||||||
|
|
||||||
|
https://github.com/RubyMetric/chsrc
|
||||||
|
"@
|
||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
|
|
||||||
function output_info () {
|
if ($Help) {
|
||||||
|
Help
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
|
||||||
|
function Output_Info () {
|
||||||
Write-Host "[INFO] $args"
|
Write-Host "[INFO] $args"
|
||||||
}
|
}
|
||||||
|
|
||||||
function output_error () {
|
function Output_Error () {
|
||||||
Write-Host "[ERROR] $args"
|
Write-Host "[ERROR] $args"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function Get_Downloads_Dir {
|
# https://github.com/RubyMetric/chsrc/issues/332
|
||||||
|
function Get_System_Downloads_Dir {
|
||||||
# 尝试从注册表获取实际的 Downloads 文件夹位置
|
# 尝试从注册表获取实际的 Downloads 文件夹位置
|
||||||
try {
|
try {
|
||||||
$shellFoldersKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
|
$shellFoldersKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
|
||||||
@@ -104,14 +100,14 @@ function Set_Install_Dir {
|
|||||||
# 如果用户未指定目录,则自动检测
|
# 如果用户未指定目录,则自动检测
|
||||||
if ($null -eq $Directory) {
|
if ($null -eq $Directory) {
|
||||||
# 尝试获取实际的 Downloads 目录
|
# 尝试获取实际的 Downloads 目录
|
||||||
$detectedDownloads = Get_Downloads_Dir
|
$detectedDownloads = Get_System_Downloads_Dir
|
||||||
if ($null -ne $detectedDownloads) {
|
if ($null -ne $detectedDownloads) {
|
||||||
$Directory = $detectedDownloads
|
$Directory = $detectedDownloads
|
||||||
output_info "Detected Downloads directory: $Directory"
|
Output_Info "Detected Downloads directory: $Directory"
|
||||||
} else {
|
} else {
|
||||||
# 使用当前目录作为默认值
|
# 使用当前目录作为默认值
|
||||||
$Directory = $PWD.Path
|
$Directory = $PWD.Path
|
||||||
output_info "Using current directory: $Directory"
|
Output_Info "Using current directory: $Directory"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,27 +116,27 @@ function Set_Install_Dir {
|
|||||||
# 如果目录不存在,执行下面的代码块
|
# 如果目录不存在,执行下面的代码块
|
||||||
try {
|
try {
|
||||||
New-Item -Path $Directory -ItemType Directory -Force | Out-Null
|
New-Item -Path $Directory -ItemType Directory -Force | Out-Null
|
||||||
output_info "Directory created: $Directory"
|
Output_Info "Directory created: $Directory"
|
||||||
$global:flag = 1
|
$script:create_dir_flag = $true
|
||||||
} catch {
|
} catch {
|
||||||
output_error "Failed to create directory: $_"
|
Output_Error "Failed to create directory: $_"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$global:install_dir=$Directory
|
$script:install_dir=$Directory
|
||||||
# 输出最终路径
|
# 输出最终路径
|
||||||
output_info "Set install dir to: $global:install_dir"
|
Output_Info "Set install dir to: $script:install_dir"
|
||||||
}
|
}
|
||||||
|
|
||||||
function Set_Version {
|
function Set_Version {
|
||||||
$pattern = '^(0\.[1-9]\.[0-9]|pre)$'
|
$pattern = '^(0\.[1-9]\.[0-9]|pre)$'
|
||||||
|
|
||||||
if ($Version -notmatch $pattern) {
|
if ($Version -notmatch $pattern) {
|
||||||
output_error "Invalid version '$Version'. Please provide a valid version: 0.x.y (>=0.1.4) or 'pre'"
|
Output_Error "Invalid version '$Version'. Please provide a valid version: 0.x.y (>=0.1.4) or 'pre'"
|
||||||
}
|
}
|
||||||
|
|
||||||
# 设置版本号
|
# 设置版本号
|
||||||
$global:version=$Version
|
$script:target_version=$Version
|
||||||
output_info "Set chsrc version: $global:version"
|
Output_Info "Set chsrc version: $script:target_version"
|
||||||
}
|
}
|
||||||
|
|
||||||
function Set_URL {
|
function Set_URL {
|
||||||
@@ -149,33 +145,33 @@ function Set_URL {
|
|||||||
| Select-Object -First 1 -ExpandProperty Architecture
|
| Select-Object -First 1 -ExpandProperty Architecture
|
||||||
|
|
||||||
switch ($cpuArchitecture) {
|
switch ($cpuArchitecture) {
|
||||||
0 { $global:arch = 'x86' }
|
0 { $script:arch = 'x86' }
|
||||||
9 {
|
9 {
|
||||||
# 如果是 64 位操作系统,选择 x64 安装包,否则选择 x86
|
# 如果是 64 位操作系统,选择 x64 安装包,否则选择 x86
|
||||||
if ([Environment]::Is64BitOperatingSystem) {
|
if ([Environment]::Is64BitOperatingSystem) {
|
||||||
$global:arch = "x64"
|
$script:arch = "x64"
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$global:arch = "x86"
|
$script:arch = "x86"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
default {
|
default {
|
||||||
output_error "Unsupported architecture '$cpuArchitecture'. Only x86 or x64 architectures are supported."
|
Output_Error "Unsupported architecture '$cpuArchitecture'. Only x86 or x64 architectures are supported."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
output_info "Get my CPU architecture: $global:arch"
|
Output_Info "Get my CPU architecture: $script:arch"
|
||||||
|
|
||||||
# Set URL
|
# Set URL
|
||||||
if ($version -eq "pre") {
|
if ($script:target_version -eq "pre") {
|
||||||
$global:url = "https://gitee.com/RubyMetric/chsrc/releases/download/" + `
|
$script:url = "https://gitee.com/RubyMetric/chsrc/releases/download/" + `
|
||||||
"${global:version}/chsrc-${global:arch}-windows.exe"
|
"${script:target_version}/chsrc-${script:arch}-windows.exe"
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$global:url = "https://gitee.com/RubyMetric/chsrc/releases/download/" + `
|
$script:url = "https://gitee.com/RubyMetric/chsrc/releases/download/" + `
|
||||||
"v" + "${global:version}/chsrc-${global:arch}-windows.exe"
|
"v" + "${script:target_version}/chsrc-${script:arch}-windows.exe"
|
||||||
}
|
}
|
||||||
|
|
||||||
output_info "Set download URL: $global:url"
|
Output_Info "Set download URL: $script:url"
|
||||||
}
|
}
|
||||||
|
|
||||||
function Install {
|
function Install {
|
||||||
@@ -184,40 +180,40 @@ function Install {
|
|||||||
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||||||
|
|
||||||
# 检查 URL 是否可访问
|
# 检查 URL 是否可访问
|
||||||
$response = Invoke-WebRequest -Uri $global:url -Method Head -ErrorAction Stop
|
$response = Invoke-WebRequest -Uri $script:url -Method Head -ErrorAction Stop
|
||||||
|
|
||||||
if ($response.StatusCode -ne 200) {
|
if ($response.StatusCode -ne 200) {
|
||||||
output_error "Unable to access $global:url. Status code: $($response.StatusCode)"
|
Output_Error "Unable to access $script:url. Status code: $($response.StatusCode)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
Write-Host "Unable to download ${binary_name}. Please check your internet connection."
|
Write-Host "Unable to download ${script:binary_name}. Please check your internet connection."
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$outfile = "\${binary_name}.exe"
|
$outfile = "\${script:binary_name}.exe"
|
||||||
output_info "Downloading $binary_name (architecture: $global:arch, platform: $platform, version: $global:version) to $global:install_dir"
|
Output_Info "Downloading $script:binary_name (architecture: $script:arch, platform: $script:platform, version: $script:target_version) to $script:install_dir"
|
||||||
Invoke-WebRequest -OutFile ($global:install_dir + $outfile) -Uri $global:url -ErrorAction Stop
|
Invoke-WebRequest -OutFile ($script:install_dir + $outfile) -Uri $script:url -ErrorAction Stop
|
||||||
# 🎉 这个符号会变成 ??? 不要添加
|
# 🎉 这个符号会变成 ??? 不要添加
|
||||||
output_info "Installation completed, destination:" ($global:install_dir + $outfile)
|
Output_Info "Installation completed, destination:" ($script:install_dir + $outfile)
|
||||||
} catch {
|
} catch {
|
||||||
output_error "Unable to download $binary_name. Error: $_"
|
Output_Error "Unable to download $script:binary_name. Error: $_"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function cleanup {
|
function Cleanup {
|
||||||
if ($flag -eq 1) {
|
if ($script:create_dir_flag -eq $true) {
|
||||||
if (Test-Path -Path $global:install_dir) {
|
if (Test-Path -Path $script:install_dir) {
|
||||||
Remove-Item -Path $global:install_dir -Recurse -Force # 删除路径及其内容
|
Remove-Item -Path $script:install_dir -Recurse -Force # 删除路径及其内容
|
||||||
output_info "Deleted the directory: $global:install_dir"
|
Output_Info "Deleted the directory: $script:install_dir"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$null = Register-EngineEvent PowerShell.Exiting -Action { cleanup }
|
$null = Register-EngineEvent PowerShell.Exiting -Action { Cleanup }
|
||||||
|
|
||||||
|
|
||||||
# main
|
# main
|
||||||
|
|||||||
Reference in New Issue
Block a user