2023-11-26 23:38:55 +00:00
|
|
|
#!/usr/bin/env bash
|
2023-11-27 19:42:43 +00:00
|
|
|
|
|
|
|
# Exit immediately if the script encounters a non-zero status.
|
2023-11-27 11:56:04 +00:00
|
|
|
set -e
|
2023-11-26 23:38:55 +00:00
|
|
|
|
2023-11-27 19:42:43 +00:00
|
|
|
# If set, Bash includes filenames beginning with a `.` in the results of
|
|
|
|
# filename expansion. The filenames `.` and `..` must always be matched
|
|
|
|
# explicitly, even if dotglob is set.
|
|
|
|
shopt -s dotglob
|
|
|
|
|
|
|
|
# ============================================================
|
|
|
|
# PROLOGUE
|
|
|
|
# ============================================================
|
|
|
|
|
|
|
|
# Create a new top-level directory as fallback in case $BUILD (defined below)
|
|
|
|
# is ever empty.
|
|
|
|
mkdir -p "/tmp/bs.mix"
|
|
|
|
|
|
|
|
# Create an intermediate build directory. The final step of this script will
|
|
|
|
# copy the content from this directory to $OUT.
|
|
|
|
BUILD=$(mktemp -d -p "/tmp/bs.mix")
|
2023-11-27 11:56:04 +00:00
|
|
|
|
2023-11-27 19:42:43 +00:00
|
|
|
if [ -z "$BUILD" ]; then
|
|
|
|
>&2 echo "Failed to create temp directory."
|
2023-11-27 11:56:04 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-11-27 19:42:43 +00:00
|
|
|
# Deletes the intermediate build directory on exit. We use a concatenation of
|
|
|
|
# the intermediate directory with the basename of the generated temp directory
|
|
|
|
# to ensure we never evaluate to root (i.e. `/`). That should never actually
|
|
|
|
# happen but a good habit to establish nonetheless.
|
2023-11-27 11:56:04 +00:00
|
|
|
function cleanup {
|
2023-11-27 19:42:43 +00:00
|
|
|
rm -r "/tmp/bs.mix/$(basename "$BUILD")"
|
2023-11-27 11:56:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
2023-11-27 19:42:43 +00:00
|
|
|
# ============================================================
|
|
|
|
# BUILD
|
|
|
|
# ============================================================
|
|
|
|
|
|
|
|
# Copy template contents over to the intermediate build directory.
|
|
|
|
cp -r template/* "$BUILD"
|
|
|
|
|
2023-11-28 16:17:09 +00:00
|
|
|
# Explicitly set permissions on all copied files.
|
|
|
|
find "$BUILD" -type f -execdir chmod 644 {} +
|
|
|
|
find "$BUILD" -type d -execdir chmod 755 {} +
|
|
|
|
|
2023-11-27 19:42:43 +00:00
|
|
|
# Generate a new project with the specified name using `mix`. Generate in a
|
2023-11-28 16:17:09 +00:00
|
|
|
# subdirectory to avoid interactive conflict resolution.
|
2023-11-27 19:42:43 +00:00
|
|
|
nix develop "$BUILD" --command bash -c "mix new $BUILD/project --app '$APP'"
|
|
|
|
|
2023-11-28 16:17:09 +00:00
|
|
|
# Copy the generated files into the intermediate build directory.
|
2023-11-27 19:45:41 +00:00
|
|
|
mv -f "$BUILD"/project/* "$BUILD"
|
2023-11-27 19:42:43 +00:00
|
|
|
rmdir "$BUILD"/project
|
2023-11-28 16:17:09 +00:00
|
|
|
|
|
|
|
# Overwrite the generated README in favor of that defined in our template.
|
2023-11-27 19:42:43 +00:00
|
|
|
cp template/README.md "$BUILD"
|
2023-11-28 16:17:09 +00:00
|
|
|
chmod 644 "$BUILD"/README.md
|
2023-11-27 19:42:43 +00:00
|
|
|
|
|
|
|
# Replace the template name found in the flake.nix file. The mix generator would
|
|
|
|
# fail if $APP did not consist of just lowercase ASCII letters, numbers, or
|
|
|
|
# underscores. Thus $APP is safe to interpolate into the following command.
|
|
|
|
sed -i "s/<APP_NAME>/$APP/g" "$BUILD/flake.nix"
|
2023-11-26 23:38:55 +00:00
|
|
|
|
2023-11-27 19:42:43 +00:00
|
|
|
# ============================================================
|
|
|
|
# EPILOGUE
|
|
|
|
# ============================================================
|
2023-11-27 11:56:04 +00:00
|
|
|
|
2023-11-27 19:42:43 +00:00
|
|
|
# Success! Copy contents to target directory.
|
|
|
|
cp -r "$BUILD"/* "$OUT"
|