diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-06-11 08:00:45 +0200 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-06-11 08:00:45 +0200 |
commit | e4a334234b91f230571b5111a2b698230b27c660 (patch) | |
tree | 7545deca502c7ef7f676fc689f8caaeee6e19801 /instantiate_templates.sh | |
parent | chore(templates/rust): Quickly Update [THIS WILL BE REBASED] (diff) | |
download | flake-templates-e4a334234b91f230571b5111a2b698230b27c660.tar.gz flake-templates-e4a334234b91f230571b5111a2b698230b27c660.zip |
refactor(common): Use symlinks to state, which files should be replaced
Every symlink in the `./templates` directory is now replaced with the file it points to, making it obvious that this file should not be edited.
Diffstat (limited to 'instantiate_templates.sh')
-rwxr-xr-x | instantiate_templates.sh | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/instantiate_templates.sh b/instantiate_templates.sh new file mode 100755 index 0000000..eaffcb2 --- /dev/null +++ b/instantiate_templates.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env sh + +#! Copies the templates from `templates/*` to `build/*` and replaces every symlink with a +#! copy. + +# Source: https://stackoverflow.com/a/20460402 +# Checks if the string (arg2) starts with the substring (arg1) +# +# arg1: substring +# arg2: string +stringContain() { case $2 in $1*) return 0 ;; *) return 1 ;; esac } + +ROOT="$(git rev-parse --show-toplevel)" + +DRY_RUN_CMD="" + +$DRY_RUN_CMD mkdir --parents "$ROOT/build" + +fd . "$ROOT/templates" --max-depth 1 --type directory | while read -r template; do + echo "Instantiating template: '.${template#"$ROOT"}'.." + template_new_path="$ROOT/build/$(basename "$template")" + $DRY_RUN_CMD cp --no-target-directory --recursive "${template%/}" "$template_new_path" + + { + # Show directories + fd . "$template_new_path" --type symlink --follow --hidden + + # Show simple files + fd . "$template_new_path" --type symlink --hidden + } | while read -r symlink; do + symlink_path="$(readlink --canonicalize "$symlink")" + + if stringContain "$ROOT" "$symlink_path"; then + echo "Instantiating symlink: '.${symlink#"$ROOT"}'.." + $DRY_RUN_CMD rm "$symlink" + $DRY_RUN_CMD cp "$symlink_path" "$symlink" + else + echo "Not instantiating symlink '.${symlink#"$ROOT"}', as it points outside the repo!" + fi + done +done + +# vim: ft=sh |