forked from google/docsy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake-site.sh
executable file
·192 lines (166 loc) · 5.02 KB
/
make-site.sh
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
#!/bin/bash
set -eo pipefail
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="" # Use along with VERBOSE
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 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="> /dev/null 2>&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
}
# Create site directory, checking if it exists first
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
npm install --omit dev --save $DEPS
}
function set_up_and_cd_into_site() {
$HUGO new site --format yaml --quiet "$SITE_NAME"
cd "$SITE_NAME"
eval _npm_install $OUTPUT_REDIRECT
if [[ "$DOCSY_SRC" == HUGO* ]]; then
_set_up_site_using_hugo_modules
else
echo "theme: docsy" >> hugo.yaml
echo "themesDir: $THEMESDIR" >> hugo.yaml
fi
}
function _set_up_site_using_hugo_modules() {
local user_name=$(whoami)
# : ${user_name:=$USER}
# : ${user_name:="me"}
HUGO_MOD_WITH_VERS=$DOCSY_REPO
if [[ -n $DOCSY_VERS ]]; then
HUGO_MOD_WITH_VERS+="@$DOCSY_VERS"
fi
echo "[INFO] Getting Docsy as Hugo module $HUGO_MOD_WITH_VERS"
eval "$HUGO mod init github.com/$user_name/$SITE_NAME" $OUTPUT_REDIRECT
if [[ "$DOCSY_REPO" == "$DOCSY_REPO_DEFAULT" ]]; then
eval "$HUGO mod get github.com/$HUGO_MOD_WITH_VERS" $OUTPUT_REDIRECT
else
echo "[INFO] Fetch Docsy GitHub repo '$DOCSY_REPO' @ '$DOCSY_VERS'"
mkdir tmp
BRANCH_SPEC=""
if [[ -n $DOCSY_VERS ]]; then
BRANCH_SPEC="-b $DOCSY_VERS"
fi
git clone --depth=1 https://github.com/$DOCSY_REPO $BRANCH_SPEC tmp/docsy
(cd tmp/docsy && git log -1)
echo "replace github.com/$DOCSY_REPO_DEFAULT => ./tmp/docsy" >> go.mod
eval "$HUGO mod get github.com/$DOCSY_REPO_DEFAULT" $OUTPUT_REDIRECT
fi
echo "module: {proxy: direct, hugoVersion: {extended: true}, imports: [{path: github.com/$DOCSY_REPO_DEFAULT, disable: false}]}" >> hugo.yaml
}
function main() {
process_CLI_args "$@"
create_site_directory
if [[ "$DOCSY_SRC" == "NPM" ]]; then
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"
fi
[[ $VERBOSE ]] && set -x
set_up_and_cd_into_site
eval $HUGO $OUTPUT_REDIRECT # 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 "$@"