Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -e
  4.  
  5. platform=$(uname)
  6. if [ "$platform" == 'Darwin' ]
  7. then
  8. sed=$(which gsed || errcho "install gnu-sed")
  9. else
  10. sed=$(which sed)
  11. fi
  12.  
  13. semver_regex="^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(\-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$"
  14.  
  15. usage="\
  16. Usage:
  17. $0
  18. $0 show
  19. $0 major
  20.  
  21. Commands:
  22. show fetch and show current versions.
  23. major bump the major level of current version.
  24. minor bump the minor level of current version.
  25. patch bump the patch level of current version."
  26.  
  27. function errcho {
  28. >&2 echo -e "error: $*"
  29. exit 1
  30. }
  31.  
  32. function update {
  33. git fetch --tags
  34. }
  35.  
  36. function show {
  37. echo "Current git tag: $current_tag"
  38. echo "Current meta version: $current_version"
  39. }
  40.  
  41. function validate {
  42. local version="$1"
  43. if [[ "$version" =~ $semver_regex ]]
  44. then
  45. apply "$version"
  46. else
  47. errcho "version $version does not follow semver standard"
  48. fi
  49. }
  50.  
  51. function bump {
  52. local level="$1"
  53. IFS=$'.'
  54. set -- $current_version
  55. local major=$1
  56. local minor=$2
  57. local patch=$3
  58. case $level in
  59. major) ((major++));;
  60. minor) ((minor++));;
  61. patch) ((patch++));;
  62. esac
  63. validate "$major.$minor.$patch"
  64. }
  65.  
  66. function apply {
  67. local new_version="$1"
  68. $sed -i "s/$current_version/$new_version/" "metadata.rb"
  69. (git add metadata.rb && git commit -m"bump to $new_version") || true
  70. git tag "$new_version"
  71. git push origin
  72. git push origin --tags
  73. }
  74.  
  75. if [ ! -f metadata.rb ]
  76. then
  77. errcho "metadata.rb file missing"
  78. fi
  79.  
  80. current_version=$(grep -E '^version' metadata.rb | cut -d"'" -f2 || errcho "a cookbook version was not found")
  81. current_tag=$(update | git describe --abbrev=0 --tags || echo 'none found')
  82.  
  83. if [ -z "$1" ]
  84. # there are no args
  85. then
  86. show
  87. read -r -p "enter a new semver version: " answer
  88. validate "$answer"
  89. # there are args
  90. else
  91. # first arg was semver
  92. if [[ "$1" =~ $semver_regex ]]
  93. then
  94. apply "$1"
  95. else
  96. # first arg is either a known command or throw usage
  97. case "$1" in
  98. show) show;;
  99. major|minor|patch) bump "$1";;
  100. *) echo "$usage"; exit 1;;
  101. esac
  102. fi
  103. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement