-
Notifications
You must be signed in to change notification settings - Fork 546
Expand file tree
/
Copy pathadd_practice_exercise
More file actions
executable file
·112 lines (86 loc) · 4.29 KB
/
add_practice_exercise
File metadata and controls
executable file
·112 lines (86 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
# see comment in generator-utils/utils.sh
# shellcheck source=bin/generator-utils/utils.sh
# shellcheck source=bin/generator-utils/templates.sh
# shellcheck source=bin/generator-utils/prompts.sh
# shellcheck source=./generator-utils/utils.sh
# shellcheck source=./generator-utils/prompts.sh
# shellcheck source=./generator-utils/templates.sh
source ./bin/generator-utils/utils.sh
source ./bin/generator-utils/prompts.sh
source ./bin/generator-utils/templates.sh
# Exit if anything fails.
set -euo pipefail
# If argument not provided, print usage and exit
if [ $# -ne 1 ] && [ $# -ne 2 ] && [ $# -ne 3 ]; then
echo "Usage: bin/add_practice_exercise <exercise-slug> [difficulty] [author-github-handle]"
exit 1
fi
# Check if sed is gnu-sed
if ! sed --version | grep -q "GNU sed"; then
echo "GNU sed is required. Please install it and make sure it's in your PATH."
exit 1
fi
# Check if jq and curl are installed
command -v jq >/dev/null 2>&1 || {
echo >&2 "jq is required but not installed. Please install it and make sure it's in your PATH."
exit 1
}
command -v curl >/dev/null 2>&1 || {
echo >&2 "curl is required but not installed. Please install it and make sure it's in your PATH."
exit 1
}
# Build configlet
bin/fetch-configlet
message "success" "Fetched configlet successfully!"
# Check if exercise exists in configlet info or in config.json
check_exercise_existence "$1"
# ==================================================
slug="$1"
# Fetch canonical data
canonical_json=$(bin/fetch_canonical_data "$slug")
has_canonical_data=true
if [ "${canonical_json}" == "404: Not Found" ]; then
has_canonical_data=false
message "warning" "This exercise doesn't have canonical data"
else
echo "$canonical_json" >canonical_data.json
message "success" "Fetched canonical data successfully!"
fi
underscored_slug=$(dash_to_underscore "$slug")
exercise_dir="exercises/practice/${slug}"
exercise_name=$(format_exercise_name "$slug")
message "info" "Using ${yellow}${exercise_name}${blue} as a default exercise name. You can edit this later in the config.json file"
# using default value for difficulty
exercise_difficulty=$(validate_difficulty_input "${2:-$(get_exercise_difficulty)}")
message "info" "The exercise difficulty has been set to ${yellow}${exercise_difficulty}${blue}. You can edit this later in the config.json file"
# using default value for author
author_handle=${3:-$(get_author_handle)}
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"
create_rust_files "$exercise_dir" "$slug" "$has_canonical_data"
# ==================================================
# Preparing config.json
message "info" "Adding instructions and configuration files..."
uuid=$(bin/configlet uuid)
# Add exercise-data to global config.json
jq --arg slug "$slug" --arg uuid "$uuid" --arg name "$exercise_name" --argjson difficulty "$exercise_difficulty" \
'.exercises.practice += [{slug: $slug, name: $name, uuid: $uuid, practices: [], prerequisites: [], difficulty: $difficulty}]' \
config.json >config.json.tmp
# jq always rounds whole numbers, but average_run_time needs to be a float
sed -i 's/"average_run_time": \([0-9]\+\)$/"average_run_time": \1.0/' config.json.tmp
mv config.json.tmp config.json
message "success" "Added instructions and configuration files"
# Create instructions and config files
echo "Creating instructions and config files"
./bin/configlet sync --update --yes --docs --metadata --exercise "$slug"
./bin/configlet sync --update --yes --filepaths --exercise "$slug"
./bin/configlet sync --update --tests include --exercise "$slug"
message "success" "Created instructions and config files"
# Push author to "authors" array in ./meta/config.json
meta_config="$exercise_dir"/.meta/config.json
jq --arg author "$author_handle" '.authors += [$author]' "$meta_config" >"$meta_config".tmp && mv "$meta_config".tmp "$meta_config"
message "success" "You've been added as the author of this exercise."
sed -i "s/name = \".*\"/name = \"$underscored_slug\"/" "$exercise_dir"/Cargo.toml
message "done" "All stub files were created."
message "info" "After implementing the solution, tests and configuration, please run:"
echo "./bin/configlet fmt --update --yes --exercise ${slug}"