about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-09-30 17:15:42 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-09-30 17:15:42 +0200
commit103e6ddb061248bff57e1df2cd737dfc6dc9bdb5 (patch)
tree12c8a95caf0fdf72a3cad3c05680e254a2b6c3b4
parentfix(templates/rust/cog.toml): Update to perform new `reuse lint` (diff)
downloadflake-templates-103e6ddb061248bff57e1df2cd737dfc6dc9bdb5.tar.gz
flake-templates-103e6ddb061248bff57e1df2cd737dfc6dc9bdb5.zip
build(instantiate_templates.sh): Only copy files tracked by git
-rwxr-xr-xinstantiate_templates.sh22
1 files changed, 17 insertions, 5 deletions
diff --git a/instantiate_templates.sh b/instantiate_templates.sh
index c991267..c120b3f 100755
--- a/instantiate_templates.sh
+++ b/instantiate_templates.sh
@@ -1,6 +1,6 @@
 #!/usr/bin/env sh
 
-#! Copies the templates from `templates/*` to `build/*` and replaces every symlink with a
+#! Copies the templates from `templates/*` to `build/*` and replaces every symbolic link with a
 #! copy.
 
 # Source: https://stackoverflow.com/a/20460402
@@ -12,19 +12,31 @@ stringContain() { case $2 in $1*) return 0 ;; *) return 1 ;; esac }
 
 ROOT="$(git rev-parse --show-toplevel)"
 
-DRY_RUN_CMD=""
-
 # TODO: Find a way to do the building step incrementally (i.e. without removing everything) <2024-06-11>
 [ -n "$ROOT" ] && $DRY_RUN_CMD rm --recursive "$ROOT/build"
 $DRY_RUN_CMD mkdir --parents "$ROOT/build"
 
+cd "$ROOT" || {
+    echo "No '$ROOT' (Root) ?!"
+    exit 1
+}
+
 instantiate_dir() {
     dir="$1"
 
     fd . "$dir" --max-depth 1 --type directory | while read -r template; do
-        echo "Instantiating template: '.${template#"$ROOT"}'.."
+        template_path="${template#"$ROOT/"}"
+        echo "Instantiating template: '$template_path'.."
         template_new_path="$ROOT/build/$(basename "$template")"
-        $DRY_RUN_CMD cp --no-target-directory --recursive "${template%/}" "$template_new_path"
+
+        git ls-files --cached --full-name "$template" | while read -r file; do
+            new_file_path="${file#"$template_path"}"
+            new_path="$template_new_path/$new_file_path"
+
+            $DRY_RUN_CMD mkdir --parents "$(dirname "$new_path")"
+
+            $DRY_RUN_CMD cp "$file" "$new_path"
+        done
 
         {
             # Show directories