-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_buildtools.sh
executable file
·198 lines (178 loc) · 4.99 KB
/
install_buildtools.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env bash
#MIT License
#
#Copyright (c) 2019 buildtool
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
set -euo pipefail
check_required_binaries() {
if ! is_command tar; then
log_err "missing command tar"
fi
if ! (is_command curl || is_command wget); then
log_err "missing command curl or wget"
fi
}
fetch_curl() {
log_debug "http_download $2"
if is_command curl; then
http_download_curl "$@"
return
elif is_command wget; then
http_download_wget "$@"
return
fi
log_crit "http_download unable to find wget or curl"
return 1
}
http_download_curl() {
source_url="${1}"
local_file="${2}"
header="${3:-}"
if [ -z "$header" ]; then
code=$(curl -w '%{http_code}' -sL -o "$local_file" "$source_url")
else
code=$(curl -w '%{http_code}' -sL -H "$header" -o "$local_file" "$source_url")
fi
if [ "$code" != "200" ]; then
log_debug "http_download_curl received HTTP status $code"
return 1
fi
return 0
}
http_download_wget() {
source_url="${1}"
local_file="${2}"
header="${3:-}"
if [ -z "$header" ]; then
wget -q -O "$local_file" "$source_url"
else
wget -q --header "$header" -O "$local_file" "$source_url"
fi
}
http_download() {
log_debug "http_download ${1} ${2}"
if is_command curl; then
http_download_curl "$@"
return
elif is_command wget; then
http_download_wget "$@"
return
fi
log_crit "http_download unable to find wget or curl"
return 1
}
http_copy() {
tmp=$(mktemp)
http_download "${1}" "${tmp}" "${2:-}" || return 1
body=$(cat "$tmp")
rm -f "${tmp}"
echo "$body"
}
wanted_version() {
local version github_url
version="${1}"
github_url="${GITHUB_BASE}/${version}"
json=$(http_copy "$github_url" "Accept:application/json")
test -z "$json" && return 1
version=$(echo "$json" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//')
test -z "$version" && return 1
echo "${version#v}"
}
download_if_needed() {
local wanted current buildtools_path github_url
wanted=$(wanted_version "${1}")
buildtools_path="/tmp/buildtools-v${wanted}"
log_info "want version: ${wanted}"
if [[ -f "${buildtools_path}/build" ]]; then
log_debug "got version: ${wanted}"
else
mkdir -p "${buildtools_path}"
log_debug "downloading buildtools version ${wanted}"
github_url="${GITHUB_BASE}/download/v${wanted}/build-tools_${wanted}_${OS}_${ARCH}.tar.gz"
http_download "${github_url}" "${buildtools_path}/buildtools.tgz"
tar -C "${buildtools_path}" -xzf "${buildtools_path}/buildtools.tgz"
fi
log_debug "adding ${buildtools_path} to path"
echo "${buildtools_path}" >>"${GITHUB_PATH}"
}
is_command() {
command -v "$1" >/dev/null
}
log() {
echo "$@" 1>&2
}
log_err() {
log "error:" "$@"
exit 1
}
log_debug() {
[ "${DEBUG_LOG}" != "true" ] && return
log "debug:" "$@"
}
log_info() {
log "info:" "$@"
}
adjust_arch() {
# adjust ARCHive name based on ARCH
local org
org=${ARCH}
case ${ARCH} in
X86) ARCH=x86_64 ;;
X64) ARCH=x86_64 ;;
ARM) ARCH=arm64 ;;
ARM64) ARCH=arm64 ;;
x86) ARCH=x86_64 ;;
x64) ARCH=x86_64 ;;
arm) ARCH=arm64 ;;
arm64) ARCH=arm64 ;;
*) log_err "Unsupported release ARCH ${ARCH}"
esac
if [[ "${org}" != "${ARCH}" ]]; then
log_debug "adjusted architecture from ${org} to ${ARCH}"
fi
}
adjust_os() {
# adjust archive name based on OS
local org
org=${OS}
case ${OS} in
macOS) OS=Darwin ;;
Linux) OS=Linux ;;
Windows) OS=Windows ;;
*) log_err "Unsupported release OS ${OS}"
esac
if [[ "${org}" != "${OS}" ]]; then
log_debug "adjusted operating system from ${org} to ${ARCH}"
fi
}
# start
VERSION=${1:-latest}
DEBUG_LOG=${2:-false}
GITHUB_BASE="https://github.com/buildtool/build-tools/releases"
# RUNNER_ARCH The architecture of the runner executing the job.
# Possible values are X86, X64, ARM, or ARM64.
ARCH="${RUNNER_ARCH}"
# RUNNER_OS The operating system of the runner executing the job.
# Possible values are Linux, Windows, or macOS.
OS="${RUNNER_OS}"
check_required_binaries
adjust_arch
adjust_os
download_if_needed "${VERSION}"