6
6
7
7
shopt -s dotglob
8
8
9
+ function remove_comments {
10
+ sed -i -re ' s/^(.*)[[:blank:]]+(\/\/.*)$/\1/' ${1}
11
+ }
12
+
9
13
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
13
16
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
16
21
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
22
28
23
- OPTION_VALUE=$( jq -r " .${SCENARIO} .${OPTION} " test-project/scenarios.json)
29
+ rm .devcontainer/devcontainer.json.bak
30
+ }
24
31
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
30
50
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
46
67
fi
47
68
}
48
69
@@ -61,7 +82,7 @@ function create_scenario {
61
82
62
83
pushd " ${SRC_DIR} "
63
84
64
- merge
85
+ gen_config
65
86
66
87
popd
67
88
0 commit comments