Skip to content

Commit

Permalink
modify: install.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
MapoMagpie committed Feb 10, 2024
1 parent 51780f7 commit a42b5cb
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 136 deletions.
2 changes: 1 addition & 1 deletion core/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/goccy/go-yaml"
)

var version = "1.0.2"
var version = "1.0.3"

type Options struct {
RestartRimeCmd string `yaml:"restart_rime_cmd"`
Expand Down
47 changes: 10 additions & 37 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@
# 2. $exe or $e

$ErrorActionPreference = 'Stop'
$inputRepo = if ($repo) {
"${repo}"
} else {
"${r}"
}

$inputVersion = if ($version) {
"${version}"
} else {
"${v}"
}

$inputExe = if ($exe) {
"${exe}"
} else {
"${e}"
}

$githubUrl = if ($github) {
"${github}"
Expand All @@ -34,38 +17,28 @@ $githubUrl = if ($github) {
"https://github.com"
}

$arr = $inputRepo.Split('/')

$owner = $arr.Get(0)
$repoName = $arr.Get(1)
$exeName = "${inputExe}"

if ($exeName -eq "") {
$exeName = "${repoName}"
}

if ($inputVersion) {
$version = "${inputVersion}"
}
$owner = "MapoMagpie"
$repoName = "rimedm"
$exeName = "rimedm"

if ([Environment]::Is64BitProcess) {
$arch = "amd64"
$arch = "x86_64"
} else {
$arch = "386"
$arch = "i386"
}

$BinDir = "$Home\bin"
$downloadedTagGz = "$BinDir\${exeName}.tar.gz"
$downloadedTagGz = "$BinDir\${exeName}.zip"
$downloadedExe = "$BinDir\${exeName}.exe"
$Target = "windows_$arch"
$Target = "Windows_$arch"

# GitHub requires TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$ResourceUri = if (!$version) {
"${githubUrl}/${owner}/${repoName}/releases/latest/download/${exeName}_${Target}.tar.gz"
"${githubUrl}/${owner}/${repoName}/releases/latest/download/${exeName}_${Target}.zip"
} else {
"${githubUrl}/${owner}/${repoName}/releases/download/${Version}/${exeName}_${Target}.tar.gz"
"${githubUrl}/${owner}/${repoName}/releases/download/${Version}/${exeName}_${Target}.zip"
}

if (!(Test-Path $BinDir)) {
Expand Down Expand Up @@ -115,4 +88,4 @@ if (!(";$Path;".ToLower() -like "*;$BinDir;*".ToLower())) {
}

Write-Output "${exeName} was installed successfully to $downloadedExe"
Write-Output "Run '${exeName} --help' to get started"
Write-Output "Run '${exeName} --h' to get started"
170 changes: 72 additions & 98 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,128 +2,102 @@

set -e

if [ $# -eq 0 ]; then
echo "ERROR: Need to specify the install repository"
exit 1
fi

# eg. release-lab/whatchanged
target=""
owner=""
repo=""
exe_name=""
target="MapoMagpie/rimedm"
owner="MapoMagpie"
repo="rimedm"
exe_name="rimedm"
githubUrl=""
githubApiUrl=""
version=""

get_arch() {
# darwin/amd64: Darwin axetroydeMacBook-Air.local 20.5.0 Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64 x86_64
# linux/amd64: Linux test-ubuntu1804 5.4.0-42-generic #46~18.04.1-Ubuntu SMP Fri Jul 10 07:21:24 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
a=$(uname -m)
case ${a} in
"x86_64" | "amd64" )
echo "amd64"
;;
"i386" | "i486" | "i586")
echo "386"
;;
"aarch64" | "arm64" | "arm")
echo "arm64"
;;
"mips64el")
echo "mips64el"
;;
"mips64")
echo "mips64"
;;
"mips")
echo "mips"
;;
*)
echo ${NIL}
;;
esac
# darwin/amd64: Darwin axetroydeMacBook-Air.local 20.5.0 Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64 x86_64
# linux/amd64: Linux test-ubuntu1804 5.4.0-42-generic #46~18.04.1-Ubuntu SMP Fri Jul 10 07:21:24 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
a=$(uname -m)
case ${a} in
"x86_64" | "amd64")
echo "x86_64"
;;
"i386" | "i486" | "i586")
echo "i386"
;;
"aarch64" | "arm64" | "arm")
echo "arm64"
;;
"mips64el")
echo "mips64el"
;;
"mips64")
echo "mips64"
;;
"mips")
echo "mips"
;;
*)
echo ${NIL}
;;
esac
}

