Skip to content

Commit 1b3cdee

Browse files
committed
Use property as fn name if there is canonical data, use slug if there isn't
1 parent 4ec2492 commit 1b3cdee

File tree

6 files changed

+49
-13
lines changed

6 files changed

+49
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ bin/configlet.exe
88
bin/exercise
99
bin/exercise.exe
1010
bin/generator-utils/ngram
11+
bin/test_template
1112
exercises/*/*/Cargo.lock
1213
exercises/*/*/clippy.log
1314
canonical_data.json

bin/generator-utils/fetch_canonical_data renamed to bin/fetch_canonical_data

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env bash
2+
# This script fetches the canonical data of the exercise.
23

34
if [ $# -ne 1 ]; then
4-
echo "Usage: bin/generator-utils/fetch_canonical_data <exercise-slug>"
5+
echo "Usage: bin/fetch_canonical_data <exercise-slug>"
56
exit 1
67
fi
78

bin/generate_practice_exercise.sh renamed to bin/generate_practice_exercise

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ check_exercise_existence "$1"
3636
# ==================================================
3737

3838
SLUG="$1"
39+
HAS_CANONICAL_DATA=true
40+
# Fetch canonical data
41+
canonical_json=$(bin/fetch_canonical_data "$SLUG")
42+
43+
if [ "${canonical_json}" == "404: Not Found" ]; then
44+
HAS_CANONICAL_DATA=false
45+
message "warning" "This exercise doesn't have canonical data"
46+
47+
else
48+
echo "$canonical_json" >canonical_data.json
49+
message "success" "Fetched canonical data successfully!"
50+
fi
51+
3952
UNDERSCORED_SLUG=$(dash_to_underscore "$SLUG")
4053
EXERCISE_DIR="exercises/practice/${SLUG}"
4154
EXERCISE_NAME=$(format_exercise_name "$SLUG")
@@ -52,9 +65,9 @@ cargo new --lib "$EXERCISE_DIR" -q
5265
mkdir -p "$EXERCISE_DIR"/tests
5366
touch "${EXERCISE_DIR}/tests/${SLUG}.rs"
5467

55-
create_test_file_template "$EXERCISE_DIR" "$SLUG"
56-
create_lib_rs_template "$EXERCISE_DIR" "$SLUG"
57-
create_example_rs_template "$EXERCISE_DIR" "$SLUG"
68+
create_test_file_template "$EXERCISE_DIR" "$SLUG" "$HAS_CANONICAL_DATA"
69+
create_lib_rs_template "$EXERCISE_DIR" "$SLUG" "$HAS_CANONICAL_DATA"
70+
create_example_rs_template "$EXERCISE_DIR" "$SLUG" "$HAS_CANONICAL_DATA"
5871
overwrite_gitignore "$EXERCISE_DIR"
5972

6073
message "success" "Created Rust files succesfully!"

bin/generate_tests

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#!/usr/bin/env bash
22

3+
set -euo pipefail
34
# shellcheck source=/dev/null
45
source ./bin/generator-utils/utils.sh
56

67
function digest_template() {
7-
template=$(cat bin/generator-utils/test_template)
8+
template=$(cat bin/test_template)
89
# shellcheck disable=SC2001
910
# turn every token into a jq command
1011
echo "$template" | sed 's/${\([^}]*\)\}\$/$(echo $case | jq -r '\''.\1'\'')/g'

bin/generator-utils/templates.sh

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ source ./bin/generator-utils/utils.sh
66
function create_test_file_template() {
77
local exercise_dir=$1
88
local slug=$2
9+
local has_canonical_data=$3
910
local test_file="${exercise_dir}/tests/${slug}.rs"
1011

1112
cat <<EOT >"$test_file"
@@ -14,11 +15,7 @@ use $(dash_to_underscore "$slug")::*;
1415
1516
EOT
1617

17-
canonical_json=$(bin/generator-utils/fetch_canonical_data "$slug")
18-
echo "$canonical_json" >canonical_data.json
19-
20-
if [ "${canonical_json}" == "404: Not Found" ]; then
21-
canonical_json=$(jq --null-input '{cases: []}')
18+
if [ "$has_canonical_data" == false ]; then
2219

2320
cat <<EOT >>"$test_file"
2421
// As there isn't a canonical data file for this exercise, you will need to craft your own tests.
@@ -28,6 +25,8 @@ EOT
2825
message "info" "This exercise doesn't have canonical data."
2926
message "success" "Stub file for tests has been created!"
3027
else
28+
canonical_json=$(cat canonical_data.json)
29+
3130
# sometimes canonical data has multiple levels with multiple `cases` arrays.
3231
#(see kindergarten-garden https://github.com/exercism/problem-specifications/blob/main/exercises/kindergarten-garden/canonical-data.json)
3332
# so let's flatten it
@@ -64,6 +63,8 @@ EOT
6463
function create_lib_rs_template() {
6564
local exercise_dir=$1
6665
local slug=$2
66+
local has_canonical_data=$3
67+
fn_name=$(create_fn_name "$slug" "$has_canonical_data")
6768
cat <<EOT >"${exercise_dir}/src/lib.rs"
6869
pub fn $(dash_to_underscore "$slug")() {
6970
unimplemented!("implement ${slug} exercise")
@@ -88,15 +89,33 @@ EOT
8889
}
8990

9091
function create_example_rs_template() {
91-
exercise_dir=$1
92-
slug=$2
92+
local exercise_dir=$1
93+
local slug=$2
94+
local has_canonical_data=$3
95+
96+
fn_name=$(create_fn_name "$slug" "$has_canonical_data")
97+
9398
mkdir "${exercise_dir}/.meta"
9499
cat <<EOT >"${exercise_dir}/.meta/example.rs"
95-
pub fn $(dash_to_underscore "$slug")() {
100+
pub fn ${fn_name}() {
96101
// TODO: Create a solution that passes all the tests
97102
unimplemented!("implement ${slug} exercise")
98103
}
99104
100105
EOT
101106
message "success" "Stub file for example.rs has been created!"
102107
}
108+
109+
function create_fn_name() {
110+
slug=$1
111+
has_canonical_data=$2
112+
113+
if [ "$has_canonical_data" == true ]; then
114+
fn_name=$(dash_to_underscore "$slug")
115+
else
116+
fn_name=$(jq -r 'first(.. | .property? // empty)' canonical_data.json)
117+
fi
118+
119+
echo "$fn_name"
120+
121+
}

bin/generator-utils/utils.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function message() {
1010
case "$flag" in
1111
"success") printf "${GREEN}%s${RESET}\n" "[success]: $message" ;;
1212
"info") printf "${BLUE}%s${RESET}\n" "[info]: $message" ;;
13+
"warning") printf "${YELLOW}%s${RESET}\n" "[warning]: $message" ;;
1314
"error") printf "${RED}%s${RESET}\n" "[error]: $message" ;;
1415
"done")
1516
echo

0 commit comments

Comments
 (0)