blob: c5d3f1e2eb9c9becfd7dee0540757324e5c7df42 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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 "$@"
|