|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# shellcheck source=/dev/null |
| 4 | +source ./bin/generator-utils/utils.sh |
| 5 | +source ./bin/generator-utils/prompts.sh |
| 6 | +source ./bin/generator-utils/templates.sh |
| 7 | + |
| 8 | +# Exit if anything fails. |
| 9 | +set -euo pipefail |
| 10 | + |
| 11 | +# If argument not provided, print usage and exit |
| 12 | +if [ $# -ne 1 ] && [ $# -ne 2 ] && [ $# -ne 3 ]; then |
| 13 | + echo "Usage: bin/add_practice_exercise <exercise-slug> [difficulty] [author-github-handle]" |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +# Check if sed is gnu-sed |
| 18 | +if ! sed --version | grep -q "GNU sed"; then |
| 19 | + echo "GNU sed is required. Please install it and make sure it's in your PATH." |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +# Check if jq and curl are installed |
| 24 | +command -v jq >/dev/null 2>&1 || { |
| 25 | + echo >&2 "jq is required but not installed. Please install it and make sure it's in your PATH." |
| 26 | + exit 1 |
| 27 | +} |
| 28 | +command -v curl >/dev/null 2>&1 || { |
| 29 | + echo >&2 "curl is required but not installed. Please install it and make sure it's in your PATH." |
| 30 | + exit 1 |
| 31 | +} |
| 32 | + |
| 33 | +# Check if exercise exists in configlet info or in config.json |
| 34 | +check_exercise_existence "$1" |
| 35 | + |
| 36 | +# ================================================== |
| 37 | + |
| 38 | +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 | + |
| 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" |
| 56 | +# 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" |
| 59 | +# 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" |
| 65 | + |
| 66 | + |
| 67 | +# ================================================== |
| 68 | + |
| 69 | +# build configlet |
| 70 | +./bin/fetch-configlet |
| 71 | +message "success" "Fetched configlet successfully!" |
| 72 | + |
| 73 | +# Preparing config.json |
| 74 | +message "info" "Adding instructions and configuration files..." |
| 75 | +UUID=$(bin/configlet uuid) |
| 76 | + |
| 77 | +jq --arg slug "$SLUG" --arg uuid "$UUID" --arg name "$EXERCISE_NAME" --arg difficulty "$EXERCISE_DIFFICULTY" \ |
| 78 | + '.exercises.practice += [{slug: $slug, name: $name, uuid: $uuid, practices: [], prerequisites: [], difficulty: $difficulty}]' \ |
| 79 | + config.json >config.json.tmp |
| 80 | +# jq always rounds whole numbers, but average_run_time needs to be a float |
| 81 | +sed -i 's/"average_run_time": \([0-9]\+\)$/"average_run_time": \1.0/' config.json.tmp |
| 82 | +mv config.json.tmp config.json |
| 83 | +message "success" "Added instructions and configuration files" |
| 84 | + |
| 85 | +# Create instructions and config files |
| 86 | +echo "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" |
| 90 | +message "success" "Created instructions and config files" |
| 91 | + |
| 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" |
| 94 | +message "success" "You've been added as the author of this exercise." |
| 95 | + |
| 96 | +sed -i "s/name = \".*\"/name = \"$UNDERSCORED_SLUG\"/" "$EXERCISE_DIR"/Cargo.toml |
| 97 | + |
| 98 | +message "done" "All stub files were created." |
| 99 | + |
| 100 | +message "info" "After implementing the solution, tests and configuration, please run:" |
| 101 | +echo "./bin/configlet fmt --update --yes --exercise ${SLUG}" |
0 commit comments