-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.sh
More file actions
78 lines (63 loc) · 2.68 KB
/
Copy pathutils.sh
File metadata and controls
78 lines (63 loc) · 2.68 KB
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
#!/usr/bin/env bash
set -euo pipefail
# Build a docker image
# Arguments:
# - devcontainer image name
# - variant for the upstream image
create_image() {
local IMAGE=$1
local VARIANT=$2
# Validate inputs
if [ -z "$IMAGE" ] || [ -z "$VARIANT" ]; then
echo "Error: IMAGE and VARIANT must be set"
exit 1
fi
# Define project directories
local SRC_DIR
SRC_DIR=$(dirname "$(realpath "$0")")
local SRC_PATH="$SRC_DIR/../src/$IMAGE"
local TARGET_DIR="$SRC_DIR/../dist/${IMAGE}/${VARIANT}"
# Check if the source directory exists
if [ ! -d "$SRC_PATH" ]; then
echo "Error: Source directory $SRC_PATH does not exist"
exit 1
fi
# Create target directory if it doesn't exist
echo "Preparing target directory: $TARGET_DIR"
mkdir -p "$TARGET_DIR"
# Copy content from the source directory to the target directory using rsync
echo "Copying files from $SRC_PATH to $TARGET_DIR"
rsync -a --exclude 'variants.json' "$SRC_PATH"/ "$TARGET_DIR"/
# Replace the placeholder with the variant provided in devcontainer.json, if exists
local DEVCONTAINER_JSON="$TARGET_DIR/.devcontainer/devcontainer.json"
if [ -f "$DEVCONTAINER_JSON" ]; then
echo "Updating devcontainer.json with variant: $VARIANT"
sed -i -e "s/__VARIANT__/${VARIANT}/g" "$DEVCONTAINER_JSON"
else
echo "Warning: $DEVCONTAINER_JSON not found, skipping variant substitution"
fi
echo "Build process complete for image: $IMAGE, variant: $VARIANT."
}
# Create the file variant-matrix.json with the images and its versions
create_variant_matrix() {
local VARIANT_MATRIX_FILE="dist/variant-matrix.json"
local IMAGE VARIANT IMAGES VARIANTS
# Initialize the variant-matrix.json file with an empty array
echo '{ "variants": [] }' > "$VARIANT_MATRIX_FILE"
# Get a list of images (directories in dist)
IMAGES=$(find dist -mindepth 1 -maxdepth 1 -type d -printf "%f\n")
# Initialize an array to collect JSON objects
variant_items=()
for IMAGE in $IMAGES; do
# Get a list of variants (subdirectories in each image folder)
VARIANTS=$(find dist/"$IMAGE" -mindepth 1 -maxdepth 1 -type d -printf "%f\n")
for VARIANT in $VARIANTS; do
# Create a JSON object with IMAGE and VARIANT attributes
variant_items+=("{\"IMAGE\": \"$IMAGE\", \"VERSION\": \"$VARIANT\"}")
done
done
# Write the accumulated JSON objects to the JSON file in one operation
jq --argjson variants "$(printf '%s\n' "${variant_items[@]}" | jq -s '.')" \
'.variants = $variants' \
"$VARIANT_MATRIX_FILE" > tmp.json && mv tmp.json "$VARIANT_MATRIX_FILE"
}