Skip to content

Commit a4c275b

Browse files
committed
Change casing, fix shellcheck disables, add .shekkcheckrc
1 parent 0f1aee1 commit a4c275b

File tree

9 files changed

+93
-125
lines changed

9 files changed

+93
-125
lines changed

bin/.shellcheckrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
shell=bash
2+
external-sources=true
3+
disable=SC2001 # https://www.shellcheck.net/wiki/SC2001 - effects most of the sed commands

bin/build_exercise_crate.sh

Lines changed: 0 additions & 27 deletions
This file was deleted.

bin/fetch_canonical_data

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env bash
22
# This script fetches the canonical data of the exercise.
33

4+
# Exit if anything fails.
5+
set -euo pipefail
6+
47
if [ $# -ne 1 ]; then
58
echo "Usage: bin/fetch_canonical_data <exercise-slug>"
69
exit 1

bin/generate_practice_exercise

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env bash
22

3-
# shellcheck source=/dev/null
3+
# shellcheck source=./generator-utils/utils.sh
4+
# shellcheck source=./generator-utils/prompts.sh
5+
# shellcheck source=./generator-utils/templates.sh
46
source ./bin/generator-utils/utils.sh
57
source ./bin/generator-utils/prompts.sh
68
source ./bin/generator-utils/templates.sh
@@ -35,46 +37,45 @@ check_exercise_existence "$1"
3537

3638
# ==================================================
3739

38-
SLUG="$1"
39-
HAS_CANONICAL_DATA=true
40+
slug="$1"
4041
# Fetch canonical data
41-
canonical_json=$(bin/fetch_canonical_data "$SLUG")
42+
canonical_json=$(bin/fetch_canonical_data "$slug")
4243

44+
has_canonical_data=true
4345
if [ "${canonical_json}" == "404: Not Found" ]; then
44-
HAS_CANONICAL_DATA=false
46+
has_canonical_data=false
4547
message "warning" "This exercise doesn't have canonical data"
4648

4749
else
4850
echo "$canonical_json" >canonical_data.json
4951
message "success" "Fetched canonical data successfully!"
5052
fi
5153

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"
54+
underscored_slug=$(dash_to_underscore "$slug")
55+
exercise_dir="exercises/practice/${slug}"
56+
exercise_name=$(format_exercise_name "$slug")
57+
message "info" "Using ${yellow}${exercise_name}${blue} as a default exercise name. You can edit this later in the config.json file"
5658
# 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+
exercise_difficulty=$(validate_difficulty_input "${2:-$(get_exercise_difficulty)}")
60+
message "info" "The exercise difficulty has been set to ${yellow}${exercise_difficulty}${blue}. You can edit this later in the config.json file"
5961
# 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"
62+
author_handle=${3:-$(get_author_handle)}
63+
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"
6564

65+
create_rust_files "$exercise_dir" "$slug" "$has_canonical_data"
6666

6767
# ==================================================
6868

69-
# build configlet
69+
# Build configlet
7070
./bin/fetch-configlet
7171
message "success" "Fetched configlet successfully!"
7272

7373
# Preparing config.json
7474
message "info" "Adding instructions and configuration files..."
75-
UUID=$(bin/configlet uuid)
75+
uuid=$(bin/configlet uuid)
7676

77-
jq --arg slug "$SLUG" --arg uuid "$UUID" --arg name "$EXERCISE_NAME" --arg difficulty "$EXERCISE_DIFFICULTY" \
77+
# Add exercise-data to global config.json
78+
jq --arg slug "$slug" --arg uuid "$uuid" --arg name "$exercise_name" --arg difficulty "$exercise_difficulty" \
7879
'.exercises.practice += [{slug: $slug, name: $name, uuid: $uuid, practices: [], prerequisites: [], difficulty: $difficulty}]' \
7980
config.json >config.json.tmp
8081
# jq always rounds whole numbers, but average_run_time needs to be a float
@@ -84,18 +85,19 @@ message "success" "Added instructions and configuration files"
8485

8586
# Create instructions and config files
8687
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"
88+
./bin/configlet sync --update --yes --docs --metadata --exercise "$slug"
89+
./bin/configlet sync --update --yes --filepaths --exercise "$slug"
90+
./bin/configlet sync --update --tests include --exercise "$slug"
9091
message "success" "Created instructions and config files"
9192

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"
93+
# Push author to "authors" array in ./meta/config.json
94+
meta_config="$exercise_dir"/.meta/config.json
95+
jq --arg author "$author_handle" '.authors += [$author]' "$meta_config" >"$meta_config".tmp && mv "$meta_config".tmp "$meta_config"
9496
message "success" "You've been added as the author of this exercise."
9597

96-
sed -i "s/name = \".*\"/name = \"$UNDERSCORED_SLUG\"/" "$EXERCISE_DIR"/Cargo.toml
98+
sed -i "s/name = \".*\"/name = \"$underscored_slug\"/" "$exercise_dir"/Cargo.toml
9799

98100
message "done" "All stub files were created."
99101

100102
message "info" "After implementing the solution, tests and configuration, please run:"
101-
echo "./bin/configlet fmt --update --yes --exercise ${SLUG}"
103+
echo "./bin/configlet fmt --update --yes --exercise ${slug}"

bin/generate_tests

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/usr/bin/env bash
22

3+
# Exit if anything fails.
34
set -euo pipefail
4-
# shellcheck source=/dev/null
5+
6+
7+
# shellcheck source=./generator-utils/utils.sh
58
source ./bin/generator-utils/utils.sh
69

7-
function digest_template() {
10+
digest_template() {
811
template=$(cat bin/test_template)
9-
# shellcheck disable=SC2001
1012
# turn every token into a jq command
1113
echo "$template" | sed 's/${\([^}]*\)\}\$/$(echo $case | jq -r '\''.\1'\'')/g'
1214
}
@@ -27,20 +29,20 @@ use $(dash_to_underscore "$SLUG")::*;
2729
EOT
2830

2931
# Flattens canonical json, extracts only the objects with a uuid
30-
cases=$(echo "$canonical_json" | jq '[ .. | objects | with_entries(select(.key | IN("uuid", "description", "input", "expected", "property"))) | select(. != {}) | select(has("uuid")) ]')
32+
CASES=$(echo "$CANONICAL_JSON" | jq '[ .. | objects | with_entries(select(.key | IN("uuid", "description", "input", "expected", "property"))) | select(. != {}) | select(has("uuid")) ]')
3133

3234
# shellcheck disable=SC2034
33-
jq -c '.[]' <<<"$cases" | while read -r case; do
35+
jq -c '.[]' <<<"$CASES" | while read -r case; do
3436

3537
# Evaluate the bash parts and replace them with their return values
36-
eval_template="$(digest_template | sed -e "s/\$(\(.*\))/\$\(\1\)/g")"
37-
eval_template="$(eval "echo \"$eval_template\"")"
38+
EVAL_TEMPLATE="$(digest_template | sed -e "s/\$(\(.*\))/\$\(\1\)/g")"
39+
EVAL_TEMPLATE="$(eval "echo \"$EVAL_TEMPLATE\"")"
3840