get_os(){
# darwin: Darwin
echo $(uname -s | awk '{print tolower($0)}')
get_os() {
# darwin: Darwin
echo $(uname -s)
}

# parse flag
for i in "$@"; do
case $i in
-r=*|--repo=*)
target="${i#*=}"
shift # past argument=value
;;
-v=*|--version=*)
version="${i#*=}"
shift # past argument=value
;;
-e=*|--exe=*)
exe_name="${i#*=}"
shift # past argument=value
;;
-g=*|--github=*)
githubUrl="${i#*=}"
shift # past argument=value
;;
*)
# unknown option
;;
esac
case $i in
-v=* | --version=*)
version="${i#*=}"
shift # past argument=value
;;
-g=* | --github=*)
githubUrl="${i#*=}"
shift # past argument=value
;;
*)
# unknown option
;;
esac
done

args=(`echo $target | tr '/' ' '`)

if [ ${#args[@]} -ne 2 ]; then
echo "ERROR: invalid params for repo '$1'"
echo "ERROR: the argument should be format like 'owner/repo'"
exit 1
else
owner=${args[0]}
repo=${args[1]}
fi

if [ -z "$exe_name" ]; then
exe_name=$repo
echo "INFO: file name is not specified, use '$repo'"
echo "INFO: if you want to specify the name of the executable, set flag --exe=name"
fi

if [ -z "$githubUrl" ]; then
githubUrl="https://github.com"
githubUrl="https://github.com"
fi
if [ -z "$githubApiUrl" ]; then
githubApiUrl="https://api.github.com"
githubApiUrl="https://api.github.com"
fi

downloadFolder="${TMPDIR:-/tmp}"
mkdir -p ${downloadFolder} # make sure download folder exists
os=$(get_os)
arch=$(get_arch)
file_name="${exe_name}_${os}_${arch}.tar.gz" # the file name should be download
file_name="${exe_name}_${os}_${arch}.tar.gz" # the file name should be download
downloaded_file="${downloadFolder}/${file_name}" # the file path should be download

executable_folder="/usr/local/bin" # Eventually, the executable file will be placed here
if [ ! -w "${executable_folder}" ]; then
executable_folder="${HOME}/.local/bin"
fi

# if version is empty
if [ -z "$version" ]; then
asset_path=$(
command curl -L \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
${githubApiUrl}/repos/${owner}/${repo}/releases |
command grep -o "/${owner}/${repo}/releases/download/.*/${file_name}" |
command head -n 1
)
if [[ ! "$asset_path" ]]; then
echo "ERROR: unable to find a release asset called ${file_name}"
exit 1
fi
asset_uri="${githubUrl}${asset_path}"
asset_path=$(
command curl -L \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
${githubApiUrl}/repos/${owner}/${repo}/releases |
command grep -o "/${owner}/${repo}/releases/download/.*/${file_name}" |
command head -n 1
)
if [[ ! "$asset_path" ]]; then
echo "ERROR: unable to find a release asset called ${file_name}"
exit 1
fi
asset_uri="${githubUrl}${asset_path}"
else
asset_uri="${githubUrl}/${owner}/${repo}/releases/download/${version}/${file_name}"
asset_uri="${githubUrl}/${owner}/${repo}/releases/download/${version}/${file_name}"
fi

echo "[1/3] Download ${asset_uri} to ${downloadFolder}"
Expand All @@ -138,11 +112,11 @@ chmod +x ${exe}
echo "[3/3] Set environment variables"
echo "${exe_name} was installed successfully to ${exe}"
if command -v $exe_name --version >/dev/null; then
echo "Run '$exe_name --help' to get started"
echo "Run '$exe_name -h' to get started"
else
echo "Manually add the directory to your \$HOME/.bash_profile (or similar)"
echo " export PATH=${executable_folder}:\$PATH"
echo "Run '$exe_name --help' to get started"
echo "Manually add the directory to your \$HOME/.bash_profile (or similar)"
echo " export PATH=${executable_folder}:\$PATH"
echo "Run '$exe_name -h' to get started"
fi

exit 0
exit 0

0 comments on commit a42b5cb

Please sign in to comment.