forked from guard/listen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
151 lines (116 loc) · 3.64 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
if ENV["CI"] != "true"
require "rubocop/rake_task"
RuboCop::RakeTask.new(:rubocop)
task default: [:spec, :rubocop]
else
task default: [:spec]
end
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
begin
STDOUT.puts "Release #{@project_name} #{@version} to RubyGems? (y/n)"
input = STDIN.gets.chomp.downcase
end while !%w(y n).include?(input)
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
begin
STDOUT.puts "Would you like to publish this GitHub release now? (y/n)"
input = STDIN.gets.chomp.downcase
end while !%w(y n).include?(input)
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 = "Listen"
CURRENT_VERSION = Listen::VERSION
def releaser
$releaser ||= Releaser.new(
project_name: PROJECT_NAME,
gem_name: "guard",
github_repo: "guard/guard",
version: CURRENT_VERSION)
end
namespace :release do
desc "Push #{PROJECT_NAME} #{CURRENT_VERSION} to RubyGems and publish"\
" its GitHub release"
task full: ["release:gem", "release:github"]
desc "Push #{PROJECT_NAME} #{CURRENT_VERSION} to RubyGems"
task :gem do
releaser.rubygems
end
desc "Publish #{PROJECT_NAME} #{CURRENT_VERSION} GitHub release"
task :github do
releaser.github
end
end