travis-doxygen.sh 3.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#!/bin/bash
# Update Doxygen documentation after push to 'master'.
# Author: @pah

set -e

DOXYGEN_VER=doxygen-1.8.7
DOXYGEN_TAR=${DOXYGEN_VER}.linux.bin.tar.gz
DOXYGEN_URL="http://ftp.stack.nl/pub/users/dimitri/${DOXYGEN_TAR}"
DOXYGEN_BIN="/usr/local/bin/doxygen"

12
: ${GITHUB_REPO:="miloyip/rapidjson"}
13 14 15
GITHUB_HOST="github.com"
GITHUB_CLONE="git://${GITHUB_HOST}/${GITHUB_REPO}"
GITHUB_URL="https://${GITHUB_HOST}/${GITHUB_PUSH-${GITHUB_REPO}}"
16 17

# if not set, ignore password
18
#GIT_ASKPASS="${TRAVIS_BUILD_DIR}/gh_ignore_askpass.sh"
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

skip() {
	echo "$@" 1>&2
	echo "Exiting..." 1>&2
	exit 0
}

abort() {
	echo "Error: $@" 1>&2
	echo "Exiting..." 1>&2
	exit 1
}

# TRAVIS_BUILD_DIR not set, exiting
[ -d "${TRAVIS_BUILD_DIR-/nonexistent}" ] || \
	abort '${TRAVIS_BUILD_DIR} not set or nonexistent.'

# check for pull-requests
[ "${TRAVIS_PULL_REQUEST}" = "false" ] || \
	skip "Not running Doxygen for pull-requests."

# check for branch name
[ "${TRAVIS_BRANCH}" = "master" ] || \
	skip "Running Doxygen only for updates on 'master' branch (current: ${TRAVIS_BRANCH})."

# check for job number
[ "${TRAVIS_JOB_NUMBER}" = "${TRAVIS_BUILD_NUMBER}.1" ] || \
	skip "Running Doxygen only on first job of build ${TRAVIS_BUILD_NUMBER} (current: ${TRAVIS_JOB_NUMBER})."

# install doxygen binary distribution
doxygen_install()
{
	wget -O - "${DOXYGEN_URL}" | \
		tar xz -C ${TMPDIR-/tmp} ${DOXYGEN_VER}/bin/doxygen
53
    export PATH="${TMPDIR-/tmp}/${DOXYGEN_VER}/bin:$PATH"
54 55 56 57
}

doxygen_run()
{
58 59
	cd "${TRAVIS_BUILD_DIR}";
	doxygen ${TRAVIS_BUILD_DIR}/build/doc/Doxyfile;
miloyip's avatar
miloyip committed
60
	doxygen ${TRAVIS_BUILD_DIR}/build/doc/Doxyfile.zh-cn;
61 62 63 64
}

gh_pages_prepare()
{
65
	cd "${TRAVIS_BUILD_DIR}/build/doc";
66 67
	[ ! -d "html" ] || \
		abort "Doxygen target directory already exists."
68
	git --version
69
	git clone -b gh-pages "${GITHUB_CLONE}" html
70
	cd html
71
	# setup git config (with defaults)
72 73
	git config user.name "${GIT_NAME-travis}"
	git config user.email "${GIT_EMAIL-"travis@localhost"}"
74 75 76 77 78 79
	# clean working dir
	rm -f .git/index
	git clean -df
}

gh_pages_commit() {
80
	cd "${TRAVIS_BUILD_DIR}/build/doc/html";
miloyip's avatar
miloyip committed
81
	echo "rapidjson.org" > CNAME
82
	git add --all;
83
	git diff-index --quiet HEAD || git commit -m "Automatic doxygen build";
84 85
}

86 87 88 89 90 91 92 93 94
gh_setup_askpass() {
	cat > ${GIT_ASKPASS} <<EOF
#!/bin/bash
echo
exit 0
EOF
	chmod a+x "$GIT_ASKPASS"
}

95 96 97 98
gh_pages_push() {
	# check for secure variables
	[ "${TRAVIS_SECURE_ENV_VARS}" = "true" ] || \
		skip "Secure variables not available, not updating GitHub pages."
99
	# check for GitHub access token
100 101
	[ "${GH_TOKEN+set}" = set ] || \
		skip "GitHub access token not available, not updating GitHub pages."
102 103
	[ "${#GH_TOKEN}" -eq 40 ] || \
		abort "GitHub token invalid: found ${#GH_TOKEN} characters, expected 40."
104

105
	cd "${TRAVIS_BUILD_DIR}/build/doc/html";
106
	# setup credentials (hide in "set -x" mode)
107 108
	git remote set-url --push origin "${GITHUB_URL}"
	git config credential.helper 'store'
109
	# ( set +x ; git config credential.username "${GH_TOKEN}" )
110 111 112
	( set +x ; [ -f ${HOME}/.git-credentials ] || \
			( echo "https://${GH_TOKEN}:@${GITHUB_HOST}" > ${HOME}/.git-credentials ; \
			 chmod go-rw ${HOME}/.git-credentials ) )
113
	# push to GitHub
114
	git push origin gh-pages
115 116 117 118 119 120 121 122
}

doxygen_install
gh_pages_prepare
doxygen_run
gh_pages_commit
gh_pages_push