#!/usr/bin/env sh #! Copies the templates from `templates/*` to `build/*` and replaces every symbolic link 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)" # 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 template_path="${template#"$ROOT/"}" echo "Instantiating template: '$template_path'.." template_new_path="$ROOT/build/$(basename "$template")" 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 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 --recursive "$symlink_path" "$symlink" else echo " -> Not instantiating symlink '.${symlink#"$ROOT"}', as it points outside the repo!" fi done done } instantiate_dir "$ROOT/templates" # vim: ft=sh