|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +#set -x |
| 4 | + |
| 5 | +help() { |
| 6 | + cat <<EOF |
| 7 | +Usage: init_ghpages.sh [OPTIONS] |
| 8 | +Initialise GitHub Pages branch. |
| 9 | +
|
| 10 | +Options: |
| 11 | + -h, --help Print this help and exit. |
| 12 | + -g, --ghpages BRANCH Name of GitHub Pages branch. Default: gh-pages |
| 13 | + -n, --ontology-name NAME Name of ontology. |
| 14 | + -p, --ontology-prefix PREFIX Ontology prefix. |
| 15 | + -n, --ontology-iri IRI Ontology IRI. |
| 16 | + -r, --remote Remote git repository. Default: origin |
| 17 | +
|
| 18 | +EOF |
| 19 | +} |
| 20 | + |
| 21 | +## Defaults |
| 22 | +GHPAGES=gh-pages |
| 23 | +ONTOLOGY_NAME= |
| 24 | +ONTOLOGY_PREFIX= |
| 25 | +ONTOLOGY_IRI= |
| 26 | +REMOTE=origin |
| 27 | + |
| 28 | +## Parse options |
| 29 | +TEMP=$(getopt -o hg: --long help,ghpages:,ontology: -- "$@") |
| 30 | +[ $? != 0 ] && exit 1 |
| 31 | +eval set -- "$TEMP" |
| 32 | +while true; do |
| 33 | + case "$1" in |
| 34 | + -h|--help) help; exit 0;; |
| 35 | + -g|--ghpages) GHPAGES=$2; shift 2;; |
| 36 | + -n|--ontology-name) ONTOLOGY_NAME=$2; shift 2;; |
| 37 | + -p|--ontology-prefix) ONTOLOGY_PREFIX=$2; shift 2;; |
| 38 | + -i|--ontology-iri) ONTOLOGY_IRI=$2; shift 2;; |
| 39 | + -r|--remote) REMOTE=$2; shift 2;; |
| 40 | + --) shift; break;; |
| 41 | + esac |
| 42 | +done |
| 43 | + |
| 44 | +current_branch="$(git branch --show-current)" |
| 45 | +rootdir="$(git rev-parse --show-toplevel)" |
| 46 | +reponame="${rootdir##*/}" |
| 47 | + |
| 48 | +# Ensure that all changes are comitted |
| 49 | +if [ -n "$(git status --porcelain --untracked-files=no)" ]; then |
| 50 | + echo "There exist uncomitted changes" |
| 51 | + exit 1 |
| 52 | +fi |
| 53 | + |
| 54 | +# Set defaults |
| 55 | +if [ -z "$ONTOLOGY_NAME" ]; then |
| 56 | + ONTOLOGY_NAME="$reponame" |
| 57 | + ONTOLOGY_NAME="${ONTOLOGY_NAME#domain-}" |
| 58 | + ONTOLOGY_NAME="${ONTOLOGY_NAME#application-}" |
| 59 | +fi |
| 60 | +if [ ! -f "$rootdir/${ONTOLOGY_NAME}.ttl" ]; then |
| 61 | + echo "Missing ontology file: $rootdir/${ONTOLOGY_NAME}.ttl" |
| 62 | + exit 1 |
| 63 | +fi |
| 64 | + |
| 65 | +if [ -z "$ONTOLOGY_IRI" ]; then |
| 66 | + ONTOLOGY_IRI="$(sed -n 's/^@prefix : *<\([^>]*\)>.*/\1/p' $rootdir/${ONTOLOGY_NAME}.ttl)" |
| 67 | + ONTOLOGY_IRI=${ONTOLOGY_IRI%#} |
| 68 | + ONTOLOGY_IRI=${ONTOLOGY_IRI%/} |
| 69 | +fi |
| 70 | + |
| 71 | +if [ -z "${ONTOLOGY_IRI}" ]; then |
| 72 | + echo "Empty ONTOLOGY_IRI" |
| 73 | + exit 1 |
| 74 | +fi |
| 75 | + |
| 76 | +if [ -z "$ONTOLOGY_PREFIX" ]; then |
| 77 | + ONTOLOGY_PREFIX="$(sed -n 's|^@prefix *\([^:]*\): *<${ONTOLOGY_IRI}>.*|\1|p' $rootdir/${ONTOLOGY_NAME}.ttl)" |
| 78 | +fi |
| 79 | + |
| 80 | +have_deps=$([ -f "${rootdir}/${ONTOLOGY_NAME}-dependencies.ttl" ] && echo true || echo false) |
| 81 | + |
| 82 | + |
| 83 | +# Show settings |
| 84 | +echo "-- Settings:" |
| 85 | +echo " - current_branch=$current_branch" |
| 86 | +echo " - rootdir=$rootdir" |
| 87 | +echo " - reponame=$reponame" |
| 88 | +echo " - have_deps=$have_deps" |
| 89 | +echo " - GHPAGES=$GHPAGES" |
| 90 | +echo " - ONTOLOGY_NAME=$ONTOLOGY_NAME" |
| 91 | +echo " - ONTOLOGY_PREFIX=$ONTOLOGY_PREFIX" |
| 92 | +echo " - ONTOLOGY_IRI=$ONTOLOGY_IRI" |
| 93 | +echo " - REMOTE=$REMOTE" |
| 94 | + |
| 95 | + |
| 96 | +# Squashed ontology |
| 97 | +echo -n "-- Checking for squashed ${ONTOLOGY_NAME}.ttl " |
| 98 | +if git show "${GHPAGES}:${ONTOLOGY_NAME}.ttl" > "/tmp/${ONTOLOGY_NAME}.ttl" 2>/dev/null; then |
| 99 | + echo "- exists" |
| 100 | +else |
| 101 | + echo "- creating" |
| 102 | + ontoconvert \ |
| 103 | + -sawe \ |
| 104 | + --namespace="emmo:https://w3id.org/emmo#" \ |
| 105 | + --namespace="${ONTOLOGY_PREFIX}:${ONTOLOGY_IRI}#" \ |
| 106 | + --base-iri="${ONTOLOGY_IRI}#" \ |
| 107 | + --iri="${ONTOLOGY_IRI}" \ |
| 108 | + "$rootdir/${ONTOLOGY_NAME}.ttl" \ |
| 109 | + "/tmp/${ONTOLOGY_NAME}.ttl" |
| 110 | +fi |
| 111 | + |
| 112 | +# Squashed dependencies |
| 113 | +echo -n "-- Checking for dependencies " |
| 114 | +if $have_deps; then |
| 115 | + if git show "${GHPAGES}:${ONTOLOGY_NAME}-dependencies.ttl" >"/tmp/${ONTOLOGY_NAME}-dependencies.ttl" 2>/dev/null; then |
| 116 | + echo "- exists" |
| 117 | + else |
| 118 | + echo "- creating" |
| 119 | + ontoconvert \ |
| 120 | + -saw \ |
| 121 | + --namespace="emmo:https://w3id.org/emmo#" \ |
| 122 | + --namespace="${ONTOLOGY_PREFIX}:${ONTOLOGY_IRI}#" \ |
| 123 | + "${ONTOLOGY_NAME}-dependencies.ttl" \ |
| 124 | + "/tmp/${ONTOLOGY_NAME}-dependencies.ttl" |
| 125 | + fi |
| 126 | +else |
| 127 | + echo "- missing ${ONTOLOGY_NAME}-dependencies.ttl (skipping)" |
| 128 | +fi |
| 129 | + |
| 130 | +# Switch to gh-pages branch (creating it if it doesn't exists) |
| 131 | +if [ -z $(git branch --list "$GHPAGES") ]; then |
| 132 | + git switch --orphan "$GHPAGES" |
| 133 | +else |
| 134 | + git switch "$GHPAGES" |
| 135 | +fi |
| 136 | + |
| 137 | +# Pull from remote |
| 138 | +git pull $REMOTE $GHPAGES 2>/dev/null |
| 139 | + |
| 140 | +# Add generated files to gh-pages branch |
| 141 | +mv -f "/tmp/${ONTOLOGY_NAME}.ttl" "$rootdir/." |
| 142 | +git add "$rootdir/${ONTOLOGY_NAME}.ttl" |
| 143 | +if $have_deps; then |
| 144 | + mv -f "/tmp/${ONTOLOGY_NAME}-dependencies.ttl" "$rootdir/." |
| 145 | + git add "$rootdir/${ONTOLOGY_NAME}-dependencies.ttl" |
| 146 | +fi |
| 147 | + |
| 148 | +# Commit gh-pages branch (if needed) |
| 149 | +if [ -n "$(git status --porcelain --untracked-files=no)" ]; then |
| 150 | + git commit -m "Added squashed ontology and dependencies" |
| 151 | +fi |
| 152 | + |
| 153 | +# Push to remote repository |
| 154 | +git push $REMOTE $GHPAGES || exit 1 |
| 155 | + |
| 156 | +# Change back to original branch |
| 157 | +git checkout "$current_branch" |
0 commit comments