Skip to content

Instantly share code, notes, and snippets.

@neenjaw
Last active February 17, 2021 00:37
Show Gist options
  • Save neenjaw/3ffa62e15ddc79025ffd3e068ad7278e to your computer and use it in GitHub Desktop.
Save neenjaw/3ffa62e15ddc79025ffd3e068ad7278e to your computer and use it in GitHub Desktop.
Bootstrap concept exercise
#!/usr/bin/env bash
set -euo pipefail
concept_exercise_path="/exercises/concept"
if [[ "${#}" -eq 1 ]]; then
language="ruby"
slug="${1:-}"
elif [[ "${#}" -eq 2 ]]; then
language="${1:-}"
slug="${2:-}"
fi
if [[ "${language}" == "ruby" ]]; then
extension="rb"
else
extension="unknown"
fi
config_file="config.json"
exercise_path="$(pwd)${concept_exercise_path}/${slug}"
docs_dir="${exercise_path}/.docs"
meta_dir="${exercise_path}/.meta"
function ruby() {
mkdir -p "${exercise_path}"
docs
meta
todo_touch "${meta_dir}/exemplar.${extension}"
todo_touch "${exercise_path}/${slug}.${extension}"
todo_touch "${exercise_path}/${slug}_test.${extension}"
insert_config
}
function docs() {
mkdir -p "${docs_dir}"
todo_touch "${docs_dir}/hints.md"
todo_touch "${docs_dir}/instructions.md"
todo_touch "${docs_dir}/introduction.md"
}
function meta() {
mkdir -p "${meta_dir}"
todo_touch "${meta_dir}/design.md"
config_touch "${meta_dir}/config.json"
}
function todo_touch() {
echo "# TODO" > "${1}"
}
function config_touch() {
user_json=$(jo github_username=placeholder exercism_username=placeholder)
files_json=$(jo solution[]="solution.${extension}" test[]="solution_test.${extension}" examplar[]=".meta/exemplar.${extension}")
jq -n --argjson user "${user_json}" --argjson files "${files_json}" \
'{"authors": $ARGS.named["user"], "contributors": $ARGS.named["user"], "language_versions": "placeholder", "files": $ARGS.named["files"]}' > "${1}"
}
function insert_config() {
uuid=$(uuidgen)
name=$(echo "${slug}" | tr '-' ' ')
name_split=(${name})
name_proper="${name_split[@]^}"
config_tmpfile=$(mktemp /tmp/config.json.XXXXXX)
entry=$(jo \
slug="${slug}" \
name="${name_proper}" \
uuid="${uuid}" \
practices=[] \
prerequisites=[] \
difficulty=1 \
status="wip")
jq --argjson entry "${entry}" '.exercises.concept[.exercises.concept | length] |= .+ $ARGS.named["entry"]' < "${config_file}" > config_tmpfile
mv config_tmpfile "${config_file}"
npx prettier --write "${config_file}"
}
function installed {
cmd=$(command -v "${1}")
[[ -n "${cmd}" ]] && [[ -f "${cmd}" ]]
return ${?}
}
function die {
>&2 echo "❌ Fatal: ${@}"
exit 1
}
deps=(jo jq uuidgen npx)
for dep in "${deps[@]}"; do
installed "${dep}" || die "Missing '${dep}'"
done
if [[ -z "${language}" ]]; then
die "Missing language"
fi
if [[ -z "${slug}" ]]; then
die "Missing exercise slug"
fi
if [[ ! -f "${config_file}" ]]; then
die "run this only in the root track repo"
fi
$language; exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment