Skip to content

Commit 6465978

Browse files
committed
Lowercase vars, remove function, add .shellcheckrc
1 parent 7002cf1 commit 6465978

File tree

8 files changed

+110
-113
lines changed

8 files changed

+110
-113
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/add_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/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_tests

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,53 @@
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() {
11+
local template
812
template=$(cat bin/test_template)
9-
# shellcheck disable=SC2001
10-
# turn every token into a jq command
13+
# Turn every token into a jq command
1114
echo "$template" | sed 's/${\([^}]*\)\}\$/$(echo $case | jq -r '\''.\1'\'')/g'
1215
}
1316

1417
message "info" "Generating tests.."
1518
canonical_json=$(cat canonical_data.json)
16-
SLUG=$(echo "$canonical_json" | jq '.exercise')
17-
# shellcheck disable=SC2001
19+
slug=$(echo "$canonical_json" | jq '.exercise')
1820
# Remove double quotes
19-
SLUG=$(echo "$SLUG" | sed 's/"//g')
20-
EXERCISE_DIR="exercises/practice/$SLUG"
21-
TEST_FILE="$EXERCISE_DIR/tests/$SLUG.rs"
21+
slug=$(echo "$slug" | sed 's/"//g')
22+
exercise_dir="exercises/practice/$slug"
23+
test_file="$exercise_dir/tests/$slug.rs"
2224

23-
cat <<EOT >"$TEST_FILE"
24-
use $(dash_to_underscore "$SLUG")::*;
25+
cat <<EOT >"$test_file"
26+
use $(dash_to_underscore "$slug")::*;
2527
// Add tests here
2628
2729
EOT
2830

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

34+
# Shellcheck doesn't recognize that `case` is not unused
3235
# shellcheck disable=SC2034
3336
jq -c '.[]' <<<"$cases" | while read -r case; do
3437

3538
# Evaluate the bash parts and replace them with their return values
3639
eval_template="$(digest_template | sed -e "s/\$(\(.*\))/\$\(\1\)/g")"
3740
eval_template="$(eval "echo \"$eval_template\"")"
3841

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

43-
echo "$formatted_template" >>"$TEST_FILE"
44-
printf "\\n" >>"$TEST_FILE"
45+
# Push to test file
46+
echo "$formatted_template" >>"$test_file"
47+
printf "\\n" >>"$test_file"
4548

4649
done
4750

48-
rustfmt "$TEST_FILE"
51+
rustfmt "$test_file"
4952

50-
message "success" "Generated tests successfully! Check out ${TEST_FILE}"
53+
message "success" "Generated tests successfully! Check out ${test_file}"

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+
local default_author_handle
29+
default_author_handle="$(git config user.name)"
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: 15 additions & 9 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
@@ -39,12 +40,15 @@ EOT
3940
message "info" "This exercise doesn't have canonical data."
4041
message "success" "Stub file for tests has been created!"
4142
else
43+
local canonical_json
4244
canonical_json=$(cat canonical_data.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
49+
local cases
4750
cases=$(echo "$canonical_json" | jq '[ .. | objects | with_entries(select(.key | IN("uuid", "description", "input", "expected"))) | select(. != {}) | select(has("uuid")) ]')
51+
local fn_name
4852
fn_name=$(echo "$canonical_json" | jq -r 'first(.. | .property? // empty)')
4953

5054
first_iteration=true
@@ -74,10 +78,11 @@ EOT
7478

7579
}
7680

77-
function create_lib_rs_template() {
81+
create_lib_rs_template() {
7882
local exercise_dir=$1
7983
local slug=$2
8084
local has_canonical_data=$3
85+
local fn_name
8186
fn_name=$(create_fn_name "$slug" "$has_canonical_data")
8287
cat <<EOT >"${exercise_dir}/src/lib.rs"
8388
pub fn ${fn_name}() {
@@ -87,7 +92,7 @@ EOT
8792
message "success" "Stub file for lib.rs has been created!"
8893
}
8994

90-
function overwrite_gitignore() {
95+
overwrite_gitignore() {
9196
local exercise_dir=$1
9297
cat <<EOT >"$exercise_dir"/.gitignore
9398
# Generated by Cargo
@@ -102,11 +107,12 @@ EOT
102107
message "success" ".gitignore has been overwritten!"
103108
}
104109

105-
function create_example_rs_template() {
110+
create_example_rs_template() {
106111
local exercise_dir=$1
107112
local slug=$2
108113
local has_canonical_data=$3
109114

115+
local fn_name
110116
fn_name=$(create_fn_name "$slug" "$has_canonical_data")
111117

112118
mkdir "${exercise_dir}/.meta"
@@ -120,7 +126,7 @@ EOT
120126
message "success" "Stub file for example.rs has been created!"
121127
}
122128

123-
function create_rust_files() {
129+
create_rust_files() {
124130
local exercise_dir=$1
125131
local slug=$2
126132
local has_canonical_data=$3

0 commit comments

Comments
 (0)