-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·241 lines (218 loc) · 6.25 KB
/
run
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
#!/usr/bin/env bash
###############################################################################
# run is a utility for organizing your project’s CLI commands.
# https://run.jotaen.net
###############################################################################
# MIT License
# Copyright (c) 2022 Jan Heuermann, https://www.jotaen.net
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
###############################################################################
# Runtime configuration.
# These variables can be overriden while parsing the flags.
TASK_FILE='./run.sh'
ACTION=(run::action-list)
# Regex patterns for parsing the task file.
COMMENT_PATTERN='^#{1,}[[:blank:]]?(.*)$'
TASK_NAME_PATTERN='[a-zA-Z]+[a-zA-Z0-9:_-]*'
TASK_DEF_PATTERN_1='^[[:blank:]]*run::('"${TASK_NAME_PATTERN}"')[[:blank:]]*\([[:blank:]]*\)'
TASK_DEF_PATTERN_2='^[[:blank:]]*function[[:blank:]]*run::('"${TASK_NAME_PATTERN}"')'
# The --help procedure.
run::action-help() {
echo 'Run tasks from a run.sh file.'
echo
echo 'Usage: run [OPTION] TASK [TASK_ARGS...]'
echo
echo 'Options:'
echo ' -f, --file Specify the task file (default: ./run.sh)'
echo ' -i, --info task Show task description'
echo ' -l, --list List all available tasks'
echo ' -h, --help Print this help'
echo ' --version Print version of this command'
}
# The --version procedure.
run::action-version() {
echo 'Version 2.0'
echo 'https://run.jotaen.net'
}
# The --list procedure.
run::action-list() {
local title_candidate=""
local tasks=() # All encountered task names.
local titles=() # All encountered titles. (Same arity as $tasks.)
local longest_task_name=0 # Needed for aligning the output.
# Parse the task file.
while IFS= read -r line; do
# Process comment block.
if [[ "${line}" =~ $COMMENT_PATTERN ]]; then
if [[ -z "${title_candidate}" ]]; then
local comment_text="${BASH_REMATCH[1]}"
title_candidate="${comment_text}"
fi
continue
fi
# After a comment block, there must be the task definition.
# Allow both definition styles: `foo() {...` and `function foo {`.
if [[ "${line}" =~ $TASK_DEF_PATTERN_1 || "${line}" =~ $TASK_DEF_PATTERN_2 ]]; then
local task="${BASH_REMATCH[1]}"
tasks+=("${task}")
titles+=("${title_candidate}")
if [[ ${#task} -gt $longest_task_name ]]; then
longest_task_name=${#task}
fi
fi
# Reset parser for next iteration.
title_candidate=""
done < "${TASK_FILE}"
# Print out the gathered information.
local column_offset=3
for (( i=0; i<${#tasks[@]}; i++ )); do
local padding=0
if [[ "${titles[$i]}" != "" ]]; then
padding=$((longest_task_name+column_offset))
fi
printf "%-${padding}s%s\n" "${tasks[$i]}" "${titles[$i]}"
done
}
# The --info procedure.
run::action-info() {
local requested_task="$1"
local comment_block_candidate=() # All comment lines for the task.
# Abort if no task name was specified.
if [[ "${requested_task}" == '' ]]; then
echo 'No task specified' >&2
exit 1
fi
# Parse the task file.
while IFS= read -r line; do
if [[ "${line}" =~ $COMMENT_PATTERN ]]; then
comment_block_candidate+=("${BASH_REMATCH[1]}")
continue
fi
if [[ "${line}" =~ $TASK_DEF_PATTERN_1 || "${line}" =~ $TASK_DEF_PATTERN_2 ]]; then
if [[ "${BASH_REMATCH[1]}" == "${requested_task}" ]]; then
# Print out the gathered comment lines.
for i in "${comment_block_candidate[@]}"; do
echo "${i}"
done
exit
fi
fi
comment_block_candidate=()
done < "${TASK_FILE}"
echo "No such task: ${requested_task}" >&2
exit 1
}
# The procedure when invoking a task.
run::action-execute() {
readonly task_name="$1"
readonly task_identifier="run::${task_name}"
readonly input=( "${@:2}" )
readonly task_file="${TASK_FILE}"
# Check shebang of task file.
shebang=$(head -n 1 "${task_file}")
if [[ "${shebang}" == '#!'* &&
"${shebang}" != *'bash'* ]]; then
echo "Unsupported shebang." >&2
exit 1
fi
# Assemble final script.
run_sh=''
# Include task file.
run_sh+='
source '"${task_file}"'
'
# Add check, whether subcommand is defined.
# shellcheck disable=SC2016
run_sh+='
if [[ "$(type -t '"${task_identifier}"')" != "function" ]]; then
echo "No such task: '"${task_name}"'" >&2
exit 1
fi
'
# Add task invocation.
run_sh+='
'"${task_identifier} ${input[*]}"'
'
# Run the final script.
bash -c "$run_sh"
}
# Parse CLI arguments.
while [[ $# -gt 0 ]]; do
case $1 in
--help|-h)
run::action-help
exit
;;
--version)
run::action-version
exit
;;
--list|--ls|-l)
ACTION=( run::action-list )
shift
continue
;;
--info=*|-i=*)
ACTION=( run::action-info "${1#*=}" )
shift
continue
;;
--info|-i)
ACTION=( run::action-info "$2" )
shift
shift
continue
;;
--file=*|-f=*)
TASK_FILE="${1#*=}"
shift
continue
;;
--file|-f)
TASK_FILE="$2"
shift
shift
continue
;;
-*)
echo "Unknown option $1" >&2
exit 1
;;
*)
ACTION=( run::action-execute "$1" "${@:2}" )
break
;;
esac
done
# If task file is given, check it.
if [[ -n "${TASK_FILE}" ]]; then
# Check that it’s a regular file.
if [[ -d "${TASK_FILE}" ]]; then
echo "Not a file: ${TASK_FILE}" >&2
exit 2
fi
# Check that it exists.
if [[ ! -f "${TASK_FILE}" ]]; then
echo "No such file: ${TASK_FILE}" >&2
exit 2
fi
fi
# Execute action.
"${ACTION[@]}"