3941
# Turn function name unto snake_case
40-
formatted_template=$(echo "$eval_template" | sed -e ':loop' -e 's/\(fn[^(]*\)[ -]/\1_/g' -e 't loop' | sed 's/fn_/fn /')
42+
FORMATTED_TEMPLATE=$(echo "$EVAL_TEMPLATE" | sed -e ':loop' -e 's/\(fn[^(]*\)[ -]/\1_/g' -e 't loop' | sed 's/fn_/fn /')
4143
# Push to file
4244

43-
echo "$formatted_template" >>"$TEST_FILE"
45+
echo "$FORMATTED_TEMPLATE" >>"$TEST_FILE"
4446
printf "\\n" >>"$TEST_FILE"
4547

4648
done

bin/generator-utils/colors.sh

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,14 @@
11
#!/usr/bin/env bash
22

3-
# shellcheck disable=SC2034
4-
# Reset
5-
RESET=$(echo -e '\033[0m')
3+
reset_color=$(echo -e '\033[0m')
64

7-
# Regular Colors
8-
BLACK=$(echo -e '\033[0;30m')
9-
RED=$(echo -e '\033[0;31m')
10-
GREEN=$(echo -e '\033[0;32m')
11-
YELLOW=$(echo -e '\033[0;33m')
12-
BLUE=$(echo -e '\033[0;34m')
13-
PURPLE=$(echo -e '\033[0;35m')
14-
CYAN=$(echo -e '\033[0;36m')
15-
WHITE=$(echo -e '\033[0;37m')
5+
red=$(echo -e '\033[0;31m')
6+
green=$(echo -e '\033[0;32m')
7+
yellow=$(echo -e '\033[0;33m')
8+
blue=$(echo -e '\033[0;34m')
9+
cyan=$(echo -e '\033[0;36m')
1610

17-
# Bold
18-
BOLD_BLACK=$(echo -e '\033[1;30m')
19-
BOLD_RED=$(echo -e '\033[1;31m')
20-
BOLD_GREEN=$(echo -e '\033[1;32m')
21-
BOLD_YELLOW=$(echo -e '\033[1;33m')
22-
BOLD_BLUE=$(echo -e '\033[1;34m')
23-
BOLD_PURPLE=$(echo -e '\033[1;35m')
24-
BOLD_CYAN=$(echo -e '\033[1;36m')
25-
BOLD_WHITE=$(echo -e '\033[1;37m')
11+
bold_green=$(echo -e '\033[1;32m')
2612

27-
# Underline
28-
UNDERLINE=$(echo -e ='\033[4m')
2913

