-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy
executable file
·281 lines (245 loc) · 7.62 KB
/
deploy
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
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
#
# Copyright (c) 2018, 2020 Érik Martin-Dorel
#
# This script is contributed under the terms of the MIT license,
# cf. <https://spdx.org/licenses/MIT.html>
#
_version="1.0.5"
#
# The args parser was generated from:
#
# ARG_OPTIONAL_SINGLE([image],[],[Learn-OCaml Docker image],[ocamlsf/learn-ocaml:master])
# ARG_OPTIONAL_SINGLE([base-url],[],[Base URL of the website without trailing slash])
# ARG_POSITIONAL_SINGLE([repo],[Absolute path to Learn-OCaml repository],[$PWD/.build/learn-ocaml/demo-repository])
# ARG_HELP([Deploy Learn-OCaml statically])
# ARG_VERSION([echo "$0 version $_version"])
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.10.0 one line above ###
# Argbash is a bash code generator used to get arguments parsing right.
# Argbash is FREE SOFTWARE, see https://argbash.io for more info
die()
{
local _ret="${2:-1}"
test "${_PRINT_HELP:-no}" = yes && print_help >&2
echo "$1" >&2
exit "${_ret}"
}
begins_with_short_option()
{
local first_option all_short_options='hv'
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}
# THE DEFAULTS INITIALIZATION - POSITIONALS
_positionals=()
_arg_repo="$PWD/.build/learn-ocaml/demo-repository"
# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_image="ocamlsf/learn-ocaml:master"
_arg_base_url=
print_help()
{
printf '%s\n' "Deploy Learn-OCaml statically"
printf 'Usage: %s [--image <arg>] [--base-url <arg>] [-h|--help] [-v|--version] [<repo>]\n' "$0"
printf '\t%s\n' "<repo>: Absolute path to Learn-OCaml repository (default: '$PWD/.build/learn-ocaml/demo-repository')"
printf '\t%s\n' "--image: Learn-OCaml Docker image (default: 'ocamlsf/learn-ocaml:master')"
printf '\t%s\n' "--base-url: Base URL of the website without trailing slash (no default)"
printf '\t%s\n' "-h, --help: Prints help"
printf '\t%s\n' "-v, --version: Prints version"
}
parse_commandline()
{
_positionals_count=0
while test $# -gt 0
do
_key="$1"
case "$_key" in
--image)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_image="$2"
shift
;;
--image=*)
_arg_image="${_key##--image=}"
;;
--base-url)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_base_url="$2"
shift
;;
--base-url=*)
_arg_base_url="${_key##--base-url=}"
;;
-h|--help)
print_help
exit 0
;;
-h*)
print_help
exit 0
;;
-v|--version)
echo "$0 version $_version"
exit 0
;;
-v*)
echo "$0 version $_version"
exit 0
;;
*)
_last_positional="$1"
_positionals+=("$_last_positional")
_positionals_count=$((_positionals_count + 1))
;;
esac
shift
done
}
handle_passed_args_count()
{
test "${_positionals_count}" -le 1 || _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect between 0 and 1, but got ${_positionals_count} (the last one was: '${_last_positional}')." 1
}
assign_positional_args()
{
local _positional_name _shift_for=$1
_positional_names="_arg_repo "
shift "$_shift_for"
for _positional_name in ${_positional_names}
do
test $# -gt 0 || break
eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
shift
done
}
parse_commandline "$@"
handle_passed_args_count
assign_positional_args 1 "${_positionals[@]}"
# OTHER STUFF GENERATED BY Argbash
### END OF CODE GENERATED BY Argbash (sortof) ### ])
# [ <-- needed because of Argbash
get_url() {
# if origin = [email protected]:pfitaxel/pfitaxel-demo.git
# or origin = https://github.com/pfitaxel/pfitaxel-demo.git
# will return: https://pfitaxel.github.io/pfitaxel-demo
local remote
remote=$(git remote get-url origin)
remote=${remote#https://*/}
remote=${remote##*:}
orga=${remote%/*}
repo=${remote##*/}
repo=${repo%.git}
echo "https://$orga.github.io/$repo"
}
# test: get_url [email protected]:pfitaxel/pfitaxel-demo.git
# test: get_url https://github.com/pfitaxel/pfitaxel-demo.git
echo "Value of --image: $_arg_image"
# Examples of possible values:
# ocamlsf/learn-ocaml:master
# pfitaxel/staging-learn-ocaml-editor:fix-static-deploy-docker
# pfitaxel/staging-learn-ocaml-editor:learn-ocaml-editor-docker
echo "Value of --base-url: $_arg_base_url"
if [ -z "$_arg_base_url" ]; then
target_url=$(get_url)
echo "So, the base URL is: $target_url"
else
target_url="$_arg_base_url"
fi
echo "Value of repo: $_arg_repo"
set -e # exit on failure
srcdir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )
pause() {
read -r -s -n 1 -p "Press any key to ${1:-continue} (or ^C to exit)... "
echo
}
clone_subdir() {
local repo="learn-ocaml"
if test -d "${srcdir}/.build/$repo"; then
pushd "${srcdir}/.build/$repo"
git fetch --depth=1 origin
git reset --hard "@{u}"
else
# git clone https://github.com/pfitaxel/learn-ocaml.git -b fix-static-deploy --depth=1 "${srcdir}/.build/$repo"
git clone https://github.com/ocaml-sf/learn-ocaml.git -b master --depth=1 "${srcdir}/.build/$repo"
pushd "${srcdir}/.build/$repo"
fi
popd
}
# deprecated function
make_gen() {
local target_url="$1"
local repo="learn-ocaml"
pushd "${srcdir}/.build/$repo"
if [ "$2" = "-B" ]; then
make clean
fi
if [ "$2" != "-B" ]; then
cat >&2 <<EOF
Usage: make_gen URL [-B] # where -B runs 'make clean' beforehand
EOF
fi
# REMARK: needs an opam switch (opam install, etc.)
export PS4='+ \e[36;1m(${0##*/} @ line $LINENO) \$\e[0m ' #]]
set -x # trace for debug
# make build
# make process-repo
make
make opaminstall
learn-ocaml build --base-url "$target_url" --repo=demo-repository
rsync -av --delete-delay www/ "${srcdir}/docs"
git describe --long --always > "${srcdir}/VERSION"
popd
}
function failtemp () {
echo "Failed to create a temporary directory. Please report." >&2
exit 1
}
docker_gen() {
local target_url="$1"
local repo="learn-ocaml"
local repository="${2:-${srcdir}/.build/$repo/demo-repository}"
export PS4='+ \e[36;1m(${0##*/} @ line $LINENO) \$\e[0m ' #]]
set -x # trace for debug
: Create temporary directory...
T=$(mktemp -d /tmp/learn-ocaml-www.XXX) || failtemp
: Generate www using Docker...
sudo docker pull "$_arg_image"
sudo docker run --rm -i -v "$repository:/repository" -v "$T:/home/learn-ocaml/tmp" --entrypoint="" "$_arg_image" sh -c "learn-ocaml build --repo=/repository --base-url $target_url && mv -v www tmp/"
# : Replace docs folder...
# rm -fr docs/
# mv -v -T "$T/www" ./docs
# rmdir "$T"
# : Mostly equivalent to:
rsync -av --delete-delay "$T/www/" "${srcdir}/docs"
: Remove temporary directory...
rm -fr "$T"
echo "$_arg_image" > "${srcdir}/VERSION"
}
deploy_locally() {
clone_subdir
port="8080"
target_url="http://localhost:$port"
docker_gen "$target_url"
touch "${srcdir}/.nojekyll"
cp -pv "${srcdir}/.nojekyll" "${srcdir}/docs/"
python3 -m http.server -d "${srcdir}/docs" "$port"
}
deploy() {
repository="$1" # can be the empty string, see docker_gen
clone_subdir
docker_gen "$target_url" "$repository"
touch "${srcdir}/.nojekyll"
cp -pv "${srcdir}/.nojekyll" "${srcdir}/docs/"
cd "${srcdir}"
git add VERSION
git add .nojekyll
git add -v -A docs
repo="learn-ocaml"
# TODO: replace previous commit?
git commit -m "Build $repo ($(<VERSION))" -e
pause "git push" && git push
}
# deploy_locally "$_arg_repo"
deploy "$_arg_repo"
# ] <-- needed because of Argbash