generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 16
/
build.sh
305 lines (274 loc) · 9.85 KB
/
build.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/bash
CURRENT_DIR=$(dirname "$(readlink -f "$0" || exit 1)")
BASEDIR=$(realpath "${CURRENT_DIR}/..")
RED=$(tput setaf 1)
POWDER_BLUE=$(tput setaf 153)
NORMAL=$(tput sgr0)
_die() {
[[ -n $1 ]] || {
printf >&2 -- 'Usage:\n\t_die <message> [return code]\n'
[[ $- == *i* ]] && return 1 || exit 1
}
printf >&2 -- '%s\n' "${RED}$1${NORMAL}"
exit "${2:-1}"
}
_status() {
echo -e "${POWDER_BLUE}${1}${NORMAL}"
}
# Shared variables
FOXHOUND_OBJ_DIR=
FOXHOUND_NAME=
FOXHOUND_DIR=
PLAYWRIGHT_DIR=
PLAYWRIGHT_VERSION=
FIREFOX_VERSION=
RUST_VERSION=
WITH_PLAYWRIGHT_INTEGRATION=
NO_CLOBBER=
MAKE_GIT_COMMIT=
SKIP_PREPARATION=
SKIP_BOOTSTRAP=
SKIP_PATCHING=
SKIP_BUILD=
SKIP_PACKAGE=
RESET_GIT_REPO=
DRY_RUN=
_determine_obj_dir() {
if [ ! -d "${FOXHOUND_OBJ_DIR}" ]; then
OBJ_DIR="$(grep -v -e "^#" "${FOXHOUND_DIR}/.mozconfig" | grep "MOZ_OBJDIR=@TOPSRCDIR@" | cut -d "/" -f 2)"
if [ -z "${OBJ_DIR}" ]; then
_die "Unable to determine object directory from mozconfig, should be called MOZ_OBJDIR"
fi
#_status "FOXHOUND_OBJ_DIR not set, suggesting to set it to: '${OBJ_DIR}'" 1
fi
FOXHOUND_OBJ_DIR="$OBJ_DIR"
}
_determine_bin_name() {
if [ ! -d "${FOXHOUND_NAME}" ]; then
BIN_NAME="$(grep -v -e "^#" "${FOXHOUND_DIR}/.mozconfig" | grep "MOZ_APP_NAME=" | cut -d "=" -f 2)"
if [ -z "${BIN_NAME}" ]; then
_status "Could not find MOZ_APP_NAME in .mozconfig, using default firefox"
BIN_NAME=firefox
fi
fi
FOXHOUND_NAME="$BIN_NAME"
}
_make_git_commit() {
pushd "${FOXHOUND_DIR}" > /dev/null || _die "Can't change into foxhound dir: ${FOXHOUND_DIR}"
NEW_BRANCHNAME="pw-build-$(date +%s)"
git switch -c "${NEW_BRANCHNAME}" || _die "Can't create new Git branch called: ${NEW_BRANCHNAME}"
git add .
git commit --author="Foxhound builder <foxhound@whereismymind.info>" --message="Added Playwright support to Foxhound."
popd > /dev/null || exit 1
}
_get_playwright_version() {
pushd "${FOXHOUND_DIR}" > /dev/null || _die "Can't change into foxhound dir: ${FOXHOUND_DIR}"
if [ ! -f "${CURRENT_DIR}/.PLAYWRIGHT_VERSION" ]; then
_die "Can not determine playwright version, as \".PLAYWRIGHT_VERSION\" file is missing. Please open an issue on Github!"
fi
# This file also contains the rust version
. "${CURRENT_DIR}/.PLAYWRIGHT_VERSION"
_status "Detected Firefox version: ${FIREFOX_VERSION}"
_status "Set playwright version to: ${PLAYWRIGHT_VERSION}"
_status "Required Rust version: ${RUST_VERSION}"
popd > /dev/null || exit 1
}
_prepare_playwright() {
_status "Fetching Playwright and checking out ${PLAYWRIGHT_VERSION} branch"
if [ -z "$DRY_RUN" ]; then
pushd "${PLAYWRIGHT_DIR}" > /dev/null || _die "Can't change into playwright dir: ${PLAYWRIGHT_DIR}"
git fetch --all || _die "Git fetch failed for Playwright"
git checkout "${PLAYWRIGHT_VERSION}" || _die "Can't checkout playwright version ${PLAYWRIGHT_VERSION}.."
popd > /dev/null || exit 1
fi
}
_checkout_playwright() {
_status "Cloning playwright into ${PLAYWRIGHT_DIR}"
if [ -z "$DRY_RUN" ]; then
git clone https://github.com/microsoft/playwright.git "${PLAYWRIGHT_DIR}" || _die "Cloning playwright failed!"
fi
}
_check_foxhound_repo_state() {
pushd "${FOXHOUND_DIR}" > /dev/null || _die "Can't change into foxhound dir: ${FOXHOUND_DIR}"
test -z "$(git status --porcelain)" || _die "Dirty Worktree, please commit all changes before running the build script!"
popd > /dev/null || exit 1
}
_prepare_foxhound() {
pushd "${FOXHOUND_DIR}" > /dev/null || _die "Can't change into foxhound dir: ${FOXHOUND_DIR}"
_status "Preparing the foxhound build environment"
if [ -d "./juggler" ]; then
_status "Deleting stale juggler"
if [ -z "$DRY_RUN" ]; then
rm -rf "${FOXHOUND_DIR}/juggler"
fi
fi
if [ ! -f "${FOXHOUND_DIR}/.mozconfig" ]; then
_status "Setting default mozconfig from Ubuntu profile"
if [ -z "$DRY_RUN" ]; then
cp "${FOXHOUND_DIR}/taintfox_mozconfig_ubuntu" "${FOXHOUND_DIR}/.mozconfig"
fi
fi
if [ -n "$RESET_GIT_REPO" ]; then
_status "Resetting Git repository"
if [ -z "$DRY_RUN" ]; then
git reset --hard HEAD
fi
fi
if [ -z "$NO_CLOBBER" ]; then
_status "Clobbering the build environment"
if [ -z "$DRY_RUN" ]; then
./mach --no-interactive clobber
fi
fi
if [ -z "$SKIP_BOOTSTRAP" ]; then
_status "Preparing environment via './mach bootstrap'"
if [ -z "$DRY_RUN" ]; then
./mach --no-interactive bootstrap --no-system-changes --application-choice=browser || _die "Bootstrapping failed! You can install dependencies manually and skip this step via the '-b' flag"
fi
_status "Installing Rust"
if [ -z "$DRY_RUN" ]; then
# This is the recommended way to install rust from https://www.rust-lang.org/tools/install
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain ${RUST_VERSION} || _die "Rust installation failed! You can install dependencies manually and skip this step via the '-b' flag"
fi
fi
popd > /dev/null || exit 1
}
_patch_foxhound() {
_status "Patching foxhound for playwright integration"
pushd "${FOXHOUND_DIR}" > /dev/null || _die "Can't change into foxhound dir: ${FOXHOUND_DIR}"
if [ -z "$DRY_RUN" ]; then
cp -r "${PLAYWRIGHT_DIR}/browser_patches/firefox/juggler" "juggler"
git apply --verbose --index --whitespace=nowarn --recount "${PLAYWRIGHT_DIR}/browser_patches/firefox/patches"/* || _die "Playwright patches failed to apply."
fi
if [ -n "$MAKE_GIT_COMMIT" ]; then
_status "Creating Git commit"
if [ -z "$DRY_RUN" ]; then
_make_git_commit
fi
fi
popd > /dev/null || exit 1
}
_build_foxhound() {
pushd "${FOXHOUND_DIR}" > /dev/null || _die "Can't change into foxhound dir: ${FOXHOUND_DIR}"
_status "Starting Foxhound build.. This can take a while"
if [ -z "$DRY_RUN" ]; then
./mach build || _die "./mach build error"
fi
popd > /dev/null || exit 1
}
_package_foxhound() {
pushd "${FOXHOUND_DIR}" > /dev/null || _die "Can't change into foxhound dir: ${FOXHOUND_DIR}"
_status "Packaging foxhound.."
if [ -z "$DRY_RUN" ]; then
./mach package || _die "./mach package error"
fi
if [ -n "$WITH_PLAYWRIGHT_INTEGRATION" ]; then
_status "Applying playwright preferences"
if [ -z "$DRY_RUN" ]; then
mkdir -p "${FOXHOUND_OBJ_DIR}/dist/${FOXHOUND_NAME}/defaults/pref"
cp "${PLAYWRIGHT_DIR}/browser_patches/firefox/preferences/playwright.cfg" "${FOXHOUND_OBJ_DIR}/dist/${FOXHOUND_NAME}/"
cp "${PLAYWRIGHT_DIR}/browser_patches/firefox/preferences/00-playwright-prefs.js" "${FOXHOUND_OBJ_DIR}/dist/${FOXHOUND_NAME}/defaults/pref/"
fi
fi
_status "Creating Foxhound zip"
if [ -z "$DRY_RUN" ]; then
pushd "${FOXHOUND_OBJ_DIR}/dist" || exit 1
zip -r foxhound_linux.zip $FOXHOUND_NAME
if [ -n "$WITH_PLAYWRIGHT_INTEGRATION" ]; then
cp foxhound_linux.zip "foxhound_linux_${PLAYWRIGHT_VERSION}.zip"
fi
_status "Zip located at '$(pwd || true)/foxhound_linux.zip', done!"
popd > /dev/null || exit 1
fi
popd > /dev/null || exit 1
}
main() {
FOXHOUND_DIR="${CURRENT_DIR}"
PLAYWRIGHT_DIR="${BASEDIR}/playwright"
_status "Starting Foxhound build in ${FOXHOUND_DIR}"
# First get playwright / rust versions
_get_playwright_version
if [ -z "$SKIP_PREPARATION" ] && [ -n "$WITH_PLAYWRIGHT_INTEGRATION" ] && [ ! -d "${PLAYWRIGHT_DIR}" ]; then
_checkout_playwright
fi
if [ -z "$SKIP_PREPARATION" ]; then
_prepare_foxhound
fi
if [[ -n "$WITH_PLAYWRIGHT_INTEGRATION" && ( -z "$SKIP_PREPARATION" && -z "$SKIP_PATCHING") ]]; then
if [ -z "$SKIP_GIT_CHECK" ]; then
_check_foxhound_repo_state
fi
_prepare_playwright
_patch_foxhound
fi
_determine_obj_dir
_status "Determined MOZ_OBJDIR as: $FOXHOUND_OBJ_DIR"
_determine_bin_name
_status "Determined binary name as: $FOXHOUND_NAME"
if [ -z "$SKIP_BUILD" ]; then
_build_foxhound
fi
if [ -z "$SKIP_PACKAGE" ]; then
_package_foxhound
fi
}
_help() {
echo "Builds project foxhound"
echo
echo "Syntax: build.sh [-c|s|t|u|v|b|r|p|g|n|h]"
echo
echo "For example, to build from scratch with playwright:"
echo "> bash build.sh -p"
echo
echo "options:"
echo "c Does not clobber the build prior to building."
echo "s Skip the preparation phase (i.e., avoid applying playwright patches again)."
echo "t Skip applying the playwright patches"
echo "u Skip the build itself"
echo "v Skip the packaging stage"
echo "b Skip './mach bootstrap' (This can help if the binaries are not available for download anymore)."
echo "r Resets the Git repository prior to building. This will delete any (uncommitted) changes you made!"
echo "p Builds with playwright integration."
echo "g Create a Git commit with the playwright patches."
echo "n Dry run. Only print the step the script would perform."
echo "h Print this Help."
echo
echo
echo "Environment variables:"
echo
echo "These are meant to be used if you know what you are doing and can lead to states that are difficult to revert. Use with caution!"
echo
echo "SKIP_GIT_CHECK:"
echo "If set to any value this skips the check whether the git repository is dirty."
echo "Warning: When used together with Playwright support this can lead to a state where untangling the Playwright patches from your changes to commit them is very cumbersome!"
}
while getopts "hpcstuvrbgn" option; do
case "$option" in
h)
_help
exit;;
b)
SKIP_BOOTSTRAP=1;;
r)
RESET_GIT_REPO=1;;
s)
SKIP_PREPARATION=1;;
t)
SKIP_PATCHING=1;;
u)
SKIP_BUILD=1;;
v)
SKIP_PACKAGE=1;;
c)
NO_CLOBBER=1;;
g)
MAKE_GIT_COMMIT=1;;
p)
WITH_PLAYWRIGHT_INTEGRATION=1;;
n)
DRY_RUN=1;;
\?)
_die "Error: Invalid option: $option";;
esac
done
main