-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtaskw
More file actions
executable file
·190 lines (163 loc) · 4.7 KB
/
taskw
File metadata and controls
executable file
·190 lines (163 loc) · 4.7 KB
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
#!/usr/bin/env sh
VERSION=${TASK_VERSION:-"3.40.0"}
# Check required tools
# ====================
check_command() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "Error! Required command '$1' is not installed"
exit 1
fi
}
check_command curl
check_command uname
check_archive_tools() {
if [ "$1" = "windows" ]; then
check_command unzip
else
check_command tar
check_command gzip
fi
}
# Define API
# ==========
# https://github.com/client9/shlib/blob/master/uname_os.sh
detect_os() {
os=$(uname -s | tr '[:upper:]' '[:lower:]')
# fixed up for https://github.com/client9/shlib/issues/3
case "$os" in
msys*) os="windows" ;;
mingw*) os="windows" ;;
cygwin*) os="windows" ;;
win*) os="windows" ;; # for windows busybox and like # https://frippery.org/busybox/
esac
# other fixups here
echo "$os"
}
# https://github.com/client9/shlib/blob/master/uname_arch.sh
detect_arch() {
arch=$(uname -m)
case $arch in
x86_64) arch="amd64" ;;
x86) arch="386" ;;
i686) arch="386" ;;
i386) arch="386" ;;
aarch64) arch="arm64" ;;
armv5*) arch="armv5" ;;
armv6*) arch="armv6" ;;
armv7*) arch="armv7" ;;
esac
echo "${arch}"
}
# https://github.com/client9/shlib/blob/master/http_download.sh
download_file() {
local_file=$1
source_url=$2
header=$3
attempt=1
max_attempts=3
while [ $attempt -le $max_attempts ]; do
if [ -z "$header" ]; then
code=$(curl -w '%{http_code}' -skL --connect-timeout 30 --max-time 300 -o "$local_file" "$source_url")
else
code=$(curl -w '%{http_code}' -skL --connect-timeout 30 --max-time 300 -H "$header" -o "$local_file" "$source_url")
fi
if [ "$code" = "200" ]; then
return 0
fi
if [ "$code" = "000" ]; then
echo "Warning: Cannot connect to '$source_url' (SSL/network issue). Try manual download. (attempt $attempt/$max_attempts)"
else
echo "Warning: Downloading file from URL '$source_url' received HTTP status '$code' (attempt $attempt/$max_attempts)"
fi
rm -f "$local_file"
if [ $attempt -lt $max_attempts ]; then
sleep 3
fi
attempt=$((attempt + 1))
done
echo "Error! Downloading file from URL '$source_url' failed after $max_attempts attempts"
return 1
}
download_file_once () {
URL=$1
FILE=$2
if [ ! -f "${FILE}" ]; then
mkdir -p "$(dirname "$FILE")"
FILE_TMP="$2.tmp"
if ! download_file "$FILE_TMP" "$URL"; then
rm -f "$FILE_TMP"
return 1
fi
mv "$FILE_TMP" "$FILE"
fi
}
unarchive_file() {
FILE=$1
DIR=$2
rm -fr "$DIR"
mkdir -p "$DIR"
if [ "${FILE##*.}" = "zip" ] ; then
if ! unzip -t "$FILE" > /dev/null 2>&1; then
echo "Error! Archive file '$FILE' is corrupted or not a valid zip"
return 1
fi
unzip "$FILE" -d "$DIR"
else
if ! gzip -t "$FILE" 2>/dev/null; then
echo "Error! Archive file '$FILE' is corrupted or not a valid gzip"
return 1
fi
tar -xf "$FILE" -C "$DIR"
fi
}
# Download tool
# =============
OS=$(detect_os)
ARCH=$(detect_arch)
check_archive_tools "$OS"
AEM_DIR="aem"
HOME_DIR="${AEM_DIR}/home"
DOWNLOAD_DIR="${HOME_DIR}/opt"
BIN_DOWNLOAD_NAME="task"
BIN_ARCHIVE_EXT="tar.gz"
if [ "$OS" = "windows" ] ; then
BIN_ARCHIVE_EXT="zip"
fi
BIN_DOWNLOAD_URL="https://github.com/go-task/task/releases/download/v${VERSION}/${BIN_DOWNLOAD_NAME}_${OS}_${ARCH}.${BIN_ARCHIVE_EXT}"
BIN_ROOT="${DOWNLOAD_DIR}/${BIN_DOWNLOAD_NAME}/${VERSION}"
BIN_ARCHIVE_FILE="${BIN_ROOT}/${BIN_DOWNLOAD_NAME}.${BIN_ARCHIVE_EXT}"
BIN_ARCHIVE_DIR="${BIN_ROOT}/${BIN_DOWNLOAD_NAME}"
BIN_NAME="task"
BIN_EXEC_FILE="${BIN_ARCHIVE_DIR}/${BIN_NAME}"
if [ ! -f "${BIN_EXEC_FILE}" ]; then
mkdir -p "${BIN_ARCHIVE_DIR}"
if ! download_file_once "${BIN_DOWNLOAD_URL}" "${BIN_ARCHIVE_FILE}"; then
echo "Error! Cannot download Task CLI"
echo "Manual download: ${BIN_DOWNLOAD_URL}"
echo "Place the file at: ${BIN_ARCHIVE_FILE}"
exit 1
fi
if ! unarchive_file "${BIN_ARCHIVE_FILE}" "${BIN_ARCHIVE_DIR}"; then
rm -f "${BIN_ARCHIVE_FILE}"
echo "Error! Cannot unarchive Task CLI"
exit 1
fi
if [ ! -f "${BIN_EXEC_FILE}" ]; then
echo "Error! Task CLI binary '${BIN_EXEC_FILE}' not found after extraction"
exit 1
fi
chmod +x "${BIN_EXEC_FILE}"
fi
# Prevent OS or shell-specific glitches
# =====================================
# https://stackoverflow.com/questions/7250130/how-to-stop-mingw-and-msys-from-mangling-path-names-given-at-the-command-line
export MSYS_NO_PATHCONV=1
export MSYS2_ARG_CONV_EXCL="*"
export MSYS2_ENV_CONV_EXCL="*"
# Execute Task Tool
# =================
# https://taskfile.dev/api/#env
export TASK_COLOR_GREEN=35
# https://taskfile.dev/experiments/env-precedence
export TASK_X_ENV_PRECEDENCE=1
"./${BIN_EXEC_FILE}" "$@"