Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow create script to receive author/difficulty on the command line #222

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 45 additions & 35 deletions bin/create-exercise
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
#!/usr/bin/env bash
set -e

usage() { die "usage: $0 [-a author] [-d difficulty] exercise_slug"; }

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

toPascalCase() {
# "some-kebab-case-word" => "SomeKebabCaseWord"
local word=${1^}
while [[ ${word} =~ (.*)-(.*) ]]; do
word=${BASH_REMATCH[1]}${BASH_REMATCH[2]^}
done
echo "${word}"
}

if [[ $PWD != $(realpath "$(dirname "$0")/..") ]]; then
die "You must be in the track root directory."
fi
if [[ -z $1 ]]; then
die "usage: $0 exercise_slug"
fi

author=
difficulty=

while getopts :a:d: opt; do
case ${opt} in
a) author=${OPTARG} ;;
d) difficulty=${OPTARG} ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))

[[ -n ${1} ]] || usage

slug=$1

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

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

bin/fetch-configlet
bin/configlet create --practice-exercise "${slug}"
bin/configlet create \
--author "${author}" \
--difficulty "${difficulty}" \
--practice-exercise "${slug}"

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

############################################################
cat <<'__LICENSE__' > "${exDir}/LICENSE"
Expand Down Expand Up @@ -106,7 +128,19 @@ __SPEC_WREN__

# throw the canonical data into the spec
specDir=${XDG_CACHE_DIR:-${HOME}/.cache}/exercism/configlet/problem-specifications
cat "${specDir}/exercises/${slug}/canonical-data.json" >> "${exDir}/${slug}.spec.wren"
canonData="${specDir}/exercises/${slug}/canonical-data.json"
if [[ -f "${canonData}" ]]; then
{
echo
echo "// canonical data"
cat "${canonData}"
} >> "${exDir}/${slug}.spec.wren"
else
{
echo
echo "// no canonical data for ${slug}"
} >> "${exDir}/${slug}.spec.wren"
fi

############################################################
sed -e "s/%{slug}/${slug}/g" \
Expand All @@ -115,27 +149,3 @@ class %{PascalSlug} {
// implement me!
}
__PROOF_WREN__

############################################################

read -p "What's your GitHub handle? "
if [[ -n ${REPLY} ]]; then
conf=${exDir}/.meta/config.json
tmp=$(mktemp)
jq --arg who "${REPLY}" '.authors += [$who]' "${conf}" > "${tmp}" \
&& mv "${tmp}" "${conf}"
fi

read -p "What's the (likely) difficult for ${slug}? "
if [[ -n ${REPLY} && ${REPLY} == +([0-9]) ]]; then
tmp=$(mktemp)
jq --argjson diff "${REPLY}" --arg slug "${slug}" '
.exercises.practice |= map(
if (.slug == $slug)
then .difficulty = $diff
else .
end
)
' config.json > "${tmp}" \
&& mv "${tmp}" config.json
fi