Skip to content

Commit 0d356eb

Browse files
committed
Refactor bare template args, add interpreter var
1 parent 9756ec4 commit 0d356eb

File tree

6 files changed

+63
-42
lines changed

6 files changed

+63
-42
lines changed

man/mkx.1

+13-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Only write a bare script with only the shebang line, without template.
1616
Show help screen.
1717
.TP
1818
.B \-i \fIINTERPRETER\fR
19-
Specify the interpreter to use in the shebang line.
19+
Specify the interpreter to use in the shebang line. Defaults to $fI/usr/bin/env bash\fR.
2020
.TP
2121
.B \-s
2222
Use /bin/sh as interpreter in the shebang line. Implies \-b.
@@ -37,17 +37,23 @@ The directory \fI$XDG_CONFIG_HOME/mkx/templates\fR if the environment variable \
3737
.TP
3838
The following variables are available:
3939
.TP
40-
.B \fIscript_name\fR
41-
The name of the script.
42-
.TP
43-
.B \fIscript_name_sc\fR
44-
The name of the script in lowercase.
45-
.TP
4640
.B \fIdate\fR
4741
The date when the script was created in format \fI%Y-%m-%d %H:%M:%S %Z\fR.
4842
.TP
43+
.B \fIinterpreter\fR
44+
The interpreter specified with the \fI-i\fR option or the default interpreter.
45+
.TP
46+
.B \fImkx_version\fR
47+
The version of mkx.
48+
.TP
4949
.B \fImkx_version\fR
5050
The version of mkx.
51+
.TP
52+
.B \fIscript_name\fR
53+
The name of the script.
54+
.TP
55+
.B \fIscript_name_sc\fR
56+
The name of the script in lowercase.
5157
.SH AUTHOR
5258
Written by Rémino Rem: <https://remino.net/>
5359
.SH "ISSUES"

mkx

+20-25
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ mkx_main() {
2424

2525
while getopts bhi:lst:v opt; do
2626
case $opt in
27-
b) BARE=1 ;;
27+
b) TEMPLATE_NAME='bare' ;;
2828
h) _help && return ;;
2929
i)
30-
BARE=1
30+
TEMPLATE_NAME='bare'
3131
INTERPRETER="$OPTARG"
3232
;;
3333
s)
34-
BARE=1
34+
TEMPLATE_NAME='bare'
3535
INTERPRETER="/bin/sh"
3636
;;
3737
t) TEMPLATE_NAME="$OPTARG" ;;
@@ -48,32 +48,20 @@ mkx_main() {
4848

4949
shift
5050

51-
if [ $BARE -eq 0 ]; then
52-
_template
53-
else
54-
_bare
55-
fi
51+
_template
5652

5753
chmod a+x "$NEW_SCRIPT"
5854
_echo "$NEW_SCRIPT"
5955
_exit
6056
}
6157

