Created
March 11, 2013 15:07
-
-
Save eldstal/5134890 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Generates version.h in all app_* directories | |
# If filenames are supplied on the command line, create those instead. | |
# If a directory is supplied on the command line, output the version of that directory | |
# Determine if git can provide a sensible version string | |
have_version() { | |
git describe >/dev/null 2>&1 && return 0 | |
return 1 | |
} | |
# | |
# Return the last commit to a given directory/file | |
# | |
commit() { | |
git log --max-count=1 --pretty=format:%H "${1}" | |
} | |
# | |
# Print a string which describes this version of the repo | |
# If run outside of a git repo (have_version returns non-zero), do nothing | |
# $1: a directory to date. If empty, uses . | |
version() { | |
local DIR=$1 | |
[ -z "$DIR" ] && DIR="." | |
local LAST_COMMIT=$(commit ${DIR}) | |
have_version && git describe --always "${LAST_COMMIT}" | |
} | |
# Print a date and time | |
timestamp() { | |
date +%y%m%d-%H:%M:%S | |
} | |
# Create C header with filename provided by $1 | |
# If no version information is available, creates file | |
# but does not clobber. | |
# The created file defines the VERSION macro as a const char[] | |
create_file() { | |
local FILENAME=$1 | |
[ -z "$FILENAME" ] && return | |
local TIMESTAMP=$(timestamp) | |
if have_version; then | |
local DIRNAME=$(dirname "${FILENAME}") | |
local TAG=$(version "${DIRNAME}" | sed -re 's/([^-]*).*/\1/') | |
local REVISIONS=$(version "${DIRNAME}" | sed -re 's/([^-]*)-?//' | sed -re 's/([0-9]+).*/\1/') | |
local COMMIT=$(commit "${DIRNAME}") | |
# The tag is something like 0.2 | |
local VERSION=${TAG} | |
# Add the potential number of commits since the tag, making 0.2.26 | |
[ -n "${REVISIONS}" ] && VERSION="${VERSION}.${REVISIONS}" | |
[ -z "${VERSION}" ] && VERSION="0.0.1" | |
echo "#define VERSION \"${VERSION}\"" > ${FILENAME} | |
echo "#define BUILD_TIME \"${TIMESTAMP}\"" >> ${FILENAME} | |
echo "#define BUILD_COMMIT \"${COMMIT}\"" >> ${FILENAME} | |
else | |
if [ ! -f "${FILENAME}" ]; then | |
echo "#define VERSION \"unknown\"" > ${FILENAME} | |
echo "#define BUILD_TIME \"${TIMESTAMP}\"" >> ${FILENAME} | |
echo "#define BUILD_COMMIT \"${VERSION_STRING}\"" >> ${FILENAME} | |
fi | |
fi | |
} | |
if [ -z "$1" ]; then | |
# Nothing specified, create app_*/version.h | |
ROOT=$(dirname "$0") | |
for APPDIR in ${ROOT}/app_*/; do | |
create_file ${APPDIR}/version.h | |
done | |
else | |
# One or more explicit file/directory names specified | |
for VERSION_ARG in "${@}"; do | |
# That's a directory, output its version | |
if [ -d "${VERSION_ARG}" ]; then | |
version ${VERSION_ARG} | |
else | |
# It's a file to be created | |
echo "Creating ${VERSION_ARG}" | |
create_file "${VERSION_ARG}" | |
fi | |
done | |
fi | |
~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment