|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | | -# Usage: |
4 | | -# BOT_TOKEN=<dotty-bot password> ./genDocs [-doc-snapshot] |
5 | | - |
6 | 3 | set -e |
7 | | -GENDOC_EXTRA_ARGS=$@ |
8 | | - |
9 | | -# set extended glob, needed for rm everything but x |
10 | | -shopt -s extglob |
| 4 | +shopt -s extglob # needed for rm everything but x |
| 5 | +echo "Working directory: $PWD" |
11 | 6 |
|
12 | | -# make sure that BOT_TOKEN is set |
13 | | -if [ -z "$BOT_TOKEN" ]; then |
14 | | - echo "Error: BOT_TOKEN env unset, unable to push without password" 1>&2 |
15 | | - exit 1 |
| 7 | +GENDOC_EXTRA_ARGS=$@ |
| 8 | +GIT_HEAD=$(git rev-parse HEAD) # save current head for commit message in gh-pages |
| 9 | +PREVIOUS_SNAPSHOTS_DIR="$PWD/../prev_snapshots" |
| 10 | +SCRIPT_DIR="(cd "$(dirname "${BASH_SOURCE[0]}")" >& /dev/null && pwd)" |
| 11 | +SITE_OUT_DIR="$PWD/docs/_site" |
| 12 | + |
| 13 | +### Obtain the previous versions of the website ### |
| 14 | +if [ -d "$PREVIOUS_SNAPSHOTS_DIR" ]; then |
| 15 | + rm -rf "$PREVIOUS_SNAPSHOTS_DIR" |
16 | 16 | fi |
17 | 17 |
|
18 | | -echo "Working directory: $PWD" |
| 18 | +mkdir -pv "$PREVIOUS_SNAPSHOTS_DIR" |
| 19 | +git remote add doc-remote "https://github.com/lampepfl/dotty-website.git" |
| 20 | +git fetch doc-remote gh-pages |
| 21 | +git checkout gh-pages |
| 22 | +(cp -vr 0.*/ "$PREVIOUS_SNAPSHOTS_DIR"; true) # Don't fail if no `0.*` found to copy |
| 23 | +git checkout "$GIT_HEAD" |
19 | 24 |
|
| 25 | +### Generate the current snapshot of the website ### |
20 | 26 | # this command will generate docs in $PWD/docs/_site |
21 | | -SBT="$(cd "$(dirname "${BASH_SOURCE[0]}")" >& /dev/null && pwd)/sbt" |
| 27 | +SBT="$SCRIPT_DIR/sbt" |
22 | 28 | "$SBT" "dotty-bootstrapped/genDocs $GENDOC_EXTRA_ARGS" |
23 | 29 |
|
24 | 30 | # make sure that the previous command actually succeeded |
25 | | -if [ ! -d "$PWD/docs/_site" ]; then |
26 | | - echo "Output directory did not exist: $PWD/docs/_site" 1>&2 |
27 | | - exit 1 |
| 31 | +if [ ! -d "$SITE_OUT_DIR" ]; then |
| 32 | + echo "Output directory did not exist: $SITE_OUT_DIR" 1>&2 |
| 33 | + exit 1 |
28 | 34 | fi |
29 | 35 |
|
30 | | -# save current head for commit message in gh-pages |
31 | | -GIT_HEAD=$(git rev-parse HEAD) |
32 | | - |
33 | | - |
34 | | -# set up remote and github credentials |
35 | | -git remote add doc-remote "https://dotty-bot:$BOT_TOKEN@github.com/lampepfl/dotty-website.git" |
36 | | -git config user.name "dotty-bot" |
37 | | -git config user.email "dotty-bot@d-d.me" |
38 | | - |
39 | | -# check out correct branch |
40 | | -git fetch doc-remote gh-pages |
41 | | -git checkout gh-pages |
42 | | - |
43 | | -# save the old snapshots to the newly generated site |
44 | | -# This command must never fail since failures short-circuit the script |
45 | | -# The reason for failure is that no folder matches "0.*" pattern |
46 | | -# because snapshots may not be generated yet |
47 | | -(mv 0.*/ $PWD/docs/_site; true) |
48 | | - |
49 | | -# move newly generated _site dir to $PWD |
50 | | -mv $PWD/docs/_site . |
51 | | - |
52 | | -# remove everything BUT _site dir |
53 | | -rm -rf !(_site) |
54 | | - |
55 | | -# copy new contents to $PWD |
56 | | -mv _site/* . |
57 | | - |
58 | | -# remove now empty _site dir |
59 | | -rm -rf _site |
60 | | - |
61 | | -# add all contents of $PWD to commit |
62 | | -git add -A |
63 | | -git commit -m "Update gh-pages site for $GIT_HEAD" || echo "nothing new to commit" |
64 | | - |
65 | | -# push to doc-remote |
66 | | -git push doc-remote || echo "couldn't push, since nothing was added" |
| 36 | +### Move previous versions' snapshots to _site ### |
| 37 | +mv -v "$PREVIOUS_SNAPSHOTS_DIR"/* "$SITE_OUT_DIR" |
| 38 | +rm -rf "$PREVIOUS_SNAPSHOTS_DIR" |
0 commit comments