30-
# Background
31-
BG_BLACK=$(echo -e ='\033[40m')
32-
BG_RED=$(echo -e ='\033[41m')
33-
BG_GREEN=$(echo -e ='\033[42m')
34-
BG_YELLOW=$(echo -e ='\033[43m')
35-
BG_BLUE=$(echo -e ='\033[44m')
36-
BG_PURPLE=$(echo -e ='\033[45m')
37-
BG_CYAN=$(echo -e ='\033[46m')
38-
BG_WHITE=$(echo -e ='\033[47m')
14+
export red green blue yellow bold_green reset_color cyan

bin/generator-utils/prompts.sh

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
11
#!/usr/bin/env bash
22

3-
# shellcheck source=/dev/null
3+
# shellcheck source=./colors.sh
44
source ./bin/generator-utils/colors.sh
55

6-
function get_exercise_difficulty() {
6+
get_exercise_difficulty() {
77
read -rp "Difficulty of exercise (1-10): " exercise_difficulty
88
echo "$exercise_difficulty"
99
}
1010

11-
function validate_difficulty_input() {
11+
validate_difficulty_input() {
1212

13-
valid_input=false
13+
local valid_input=false
1414
while ! $valid_input; do
1515
if [[ "$1" =~ ^[1-9]$|^10$ ]]; then
16-
exercise_difficulty=$1
17-
valid_input=true
16+
local exercise_difficulty=$1
17+
local valid_input=true
1818
else
19-
read -rp "${RED}Invalid input. ${RESET}Please enter an integer between 1 and 10. " exercise_difficulty
19+
read -rp "${red}Invalid input. ${reset_color}Please enter an integer between 1 and 10. " exercise_difficulty
2020
[[ "$exercise_difficulty" =~ ^[1-9]$|^10$ ]] && valid_input=true
2121

2222
fi
2323
done
2424
echo "$exercise_difficulty"
2525
}
2626

27-
function get_author_handle {
28-
DEFAULT_AUTHOR_HANDLE="$(git config user.name)"
27+
get_author_handle() {
28+
default_author_handle="$(git config user.name)"
29+
local default_author_handle
2930

30-
if [ -z "$DEFAULT_AUTHOR_HANDLE" ]; then
31-
read -rp "Hey! Couldn't find your Github handle. Add it now or skip with enter and add it later in the .meta.config.json file: " AUTHOR_HANDLE
31+
if [ -z "$default_author_handle" ]; then
32+
read -rp "Hey! Couldn't find your Github handle. Add it now or skip with enter and add it later in the .meta.config.json file: " author_handle
3233
else
33-
AUTHOR_HANDLE="$DEFAULT_AUTHOR_HANDLE"
34+
local author_handle="$default_author_handle"
3435

3536
fi
36-
echo "$AUTHOR_HANDLE"
37+
echo "$author_handle"
3738

3839
}

bin/generator-utils/templates.sh

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

3-
# shellcheck source=/dev/null
3+
# shellcheck source=./utils.sh
44
source ./bin/generator-utils/utils.sh
55

6-
function create_fn_name() {
7-
slug=$1
8-
has_canonical_data=$2
6+
create_fn_name() {
7+
local slug=$1
8+
local has_canonical_data=$2
99

1010
if [ "$has_canonical_data" == false ]; then
1111
fn_name=$(dash_to_underscore "$slug")
12+
local fn_name
1213
else
1314
fn_name=$(jq -r 'first(.. | .property? // empty)' canonical_data.json)
1415
fi
@@ -17,7 +18,7 @@ function create_fn_name() {
1718

1819
}
1920

20-
function create_test_file_template() {
21+
create_test_file_template() {
2122
local exercise_dir=$1
2223
local slug=$2
2324
local has_canonical_data=$3
@@ -40,12 +41,15 @@ EOT
4041
message "success" "Stub file for tests has been created!"
4142
else
4243
canonical_json=$(cat canonical_data.json)
44+
local canonical_json
4345

4446
# sometimes canonical data has multiple levels with multiple `cases` arrays.
4547
#(see kindergarten-garden https://github.com/exercism/problem-specifications/blob/main/exercises/kindergarten-garden/canonical-data.json)
4648
# so let's flatten it
4749
cases=$(echo "$canonical_json" | jq '[ .. | objects | with_entries(select(.key | IN("uuid", "description", "input", "expected"))) | select(. != {}) | select(has("uuid")) ]')
50+
local cases
4851
fn_name=$(echo "$canonical_json" | jq -r 'first(.. | .property? // empty)')
52+
local fn_name
4953

5054
first_iteration=true
5155
# loop through each object
@@ -108,6 +112,7 @@ function create_example_rs_template() {
108112
local has_canonical_data=$3
109113

110114
fn_name=$(create_fn_name "$slug" "$has_canonical_data")
115+
local fn_name
111116

112117
mkdir "${exercise_dir}/.meta"
113118
cat <<EOT >"${exercise_dir}/.meta/example.rs"

0 commit comments

Comments
 (0)