Skip to content

Instantly share code, notes, and snippets.

@kkaefer
Created May 11, 2011 15:44
Show Gist options
  • Save kkaefer/966711 to your computer and use it in GitHub Desktop.
Save kkaefer/966711 to your computer and use it in GitHub Desktop.
Bumps version numbers in package.json and creates a git tag for it
#!/usr/bin/env sh
version() {
ruby <<"EOS"
doc = File.read './package.json'
regex = /("version"\s*:\s*")([^"]+)(")/
cur = regex.match(doc)
if ENV['CMD'] == 'major' then
nxt = (cur[2].to_i + 1).to_s + ".0.0"
elsif ENV['CMD'] == 'minor' then
nxt = cur[2].gsub(/\d+\.\d+$/) { |n| (n.to_i + 1).to_s + ".0" }
else
nxt = cur[2].gsub(/\d+$/) { |n| n.to_i + 1 }
end
doc = doc.gsub(regex, "\\1" + nxt + "\\3")
File.open('./package.json', 'w') {|f| f.write(doc) }
print nxt
EOS
}
! test -z "`git diff package.json`" && \
echo "ERROR: Can't bump with changes to package.json." && \
exit 1
! test -z "`git diff --cached`" && \
git status -s -uno && \
echo "ERROR: Can't bump with staged changes." && \
exit 1
VERSION=`CMD=$1 version` && \
git add -p ./package.json
! test -z "`git diff package.json`" && \
git checkout -- package.json && \
echo "NOTICE: Aborted version bump." && \
exit 1
git commit -m "bump to version v$VERSION"
git tag "v$VERSION" && \
git push && \
git push --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment