-
-
Notifications
You must be signed in to change notification settings - Fork 813
/
Copy pathpublish
executable file
·276 lines (228 loc) · 6.59 KB
/
publish
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
#!/usr/bin/env bash
#
# @license Apache-2.0
#
# Copyright (c) 2017 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Builds and publishes API documentation.
#
# TODO:
# 1. Add support for specifying the project branch to build.
# 2. Generate log file similar to CircleCI build script.
# 3. Add support for specifying a commit message.
# 4. Add support for specifying a documentation branch.
# 5. Add support for specifying a documentation remote repository name.
# 6. Make determining a temporary build directory more robust. Ideally, we would use the `tmpdir` utility to build in the "proper" tmp directory.
# 7. Print total time elapsed for entire build sequence.
#
# VARIABLES #
# Project repository URL:
project_repo='https://github.com/stdlib-js/stdlib.git'
# The branch or release to checkout and from which to build project documentation:
project_checkout='develop'
# Documentation repository URL:
docs_repo='https://github.com/stdlib-js/www.git'
# Documentation repository name:
docs_repo_name='origin'
# The documentation repository remote branch to which to commit project documentation:
docs_checkout='master'
# Commit message when files are committed to documentation repository:
commit_message='Update documentation'
# Cache the working directory:
curr_dir="$PWD"
# Determine root directory:
root_dir="$(git rev-parse --show-toplevel)"
# Temporary directory for building documentation:
tmp_dir="${root_dir}/../tmp"
# Directory into which to clone the project repository:
project_dir="${tmp_dir}/project"
# Project documentation build directory:
project_build_dir="${project_dir}/build"
# Define the path to the project source library:
source_dir="${project_dir}/lib/node_modules"
# Define the path to the project utility for building API documentation:
build_docs="${source_dir}/@stdlib/_tools/docs/api/build-docs/bin/cli"
# Directory into which to clone the documentation repository:
docs_dir="${tmp_dir}/docs"
# FUNCTIONS #
# Defines an error handler.
#
# $1 - error status
on_error() {
echo 'ERROR: An error was encountered during execution.' >&2
cleanup
exit "$1"
}
# Runs clean-up tasks.
cleanup() {
cd "${curr_dir}"
# remove_dir "${tmp_dir}"
}
# Creates a directory.
#
# $1 - directory path
create_dir() {
mkdir -p "$1"
if [[ "$?" -ne 0 ]]; then
echo "Unable to create directory: $1." >&2
return 1
fi
echo "Created directory: $1." >&2
return 0
}
# Removes a directory.
#
# $1 - directory path
remove_dir() {
rm -rf "$1"
if [[ "$?" -ne 0 ]]; then
echo "Unable to remove directory: $1." >&2
return 1
fi
echo "Removed directory: $1." >&2
return 0
}
# Moves a directory.
#
# $1 - directory to move
# $2 - output directory
move_dir() {
mv "$1" "$2"
if [[ "$?" -ne 0 ]]; then
echo "Unable to move directory $1 to $2." >&2
return 1
fi
echo "Moved directory $1 to $2." >&2
return 0
}
# Shallow clones a repository.
#
# $1 - repository URL
# $2 - output directory
shallow_clone() {
git clone --depth=1 "$1" "$2"
if [[ "$?" -ne 0 ]]; then
echo "Unable to clone repository: $1." >&2
return 1
fi
echo "Cloned repository $1 to $2." >&2
return 0
}
# Installs node module dependencies.
install_node_deps() {
npm install
if [[ "$?" -ne 0 ]]; then
echo "Unable to install node module dependencies." >&2
return 1
fi
echo "Successfully installed node module dependencies." >&2
return 0
}
# Prints a success message.
print_success() {
echo 'Success!' >&2
}
# Main execution sequence:
main() {
echo 'Creating temporary build directory...' >&2
create_dir "${tmp_dir}"
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
echo 'Navigating to temporary build directory...' >&2
cd "${tmp_dir}"
echo 'Cloning project repository...' >&2
shallow_clone "${project_repo}" "${project_dir}"
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
echo 'Navigating to project repository...' >&2
cd "${project_dir}"
echo "Switching to ${project_checkout}..." >&2
git checkout "${project_checkout}"
if [[ "$?" -ne 0 ]]; then
echo "Unable to switch to ${project_checkout} in the project repository." >&2
on_error 1
fi
echo 'Returning to temporary build directory...' >&2
cd "${tmp_dir}"
echo 'Cloning documentation repository...' >&2
shallow_clone "${docs_repo}" "${docs_dir}"
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
echo 'Navigating to documentation repository...' >&2
cd "${docs_dir}"
echo "Switching to ${docs_checkout}..." >&2
git checkout "${docs_checkout}"
if [[ "$?" -ne 0 ]]; then
echo "Unable to switch to ${docs_checkout} in the documentation repository." >&2
on_error 1
fi
echo 'Navigating to project repository...' >&2
cd "${project_dir}"
echo 'Installing project dependencies...' >&2
install_node_deps
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
echo 'Building project documentation...' >&2
NODE_PATH="${source_dir}" DEBUG='*' "${build_docs}"
if [[ "$?" -ne 0 ]]; then
echo 'Failed to successfully build project documentation.' >&2
on_error 1
fi
echo 'Returning to temporary build directory...' >&2
cd "${tmp_dir}"
echo "If the documentation repository already has ${project_checkout} documentation, remove the existing documentation..." >&2
remove_dir "${docs_dir}/public/${project_checkout}"
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
echo "Moving ${project_checkout} documentation to documentation repository..." >&2
move_dir "${project_build_dir}/www/public/${project_checkout}" "${docs_dir}/public/${project_checkout}"
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
echo 'Navigating to documentation repository...' >&2
cd "${docs_dir}"
echo 'Pulling latest changes...' >&2
git pull "${docs_repo_name}" "${docs_checkout}"
if [[ "$?" -ne 0 ]]; then
echo 'Unable to pull down latest changes.' >&2
on_error 1
fi
echo 'Committing files...' >&2
git add -A
if [[ "$?" -ne 0 ]]; then
echo 'Unable to add files.' >&2
on_error 1
fi
git commit -m "${commit_message}"
if [[ "$?" -ne 0 ]]; then
echo 'Unable to commit files.' >&2
on_error 1
fi
echo 'Pushing to remote repository...' >&2
git push "${docs_repo_name}" "${docs_checkout}"
if [[ "$?" -ne 0 ]]; then
echo 'Unable to push commits to remote.' >&2
on_error 1
fi
print_success
cleanup
exit 0
}
# Run main:
main