Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 67 additions & 20 deletions build-problem
Original file line number Diff line number Diff line change
@@ -1,23 +1,70 @@
#!/bin/bash
#!/usr/bin/env bash
set -euo pipefail

set -e

die () {
echo "$@" >&2
exit 1
die() {
echo "❌ $@" >&2
exit 1
}

cd "$1"

jq -n \
--arg description "$(cat description.md)" \
--arg starter "$(cat starter.py)" \
--slurpfile meta meta.json \
--slurpfile io io.json \
'{
starter: $starter,
description: $description,
meta: $meta[0],
io: $io[0],
}' \
> problem.json || die "Failed to build problem $1"
if [ $# -ne 1 ]; then
die "Usage: $0 <problem-directory>"
fi

PROB_DIR="$1"
cd "$PROB_DIR"

# read question_type array from meta.json
question_types=$(jq -r '.question_type[]' meta.json)

# common jq args: description, meta, io
# --rawfile automatically slurps & escapes the file content as a JSON string
jq_args=(
--rawfile description description.md
--slurpfile meta meta.json
--slurpfile io io.json
)

if echo "$question_types" | grep -qx "mutation"; then
# mutation‐style problem
jq_args+=( --rawfile solution solution.py )

# build a true JSON array of all mutation_*.py bodies
mutations=$( {
printf '['
sep=""
for f in mutation_*.py; do
[ -f "$f" ] || continue
printf "%s" "$sep"
jq -Rs . "$f"
sep=","
done
printf ']'
} )

jq_args+=( --argjson mutations "$mutations" )

jq -n "${jq_args[@]}" '
{
description: $description,
meta: $meta[0],
io: $io[0],
solution: $solution,
mutations: $mutations
}
' > problem.json \
|| die "Failed to build mutation problem in $PROB_DIR"

else
# coding‐style problem
jq_args+=( --rawfile starter starter.py )

jq -n "${jq_args[@]}" '
{
description: $description,
starter: $starter,
meta: $meta[0],
io: $io[0]
}
' > problem.json \
|| die "Failed to build coding problem in $PROB_DIR"
fi