SHARE
TWEET

Untitled

a guest Apr 21st, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env bash
  2.  
  3. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  4. #
  5. #   Project configuration variables
  6. #   These can be adapted to fit your project structure.
  7. #
  8. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  9. VERSION_FILE="include/version.h"
  10. CHANGELOG_FILE="debian/changelog"
  11.  
  12. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  13. #
  14. #   Scripts internal variables
  15. #
  16. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  17. LF="\n"
  18. PARAMS=""
  19. DRYRUN=0
  20.  
  21. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  22. #
  23. #   Helper functions
  24. #
  25. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  26. die()
  27. {
  28.   echo "Error: $@" 1>&2
  29.   exit 1
  30. }
  31.  
  32. print_usage()
  33. {
  34.   echo
  35.   echo "Usage: $0 [OPTIONS] ACTION"
  36.   echo
  37.   echo "Available options:"
  38.   echo "  -s,--script   Format output in \"script\" mode (no trailing newline)."
  39.   echo "  -d,--dry-run  Does not commit anything to any file, just prints."
  40.   echo
  41.   echo "Available actions:"
  42.   echo "  print   [minor|major|patch]  Print the current version."
  43.   echo "  release [minor|major|patch]  Bump the specified version part"
  44.   echo "  check                        Check the changelog latest released"
  45.   echo "                               version against the version file."
  46.   echo
  47.   echo "Please run this script from the project root folder."
  48.   echo
  49. }
  50.  
  51. check_files()
  52. {
  53.   [ -f ${VERSION_FILE}   ] || die "No ${VERSION_FILE} found!";
  54.   [ -f ${CHANGELOG_FILE} ] || die "No ${CHANGELOG_FILE} found!"
  55. }
  56.  
  57. # Gets a part of the version from the project version file (version.h).
  58. # Takes one argument: the matching version identifier in the version file, e.g.
  59. #   TINYALSA_VERSION_MAJOR
  60. get_version_part()
  61. {
  62.   local V=$(grep -m 1 "^#define\([ \t]*\)$1" ${VERSION_FILE} | sed 's/[^0-9]*//g')
  63.  
  64.   if [ -z ${V} ]; then
  65.     die "Could not get $1 from ${VERSION_FILE}"
  66.   fi
  67.  
  68.   echo ${V}
  69. }
  70.  
  71.  
  72. # Gets the complete version from the version file.
  73. # Sets VERSION_MAJOR, VERSION_MINOR and VERSION_PATCH globals
  74. get_version()
  75. {
  76.   VERSION_MAJOR=$(get_version_part "TINYALSA_VERSION_MAJOR")
  77.   VERSION_MINOR=$(get_version_part "TINYALSA_VERSION_MINOR")
  78.   VERSION_PATCH=$(get_version_part "TINYALSA_VERSION_PATCH")
  79. }
  80.  
  81. # Commits the new version part to the version file.
  82. # Takes two arguments: the version part identifier in the version file and the
  83. #   new version number. If no arguments, do nothing.
  84. commit_version_part()
  85. {
  86.   if [ -z $1 ] || [ -z $2 ]; then
  87.     return 0
  88.   fi
  89.  
  90.   sed -i "s/\(^#define[ \t]*$1\)[ \t]*\([0-9]*\)/\1 $2/g" ${VERSION_FILE} \
  91.     || die "Could not commit version for $1";
  92.  
  93.   [ $(get_version_part $1) = "$2" ] || die "Version check after commit failed for $1"
  94.  
  95.   return 0;
  96. }
  97.  
  98. # Commits the new version to the version file.
  99. # Takes three arguments, the new version numbers for major, minor and patch
  100. commit_version()
  101. {
  102.   commit_version_part "TINYALSA_VERSION_PATCH" $1
  103.   commit_version_part "TINYALSA_VERSION_MINOR" $2
  104.   commit_version_part "TINYALSA_VERSION_MAJOR" $3
  105.  
  106.   return 0
  107. }
  108.  
  109. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  110. #
  111. #   Actions implementations / functions
  112. #
  113. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  114. print_version()
  115. {
  116.   if [ -z $1 ]; then
  117.     printf "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}${LF}"
  118.   else
  119.     case "$1" in
  120.       major)
  121.         printf "${VERSION_MAJOR}${LF}"
  122.         ;;
  123.       minor)
  124.         printf "${VERSION_MINOR}${LF}"
  125.         ;;
  126.       patch)
  127.         printf "${VERSION_PATCH}${LF}"
  128.         ;;
  129.       *)
  130.         die "Unknown part \"$1\" (must be one of minor, major and patch)."
  131.         ;;
  132.     esac
  133.   fi
  134.  
  135.   return 0
  136. }
  137.  
  138. bump_version()
  139. {
  140.   local PART="patch"
  141.  
  142.   if [ ! -z $1 ]; then
  143.     PART="$1"
  144.   fi
  145.  
  146.   case "$PART" in
  147.     major)
  148.       VERSION_MAJOR=$((VERSION_MAJOR+1))
  149.       VERSION_MINOR=0
  150.       VERSION_PATCH=0
  151.     ;;
  152.     minor)
  153.       VERSION_MINOR=$((VERSION_MINOR+1))
  154.       VERSION_PATCH=0
  155.     ;;
  156.     patch)
  157.       VERSION_PATCH=$((VERSION_PATCH+1))
  158.     ;;
  159.     *)
  160.       die "Unknown part \"$1\" (must be one of minor, major and patch)."
  161.     ;;
  162.   esac
  163.  
  164.   if [ ${DRYRUN} -ne 1 ]; then
  165.     commit_version ${VERSION_PATCH} ${VERSION_MINOR} ${VERSION_MAJOR}
  166.   fi
  167.  
  168.   print_version
  169.   return 0
  170. }
  171.  
  172. check_version()
  173. {
  174.   local LOG_VERSION=$(grep -m 1 "^tinyalsa (" ${CHANGELOG_FILE}| sed "s/[^0-9.]*//g")
  175.   local REF_VERSION="${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
  176.  
  177.   if [ "${LOG_VERSION}" != "${REF_VERSION}" ]; then
  178.     die "Changelog version (${LOG_VERSION}) does not match package version (${REF_VERSION})."
  179.   fi
  180.  
  181.   printf "Changelog version (${LOG_VERSION}) OK!${LF}"
  182.   return 0
  183. }
  184.  
  185. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  186. #
  187. #   Command Line parsing
  188. #
  189. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  190. parse_command()
  191. {
  192.   if [ "$#" -eq "0" ]; then
  193.     print_usage
  194.     exit 1
  195.   fi
  196.  
  197.   case "$1" in
  198.     print)
  199.       get_version
  200.       print_version "$2"
  201.       exit $?
  202.       ;;
  203.     release)
  204.       get_version
  205.       bump_version "$2"
  206.       exit $?
  207.       ;;
  208.     check)
  209.       get_version
  210.       check_version
  211.       exit $?
  212.       ;;
  213.     *)
  214.       die "Unsupported action \"$1\"."
  215.       ;;
  216.   esac
  217. }
  218.  
  219. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  220. #
  221. #   Main
  222. #
  223. # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  224.  
  225. set -e
  226. trap "set +e" 0
  227.  
  228. # Checking parameters
  229. if [ "$#" -eq "0" ]; then
  230.   print_usage
  231.   exit 0
  232. fi
  233.  
  234. while [ "$#" -ne "0" ]; do
  235.   case "$1" in
  236.     -s|--script)
  237.       unset LF
  238.       shift
  239.       ;;
  240.     -d|--dry-run)
  241.       DRYRUN=1
  242.       shift
  243.       ;;
  244.     --)
  245.       shift
  246.       break
  247.       ;;
  248.     -*|--*=)
  249.       die "Unsupported flag \"$1\"."
  250.       ;;
  251.     *)
  252.       PARAMS="$PARAMS ${1}"
  253.       shift
  254.       ;;
  255.   esac
  256. done
  257.  
  258. # set positional arguments in their proper place
  259. set -- "${PARAMS}"
  260.  
  261. check_files
  262. parse_command ${PARAMS}
  263.  
  264. # The script should never reach this place.
  265. die "Internal error. Please report this."
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top