about summary refs log tree commit diff stats
path: root/instantiate_templates.sh
blob: 8b7b3e8a9ee511555260e62697a5ed6b5e584dee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/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=""

# 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"

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