-
Notifications
You must be signed in to change notification settings - Fork 48
/
install.sh
executable file
·150 lines (125 loc) · 3.54 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
#!/usr/bin/env bash
set -e
PROTOSTAR_REPO="https://github.com/software-mansion/protostar"
main() {
local version_arg=$1
local requested_ref
local version
if [ -n "$version_arg" ]; then
requested_ref="tag/v${version_arg}"
version=$version_arg
else
requested_ref="latest"
version="latest"
fi
echo "Installing protostar"
get_platform_name
platform_name=$RETVAL
get_requested_version $version $requested_ref
requested_version=$RETVAL
create_protostar_directory
protostar_dir=$RETVAL
download_protostar $requested_version $platform_name $protostar_dir
protostar_binary_dir=$RETVAL
add_protostar_to_path $protostar_binary_dir
}
get_platform_name() {
RETVAL=""
local platform_name="$(uname -s)"
case $platform_name in
Linux)
RETVAL="Linux"
;;
Darwin)
RETVAL="macOS"
;;
*)
echo "unsupported platform: $PLATFORM"
exit 1
;;
esac
}
get_requested_version() {
RETVAL=""
local version=$1
local requested_ref=$2
echo "Retrieving $version version from $PROTOSTAR_REPO..."
response=$(curl -L -s -H 'Accept: application/json' "${PROTOSTAR_REPO}/releases/${requested_ref}")
if [ "$response" == "{\"error\":\"Not Found\"}" ]; then
echo "Version $version not found"
exit
fi
requested_version=$(echo $response | sed -e 's/.*"tag_name":"\([^"]*\)".*/\1/')
echo "Using version $requested_version"
RETVAL=$requested_version
}
create_protostar_directory() {
RETVAL=""
local protostar_dir=${protostar_dir-"$HOME/.protostar"}
mkdir -p "$protostar_dir"
RETVAL=$protostar_dir
}
download_protostar() {
RETVAL=""
local version=$1
local platform=$2
local output=$3
local requested_release_url="${PROTOSTAR_REPO}/releases/download/${version}"
local protostar_tarball_name="protostar-${platform}.tar.gz"
local tarball_download_url="${requested_release_url}/${protostar_tarball_name}"
echo "Downloading protostar from ${tarball_download_url}"
curl -L $tarball_download_url | tar -xvzC $output
local protostar_binary_dir="${output}/dist/protostar"
local protostar_binary="${protostar_binary_dir}/protostar"
chmod +x $protostar_binary
RETVAL=$protostar_binary_dir
}
add_protostar_to_path() {
RETVAL=""
local protostar_binary_dir=$1
local profile
local pref_shell
case $SHELL in
*/zsh)
profile=$HOME/.zshrc
pref_shell=zsh
;;
*/ash)
profile=$HOME/.profile
pref_shell=ash
;;
*/bash)
profile=$HOME/.bashrc
pref_shell=bash
;;
*/fish)
profile=$HOME/.config/fish/config.fish
pref_shell=fish
;;
*)
echo "error: could not detect shell, manually add ${protostar_binary_dir} to your PATH."
exit 1
;;
esac
if [[ ":$PATH:" != *":${protostar_binary_dir}:"* ]]; then
echo >>$profile && echo "export PATH=\"\$PATH:$protostar_binary_dir\"" >>$profile
fi
echo && echo "Detected your preferred shell is ${pref_shell} and added Protostar to PATH. Run 'source ${profile}' or start a new terminal session to use Protostar."
echo "Then, run 'protostar --help'."
}
while getopts ":v:" opt; do
case $opt in
v)
PROVIDED_VERSION=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
main $PROVIDED_VERSION