Skip to content

Commit b5a2876

Browse files
committed
feat: add merging behaviour in scenario build script for options not defined in template
1 parent 8ee88a3 commit b5a2876

File tree

1 file changed

+53
-32
lines changed

1 file changed

+53
-32
lines changed

.github/actions/scenario-test/build.sh

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,64 @@ set -e
66

77
shopt -s dotglob
88

9+
function remove_comments {
10+
sed -i -re 's/^(.*)[[:blank:]]+(\/\/.*)$/\1/' ${1}
11+
}
12+
913
function merge {
10-
echo "(*) Generating config files"
11-
# # Configure templates only if `devcontainer-template.json` contains the `options` property.
12-
OPTION_PROPERTY=( $(jq -r '.options' devcontainer-template.json) )
14+
cp .devcontainer/devcontainer.json .devcontainer/devcontainer.json.bak
15+
remove_comments .devcontainer/devcontainer.json.bak
1316

14-
if [ "${OPTION_PROPERTY}" != "" ] && [ "${OPTION_PROPERTY}" != "null" ] ; then
15-
OPTIONS=( $(jq -r '.options | keys[]' devcontainer-template.json) )
17+
if [ -f inputs.json ] ; then
18+
jq -s ".[0] * .[1]" .devcontainer/devcontainer.json.bak ./inputs.json > .devcontainer/devcontainer.json
19+
cp -f .devcontainer/devcontainer.json .devcontainer/devcontainer.json.bak
20+
fi
1621

17-
if [ "${OPTIONS[0]}" != "" ] && [ "${OPTIONS[0]}" != "null" ] ; then
18-
echo "(!) Configuring template options for '${TEMPLATE_ID}-${SCENARIO}'"
19-
for OPTION in "${OPTIONS[@]}"
20-
do
21-
OPTION_KEY="\${templateOption:$OPTION}"
22+
jq -s ".[0] * .[1].${SCENARIO}" .devcontainer/devcontainer.json.bak test-project/scenarios.json > .devcontainer/devcontainer.json
23+
cp -f .devcontainer/devcontainer.json .devcontainer/devcontainer.json.bak
24+
25+
# Clean up unneeded keys
26+
printf -v tmp_str '.%s,' "${OPTIONS[@]}"
27+
jq "del($(echo "${tmp_str%,}"))" .devcontainer/devcontainer.json.bak > .devcontainer/devcontainer.json
2228

23-
OPTION_VALUE=$( jq -r ".${SCENARIO}.${OPTION}" test-project/scenarios.json)
29+
rm .devcontainer/devcontainer.json.bak
30+
}
2431

25-
if ( [ "${OPTION_VALUE}" = "" ] || [ "${OPTION_VALUE}" = "null" ] ) && [ -f inputs.json ] ; then
26-
OPTION_VALUE=$(jq -r ".${OPTION}" inputs.json)
27-
fi
28-
29-
if [ "${OPTION_VALUE}" = "" ] || [ "${OPTION_VALUE}" = "null" ] ; then
32+
function gen_config {
33+
echo "(*) Generating config files"
34+
# Configure templates only if `devcontainer-template.json` contains the `options` property.
35+
OPTIONS=( $(jq -r '.options | keys[]' devcontainer-template.json) )
36+
37+
if [ "${OPTIONS[0]}" != "" ] && [ "${OPTIONS[0]}" != "null" ] ; then
38+
echo "(!) Configuring template options for '${TEMPLATE_ID}-${SCENARIO}'"
39+
for OPTION in "${OPTIONS[@]}"
40+
do
41+
OPTION_KEY="\${templateOption:$OPTION}"
42+
43+
OPTION_VALUE=$( jq -r ".${SCENARIO}.${OPTION}" test-project/scenarios.json)
44+
45+
if ( [ "${OPTION_VALUE}" = "" ] || [ "${OPTION_VALUE}" = "null" ] ) && [ -f inputs.json ] ; then
46+
OPTION_VALUE=$(jq -r ".${OPTION}" inputs.json)
47+
fi
48+
49+
if [ "${OPTION_VALUE}" = "" ] || [ "${OPTION_VALUE}" = "null" ] ; then
3050
OPTION_VALUE=$(jq -r ".options | .${OPTION} | .default" devcontainer-template.json)
31-
fi
32-
33-
# For empty default values use " "
34-
if [ "${OPTION_VALUE}" = "" ] || [ "${OPTION_VALUE}" = "null" ] ; then
35-
echo "Template '${TEMPLATE_ID}-${SCENARIO}' is missing a default value for option '${OPTION}'"
36-
exit 1
37-
fi
38-
39-
echo "(!) Replacing '${OPTION_KEY}' with '${OPTION_VALUE}'"
40-
OPTION_VALUE_ESCAPED=$(sed -e 's/[]\/$*.^[]/\\&/g' <<<"${OPTION_VALUE}")
41-
find ./ -type f -print0 | xargs -0 sed -i "s/${OPTION_KEY}/${OPTION_VALUE_ESCAPED}/g"
42-
43-
unset OPTION_VALUE
44-
done
45-
fi
51+
fi
52+
53+
# For empty default values use " "
54+
if [ "${OPTION_VALUE}" = "" ] || [ "${OPTION_VALUE}" = "null" ] ; then
55+
echo "Template '${TEMPLATE_ID}-${SCENARIO}' is missing a default value for option '${OPTION}'"
56+
exit 1
57+
fi
58+
59+
echo "(!) Replacing '${OPTION_KEY}' with '${OPTION_VALUE}'"
60+
OPTION_VALUE_ESCAPED=$(sed -e 's/[]\/$*.^[]/\\&/g' <<<"${OPTION_VALUE}")
61+
find ./ -type f -print0 | xargs -0 sed -i "s/${OPTION_KEY}/${OPTION_VALUE_ESCAPED}/g"
62+
63+
unset OPTION_VALUE
64+
done
65+
66+
merge
4667
fi
4768
}
4869

@@ -61,7 +82,7 @@ function create_scenario {
6182

6283
pushd "${SRC_DIR}"
6384

64-
merge
85+
gen_config
6586

6687
popd
6788

0 commit comments

Comments
 (0)