set -e
DOC_BRANCH="gh-pages"
CREATE_UPDATE_README=true
BUILD_LIBMODBUS_DOC=true
[[ "$(git symbolic-ref --short HEAD)" == "master" ]] || exit 0
msg() {
echo "[1;34m> [1;32m$@[0m"
}
dir="$(pwd)"
last_rev="$(git rev-parse HEAD)"
last_msg="$(git log -1 --pretty=%B)"
unset GIT_WORK_TREE
msg "Cloning into a temporary directory..."
tmp="$(mktemp -d 2>/dev/null || mktemp -d -t 'tmp-rust-docs')"
trap "cd \"$dir\"; rm -rf \"$tmp\"" EXIT
git clone -qb master "$dir" "$tmp"
cd "$tmp"
ln -s "$dir/target" "$tmp/target"
msg "Generating documentation..."
cargo doc
if "$BUILD_LIBMODBUS_DOC"; then
msg "Create libmodbus documentation from libmodbus C library"
pushd libmodbus-sys/libmodbus/doc
make --quiet htmldoc 2>/dev/null
popd
fi
msg "Replacing documentation..."
if ! git checkout --quiet "$DOC_BRANCH" 2>/dev/null; then
git checkout --quiet --orphan "$DOC_BRANCH"
git rm --quiet --ignore-unmatch -rf .
cat > .gitignore <<EOF
target
Cargo.lock
EOF
git add .gitignore
git commit -m "Initial commit."
fi
git rm --quiet --ignore-unmatch -rf .
if "$CREATE_UPDATE_README"; then
msg "Create or update index.html with the content or the projects README.md"
git checkout master README.md
git checkout master share/pandoc.css
pandoc --css share/pandoc.css --self-contained --highlight-style=tango -s -f markdown -t html5 -o index.html README.md
git add index.html
rm -r share
rm README.md
fi
git reset --quiet -- .gitignore
git checkout --quiet -- .gitignore
cp -a target/doc/* .
if $BUILD_LIBMODBUS_DOC; then
if [ ! -d libmodbus ]; then mkdir libmodbus; fi
cp libmodbus-sys/libmodbus/doc/*.html libmodbus/
rm libmodbus-sys/libmodbus -rf
fi
rm target
git add .
git commit -m "Update docs for $last_rev" -m "$last_msg"
git push --set-upstream origin "$DOC_BRANCH"
cd $dir
msg "Done."