Skip to content

Instantly share code, notes, and snippets.

@mattray
Created November 27, 2013 21:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattray/7683491 to your computer and use it in GitHub Desktop.
Save mattray/7683491 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Wrap thor-scmver to build metadata
#
require 'thor-scmversion'
module ThorSCMVersion
class Tasks < Thor
namespace "version"
desc "bump TYPE [PRERELEASE_TYPE]", "Bump version number (type is major, minor, patch, prerelease or auto)"
method_option :default, type: :string, aliases: "-d"
def bump(type, prerelease_type = nil)
if type == 'auto'
type = process_body
end
current_version.bump! type, options.merge(prerelease_type: prerelease_type)
begin
say "Updating metadata and changelog", :yellow
write_metadata_and_changelog
say "Creating and pushing tags", :yellow
current_version.tag
say "Writing files: #{version_files.join(', ')}", :yellow
write_version
say "Tagged: #{current_version}", :green
rescue => e
say "Tagging #{current_version} failed due to error", :red
say e.to_s, :red
if e.respond_to? :status_code
exit e.status_code
else
exit 1
end
end
end
private
# we're auto-incrementing over the entire commit body message
def process_body
log = ShellUtils.sh('git log -1 --pretty=%B')
if log =~ /#major/
return 'major'
elsif log =~ /#minor/
return 'minor'
elsif log =~ /#noversion/
say "#noversion in commit message, nothing to do here", :yellow
exit
else
return 'patch'
end
end
def write_metadata_and_changelog
content = parse_metadata
File.open("metadata.rb", 'w') {|file| file.write content }
content = parse_changelog
File.open("CHANGELOG.md", 'w') {|file| file.write content }
ShellUtils.sh "git add metadata.rb CHANGELOG.md"
ShellUtils.sh "git commit -m 'auto_version to #{current_version}'"
ShellUtils.sh "git push origin master"
ShellUtils.sh "git stash"
end
def parse_metadata
content = ""
File.open("metadata.rb") do |f|
while line = f.gets
if line =~ /^\s*version\s+.*/
content << "version '#{current_version}'\n"
next
end
content << line
end
end
content
end
def parse_changelog
content = ""
inserted = false
File.open("CHANGELOG.md") do |f|
while line = f.gets
if line =~ /^## / && !inserted
content << "## #{current_version}:\n"
content << "* #{ShellUtils.sh 'git log -1 --pretty=%s'}\n"
content << line
inserted = true
next
end
content << line
end
end
content
end
end
end
ThorSCMVersion::Tasks.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment