Skip to content

Commit 5fc8c0f

Browse files
committed
Allowing to apply patches with build.sh
Currently the way the build script works, especially when compiling with Playwright support is that it tries to ensure that the Git worktree is clean, i.e., has no uncommitted changes. This aims to prevent that by accident one ends up in a situation where substantial changes have been made and the build script happily applies the Playwright patches. Reverting them requires a bit of understanding of the underlying build, so we try to safeguard against this. However, this is at times a bit annoying. One could always set an environment variable to skip the check, but there are situation where that is cumbersome. Consider for example, you'd want to remove the `ChromeOnly` attribute from the [DOMParser.parseFromSafeString()](https://github.com/SAP/project-foxhound/blob/main/dom/webidl/DOMParser.webidl#L30). This is an easy change, but if your memory is as bad as mine remembering in which folder to locate the change when redoing it after an update is annoying. This can be worked around with Git stash trickery, but I feel like we can offer a nicer way to do this. This PR aims to implement the nicer approach. The core idea is, that you can provide the build script with a sequence of patch files, which are applied before and reverted after a successful build. This ensures your working tree is clean, and allows you to apply changes rather easily. The workflow now becomes as follows: 1. Remove the troublesome attribute and save the webidl file. 2. `git diff >> ../patches/expose_safe_parse_to_webpages.patch` 3. `FOXHOUND_PATCHES="$(pwd)/../expose_safe_parse_to_webpages.patch" bash build.sh -ap` Several patches can be passed to `$FOXHOUND_PATCHES` separated by semicolons. The script tries to ensure that all provided patches exist and errors out if that is not the case or they fail to apply, so it aims to make this as bulletproof as possible.
1 parent f093725 commit 5fc8c0f

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

build.sh

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ SKIP_BUILD=
4242
SKIP_PACKAGE=
4343
RESET_GIT_REPO=
4444
DRY_RUN=
45+
APPLY_PATCHES=
46+
PATCH_FILES=()
4547

4648
_determine_obj_dir() {
4749
if [ ! -d "${FOXHOUND_OBJ_DIR}" ]; then
@@ -64,7 +66,47 @@ _determine_bin_name() {
6466
fi
6567
FOXHOUND_NAME="$BIN_NAME"
6668
}
69+
_apply_patches() {
70+
pushd "${FOXHOUND_DIR}" > /dev/null || _die "Can't change into foxhound dir: ${FOXHOUND_DIR}"
71+
for pf in ${PATCH_FILES[@]}; do
72+
local SUCCESS
73+
if [ -z "$DRY_RUN" ]; then
74+
git apply "${pf}" || _die "Can not apply ${pf}.."
75+
else
76+
git apply --check "${pf}" || _die "Can not apply ${pf}.."
77+
fi
78+
_status "Successfully applied ${pf}"
79+
done
80+
popd > /dev/null || exit 1
81+
}
6782

83+
_revert_patches() {
84+
pushd "${FOXHOUND_DIR}" > /dev/null || _die "Can't change into foxhound dir: ${FOXHOUND_DIR}"
85+
for pf in ${PATCH_FILES[@]}; do
86+
local SUCCESS
87+
if [ -z "$DRY_RUN" ]; then
88+
git apply -R "${pf}" || _die "Can not revert ${pf}.."
89+
fi
90+
_status "Successfully reverted ${pf}"
91+
done
92+
popd > /dev/null || exit 1
93+
}
94+
95+
_prepare_foxhound_patches() {
96+
if [ -z "${FOXHOUND_PATCHES}" ]; then
97+
_die "FOXHOUND_PATCHES env variable is empty depite -a flag"
98+
fi
99+
local PATCHES
100+
PATCHES=(${FOXHOUND_PATCHES//;/ })
101+
for p in ${PATCHES[@]}; do
102+
if [ ! -f "$p" ]; then
103+
_die "Patch file ${p} does not exist, please check your syntax"
104+
else
105+
_status "Adding $p to patch file queue to apply"
106+
PATCH_FILES+=("$p")
107+
fi
108+
done
109+
}
68110

69111
_make_git_commit() {
70112
pushd "${FOXHOUND_DIR}" > /dev/null || _die "Can't change into foxhound dir: ${FOXHOUND_DIR}"
@@ -213,7 +255,9 @@ main() {
213255
PLAYWRIGHT_DIR="${BASEDIR}/playwright"
214256

215257
_status "Starting Foxhound build in ${FOXHOUND_DIR}"
216-
258+
if [ -n "$APPLY_PATCHES" ]; then
259+
_prepare_foxhound_patches
260+
fi
217261
# First get playwright / rust versions
218262
_get_playwright_version
219263

@@ -231,13 +275,21 @@ main() {
231275
_prepare_playwright
232276
_patch_foxhound
233277
fi
278+
if [ -n "$APPLY_PATCHES" ] && (( ${#PATCH_FILES[@]} )); then
279+
_status "Applying ${#PATCH_FILES[@]} patches prior to building Foxhound"
280+
_apply_patches
281+
fi
234282
_determine_obj_dir
235283
_status "Determined MOZ_OBJDIR as: $FOXHOUND_OBJ_DIR"
236284
_determine_bin_name
237285
_status "Determined binary name as: $FOXHOUND_NAME"
238286
if [ -z "$SKIP_BUILD" ]; then
239287
_build_foxhound
240288
fi
289+
if [ -n "$APPLY_PATCHES" ] && (( ${#PATCH_FILES[@]} )); then
290+
_status "Reverting ${#PATCH_FILES[@]} patches after successfull build"
291+
_revert_patches
292+
fi
241293
if [ -z "$SKIP_PACKAGE" ]; then
242294
_package_foxhound
243295
fi
@@ -246,7 +298,7 @@ main() {
246298
_help() {
247299
echo "Builds project foxhound"
248300
echo
249-
echo "Syntax: build.sh [-c|s|t|u|v|b|r|p|g|n|h]"
301+
echo "Syntax: build.sh [-c|s|t|u|v|b|r|p|g|n|a|h]"
250302
echo
251303
echo "For example, to build from scratch with playwright:"
252304
echo "> bash build.sh -p"
@@ -262,19 +314,23 @@ _help() {
262314
echo "p Builds with playwright integration."
263315
echo "g Create a Git commit with the playwright patches."
264316
echo "n Dry run. Only print the step the script would perform."
317+
echo "a Apply patches provided in env variable FOXHOUND_PATCHES: ${FOXHOUND_PATCHES} as a semicolon separated list"
265318
echo "h Print this Help."
266319
echo
267320
echo
268321
echo "Environment variables:"
269322
echo
323+
echo "FOXHOUND_PATCHES:"
324+
echo "Combined with the -a flag, this splits the content into a list of semicolon separated patch files that we apply before the build and revert afterwards."
325+
echo
270326
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!"
271327
echo
272328
echo "SKIP_GIT_CHECK:"
273329
echo "If set to any value this skips the check whether the git repository is dirty."
274330
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!"
275331
}
276332

277-
while getopts "hpcstuvrbgn" option; do
333+
while getopts "hpcstuvrbgna" option; do
278334
case "$option" in
279335
h)
280336
_help
@@ -299,6 +355,8 @@ while getopts "hpcstuvrbgn" option; do
299355
WITH_PLAYWRIGHT_INTEGRATION=1;;
300356
n)
301357
DRY_RUN=1;;
358+
a)
359+
APPLY_PATCHES=1;;
302360
\?)
303361
_die "Error: Invalid option: $option";;
304362
esac

0 commit comments

Comments
 (0)