Last active
January 5, 2018 15:18
-
-
Save shanecelis/2dc391cde7c26773d6d14ca6825dc4fb to your computer and use it in GitHub Desktop.
Substitute tags generated from git for versioning purposes
This file contains 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/bash | |
# Original code Copyright (c) 2018 Shane Celis[1] | |
# Licensed under the MIT License[2] | |
# | |
# Original code posted here[3]. | |
# | |
# This comment generated by code-cite[4]. | |
# | |
# [1]: https://github.com/shanecelis | |
# [2]: https://opensource.org/licenses/MIT | |
# [3]: https://gist.github.com/shanecelis/2dc391cde7c26773d6d14ca6825dc4fb | |
# [4]: https://github.com/shanecelis/code-cite | |
# version-substitute | |
# ================== | |
# | |
# Substitute tags generated from git for versioning purposes. | |
# Use Mustache[1] template written for bash called mo[2]. | |
# | |
# [1]: https://mustache.github.io | |
# [2]: https://github.com/tests-always-included/mo | |
source "$HOME/bin/mo"; | |
commentPrefix="${COMMENT:-//}"; | |
tags=(majorminor semanticversion dotnetversion revision commitcount gitdescribe commithash versiontimestamp) | |
majorminor=`git describe --tags | perl -pe 's/^v(\d+)\.(\d+).*/$1.$2/g;'`; | |
revision=`git describe --tags | perl -pe 's/^v(\d+)\.(\d+)\.(\d+).*/$3/g;'`; | |
commitcount=`git rev-list --count HEAD`; | |
gitdescribe=`git describe --tags --long`; | |
commithash=`git rev-parse --short HEAD`; | |
semanticversion="${majorminor}.$revision"; | |
dotnetversion="${majorminor}.$commitcount.$revision"; | |
versiontimestamp=`date '+%Y-%m-%dT%H:%M:%S%Z'`; | |
if [ $# -ne 1 ]; then | |
echo "Usage: $0 <template>" >&2; | |
for tag in "${tags[@]}"; do | |
echo "{{$tag}} -> ${!tag}"; | |
done | |
exit 2; | |
fi | |
# Thanks for this read-only tip! | |
# https://projectgus.com/2009/07/marking-a-file-read-only-in-emacs-and-vim/ | |
echo "$commentPrefix vim:set ro: -*- buffer-read-only:t -*-" | |
echo "$commentPrefix Warning: This file was procedurally generated from $1." | |
mo --fail-not-set $1 | |
# The following code was required without mo. | |
# cat $1 | | |
# script=$(for tag in "${tags[@]}"; do | |
# echo "s/\{\{$tag\}\}/${!tag}/g;"; | |
# done) | |
# cat $1 | perl -pe "$script" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment