forked from xwmx/nb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Baskfile
163 lines (139 loc) · 3.53 KB
/
Baskfile
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
#!/usr/bin/env bash
###############################################################################
# Baskfile
#
# Run With Bask: https://github.com/xwmx/bask
###############################################################################
export LANG=en_us.UTF-8
export LC_ALL=en_us.UTF-8
###############################################################################
# Helpers
###############################################################################
# _sed_i()
#
# `sed -i` takes an extension on macOS, but that extension can cause errors in
# GNU `sed`.
#
# https://stackoverflow.com/q/43171648
# https://stackoverflow.com/a/16746032
_sed_i() {
if sed --help >/dev/null 2>&1
then # GNU
sed -i "${@}"
else # BSD
sed -i '' "${@}"
fi
}
###############################################################################
# Tasks
###############################################################################
# g:web ################################################################# g:web
desc "g:web" <<HEREDOC
Usage:
bask g:web
Description:
Regenerate index.markdown from README.md combined with existing front matter.
HEREDOC
g:web() {
local _front_matter_lines=()
local _in_front_matter=0
local _target_file="./docs/index.markdown"
[[ ! -f "${_target_file:?}" ]] &&
_exit_1 printf "./docs/index.markdown not found.\\n"
while IFS= read -r __line || [[ -n "${__line}" ]]
do
if [[ "${__line}" =~ ^---$ ]]
then
if ((_in_front_matter))
then
_in_front_matter=0
else
_in_front_matter=1
fi
elif ((_in_front_matter))
then
_front_matter_lines+=("${__line}")
elif [[ -n "${_front_matter_lines[*]:-}" ]]
then
break
fi
done < "${_target_file:?}"
if [[ -z "${_front_matter_lines[*]:?}" ]]
then
_exit_1 printf "Front matter not found.\\n"
fi
cat << HEREDOC > "${_target_file:?}"
---
${_front_matter_lines[*]}
---
$(cat ./README.md)
HEREDOC
printf "Rebuilt: %s\\n" "${_target_file:?}"
}
# npm:publish ##################################################### npm:publish
desc "npm:publish" <<HEREDOC
Usage:
bask npm:publish
Description"
npm mangles HTML h1 elements, so convert to a normal markdown h1, publish
to npm, then revert the heading back to the HTML h1.
HEREDOC
npm:publish() {
local _h1="<h1 align=\"center\" id=\"nb\"><code>nb<\/code><\/h1>"
printf "Publishing to npm.\\n"
while true
do
read -r -p "Proceed? [y/N] " __yn
case ${__yn} in
[Yy]*)
break
;;
*)
printf "Exiting...\\n"
exit 0
;;
esac
done
_sed_i -e "s/^${_h1}$/# \`nb\`/g" "README.md"
npm publish
_sed_i -e "s/^# \`nb\`$/${_h1}/g" "./README.md"
}
# v ######################################################################### v
desc "v" <<HEREDOC
Usage:
bask v
bask v down
bask v halt
bask v ssh
bask v up
Description:
Shortcuts for Vagrant that can be called from any subdirectory.
HEREDOC
v() {
local _baskfile_path="${BASH_SOURCE[0]}"
local _root_dir="${_baskfile_path%/Baskfile}"
local _vagrant_dir="${_root_dir}/etc"
case "${1:-}" in
d*) # destroy / down
(cd "${_vagrant_dir}" && vagrant destroy)
;;
h*) # halt
(cd "${_vagrant_dir}" && vagrant halt)
;;
s*) # ssh
(cd "${_vagrant_dir}" && vagrant ssh)
;;
u*) # up
(cd "${_vagrant_dir}" && vagrant up)
;;
*)
(
cd "${_vagrant_dir}" && {
vagrant ssh || {
vagrant up && vagrant ssh
}
}
)
;;
esac
}