#!/bin/bash # vi: fdm=marker ################## # Constants {{{1 # ################## PROG_NAME=$(basename $0) VERSION=1.5 YES=yes # Set grep command if [ -z "$MAKE_VERSIONS_GREP" ] ; then MAKE_VERSIONS_GREP=grep fi # Set sed command if [ -z "$MAKE_VERSIONS_SED" ] ; then MAKE_VERSIONS_SED=sed fi # Set Dockerfile path if [ -z "$MAKE_VERSIONS_DOCKERFILE" ] ; then MAKE_VERSIONS_DOCKERFILE=Dockerfile fi ######################### # Global variables {{{1 # ######################### DEBUG=0 TAGPREFIX= BRANCHTAG= OUTPUT_FILE=PhenoMeNal_Versions.txt ################### # Print help {{{1 # ################### function print_help { echo "Usage: $PROG_NAME [options]" echo echo " -b, --branch-tag Use git branch for building container tag prefix. If branch is \"develop\", container tag prefix will be prefixed with \"dev_\"." echo " -g, --debug Debug mode." echo " -h, --help Print this help message." echo " -t, --tag-prefix Output container tag prefix." echo " -v, --version Print version." echo echo "Environment variables:" echo " MAKE_VERSIONS_DOCKERFILE Set a custom Dockerfile path. By default Dockerfile is supposed to be inside the working directory." echo " MAKE_VERSIONS_GREP Set custom grep command to use." echo " MAKE_VERSIONS_SED Set custom sed command to use." } ############ # Msg {{{1 # ############ function msg { local tag=$1 shift local code_level=1 is_numeric=$(echo $1 | grep '^[0-9]*$') if [ -n "$is_numeric" ] ; then code_level=$1 shift fi local msg=$1 # Check tag if [ -z "$tag" -o \( "$tag" != INFO -a "$tag" != DEBUG -a "$tag" != ERROR \) ] ; then echo "ERROR: Unvalid message tag \"$tag\"." >&2 exit 999 fi # Print message [ "$tag" = DEBUG -a "$DEBUG" -lt "$code_level" ] || echo "$tag: $msg" >&2 # Exit [ $tag = ERROR ] && exit $code_level return 0 } ################## # Read args {{{1 # ################## function read_args { local args="$*" # save arguments for debugging purpose # Read options while true ; do shift_count=1 case $1 in -b|--branch-tag) BRANCHTAG=$YES ;; -g|--debug) DEBUG=$((DEBUG + 1)) ;; -h|--help) print_help ; exit 0 ;; -t|--tag-prefix) TAGPREFIX=$YES ;; -v|--version) echo $VERSION ; exit 0 ;; -|--|--*) msg ERROR "Illegal option $1." ;; -?) msg ERROR "Unknown option $1." ;; -[^-]*) split_opt=$(echo $1 | sed 's/^-//' | sed 's/\([a-zA-Z]\)/ -\1/g') ; set -- $1$split_opt "${@:2}" ;; *) break esac shift $shift_count done shift $((OPTIND - 1)) # Read remaining arguments [ $# -eq 0 ] || msg ERROR "No remaining argument is allowed." # Debug msg DEBUG 1 "Arguments are : $args" } ######################### # Extract versions {{{1 # ######################### function extract_versions { local versions=() local TOOL_VERSION_names="TOOL_VERSION software.version" local CONTAINER_VERSION_names="CONTAINER_VERSION version" # Loop on all version variables for version_var in TOOL_VERSION CONTAINER_VERSION ; do msg DEBUG 2 "Looking for variable $version_var..." var_names=${version_var}_names for version_var_name in ${!var_names} ; do msg DEBUG 2 "Trying variable name $version_var_name..." # Look for lines containing version definition for cmd in 'ENV' LABEL ; do msg DEBUG 2 "Trying command $cmd..." line=$($MAKE_VERSIONS_GREP "^[[:space:]]*$cmd[[:space:]]*$version_var_name" $MAKE_VERSIONS_DOCKERFILE) [ -n "$line" ] && break done # Leave as soon as we've found a match [ -n "$line" ] && break done msg DEBUG 2 "Found line \"$line\" for variable $version_var." # Check we've found something [ -n "$line" ] || msg ERROR "Cannot find definition of $version_var inside $MAKE_VERSIONS_DOCKERFILE file." # Replace with right variable name line=$(echo $line | $MAKE_VERSIONS_SED -e "s/$version_var_name/$version_var/") msg DEBUG 2 "Replaced variable name: \"$line\"." # Extract definition of version variable version_def=$(echo $line | $MAKE_VERSIONS_SED -e "s/^.*[[:space:]]*\($version_var\)[[:space:]]*=[[:space:]]*\(.*\)$/\1=\2/") msg DEBUG "New version variable definition: $version_def" # Add definiton to array versions+=($version_def) done # Output versions printf "%s\n" "${versions[@]}" >$OUTPUT_FILE # Add tag prefix if [ -n "$TAGPREFIX" ] ; then # Load written file msg DEBUG "Sourcing written file \"$OUTPUT_FILE\"..." source $OUTPUT_FILE msg DEBUG "TOOL_VERSION=$TOOL_VERSION" msg DEBUG "CONTAINER_VERSION=$CONTAINER_VERSION" # Get git branch if [ -n "$BRANCHTAG" ] ; then msg DEBUG "Looking for git branch..." git=$(which git) if [ -n "$git" ] ; then msg DEBUG "Found git as \"$git\"." branch=$(git status | grep "On branch" | sed 's/^On branch //') msg DEBUG "Current git branch is \"$branch\"." if [ "$branch" = develop ] ; then BRANCH_PREFIX=dev_ msg DEBUG "Adding prefix \"$BRANCH_PREFIX\" to tag." fi if [[ $branch == release* ]] ; then BRANCH_PREFIX=rc_ msg DEBUG "Adding prefix \"$BRANCH_PREFIX\" to tag." fi fi fi # Append tag prefix definition to the file local VERSION_TAG="${BRANCH_PREFIX}v${TOOL_VERSION}_cv${CONTAINER_VERSION}" echo "CONTAINER_TAG_PREFIX=$VERSION_TAG" >>$OUTPUT_FILE msg DEBUG "CONTAINER_TAG_PREFIX=$VERSION_TAG" fi } ############# # MAIN {{{1 # ############# # Read arguments read_args "$@" # Test presence of Dockerfile [ -f "$MAKE_VERSIONS_DOCKERFILE" ] || msg ERROR "No \"$MAKE_VERSIONS_DOCKERFILE\" file !" # Extract versions extract_versions