11#! /usr/bin/env bash
22
3- # shellcheck source=/dev/null
3+ # see comment in generator-utils/utils.sh
4+ # shellcheck source=bin/generator-utils/utils.sh
5+ # shellcheck source=bin/generator-utils/templates.sh
6+ # shellcheck source=bin/generator-utils/prompts.sh
7+ # shellcheck source=./generator-utils/utils.sh
8+ # shellcheck source=./generator-utils/prompts.sh
9+ # shellcheck source=./generator-utils/templates.sh
10+
411source ./bin/generator-utils/utils.sh
512source ./bin/generator-utils/prompts.sh
613source ./bin/generator-utils/templates.sh
@@ -30,51 +37,52 @@ command -v curl >/dev/null 2>&1 || {
3037 exit 1
3138}
3239
40+ # Build configlet
41+ bin/fetch-configlet
42+ message " success" " Fetched configlet successfully!"
43+
3344# Check if exercise exists in configlet info or in config.json
3445check_exercise_existence " $1 "
3546
3647# ==================================================
3748
38- SLUG=" $1 "
39- HAS_CANONICAL_DATA=true
49+ slug=" $1 "
4050# Fetch canonical data
41- canonical_json=$( bin/fetch_canonical_data " $SLUG " )
51+ canonical_json=$( bin/fetch_canonical_data " $slug " )
4252
53+ has_canonical_data=true
4354if [ " ${canonical_json} " == " 404: Not Found" ]; then
44- HAS_CANONICAL_DATA =false
55+ has_canonical_data =false
4556 message " warning" " This exercise doesn't have canonical data"
4657
4758else
4859 echo " $canonical_json " > canonical_data.json
4960 message " success" " Fetched canonical data successfully!"
5061fi
5162
52- UNDERSCORED_SLUG =$( dash_to_underscore " $SLUG " )
53- EXERCISE_DIR =" exercises/practice/${SLUG } "
54- EXERCISE_NAME =$( format_exercise_name " $SLUG " )
55- message " info" " Using ${YELLOW }${EXERCISE_NAME }${BLUE } as a default exercise name. You can edit this later in the config.json file"
63+ underscored_slug =$( dash_to_underscore " $slug " )
64+ exercise_dir =" exercises/practice/${slug } "
65+ exercise_name =$( format_exercise_name " $slug " )
66+ message " info" " Using ${yellow }${exercise_name }${blue } as a default exercise name. You can edit this later in the config.json file"
5667# using default value for difficulty
57- EXERCISE_DIFFICULTY =$( validate_difficulty_input " ${2:- $(get_exercise_difficulty)} " )
58- message " info" " The exercise difficulty has been set to ${YELLOW }${EXERCISE_DIFFICULTY }${BLUE } . You can edit this later in the config.json file"
68+ exercise_difficulty =$( validate_difficulty_input " ${2:- $(get_exercise_difficulty)} " )
69+ message " info" " The exercise difficulty has been set to ${yellow }${exercise_difficulty }${blue } . You can edit this later in the config.json file"
5970# using default value for author
60- AUTHOR_HANDLE=${3:- $(get_author_handle)}
61- message " info" " Using ${YELLOW}${AUTHOR_HANDLE}${BLUE} as author's handle. You can edit this later in the 'authors' field in the ${EXERCISE_DIR} /.meta/config.json file"
62-
63-
64- create_rust_files " $EXERCISE_DIR " " $SLUG " " $HAS_CANONICAL_DATA "
71+ author_handle=${3:- $(get_author_handle)}
72+ message " info" " Using ${yellow}${author_handle}${blue} as author's handle. You can edit this later in the 'authors' field in the ${exercise_dir} /.meta/config.json file"
6573
74+ create_rust_files " $exercise_dir " " $slug " " $has_canonical_data "
6675
6776# ==================================================
6877
69- # build configlet
70- ./bin/fetch-configlet
71- message " success" " Fetched configlet successfully!"
7278
7379# Preparing config.json
7480message " info" " Adding instructions and configuration files..."
75- UUID=$( bin/configlet uuid)
7681
77- jq --arg slug " $SLUG " --arg uuid " $UUID " --arg name " $EXERCISE_NAME " --arg difficulty " $EXERCISE_DIFFICULTY " \
82+ uuid=$( bin/configlet uuid)
83+
84+ # Add exercise-data to global config.json
85+ jq --arg slug " $slug " --arg uuid " $uuid " --arg name " $exercise_name " --arg difficulty " $exercise_difficulty " \
7886 ' .exercises.practice += [{slug: $slug, name: $name, uuid: $uuid, practices: [], prerequisites: [], difficulty: $difficulty}]' \
7987 config.json > config.json.tmp
8088# jq always rounds whole numbers, but average_run_time needs to be a float
@@ -84,18 +92,21 @@ message "success" "Added instructions and configuration files"
8492
8593# Create instructions and config files
8694echo " Creating instructions and config files"
87- ./bin/configlet sync --update --yes --docs --metadata --exercise " $SLUG "
88- ./bin/configlet sync --update --yes --filepaths --exercise " $SLUG "
89- ./bin/configlet sync --update --tests include --exercise " $SLUG "
95+
96+ ./bin/configlet sync --update --yes --docs --metadata --exercise " $slug "
97+ ./bin/configlet sync --update --yes --filepaths --exercise " $slug "
98+ ./bin/configlet sync --update --tests include --exercise " $slug "
9099message " success" " Created instructions and config files"
91100
92- META_CONFIG=" $EXERCISE_DIR " /.meta/config.json
93- jq --arg author " $AUTHOR_HANDLE " ' .authors += [$author]' " $META_CONFIG " > " $META_CONFIG " .tmp && mv " $META_CONFIG " .tmp " $META_CONFIG "
101+ # Push author to "authors" array in ./meta/config.json
102+ meta_config=" $exercise_dir " /.meta/config.json
103+ jq --arg author " $author_handle " ' .authors += [$author]' " $meta_config " > " $meta_config " .tmp && mv " $meta_config " .tmp " $meta_config "
94104message " success" " You've been added as the author of this exercise."
95105
96- sed -i " s/name = \" .*\" /name = \" $UNDERSCORED_SLUG \" /" " $EXERCISE_DIR " /Cargo.toml
106+ sed -i " s/name = \" .*\" /name = \" $underscored_slug \" /" " $exercise_dir " /Cargo.toml
97107
98108message " done" " All stub files were created."
99109
100110message " info" " After implementing the solution, tests and configuration, please run:"
101- echo " ./bin/configlet fmt --update --yes --exercise ${SLUG} "
111+
112+ echo " ./bin/configlet fmt --update --yes --exercise ${slug} "
0 commit comments