Skip to content

Commit 5da7fe7

Browse files
Merge pull request exercism#222 from glennj/create-script-options
allow create script to receive author/difficulty on the command line
2 parents 4bee6e4 + 4f31fff commit 5da7fe7

File tree

1 file changed

+45
-35
lines changed

1 file changed

+45
-35
lines changed

bin/create-exercise

+45-35
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
11
#!/usr/bin/env bash
2-
set -e
2+
3+
usage() { die "usage: $0 [-a author] [-d difficulty] exercise_slug"; }
34

45
die() { echo "$*" >&2; exit 1; }
56

7+
toPascalCase() {
8+
# "some-kebab-case-word" => "SomeKebabCaseWord"
9+
local word=${1^}
10+
while [[ ${word} =~ (.*)-(.*) ]]; do
11+
word=${BASH_REMATCH[1]}${BASH_REMATCH[2]^}
12+
done
13+
echo "${word}"
14+
}
15+
616
if [[ $PWD != $(realpath "$(dirname "$0")/..") ]]; then
717
die "You must be in the track root directory."
818
fi
9-
if [[ -z $1 ]]; then
10-
die "usage: $0 exercise_slug"
11-
fi
19+
20+
author=
21+
difficulty=
22+
23+
while getopts :a:d: opt; do
24+
case ${opt} in
25+
a) author=${OPTARG} ;;
26+
d) difficulty=${OPTARG} ;;
27+
*) usage ;;
28+
esac
29+
done
30+
shift $((OPTIND - 1))
31+
32+
[[ -n ${1} ]] || usage
1233

1334
slug=$1
1435

@@ -17,16 +38,17 @@ if [[ -n ${existing} ]]; then
1738
die "${slug} already exists in config.json"
1839
fi
1940

20-
pascal=${slug^}
21-
while [[ ${pascal} =~ (.*)-(.*) ]]; do
22-
pascal=${BASH_REMATCH[1]}${BASH_REMATCH[2]^}
23-
done
41+
[[ -n ${author} ]] || read -rp "What's your GitHub handle? " author
42+
[[ -n ${difficulty} ]] || read -rp "What's the (likely) difficult for ${slug}? " difficulty
2443

2544
bin/fetch-configlet
26-
bin/configlet create --practice-exercise "${slug}"
45+
bin/configlet create \
46+
--author "${author}" \
47+
--difficulty "${difficulty}" \
48+
--practice-exercise "${slug}"
2749

28-
shDir=./exercises/shared/new-exercise-templates
2950
exDir=./exercises/practice/${slug}
51+
pascal=$(toPascalCase "${slug}")
3052

3153
############################################################
3254
cat <<'__LICENSE__' > "${exDir}/LICENSE"
@@ -106,7 +128,19 @@ __SPEC_WREN__
106128

107129
# throw the canonical data into the spec
108130
specDir=${XDG_CACHE_DIR:-${HOME}/.cache}/exercism/configlet/problem-specifications
109-
cat "${specDir}/exercises/${slug}/canonical-data.json" >> "${exDir}/${slug}.spec.wren"
131+
canonData="${specDir}/exercises/${slug}/canonical-data.json"
132+
if [[ -f "${canonData}" ]]; then
133+
{
134+
echo
135+
echo "// canonical data"
136+
cat "${canonData}"
137+
} >> "${exDir}/${slug}.spec.wren"
138+
else
139+
{
140+
echo
141+
echo "// no canonical data for ${slug}"
142+
} >> "${exDir}/${slug}.spec.wren"
143+
fi
110144

111145
############################################################
112146
sed -e "s/%{slug}/${slug}/g" \
@@ -115,27 +149,3 @@ class %{PascalSlug} {
115149
// implement me!
116150
}
117151
__PROOF_WREN__
118-
119-
############################################################
120-
121-
read -p "What's your GitHub handle? "
122-
if [[ -n ${REPLY} ]]; then
123-
conf=${exDir}/.meta/config.json
124-
tmp=$(mktemp)
125-
jq --arg who "${REPLY}" '.authors += [$who]' "${conf}" > "${tmp}" \
126-
&& mv "${tmp}" "${conf}"
127-
fi
128-
129-
read -p "What's the (likely) difficult for ${slug}? "
130-
if [[ -n ${REPLY} && ${REPLY} == +([0-9]) ]]; then
131-
tmp=$(mktemp)
132-
jq --argjson diff "${REPLY}" --arg slug "${slug}" '
133-
.exercises.practice |= map(
134-
if (.slug == $slug)
135-
then .difficulty = $diff
136-
else .
137-
end
138-
)
139-
' config.json > "${tmp}" \
140-
&& mv "${tmp}" config.json
141-
fi

0 commit comments

Comments
 (0)