-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·175 lines (143 loc) · 4.67 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/sh
set -e -u
if ! command -v curl >/dev/null; then
echo "Error: curl is required to install ghjk." 1>&2
exit 1
fi
if ! command -v tar >/dev/null; then
echo "Error: tar is required to install ghjk." 1>&2
exit 1
fi
ORG=metatypedev
REPO=ghjk
EXT=tar.gz
NAME=ghjk
EXE=ghjk
INSTALLER_URL="https://raw.githubusercontent.com/$ORG/$REPO/main/install.sh"
RELEASE_URL="https://github.com/$ORG/$REPO/releases"
LATEST_VERSION=$(curl "$RELEASE_URL/latest" -s -L -I -o /dev/null -w '%{url_effective}')
PLATFORM="${PLATFORM:-}"
TMP_DIR=$(mktemp -d)
GHJK_INSTALL_EXE_DIR="${GHJK_INSTALL_EXE_DIR:-$HOME/.local/bin}"
VERSION="${VERSION:-$LATEST_VERSION}"
# make sure the version is prepended with v
if [ "${VERSION#"v"}" = "$VERSION" ]; then
cat >&2 <<EOF
WARN: Resolved version "$VERSION" doesn't have "v" prefix. This may affect asset resolution. Expected format: v0.1.0
EOF
fi
if [ "${PLATFORM:-x}" = "x" ]; then
MACHINE=$(uname -m)
case "$(uname -s | tr '[:upper:]' '[:lower:]')" in
"linux")
case "$MACHINE" in
"arm64"* | "aarch64"* ) PLATFORM='aarch64-unknown-linux-gnu' ;;
*"64") PLATFORM='x86_64-unknown-linux-gnu' ;;
esac
;;
"darwin")
case "$MACHINE" in
"arm64"* | "aarch64"* ) PLATFORM='aarch64-apple-darwin' ;;
*"64") PLATFORM='x86_64-apple-darwin' ;;
esac
;;
"msys"*|"cygwin"*|"mingw"*|*"_nt"*|"win"*)
case "$MACHINE" in
*"64") PLATFORM='x86_64-pc-windows-msvc' ;;
esac
;;
esac
if [ "${PLATFORM:-x}" = "x" ]; then
cat >&2 <<EOF
/!\\ We couldn't automatically detect your operating system. /!\\
To continue with installation, please choose from one of the following values:
- aarch64-unknown-linux-gnu
- x86_64-unknown-linux-gnu
- x86_64-unknown-linux-musl
- aarch64-apple-darwin
- x86_64-apple-darwin
- x86_64-pc-windows-msvc
Then set the PLATFORM environment variable, and re-run this script:
$ curl -fsSL $INSTALLER_URL | PLATFORM=x86_64-unknown-linux-musl bash
EOF
exit 1
fi
printf "Detected platform: %s\n" "$PLATFORM"
fi
printf "Detected version: %s\n" "$VERSION"
ASSET="$NAME-$VERSION-$PLATFORM"
DOWNLOAD_URL="$RELEASE_URL/download/$VERSION/$ASSET.$EXT"
if curl --fail --silent --location --tlsv1.2 --proto '=https' --output "$TMP_DIR/$ASSET.$EXT" "$DOWNLOAD_URL"; then
printf "Downloaded successfully: %s\n" "$ASSET.$EXT"
else
cat >&2 <<EOF
/!\\ The asset $ASSET.$EXT doesn't exist. /!\\
To continue with installation, please make sure the release exists in:
$DOWNLOAD_URL
Then set the PLATFORM and VERSION environment variables, and re-run this script:
$ curl -fsSL $INSTALLER_URL | PLATFORM=x86_64-unknown-linux-musl VERSION=<version> bash
EOF
exit 1
fi
tar -C "$TMP_DIR" -xvzf "$TMP_DIR/$ASSET.$EXT" "$EXE"
chmod +x "$TMP_DIR/$EXE"
if [ "${GHJK_INSTALL_EXE_DIR}" = "." ]; then
mv "$TMP_DIR/$EXE" .
printf "\n\n%s has been extracted to your current directory\n" "$EXE"
else
cat <<EOF
$EXE will be moved to $GHJK_INSTALL_EXE_DIR
Set the GHJK_INSTALL_EXE_DIR environment variable to change the installation directory:
$ curl -fsSL $INSTALLER_URL | GHJK_INSTALL_EXE_DIR=. bash
EOF
if [ ! -d "${GHJK_INSTALL_EXE_DIR}" ]; then
mkdir -p "$GHJK_INSTALL_EXE_DIR"
fi
if [ -w "${GHJK_INSTALL_EXE_DIR}" ]; then
printf "Press enter to continue (or cancel with Ctrl+C):" >&2
read -r _throwaway
mv "$TMP_DIR/$EXE" "$GHJK_INSTALL_EXE_DIR"
rm -r "$TMP_DIR"
else
echo "$GHJK_INSTALL_EXE_DIR is not writable."
exit 1
fi
fi
GHJK_INSTALLER_URL="${GHJK_INSTALLER_URL:-https://raw.github.com/$ORG/$REPO/$VERSION/install.ts}"
"$GHJK_INSTALL_EXE_DIR/$EXE" deno run -A "$GHJK_INSTALLER_URL"
SHELL_TYPE=$(basename "$SHELL")
case $SHELL_TYPE in
bash|zsh|ksh)
SHELL_CONFIG="$HOME/.$SHELL_TYPE"rc
;;
fish)
SHELL_CONFIG="$HOME/.config/fish/config.fish"
;;
*)
SHELL_CONFIG=""
esac
if [ -n "$SHELL_CONFIG" ]; then
printf "\nDetected shell: %s\n" "$SHELL_TYPE"
echo "Do you want to append the new PATH to your configuration ($SHELL_CONFIG)? (y/n): " >&2
read -r answer
answer=$(echo "$answer" | tr "[:upper:]" "[:lower:]")
case $SHELL_TYPE in
bash|zsh|ksh)
APPEND_CMD="export PATH=\"$GHJK_INSTALL_EXE_DIR:\$PATH\""
;;
fish)
APPEND_CMD="fish_add_path $GHJK_INSTALL_EXE_DIR"
;;
esac
if [ "$answer" = "y" ] || [ "$answer" = "yes" ]; then
echo "$APPEND_CMD" >> "$SHELL_CONFIG"
printf "Path added to %s\nRun 'source %s' to apply changes." "$SHELL_CONFIG" "$SHELL_CONFIG"
else
cat <<EOF
Consider adding $GHJK_INSTALL_EXE_DIR to your PATH if it is not already configured.
$ $APPEND_CMD
EOF
fi
else
printf "\nConsider adding %s to your PATH if it is not already configured." "$GHJK_INSTALL_EXE_DIR"
fi