62-
_bare() {
63-
if [ ! -f "$NEW_SCRIPT" ]; then
64-
echo "#!$INTERPRETER" >"$NEW_SCRIPT"
65-
echo "" >>"$NEW_SCRIPT"
66-
fi
67-
68-
[ -f "$NEW_SCRIPT" ] && chmod a+x "$NEW_SCRIPT"
69-
}
70-
7158
_find_template() {
7259
template_name="$1"
7360

7461
for filepath in \
7562
"$MKX_TEMPLATES_DIR/$template_name.mustache" \
76-
"${XDG_CONFIG_HOME:-$HOME/.config}/mkx/templates/$template_name.mustache"; do
63+
"${XDG_CONFIG_HOME:-$HOME/.config}/mkx/templates/$template_name.mustache" \
64+
"$1"; do
7765
if [ -f "$filepath" ]; then
7866
echo "$filepath"
7967
return
@@ -94,6 +82,13 @@ _exit() {
9482
exit $exit_code
9583
}
9684

85+
_fatal() {
86+
exit_code="$1"
87+
shift
88+
_error "$@"
89+
_exit "$exit_code"
90+
}
91+
9792
_help() {
9893
cat <<USAGE
9994
$SCRIPT_NAME $VERSION
@@ -102,16 +97,17 @@ USAGE: $SCRIPT_NAME [-bhsvx] [-i <interpreter>] [-t <template>] <scriptfile>
10297
10398
Make new shell script executable file from template.
10499
100+
If the file exists, make it executable.
101+
105102
OPTIONS:
106103
107104
-b Only write a bare script with only the shebang line, without template.
108-
If output file already exists, make it executable.
109105
110106
-h Show this help screen.
111107
112-
-i Specify interpreter in shebang line. Implies -b.
108+
-i Specify interpreter in shebang line. Implies -b if -t is not specified.
113109
114-
-s Use /bin/sh as interpreter. Implies -b.
110+
-s Use /bin/sh as interpreter, like '-i /bin/sh'.
115111
116112
-t Specify template file. If not specified, uses 'default'.
117113
@@ -128,18 +124,17 @@ _invalid_opt() {
128124
}
129125

130126
_template() {
127+
interpreter="$INTERPRETER"
131128
new_script_name="$(basename "$NEW_SCRIPT")"
132129
new_script_name_sc="$(echo "$new_script_name" | sed 's/-/_/g')"
133130
template_file="$(_find_template "$TEMPLATE_NAME")"
134131

135132
if [ -z "$template_file" ]; then
136-
_error "Template file not found: $template_file"
137-
return $E_NO_TEMPLATE
133+
_fatal $E_NO_TEMPLATE "Template file not found: $TEMPLATE_NAME"
138134
fi
139135

140136
if [ -f "$NEW_SCRIPT" ]; then
141-
_error "File already exists: $NEW_SCRIPT"
142-
return $E_EXISTS
137+
_echo "Making file exectuable: $NEW_SCRIPT"
143138
fi
144139

145140
date="$(date +'%Y-%m-%d %H:%M:%S %Z')"

templates/bare.mustache

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!{{interpreter}}
2+

templates/default.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env bash
1+
#!{{interpreter}}
22
# {{script_name}}
33

44
# Generated using mkx {{mkx_version}}: https://github.com/remino/mkx

templates/main.mustache

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!{{interpreter}}
2+
3+
{{script_name_sc}}_main() {
4+
true
5+
}
6+
7+
{{script_name_sc}}_main "$@"

tests/mkx.bats

+20-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ teardown() {
77
}
88

99
@test "shows version" {
10-
local version="$( grep VERSION ./mkx | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+' )"
10+
local version="$(grep VERSION ./mkx | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+')"
1111

1212
run ./mkx -v
1313

@@ -28,8 +28,8 @@ teardown() {
2828
run ./mkx "$OUTPUT_FILE"
2929

3030
[ "$status" -eq 0 ]
31-
[ "$( head -n 1 "$OUTPUT_FILE" )" = "#!/usr/bin/env bash" ]
32-
[ "$( wc -l < "$OUTPUT_FILE" )" -gt 2 ]
31+
[ "$(head -n 1 "$OUTPUT_FILE")" = "#!/usr/bin/env bash" ]
32+
[ "$(wc -l <"$OUTPUT_FILE")" -gt 2 ]
3333
[ -x "$OUTPUT_FILE" ]
3434
[ -f "$OUTPUT_FILE" ]
3535
}
@@ -40,8 +40,8 @@ teardown() {
4040
run ./mkx -t default "$OUTPUT_FILE"
4141

4242
[ "$status" -eq 0 ]
43-
[ "$( head -n 1 "$OUTPUT_FILE" )" = "#!/usr/bin/env bash" ]
44-
[ "$( wc -l < "$OUTPUT_FILE" )" -gt 2 ]
43+
[ "$(head -n 1 "$OUTPUT_FILE")" = "#!/usr/bin/env bash" ]
44+
[ "$(wc -l <"$OUTPUT_FILE")" -gt 2 ]
4545
[ -x "$OUTPUT_FILE" ]
4646
[ -f "$OUTPUT_FILE" ]
4747
}
@@ -52,8 +52,8 @@ teardown() {
5252
run ./mkx -b "$OUTPUT_FILE"
5353

5454
[ "$status" -eq 0 ]
55-
[ "$( head -n 1 "$OUTPUT_FILE" )" = "#!/usr/bin/env bash" ]
56-
[ "$( wc -l < "$OUTPUT_FILE" )" -eq 2 ]
55+
[ "$(head -n 1 "$OUTPUT_FILE")" = "#!/usr/bin/env bash" ]
56+
[ "$(wc -l <"$OUTPUT_FILE")" -eq 2 ]
5757
[ -x "$OUTPUT_FILE" ]
5858
[ -f "$OUTPUT_FILE" ]
5959
}
@@ -64,8 +64,19 @@ teardown() {
6464
run ./mkx -s "$OUTPUT_FILE"
6565

6666
[ "$status" -eq 0 ]
67-
[ "$( head -n 1 "$OUTPUT_FILE" )" = "#!/bin/sh" ]
68-
[ "$( wc -l < "$OUTPUT_FILE" )" -eq 2 ]
67+
[ "$(head -n 1 "$OUTPUT_FILE")" = "#!/bin/sh" ]
68+
[ "$(wc -l <"$OUTPUT_FILE")" -eq 2 ]
6969
[ -x "$OUTPUT_FILE" ]
7070
[ -f "$OUTPUT_FILE" ]
7171
}
72+
73+
@test "make file exectuable if it already exists" {
74+
OUTPUT_FILE="$(mktemp)"
75+
rm -f "$OUTPUT_FILE"
76+
touch "$OUTPUT_FILE"
77+
chmod -w "$OUTPUT_FILE"
78+
run ./mkx "$OUTPUT_FILE"
79+
80+
[ "$status" -eq 0 ]
81+
[ -x "$OUTPUT_FILE" ]
82+
}

0 commit comments

Comments
 (0)