-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplatize-legacy-infra
executable file
·99 lines (83 loc) · 2.63 KB
/
templatize-legacy-infra
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env bash
# Convert a template-infra worktree into one that minimally supports the
# Platform CLI tooling.
#
# Run from the root of a template-infra worktree.
set -eu
rename_file() {
local original_file=$1
local renamed_file=$2
mkdir -p "${renamed_file%/*}"
git mv "$original_file" "${renamed_file}"
}
rename_app_file() {
local original_file=$1
local renamed_file="${original_file/app/{{app_name\}\}}"
rename_file "${original_file}" "${renamed_file}"
}
if [[ -d .template-infra/ ]]; then
echo "Already templatized"
exit
fi
# get rid of example app junk
find app -not -name Makefile -delete &> /dev/null || :
git add -u
# rename "app" files
for f in $(git ls-files '*app*' | grep -v '^docs/' | grep -v 'template-only' | grep '\bapp\b'); do
rename_app_file "${f}"
done
# rather than copy over the upstream copier file:
#
# curl -O https://raw.githubusercontent.com/navapbc/template-infra/refs/heads/main/copier.yml
#
# we'll use a simplified version, since the only variable we are using is
# `app_name`
cat <<EOF > copier.yml
template:
type: str
choices:
- base
- app
#
# App vars
#
app_name:
type: str
help: The name of the app
validator: >-
{% if not (app_name | regex_search('^[a-z0-9\-_]+$')) %}
The app name can not be empty and should only contain lower case letters, digits, dashes, and underscores.
{% endif %}
when: &app
"{{ template == 'app' }}"
_envops:
trim_blocks: true
lstrip_blocks: true
_skip_if_exists:
- "/{{ app_name }}/"
- "/{{ app_name }}/Makefile"
_exclude:
- /.git
- /copier.yml
- /CODEOWNERS
- /CONTRIBUTING.md
- /LICENSE.md
- /README.md
EOF
# create answers file
mkdir .template-infra
echo -e "# Changes here will be overwritten by Copier\n{{ _copier_answers|to_nice_yaml -}}" > '.template-infra/{{_copier_conf.answers_file}}.jinja'
# fixup CI stuff, for file/path triggers
for f in $(rg -l -e 'app/' -e '-app-' .github/ | grep -v 'template-only' | grep -v 'README.md'); do
renamed_file=$f.jinja
git mv "${f}" "${renamed_file}"
sed -ri 's|app/|{{ app_name }}/|g' "${renamed_file}"
# sed -ri 's|-app-|-{{ app_name }}-|g' "${renamed_file}"
sed -ri "s/-app(\b.*yml)/-{{ app_name}}\1/g" "${renamed_file}"
sed -ri "s/app_name: app/app_name: {{ app_name}}/g" "${renamed_file}"
sed -ri 's/app_name: "app"/app_name: {{ app_name}}/g' "${renamed_file}"
# sed -ri 's/\$\{\{(.*)\}\}/\$\{\{"\{\{"\}\}\1\{\{"\}\}"\}\}/g' "${renamed_file}"
perl -pi -e 's|\$\{\{(.*?)\}\}|\$\{\{"\{\{"\}\}\1\{\{"\}\}"\}\}|g' "${renamed_file}"
sed -ri "s/\bApp\b/{{ app_name}}/g" "${renamed_file}"
done
git add copier.yml .template-infra/