1
1
#! /usr/bin/env bash
2
- set -e
2
+
3
+ usage () { die " usage: $0 [-a author] [-d difficulty] exercise_slug" ; }
3
4
4
5
die () { echo " $* " >&2 ; exit 1; }
5
6
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
+
6
16
if [[ $PWD != $( realpath " $( dirname " $0 " ) /.." ) ]]; then
7
17
die " You must be in the track root directory."
8
18
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
12
33
13
34
slug=$1
14
35
@@ -17,16 +38,17 @@ if [[ -n ${existing} ]]; then
17
38
die " ${slug} already exists in config.json"
18
39
fi
19
40
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
24
43
25
44
bin/fetch-configlet
26
- bin/configlet create --practice-exercise " ${slug} "
45
+ bin/configlet create \
46
+ --author " ${author} " \
47
+ --difficulty " ${difficulty} " \
48
+ --practice-exercise " ${slug} "
27
49
28
- shDir=./exercises/shared/new-exercise-templates
29
50
exDir=./exercises/practice/${slug}
51
+ pascal=$( toPascalCase " ${slug} " )
30
52
31
53
# ###########################################################
32
54
cat << '__LICENSE__ ' > "${exDir}/LICENSE"
@@ -106,7 +128,19 @@ __SPEC_WREN__
106
128
107
129
# throw the canonical data into the spec
108
130
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
110
144
111
145
# ###########################################################
112
146
sed -e " s/%{slug}/${slug} /g" \
@@ -115,27 +149,3 @@ class %{PascalSlug} {
115
149
// implement me!
116
150
}
117
151
__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