|
| 1 | +#!/bin/bash |
| 2 | +TEMPLATE_ID="$1" |
| 3 | + |
| 4 | +set -e |
| 5 | + |
| 6 | +shopt -s dotglob |
| 7 | + |
| 8 | +SRC_DIR="/tmp/${TEMPLATE_ID}" |
| 9 | +cp -R "src/${TEMPLATE_ID}" "${SRC_DIR}" |
| 10 | + |
| 11 | +pushd "${SRC_DIR}" |
| 12 | + |
| 13 | +# Configure templates only if `devcontainer-template.json` contains the `options` property. |
| 14 | +OPTION_PROPERTY=( $(jq -r '.options' devcontainer-template.json) ) |
| 15 | + |
| 16 | +if [ "${OPTION_PROPERTY}" != "" ] && [ "${OPTION_PROPERTY}" != "null" ] ; then |
| 17 | + OPTIONS=( $(jq -r '.options | keys[]' devcontainer-template.json) ) |
| 18 | + |
| 19 | + if [ "${OPTIONS[0]}" != "" ] && [ "${OPTIONS[0]}" != "null" ] ; then |
| 20 | + echo "(!) Configuring template options for '${TEMPLATE_ID}'" |
| 21 | + for OPTION in "${OPTIONS[@]}" |
| 22 | + do |
| 23 | + OPTION_KEY="\${templateOption:$OPTION}" |
| 24 | + OPTION_VALUE=$(jq -r ".options | .${OPTION} | .default" devcontainer-template.json) |
| 25 | + |
| 26 | + if [ "${OPTION_VALUE}" = "" ] || [ "${OPTION_VALUE}" = "null" ] ; then |
| 27 | + echo "Template '${TEMPLATE_ID}' is missing a default value for option '${OPTION}'" |
| 28 | + exit 1 |
| 29 | + fi |
| 30 | + |
| 31 | + echo "(!) Replacing '${OPTION_KEY}' with '${OPTION_VALUE}'" |
| 32 | + OPTION_VALUE_ESCAPED=$(sed -e 's/[]\/$*.^[]/\\&/g' <<<"${OPTION_VALUE}") |
| 33 | + find ./ -type f -print0 | xargs -0 sed -i "s/${OPTION_KEY}/${OPTION_VALUE_ESCAPED}/g" |
| 34 | + done |
| 35 | + fi |
| 36 | +fi |
| 37 | + |
| 38 | +popd |
| 39 | + |
| 40 | +TEST_DIR="test/${TEMPLATE_ID}" |
| 41 | +if [ -d "${TEST_DIR}" ] ; then |
| 42 | + echo "(*) Copying test folder" |
| 43 | + DEST_DIR="${SRC_DIR}/test-project" |
| 44 | + mkdir -p ${DEST_DIR} |
| 45 | + cp -Rp ${TEST_DIR}/* ${DEST_DIR} |
| 46 | + cp test/test-utils/test-utils.sh ${DEST_DIR} |
| 47 | +fi |
| 48 | + |
| 49 | +export DOCKER_BUILDKIT=1 |
| 50 | +echo "(*) Installing @devcontainer/cli" |
| 51 | +npm install -g @devcontainers/cli |
| 52 | + |
| 53 | +echo "Building Dev Container" |
| 54 | +ID_LABEL="test-container=${TEMPLATE_ID}" |
| 55 | +devcontainer up --id-label ${ID_LABEL} --workspace-folder "${SRC_DIR}" --log-level trace |
0 commit comments