From 72e1a62343c611868b008607e1bc74172e22013a Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Mon, 27 Nov 2023 04:56:04 -0700 Subject: [PATCH] Add cleanup handler to mix runner. (#9) --- specs/mix/runner | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/specs/mix/runner b/specs/mix/runner index 670372e..b92d729 100755 --- a/specs/mix/runner +++ b/specs/mix/runner @@ -1,13 +1,37 @@ #!/usr/bin/env bash +set -e -cp -r template/. "$OUT" +# Create a new intermediate, guaranteed unique, build directory. The final step +# of this script will copy all the content from this directory to $OUT. +mkdir -p "/tmp/bootstrap-mix" +TMPOUT=$(mktemp -d -p "/tmp/bootstrap-mix") + +if [ -z "$TMPOUT" ]; then + >&2 echo "Failed to create temp directory" + exit 1 +fi + +function cleanup { + # Use basename with an explicit path prefix to ensure we never evaluate to `/`. + # Here we already verify `$TMPOUT` isn't empty prior to registering this + # cleanup function, but this is nonetheless a good habit to establish. + rm -r "/tmp/bootstrap-mix/$(basename "$TMPOUT")" +} + +trap cleanup EXIT + +# Begin copying content over to our new directory. +cp -r template/. "$TMPOUT" # Generate a new project with the specified name using `mix`. Pipe in `yes` # since `mix` requires confirmation when writing out to a directory that already # has content. -nix develop "$OUT" --command bash -c "yes | mix new $OUT --app '$APP'" +nix develop "$TMPOUT" --command bash -c "yes | mix new $TMPOUT --app '$APP'" # Replace the template name of the flake.nix file with the new app name. The # mix generator would fail if `$APP` does not consist of just lowercase ASCII # letters, numbers, or underscores. Thus the following command is safe. -sed -i "s//$APP/g" "$OUT/flake.nix" +sed -i "s//$APP/g" "$TMPOUT/flake.nix" + +# Everything succeeded. Copy contents over. +cp -r "$TMPOUT/." "$OUT"