-
Notifications
You must be signed in to change notification settings - Fork 977
Expand file tree
/
Copy pathmake-site.sh
More file actions
executable file
·301 lines (248 loc) · 7.63 KB
/
Copy pathmake-site.sh
File metadata and controls
executable file
·301 lines (248 loc) · 7.63 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/bin/bash
# cSpell:ignore autoprefixer docsy postcss themesdir github oneline
set -eo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEPS="autoprefixer postcss-cli"
DOCSY_REPO_DEFAULT="google/docsy"
DOCSY_REPO=$DOCSY_REPO_DEFAULT
DOCSY_VERS=""
DOCSY_SRC="NPM"
FORCE_DELETE=false
: "${HUGO:=npx hugo}"
SITE_NAME="test-site"
THEMESDIR="node_modules"
VERBOSE=1
OUTPUT_REDIRECT="" # set to non-empty when -q is used
# Parsed HUGO command (supports: HUGO="npx --yes hugo-extended" etc.)
HUGO_CMD=()
function _usage() {
cat <<EOS
Usage: $(basename "$0") [options]
Creates a Docsy-themed site under SITE_NAME using the Hugo new command.
Docsy is fetched as an NPM package from $DOCSY_REPO in GitHub,
unless the -l or -s HUGO_MOD flags are used.
-f Force delete SITE_NAME if it exists before recreating it
-h Output this usage info
-l PATH Use local Docsy from PATH. Default: '$THEMESDIR'
-n SITE_NAME Name of directory to create for the Hugo generated site.
Default: '$SITE_NAME'
-q Run a bit more quietly.
-r REPO GitHub org+repo to fetch Docsy from.
Format: GITHUB_USER/DOCSY_REPO. Default: $DOCSY_REPO_DEFAULT
-s MOD_OR_PKG Docsy source: from a Hugo module or NPM package named '$DOCSY_REPO', where
MOD_OR_PKG is NPM or HUGO_MODULE (HUGO for short). Default: $DOCSY_SRC
-v VERS Docsy Hugo module or NPM package version. Default: '$DOCSY_VERS'.
Examples for Hugo modules: v1.1.1, some-branch-name
Examples for NPM: semver:1.1.1, some-branch-name
EOS
}
function usage() {
local status=${1:-0}
_usage 1>&2
exit "$status"
}
function init_hugo_cmd() {
# Split $HUGO safely into an argv array (no eval).
# Example: HUGO="npx --yes hugo-extended" -> ["npx","--yes","hugo-extended"]
read -r -a HUGO_CMD <<<"$HUGO"
if [[ ${#HUGO_CMD[@]} -eq 0 ]]; then
echo "[ERROR] HUGO command is empty"
exit 1
fi
}
function run_hugo() {
if [[ -n "$OUTPUT_REDIRECT" ]]; then
"${HUGO_CMD[@]}" "$@" >/dev/null 2>&1
else
"${HUGO_CMD[@]}" "$@"
fi
}
function validate_repo() {
# Conservative org/repo pattern
if [[ ! "$DOCSY_REPO" =~ ^[A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*$ ]]; then
echo "[ERROR] Invalid -r / DOCSY_REPO value: '$DOCSY_REPO'"
echo "[ERROR] Expected format: ORG/REPO using [A-Za-z0-9_.-]"
exit 1
fi
}
function validate_vers_hugo() {
# Allow typical git ref-ish values (tag/branch/commit-ish) but block metacharacters/whitespace.
# Note: we intentionally disallow '@' because the script injects it as a separator.
if [[ -z "$DOCSY_VERS" ]]; then
return 0
fi
if [[ "$DOCSY_VERS" == -* ]]; then
echo "[ERROR] Invalid -v / DOCSY_VERS: must not start with '-'"
exit 1
fi
if [[ ! "$DOCSY_VERS" =~ ^[A-Za-z0-9][A-Za-z0-9._/-]{0,99}$ ]]; then
echo "[ERROR] Invalid -v / DOCSY_VERS value: '$DOCSY_VERS'"
echo "[ERROR] Allowed pattern (HUGO mode): ^[A-Za-z0-9][A-Za-z0-9._/-]{0,99}$"
exit 1
fi
}
function validate_vers_npm() {
# NPM example includes "semver:1.1.1" so allow ':' and '+'
if [[ -z "$DOCSY_VERS" ]]; then
return 0
fi
if [[ "$DOCSY_VERS" == -* ]]; then
echo "[ERROR] Invalid -v / DOCSY_VERS: must not start with '-'"
exit 1
fi
if [[ ! "$DOCSY_VERS" =~ ^[A-Za-z0-9][A-Za-z0-9._/+:-]{0,99}$ ]]; then
echo "[ERROR] Invalid -v / DOCSY_VERS value: '$DOCSY_VERS'"
echo "[ERROR] Allowed pattern (NPM mode): ^[A-Za-z0-9][A-Za-z0-9._/+:-]{0,99}$"
exit 1
fi
}
function process_CLI_args() {
while getopts ":fhl:n:qr:s:v:" opt; do
case $opt in
f)
FORCE_DELETE=true
;;
h)
usage
;;
l)
DOCSY_SRC="LOCAL"
THEMESDIR="$OPTARG"
;;
n)
SITE_NAME="$OPTARG"
;;
q)
VERBOSE=""
OUTPUT_REDIRECT="1"
;;
r)
DOCSY_REPO="$OPTARG"
;;
s)
DOCSY_SRC=$(echo "$OPTARG" | tr '[:lower:]' '[:upper:]')
if [[ "$DOCSY_SRC" != "NPM" && "$DOCSY_SRC" != HUGO* ]]; then
echo "ERROR: invalid argument to -s flag: $OPTARG"
usage 1
fi
;;
v)
DOCSY_VERS="$OPTARG"
;;
\?)
echo "ERROR: unrecognized flag: -$OPTARG"
usage 1
;;
esac
done
shift $((OPTIND - 1))
if [[ "$#" -gt 0 ]]; then
echo "ERROR: extra argument(s): $*" >&2
usage 1
fi
}
function create_site_directory() {
if [[ -e "$SITE_NAME" ]]; then
if [[ "$FORCE_DELETE" = true ]]; then
echo "[INFO] Directory '$SITE_NAME' already exists. Deleting it as requested (-f)."
([[ $VERBOSE ]] && set -x; rm -rf "$SITE_NAME")
else
echo "[ERROR] Directory '$SITE_NAME' already exists. Remove it or use -f to force delete."
exit 1
fi
fi
}
function _npm_install() {
npm init -y >/dev/null
if [[ -n "$OUTPUT_REDIRECT" ]]; then
npm install --omit dev --save $DEPS >/dev/null 2>&1
else
npm install --omit dev --save $DEPS
fi
}
function set_up_and_cd_into_site() {
run_hugo new site --format yaml --quiet "$SITE_NAME"
cd "$SITE_NAME"
_npm_install
if [[ "$DOCSY_SRC" == HUGO* ]]; then
_set_up_site_using_hugo_modules
else
echo "theme: docsy/theme" >> hugo.yaml
echo "themesDir: $THEMESDIR" >> hugo.yaml
fi
}
function _set_up_site_using_hugo_modules() {
local user_name
user_name=$(whoami)
validate_repo
validate_vers_hugo
local HUGO_MOD_WITH_VERS
# Docsy theme lives in the `theme/` subfolder of the Docsy repo.
HUGO_MOD_WITH_VERS="$DOCSY_REPO/theme"
if [[ -n "$DOCSY_VERS" ]]; then
HUGO_MOD_WITH_VERS+="@$DOCSY_VERS"
fi
echo "[INFO] Getting Docsy as Hugo module $HUGO_MOD_WITH_VERS"
run_hugo mod init "github.com/$user_name/$SITE_NAME"
if [[ "$DOCSY_REPO" == "$DOCSY_REPO_DEFAULT" ]]; then
run_hugo mod get "github.com/$HUGO_MOD_WITH_VERS"
else
echo "[INFO] Fetch Docsy GitHub repo '$DOCSY_REPO' @ '$DOCSY_VERS'"
mkdir -p tmp
local DEPTH=10
local SWITCH_NEEDED=""
if [[ -n "$DOCSY_VERS" ]]; then
if ! git clone --depth="$DEPTH" -b "$DOCSY_VERS" "https://github.com/$DOCSY_REPO" tmp/docsy; then
SWITCH_NEEDED="1"
git clone --depth="$DEPTH" "https://github.com/$DOCSY_REPO" tmp/docsy
fi
else
git clone --depth="$DEPTH" "https://github.com/$DOCSY_REPO" tmp/docsy
fi
(
cd tmp/docsy
git log --oneline -"$DEPTH"
if [[ -n "$SWITCH_NEEDED" && -n "$DOCSY_VERS" ]]; then
git switch --detach "$DOCSY_VERS"
fi
)
echo "replace github.com/$DOCSY_REPO_DEFAULT/theme => ./tmp/docsy/theme" >> go.mod
run_hugo mod get "github.com/$DOCSY_REPO_DEFAULT/theme"
fi
echo "module: {proxy: direct, hugoVersion: {extended: true}, imports: [{path: github.com/$DOCSY_REPO_DEFAULT/theme, disable: false}]}" >> hugo.yaml
}
function main() {
process_CLI_args "$@"
init_hugo_cmd
if [[ "$DOCSY_SRC" == "NPM" ]]; then
validate_repo
validate_vers_npm
NPM_PKG=$DOCSY_REPO
if [[ -n "$DOCSY_VERS" ]]; then
NPM_PKG+="#$DOCSY_VERS"
fi
echo "[INFO] Getting Docsy as NPM package '$NPM_PKG'"
DEPS+=" $NPM_PKG"
elif [[ "$DOCSY_SRC" == "LOCAL" ]]; then
echo "[INFO] Getting Docsy through a local directory '$THEMESDIR'"
else
validate_repo
validate_vers_hugo
fi
create_site_directory
[[ $VERBOSE ]] && set -x
set_up_and_cd_into_site
run_hugo # Generate site
[[ $VERBOSE ]] && set +x
cd ..
echo "[INFO] '$SITE_NAME' successfully created, set up, and built."
if [[ $VERBOSE ]]; then
echo "[INFO] Here are the site files:"
echo
set -x
ls -l "$SITE_NAME"
echo
ls -l "$SITE_NAME/public"
fi
}
main "$@"