From 5cb8e97072e82b17ac27a5e206505387ac7b2f9f Mon Sep 17 00:00:00 2001 From: Trevor Rowe Date: Fri, 24 Oct 2014 16:24:44 -0700 Subject: [PATCH] Added automated release rake tasks. --- Rakefile | 4 +++- lib/jmespath.rb | 1 + lib/jmespath/version.rb | 3 +++ tasks/changelog.rake | 36 ++++++++++++++++++++++++++++++++++++ tasks/git.rake | 31 +++++++++++++++++++++++++++++++ tasks/github.rake | 29 +++++++++++++++++++++++++++++ tasks/release.rake | 41 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 lib/jmespath/version.rb create mode 100644 tasks/changelog.rake create mode 100644 tasks/git.rake create mode 100644 tasks/github.rake create mode 100644 tasks/release.rake diff --git a/Rakefile b/Rakefile index 4d6340f..6bd264f 100644 --- a/Rakefile +++ b/Rakefile @@ -1,4 +1,6 @@ -$VERSION = File.read(File.expand_path('../VERSION', __FILE__)).strip +$VERSION = ENV['VERSION'] || + File.read(File.expand_path('../VERSION', __FILE__)).strip +$GITHUB_ACCESS_TOKEN = ENV['JMESPATH_GITHUB_ACCESS_TOKEN'] Dir.glob('**/*.rake').each do |task_file| load task_file diff --git a/lib/jmespath.rb b/lib/jmespath.rb index af60219..da93fe9 100644 --- a/lib/jmespath.rb +++ b/lib/jmespath.rb @@ -8,6 +8,7 @@ module JMESPath autoload :Token, 'jmespath/token' autoload :TokenStream, 'jmespath/token_stream' autoload :TreeInterpreter, 'jmespath/tree_interpreter' + autoload :VERSION, 'jmespath/version' class << self diff --git a/lib/jmespath/version.rb b/lib/jmespath/version.rb new file mode 100644 index 0000000..1558601 --- /dev/null +++ b/lib/jmespath/version.rb @@ -0,0 +1,3 @@ +module JMESPath + VERSION = '0.2.0' +end diff --git a/tasks/changelog.rake b/tasks/changelog.rake new file mode 100644 index 0000000..f4ecf52 --- /dev/null +++ b/tasks/changelog.rake @@ -0,0 +1,36 @@ +task 'changelog:version' do + # replaces "Next Release (TBD)" in the CHANGELOG with a version and date + changelog = File.open('CHANGELOG.md', 'r', encoding: 'UTF-8') { |f| f.read } + changelog = changelog.lines + changelog[0] = "#{$VERSION} (#{Time.now.strftime('%Y-%m-%d')})\n" + changelog = changelog.join + File.open('CHANGELOG.md', 'w', encoding: 'UTF-8') { |f| f.write(changelog) } + sh("git add CHANGELOG.md") +end + +task 'changelog:next_release' do + # inserts a "Next Release (TDB)" section at the top of the CHANGELOG + lines = [] + lines << "Next Release (TBD)\n" + lines << "------------------\n" + lines << "\n" + changelog = File.open('CHANGELOG.md', 'r', encoding: 'UTF-8') { |f| f.read } + changelog = lines.join + changelog + File.open('CHANGELOG.md', 'w', encoding: 'UTF-8') { |f| f.write(changelog) } + sh("git add CHANGELOG.md") + sh("git commit -m 'Added next release section to the changelog.'") +end + +task 'changelog:latest' do + # Returns the contents of the most recent CHANGELOG section + changelog = File.open('CHANGELOG.md', 'r', encoding: 'UTF-8') { |f| f.read } + lines = [] + changelog.lines[3..-1].each do |line| + if line.match(/^\d+\.\d+\.\d+/) + break + else + lines << line + end + end + puts lines[0..-2].join +end diff --git a/tasks/git.rake b/tasks/git.rake new file mode 100644 index 0000000..c107af4 --- /dev/null +++ b/tasks/git.rake @@ -0,0 +1,31 @@ +task 'git:require-clean-workspace' do + # Ensure the git repo is free of unstaged or untracked files prior + # to building / testing / pushing a release. + unless `git diff --shortstat 2> /dev/null | tail -n1` == '' + warn('workspace must be clean to release') + exit + end +end + +task 'git:tag' do + sh("git commit -m \"Bumped version to v#{$VERSION}\"") + sh("git tag -a -m \"$(rake git:tag_message)\" v#{$VERSION}") +end + +task 'git:tag_message' do + issues = `git log $(git describe --tags --abbrev=0)...HEAD -E --grep '#[0-9]+' 2>/dev/null` + issues = issues.scan(/((?:\S+\/\S+)?#\d+)/).flatten + msg = "Tag release v#{$VERSION}" + msg << "\n\n" + unless issues.empty? + msg << "References: #{issues.uniq.sort.join(', ')}" + msg << "\n\n" + end + msg << `rake changelog:latest` + puts msg +end + +task 'git:push' do + sh('git push origin') + sh('git push origin --tags') +end diff --git a/tasks/github.rake b/tasks/github.rake new file mode 100644 index 0000000..0a6ff68 --- /dev/null +++ b/tasks/github.rake @@ -0,0 +1,29 @@ +task 'github:require-access-token' do + unless $GITHUB_ACCESS_TOKEN + warn("you must export JMESPATH_GITHUB_ACCESS_TOKEN") + exit + end +end + +task 'github:release' do + require 'octokit' + + gh = Octokit::Client.new(access_token: $GITHUB_ACCESS_TOKEN) + + repo = 'trevorrowe/jmespath.rb' + tag_ref_sha = `git show-ref v#{version}`.split(' ').first + tag = gh.tag(repo, tag_ref_sha) + + release = gh.create_release(repo, "v#{version}", { + name: 'Release v' + version + ' - ' + tag.tagger.date.strftime('%Y-%m-%d'), + body: tag.message.lines[2..-1].join, + prerelease: version.match('rc') ? true : false, + }) + + gh.upload_asset(release.url, 'docs.zip', + :content_type => 'application/octet-stream') + + gh.upload_asset(release.url, "jmespath-#{$VERSION}.gem", + :content_type => 'application/octet-stream') + +end diff --git a/tasks/release.rake b/tasks/release.rake new file mode 100644 index 0000000..35d52ed --- /dev/null +++ b/tasks/release.rake @@ -0,0 +1,41 @@ +task 'release:require-version' do + unless ENV['VERSION'] + warn("usage: VERSION=x.y.z rake release") + exit + end +end + +task 'release:bump-version' do + sh("echo '#{$VERSION}' > VERSION") + path = 'lib/jmespath/version.rb' + file = File.read(path) + file = file.gsub(/VERSION = '.+?'/, "VERSION = '#{$VERSION}'") + File.open(path, 'w') { |f| f.write(file) } + sh("git add #{path}") + sh("git add VERSION") +end + +task 'release:stage' => [ + 'release:require-version', + 'github:require-access-token', + 'git:require-clean-workspace', + 'test:unit', + 'changelog:version', + 'release:bump-version', + 'git:tag', + 'gem:build', + 'docs:zip', +] + +task 'release:publish' => [ + 'git:push', + 'gems:push', + 'github:release', +] + +task 'release:cleanup' => [ + 'changelog:next_release', +] + +desc "Public release" +task :release => ['release:stage', 'release:publish', 'release:cleanup']