-
Notifications
You must be signed in to change notification settings - Fork 0
/
split-into-steps.sh
executable file
·100 lines (84 loc) · 2.74 KB
/
split-into-steps.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
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
100
if [[ $# -ne 3 ]]; then
echo "Usage: $0 <source-file> <template.yaml> <output.yaml>"
exit 1
fi
ENV_VARS=()
for var in $(compgen -e); do
if [[ $(type -t "$var") == "function" ]]; then
continue
fi
ENV_VARS+=("$var")
done
declare -xr _BUILDSCRIPT_RUN_STEP_=none
LIBDIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
SOURCE_FILE=$(realpath "$1")
TEMPLATE=$(realpath "$2")
OUTPUT_FILE=$(realpath -m "$3")
for arg; do shift; done
SOURCE_FILE_REL=$(realpath "--relative-to=$(pwd)" "${SOURCE_FILE}")
CURRENT_FILE="${SOURCE_FILE}"
# shellcheck source=/dev/null
source "${SOURCE_FILE}"
# shellcheck source=package/include-build.sh disable=SC2312
source "${LIBDIR}/package/include-build.sh"
if [[ -z ${STEPS_DEFINE+f} ]] || [[ ${#STEPS_DEFINE[@]} -eq 0 ]]; then
die "no build steps found"
fi
function parse_file_sections() {
declare -g FILE_PREFIX='' FILE_MIDDLE='' FILE_SUFFIX='' STEP_CONTENT='' STEP_INDENT=''
local STATE=prefix LINE
while IFS='' read -r LINE; do
if [[ ${STATE} == prefix ]]; then
printf -v FILE_PREFIX '%s%s\n' "${FILE_PREFIX}" "${LINE}"
if [[ ${LINE} == 'env:' ]]; then
STATE=middle
fi
if [[ ${LINE} == *'### BUILD_SCTION START ###'* ]]; then
die "missing env: section"
fi
elif [[ ${STATE} == middle ]]; then
if [[ ${LINE} == *'### BUILD_SCTION START ###'* ]]; then
STATE=content-first
else
printf -v FILE_MIDDLE '%s%s\n' "${FILE_MIDDLE}" "${LINE}"
fi
elif [[ ${STATE} == content-first ]]; then
STATE=content
STEP_INDENT="${LINE/[^[:space:]]*/}"
printf -v STEP_CONTENT '%s%s\n' "${STEP_CONTENT}" "${LINE:${#STEP_INDENT}}"
elif [[ ${STATE} == content ]]; then
if [[ ${LINE} == *'### BUILD_SCTION END ###'* ]]; then
STATE=suffix
else
printf -v STEP_CONTENT '%s%s\n' "${STEP_CONTENT}" "${LINE:${#STEP_INDENT}}"
fi
elif [[ ${STATE} == suffix ]]; then
printf -v FILE_SUFFIX '%s%s\n' "${FILE_SUFFIX}" "${LINE}"
fi
done <"${TEMPLATE}"
}
parse_file_sections
function create_section() {
python3 "${COMMON_LIB_ROOT}/staff/build-steps/generate-section.py" "${STEP_CONTENT}" "${STEPDEF[name]}" "${STEPDEF[index]}" "${STEPDEF[title]}" \
| sed -u "s/^/${STEP_INDENT}/"
}
DIR=$(dirname "${CURRENT_FILE}")
REL_DIR=$(realpath "--relative-to=$MONO_ROOT_DIR" "${DIR}")
info_success "collected ${#STEPS_DEFINE[@]} steps"
OUTPUT=$(
echo "${FILE_PREFIX}"
printf " SOURCE_FILE: %s\n" "${SOURCE_FILE_REL}"
printf " PROJECT_DIR: %s\n" "${REL_DIR}"
printf " KNOWN_BASE_IMAGES: %s\n" "${KNOWN_BASE_IMAGES[*]}"
echo "${FILE_MIDDLE}"
declare -A STEPDEF=()
for JSON in "${STEPS_DEFINE[@]}"; do
json_map_get_back "STEPDEF" "${JSON}"
create_section
done
echo "${FILE_SUFFIX}"
)
for var in "${ENV_VARS[@]}"; do
OUTPUT=${OUTPUT//"{{$var}}"/${!var}}
done
write_file --mode 0644 "${OUTPUT_FILE}" "${OUTPUT}"