From 9d43791fd14c7f19cf35039498dbcedd2013304d Mon Sep 17 00:00:00 2001 From: Tobias K <6317548+theCalcaholic@users.noreply.github.com> Date: Tue, 7 Dec 2021 16:18:29 +0100 Subject: [PATCH 1/6] Add convenience script for (re-)building --- build.sh | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100755 build.sh diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..f88a18a --- /dev/null +++ b/build.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +( + +if [[ " $* " =~ " "("-h"|"--help")" " ]] +then + echo "Update and (re-)build backscrub" + echo "USAGE:" + echo " ./build.sh [OPTIONS]" + echo "" + echo " Options:" + echo " --h, --help Show this help message and exit" + echo " --no-update Skip fetching updates from github" + exit 0 +fi + +# Get the root directory of the repository +wd="$(cd "$(dirname $0)" || exit 1; pwd)" + +echo "working directory: $wd" +cd "$wd" + +# Make sure we have the latest version of backscrub +echo "Fetching latest version..." +[[ " $* " =~ " --no-update" ]] || git pull --recurse-submodules +echo "..done" +echo "" + + +# Backup the current build directory (if present) +if [[ -d "./build" ]] +then + echo "Backing up build directory..." + [[ ! -d "./build_bk" ]] || rm -rf "./build_bk" + mv ./build ./build_bk + echo "..done" + echo "" +fi + +# Restore the backup on error +trap 'rm -rf ./build && mv ./build_bk ./build' SIGHUP SIGINT SIGTERM + +# Abort on errors +set -e + +echo "Building backscrub..." + +#mkdir build +cd build + +# Build backscrub +cmake .. +make -j "$(nproc || echo 4)" +ln -s ../models models + +echo "..done" +echo "" + +echo "Cleaning up..." +# Delete the backup on success +[[ ! -d "./build_bk" ]] || rm -rf "./build_bk" +echo "..done" +echo "" + +echo "All done. You can run backscrub like this:" +echo " cd \"$wd/build\" && ./backscrub -h" +echo "" +echo "Or you can add the following line to your ~/.bashrc, to get a backscrub command:" +echo "backscrub() { pushd \"$wd/build\" > /dev/null && { ./backscrub \"\$@\"; popd > /dev/null; }; }" + + +) + From e544262ff971f6a1c765f73919b69dfefdb64d8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= <6317548+theCalcaholic@users.noreply.github.com> Date: Wed, 8 Dec 2021 22:11:00 +0100 Subject: [PATCH 2/6] build.sh: quote $0 Co-authored-by: BenBE --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index f88a18a..9b5a22a 100755 --- a/build.sh +++ b/build.sh @@ -15,7 +15,7 @@ then fi # Get the root directory of the repository -wd="$(cd "$(dirname $0)" || exit 1; pwd)" +wd="$(cd "$(dirname "$0")" || exit 1; pwd)" echo "working directory: $wd" cd "$wd" From e778c2583b6f1b842a210655a91d69f9816fa24a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= <6317548+theCalcaholic@users.noreply.github.com> Date: Wed, 8 Dec 2021 22:18:10 +0100 Subject: [PATCH 3/6] build.sh: Simplify bash function for bashrc Co-authored-by: BenBE --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 9b5a22a..cbbb198 100755 --- a/build.sh +++ b/build.sh @@ -66,7 +66,7 @@ echo "All done. You can run backscrub like this:" echo " cd \"$wd/build\" && ./backscrub -h" echo "" echo "Or you can add the following line to your ~/.bashrc, to get a backscrub command:" -echo "backscrub() { pushd \"$wd/build\" > /dev/null && { ./backscrub \"\$@\"; popd > /dev/null; }; }" +echo "backscrub() { ( cd \"$wd/build\" && ./backscrub \"\$@\" ) }" ) From f570365a636affab447fa0d29121dd293e87fe9f Mon Sep 17 00:00:00 2001 From: Tobias K <6317548+theCalcaholic@users.noreply.github.com> Date: Wed, 8 Dec 2021 22:25:02 +0100 Subject: [PATCH 4/6] build.sh: Use mktemp for creating a backup directory --- build.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/build.sh b/build.sh index cbbb198..1a485af 100755 --- a/build.sh +++ b/build.sh @@ -28,17 +28,19 @@ echo "" # Backup the current build directory (if present) + +backup_dir="$(mktemp -d)" + if [[ -d "./build" ]] then echo "Backing up build directory..." - [[ ! -d "./build_bk" ]] || rm -rf "./build_bk" - mv ./build ./build_bk + mv ./build "$backup_dir/build" echo "..done" echo "" fi # Restore the backup on error -trap 'rm -rf ./build && mv ./build_bk ./build' SIGHUP SIGINT SIGTERM +trap "rm -rf ./build && mv $backup_dir/build ./build" SIGHUP SIGINT SIGTERM # Abort on errors set -e @@ -58,7 +60,7 @@ echo "" echo "Cleaning up..." # Delete the backup on success -[[ ! -d "./build_bk" ]] || rm -rf "./build_bk" +rm -rf "$backup_dir/build" echo "..done" echo "" From 2d70b857718ffd5d0a8ff3506a0fa29f435c0f2e Mon Sep 17 00:00:00 2001 From: Tobias K <6317548+theCalcaholic@users.noreply.github.com> Date: Wed, 8 Dec 2021 23:14:05 +0100 Subject: [PATCH 5/6] build.sh: fix trap invocation and improve backup handling --- build.sh | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/build.sh b/build.sh index 1a485af..d151df0 100755 --- a/build.sh +++ b/build.sh @@ -1,6 +1,8 @@ #!/usr/bin/env bash ( +# Abort on errors +set -e if [[ " $* " =~ " "("-h"|"--help")" " ]] then @@ -28,7 +30,6 @@ echo "" # Backup the current build directory (if present) - backup_dir="$(mktemp -d)" if [[ -d "./build" ]] @@ -37,18 +38,27 @@ then mv ./build "$backup_dir/build" echo "..done" echo "" -fi + echo "backup dir: $backup_dir" -# Restore the backup on error -trap "rm -rf ./build && mv $backup_dir/build ./build" SIGHUP SIGINT SIGTERM + restore_backup() { + echo 'Restoring backup...' + if [[ -d "${1?}/build" ]] + then + rm -rf ./build && mv "$1/build" ./build + fi + rm -rf "$1" + echo '..done' + } -# Abort on errors -set -e + # Restore the backup on error + trap "restore_backup $backup_dir" ERR + +fi echo "Building backscrub..." -#mkdir build -cd build +mkdir -p ./build +cd ./build # Build backscrub cmake .. @@ -58,11 +68,14 @@ ln -s ../models models echo "..done" echo "" -echo "Cleaning up..." -# Delete the backup on success -rm -rf "$backup_dir/build" -echo "..done" -echo "" +if [[ -d "$backup_dir" ]]; +then + echo "Cleaning up..." + # Delete the backup on success + rm -rf "$backup_dir" + echo "..done" + echo "" +fi echo "All done. You can run backscrub like this:" echo " cd \"$wd/build\" && ./backscrub -h" @@ -70,6 +83,5 @@ echo "" echo "Or you can add the following line to your ~/.bashrc, to get a backscrub command:" echo "backscrub() { ( cd \"$wd/build\" && ./backscrub \"\$@\" ) }" - ) From c74ce535b296716f20e60c4f7f0fa28e8b6cee60 Mon Sep 17 00:00:00 2001 From: Tobias K <6317548+theCalcaholic@users.noreply.github.com> Date: Wed, 15 Dec 2021 21:32:13 +0100 Subject: [PATCH 6/6] build.sh: Remove unnecessary subshell --- build.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/build.sh b/build.sh index d151df0..fc8870b 100755 --- a/build.sh +++ b/build.sh @@ -1,6 +1,5 @@ #!/usr/bin/env bash -( # Abort on errors set -e @@ -28,7 +27,6 @@ echo "Fetching latest version..." echo "..done" echo "" - # Backup the current build directory (if present) backup_dir="$(mktemp -d)" @@ -83,5 +81,4 @@ echo "" echo "Or you can add the following line to your ~/.bashrc, to get a backscrub command:" echo "backscrub() { ( cd \"$wd/build\" && ./backscrub \"\$@\" ) }" -)