Skip to content

Commit d2e2d58

Browse files
authored
Merge pull request #142 from coding-cat-official/modify-build-problem-script
modify build-problem script
2 parents 8e43b19 + 131106e commit d2e2d58

File tree

1 file changed

+67
-20
lines changed

1 file changed

+67
-20
lines changed

build-problem

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,70 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
2+
set -euo pipefail
23

3-
set -e
4-
5-
die () {
6-
echo "$@" >&2
7-
exit 1
4+
die() {
5+
echo "$@" >&2
6+
exit 1
87
}
98

10-
cd "$1"
11-
12-
jq -n \
13-
--arg description "$(cat description.md)" \
14-
--arg starter "$(cat starter.py)" \
15-
--slurpfile meta meta.json \
16-
--slurpfile io io.json \
17-
'{
18-
starter: $starter,
19-
description: $description,
20-
meta: $meta[0],
21-
io: $io[0],
22-
}' \
23-
> problem.json || die "Failed to build problem $1"
9+
if [ $# -ne 1 ]; then
10+
die "Usage: $0 <problem-directory>"
11+
fi
12+
13+
PROB_DIR="$1"
14+
cd "$PROB_DIR"
15+
16+
# read question_type array from meta.json
17+
question_types=$(jq -r '.question_type[]' meta.json)
18+
19+
# common jq args: description, meta, io
20+
# --rawfile automatically slurps & escapes the file content as a JSON string
21+
jq_args=(
22+
--rawfile description description.md
23+
--slurpfile meta meta.json
24+
--slurpfile io io.json
25+
)
26+
27+
if echo "$question_types" | grep -qx "mutation"; then
28+
# mutation‐style problem
29+
jq_args+=( --rawfile solution solution.py )
30+
31+
# build a true JSON array of all mutation_*.py bodies
32+
mutations=$( {
33+
printf '['
34+
sep=""
35+
for f in mutation_*.py; do
36+
[ -f "$f" ] || continue
37+
printf "%s" "$sep"
38+
jq -Rs . "$f"
39+
sep=","
40+
done
41+
printf ']'
42+
} )
43+
44+
jq_args+=( --argjson mutations "$mutations" )
45+
46+
jq -n "${jq_args[@]}" '
47+
{
48+
description: $description,
49+
meta: $meta[0],
50+
io: $io[0],
51+
solution: $solution,
52+
mutations: $mutations
53+
}
54+
' > problem.json \
55+
|| die "Failed to build mutation problem in $PROB_DIR"
56+
57+
else
58+
# coding‐style problem
59+
jq_args+=( --rawfile starter starter.py )
60+
61+
jq -n "${jq_args[@]}" '
62+
{
63+
description: $description,
64+
starter: $starter,
65+
meta: $meta[0],
66+
io: $io[0]
67+
}
68+
' > problem.json \
69+
|| die "Failed to build coding problem in $PROB_DIR"
70+
fi

0 commit comments

Comments
 (0)