From 6f12d24bc9a6e366c41e3154e9cefd2b149fb861 Mon Sep 17 00:00:00 2001 From: flu0r1ne Date: Sun, 5 Sep 2021 18:35:23 -0500 Subject: Add planr-cfg bash script --- planr-cfg | 245 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 245 insertions(+) create mode 100644 planr-cfg diff --git a/planr-cfg b/planr-cfg new file mode 100644 index 0000000..c5d3f1e --- /dev/null +++ b/planr-cfg @@ -0,0 +1,245 @@ +#!/bin/bash + +# planr-cfg +# --------- +# +# This is major WIP +# Intended to make configuring planr much easier + +set -e -o pipefail + +PROGRAM="${0##*/}" + +PLANR_DIR=planr +SRC_DIR=src + +PROJECT_CONFIG_FILE="config.toml" + +die () { + if [[ $# -eq 0 ]]; then + echo -n "$PROGRAM: " 1>&2 + cat <&0 1>&2 + elif [[ $# -eq 1 ]]; then + echo "$PROGRAM: $1" 1>&2 + fi + + exit 1 +} + +err () { + printf "$1" 1>&2 +} + +create_project_config() { +cat > "$1" <<_EOF +version="$(planr version)" +_EOF +} + +make_directory_structure() { + shift + + mkdir "$1" + cd "$1" + + mkdir "${PLANR_DIR}" + mkdir "${PLANR_DIR}/rubric" + mkdir "${PLANR_DIR}/tests" + + mkdir "${SRC_DIR}" + + local project_config + project_config="${PLANR_DIR}/${PROJECT_CONFIG_FILE}" + + create_project_config "${project_config}" +} + +config_dir() { + echo "$(planr config configdir)" +} + +src_dir() { + echo "$(planr config srcdir)" +} + +rubric_dir() { + echo "$(config_dir)/rubric" +} + +test_dir() { + echo "$(config_dir)/tests" +} + +uppercase_str() { + printf '%s\n' "$1" | awk '{ print toupper($0) }' +} + +create_with_subdirs() { + local dir + dir="$(dirname $1)" + + mkdir -p "$dir" +} + +cpp_unitdef() { + local testfile + local name + + testfile="$1" + name="$2" + + cat <<_EOF +title="Example Title" +description=""" +Example description +""" + +[adapters.gtest] +name="$name" +testfile="$testfile" +_EOF +} + +bash_unitdef() { + local testfile + local name + + testfile="$1" + name="$2" + + cat <<_EOF +title="Example Title" +description=""" +Example description +""" + +[adapters.bash] +name="$name" +testfile="$testfile" +_EOF +} + +cpp_boilerplate() { + local name + name="$1" + + cat <<_EOF +#include "gtest/gtest.h" + +TEST(SUITE, $name) { + +} +_EOF +} + +bash_boilerplate() { +cat <<_EOF +#!/bin/bash + +exit 0 +_EOF +} + +add_unit() { + local fmt + local edit + + fmt="cpp" + + shift + + local opt + while [[ $# -gt 1 ]]; do + case "$1" in + --fmt|-f) + opt="$1" + + shift + + if [[ $# -eq 0 ]]; then + die "$opt requires an option" + fi + + fmt="$1" + + shift + ;; + --edit|-e) edit=1; shift; ;; + *) die "$(print_usage)" ;; + esac + done + + local pathspec + pathspec="$1" + + local unitdef + local testfile + + local name + name="$(basename $pathspec)" + name="$(uppercase_str $name)" + + unitdef="${pathspec}.toml" + + testfile="$(test_dir)/$pathspec" + + + if [[ "$fmt" = "cpp" ]]; then + testfile="${testfile}.cpp" + elif [[ "$fmt" = "bash" ]]; then + testfile="${testfile}.bash" + else + die "Format not recognized \"${fmt}\"" + fi + + create_with_subdirs "$unitdef" + create_with_subdirs "$testfile" + + local unitdef_abs + unitdef_abs="$(rubric_dir)/${unitdef}" + + if [[ "$fmt" = "cpp" ]]; then + cpp_unitdef "$unitdef" "$name" >"$unitdef_abs" + cpp_boilerplate "$name" >"$testfile" + elif [[ "$fmt" = "bash" ]]; then + bash_unitdef "$unitdef" "$name" >"$unitdef_abs" + bash_boilerplate "$name" >"$testfile" + fi + + if [[ -n "$edit" ]]; then + [[ -z "$EDITOR" ]] && die "Cannot find editor" + + "$EDITOR" "$unitdef_abs" + "$EDITOR" "$testfile" + fi +} + +print_usage() { +cat <<_EOF +${PROGRAM} + new-project + add-unit +_EOF +} + +planr-cfg() { + + if [[ $# -eq 0 ]]; then + die "$(print_usage)" + fi + + if [[ "$1" = "new-project" ]]; then + make_directory_structure "${@:1}" + exit 0 + fi + + if [[ "$1" = "add-unit" ]]; then + add_unit "${@:1}" + exit 0 + fi + + err "Command not reconized \"$1\"\n" + die "$(print_usage)" +} + +planr-cfg "$@" -- cgit v1.2.3