-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine.sh
executable file
·51 lines (45 loc) · 1.01 KB
/
combine.sh
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
#!/usr/bin/env bash
set -Eeuo pipefail
# Internal: Replace content between `<[REPLACEMENT]>` and `</[REPLACEMENT]>`
# XML tags with the contents of a shared file.
#
# $1 - File to update.
# $2 - Shared file.
# $3 - Replacement tag name.
#
# Returns nothing.
update_file() {
local file=$1
local shared=$2
local replacement=$3
local tmp="${file}.tmp"
# Remove existing content
awk '
/<'"$replacement"'>/ {
print
while (getline && !/'\<\\/"$replacement"'>/) {}
print
next
}
{print}
' "$file" > "$tmp"
# Insert shared content
awk '
/<'"$replacement"'>/ {
print
while (getline line < "'"${shared}"'") {
print line
}
print ""
next
}
{print}
' "$tmp" > "$file"
rm "$tmp"
echo "Updated '$file' with '$shared' content."
}
for file in "rules/backend.md" "rules/frontend.md" "rules/fullStack.md"; do
update_file "$file" "fragments/base.md" "base"
update_file "$file" "fragments/backend.md" "backend"
update_file "$file" "fragments/frontend.md" "frontend"
done