Permalink
Browse files

refactor Rakefile

  • Loading branch information...
e2 committed May 19, 2016
1 parent 28b2dc4 commit e96c22514455148c37a48465e60ab66de832746f
Showing with 117 additions and 120 deletions.
  1. +3 −120 Rakefile
  2. +114 −0 lib/tasks/releaser.rb
View
123 Rakefile
@@ -1,7 +1,7 @@
require "nenv"
require "bundler/gem_tasks"
-require "forwardable"
+require "tasks/releaser"
default_tasks = []
@@ -40,127 +40,10 @@ task default: default_tasks.map(&:name)
# TODO: for the above to work, also change Coveralls.wear_merged! instead of
# wear! in spec/spec_helper.rb
-class Releaser
- def initialize(options = {})
- @project_name = options.delete(:project_name) do
- fail "project_name is needed!"
- end
-
- @gem_name = options.delete(:gem_name) do
- fail "gem_name is needed!"
- end
-
- @github_repo = options.delete(:github_repo) do
- fail "github_repo is needed!"
- end
-
- @version = options.delete(:version) do
- fail "version is needed!"
- end
- end
-
- def full
- rubygems
- github
- end
-
- def rubygems
- loop do
- STDOUT.puts "Release #{@project_name} #{@version} to RubyGems? (y/n)"
- input = STDIN.gets.chomp.downcase
- break if %w(y n).include?(input)
- end
-
- exit if input == "n"
-
- Rake::Task["release"].invoke
- end
-
- def github
- tag_name = "v#{@version}"
-
- require "gems"
-
- _verify_released
- _verify_tag_pushed
-
- require "octokit"
- gh_client = Octokit::Client.new(netrc: true)
-
- gh_release = _detect_gh_release(gh_client, tag_name, true)
- return unless gh_release
-
- STDOUT.puts "Draft release for #{tag_name}:\n"
- STDOUT.puts gh_release.body
- STDOUT.puts "\n-------------------------\n\n"
-
- _confirm_publish
-
- return unless _update_release(gh_client, gh_release, tag_name)
-
- gh_release = _detect_gh_release(gh_client, tag_name, false)
-
- _success_summary(gh_release, tag_name)
- end
-
- private
-
- def _verify_released
- if @version != Gems.info(@gem_name)["version"]
- STDOUT.puts "#{@project_name} #{@version} is not yet released."
- STDOUT.puts "Please release it first with: rake release:gem"
- exit
- end
- end
-
- def _verify_tag_pushed
- tags = `git ls-remote --tags origin`.split("\n")
- return if tags.detect { |tag| tag =~ /v#{@version}$/ }
-
- STDOUT.puts "The tag v#{@version} has not yet been pushed."
- STDOUT.puts "Please push it first with: rake release:gem"
- exit
- end
-
- def _success_summary(gh_release, tag_name)
- href = gh_release.rels[:html].href
- STDOUT.puts "GitHub release #{tag_name} has been published!"
- STDOUT.puts "\nPlease enjoy and spread the word!"
- STDOUT.puts "Lack of inspiration? Here's a tweet you could improve:\n\n"
- STDOUT.puts "Just released #{@project_name} #{@version}! #{href}"
- end
-
- def _detect_gh_release(gh_client, tag_name, draft)
- gh_releases = gh_client.releases(@github_repo)
- gh_releases.detect { |r| r.tag_name == tag_name && r.draft == draft }
- end
-
- def _confirm_publish
- loop do
- STDOUT.puts "Would you like to publish this GitHub release now? (y/n)"
- input = STDIN.gets.chomp.downcase
- break if %w(y n).include?(input)
- end
-
- exit if input == "n"
- end
-
- def _update_release(gh_client, gh_release, tag_name)
- result = gh_client.update_release(gh_release.rels[:self].href, draft: false)
- return true if result
- STDOUT.puts "GitHub release #{tag_name} couldn't be published!"
- false
- end
-end
-
PROJECT_NAME = "Guard"
CURRENT_VERSION = Guard::VERSION
class GuardReleaser
- extend Forwardable
- def_delegator self, :rubygems, to: :releaser
- def_delegator self, :full, to: :releaser
-
def self.releaser
@releaser ||= Releaser.new(
project_name: PROJECT_NAME,
@@ -179,11 +62,11 @@ namespace :release do
desc "Push #{PROJECT_NAME} #{CURRENT_VERSION} to RubyGems"
task :gem do
- GuardReleaser.rubygems
+ GuardReleaser.releaser.rubygems
end
desc "Publish #{PROJECT_NAME} #{CURRENT_VERSION} GitHub release"
task :github do
- GuardReleaser.github
+ GuardReleaser.releaser.github
end
end
View
@@ -0,0 +1,114 @@
+# TODO: extract to gem?
+
+class Releaser
+ def initialize(options = {})
+ @project_name = options.delete(:project_name) do
+ fail "project_name is needed!"
+ end
+
+ @gem_name = options.delete(:gem_name) do
+ fail "gem_name is needed!"
+ end
+
+ @github_repo = options.delete(:github_repo) do
+ fail "github_repo is needed!"
+ end
+
+ @version = options.delete(:version) do
+ fail "version is needed!"
+ end
+ end
+
+ def full
+ rubygems
+ github
+ end
+
+ def rubygems
+ loop do
+ STDOUT.puts "Release #{@project_name} #{@version} to RubyGems? (y/n)"
+ input = STDIN.gets.chomp.downcase
+ break if %w(y n).include?(input)
+ end
+
+ exit if input == "n"
+
+ Rake::Task["release"].invoke
+ end
+
+ def github
+ tag_name = "v#{@version}"
+
+ require "gems"
+
+ _verify_released
+ _verify_tag_pushed
+
+ require "octokit"
+ gh_client = Octokit::Client.new(netrc: true)
+
+ gh_release = _detect_gh_release(gh_client, tag_name, true)
+ return unless gh_release
+
+ STDOUT.puts "Draft release for #{tag_name}:\n"
+ STDOUT.puts gh_release.body
+ STDOUT.puts "\n-------------------------\n\n"
+
+ _confirm_publish
+
+ return unless _update_release(gh_client, gh_release, tag_name)
+
+ gh_release = _detect_gh_release(gh_client, tag_name, false)
+
+ _success_summary(gh_release, tag_name)
+ end
+
+ private
+
+ def _verify_released
+ if @version != Gems.info(@gem_name)["version"]
+ STDOUT.puts "#{@project_name} #{@version} is not yet released."
+ STDOUT.puts "Please release it first with: rake release:gem"
+ exit
+ end
+ end
+
+ def _verify_tag_pushed
+ tags = `git ls-remote --tags origin`.split("\n")
+ return if tags.detect { |tag| tag =~ /v#{@version}$/ }
+
+ STDOUT.puts "The tag v#{@version} has not yet been pushed."
+ STDOUT.puts "Please push it first with: rake release:gem"
+ exit
+ end
+
+ def _success_summary(gh_release, tag_name)
+ href = gh_release.rels[:html].href
+ STDOUT.puts "GitHub release #{tag_name} has been published!"
+ STDOUT.puts "\nPlease enjoy and spread the word!"
+ STDOUT.puts "Lack of inspiration? Here's a tweet you could improve:\n\n"
+ STDOUT.puts "Just released #{@project_name} #{@version}! #{href}"
+ end
+
+ def _detect_gh_release(gh_client, tag_name, draft)
+ gh_releases = gh_client.releases(@github_repo)
+ gh_releases.detect { |r| r.tag_name == tag_name && r.draft == draft }
+ end
+
+ def _confirm_publish
+ loop do
+ STDOUT.puts "Would you like to publish this GitHub release now? (y/n)"
+ input = STDIN.gets.chomp.downcase
+ break if %w(y n).include?(input)
+ end
+
+ exit if input == "n"
+ end
+
+ def _update_release(gh_client, gh_release, tag_name)
+ result = gh_client.update_release(gh_release.rels[:self].href, draft: false)
+ return true if result
+ STDOUT.puts "GitHub release #{tag_name} couldn't be published!"
+ false
+ end
+end

0 comments on commit e96c225

Please sign in to comment.