-
Notifications
You must be signed in to change notification settings - Fork 33
/
Rakefile
252 lines (215 loc) · 7.55 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# frozen_string_literal: true
## --- BEGIN LICENSE BLOCK ---
# Copyright (c) 2016-present WeWantToKnow AS
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
## --- END LICENSE BLOCK ---
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'rubocop/rake_task'
require 'u3d'
UI = U3dCore::UI
# doesn't yet support dot file
# https://github.com/skywinder/github-changelog-generator/issues/473
# require 'github_changelog_generator/task'
RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new
class GithubChangelogGenerator
PATH = '.github_changelog_generator'
class << self
def future_release
s = File.read(PATH)
s.split("\n").each do |line|
m = line.match(/future-release=v(.*)/)
return m[1] if m
end
raise "Couldn't find future-release in #{PATH}"
end
def future_release=(nextv)
s = File.read(PATH)
lines = s.split("\n").map do |line|
m = line.match(/future-release=v(.*)/)
if m
"future-release=v#{nextv}"
else
line
end
end
File.write(PATH, "#{lines.join("\n")}\n")
end
end
end
class U3dCode
PATH = 'lib/u3d/version.rb'
class << self
def version=(version)
s = File.read(PATH)
lines = s.split("\n").map do |line|
m = line.match(/(.*VERSION = ').*(')/)
if m
"#{m[1]}#{version}#{m[2]}"
else
line
end
end
File.write(PATH, "#{lines.join("\n")}\n")
end
end
end
def run_command(command, error_message = nil)
output = `#{command}`
unless $CHILD_STATUS.success?
error_message = "Failed to run command '#{command}'" if error_message.nil?
UI.user_error!(error_message)
end
output
end
###
### see https://github.com/github/hub/issues/2055
def ensure_hub!
require 'English'
`which hub`
raise "Missing hub command. This script requires the hub command. Get it from https://hub.github.com" unless $CHILD_STATUS.exitstatus.zero?
end
def hub_config
@hub_config ||=
begin
require 'YAML'
File.open(File.expand_path("~/.config/hub"), 'r:bom|utf-8') { |f| Psych.safe_load(f.read) }
end
end
def hub_user(server)
hub_config[server][0]['user']
end
def hub_fork_remote_name
server = 'github.com'
user = hub_user(server)
`git remote -v`.split("\n").map(&:split).select { |a| a[2] == '(push)' && a[1] =~ /#{server}.#{user}/ }.first[0]
end
def github_team
%w[lacostej niezbop]
end
def github_reviewers
server = 'github.com'
github_team - [hub_user(server)]
end
###
desc 'Ensure the git repository is clean. Fails otherwise'
task :ensure_git_clean do
branch = run_command('git rev-parse --abbrev-ref HEAD', "Couldn't get current git branch").strip
UI.user_error!("You are not on 'master' but on '#{branch}'") unless branch == "master"
output = run_command('git status --porcelain', "Couldn't get git status")
UI.user_error!("git status not clean:\n#{output}") unless output == ""
end
desc 'Ensure we are ready to prepare a PR'
task :prepare_git_pr, [:pr_branch] do |_t, args|
pr_branch = args['pr_branch']
raise "Missing pr_branch argument" unless pr_branch
UI.user_error! "Prepare git PR stopped by user" unless UI.confirm("Creating PR branch #{pr_branch}")
run_command("git checkout -b #{pr_branch}")
end
desc 'Prepare a release: check repo status, generate changelog, create PR'
task pre_release: 'ensure_git_clean' do
require 'u3d/version'
nextversion = U3d::VERSION
# check not already released
output = run_command("git tag -l v#{nextversion}").strip
UI.user_error! "Version '#{nextversion}' already released. Run 'rake bump'" unless output == ''
gh_future_release = GithubChangelogGenerator.future_release
UI.user_error! "GithubChangelogGenerator version #{gh_future_release} != #{nextversion}" unless gh_future_release == nextversion
pr_branch = "release_#{nextversion}"
Rake::Task["prepare_git_pr"].invoke(pr_branch)
Rake::Task["changelog"].invoke
sh('git diff')
# FIXME: cleanup branch, etc
UI.user_error! "Pre release stopped by user." unless UI.confirm("CHANGELOG PR for version #{nextversion}. Confirm?")
msg = "Preparing release for #{nextversion}"
sh 'git add CHANGELOG.md'
sh "git commit -m '#{msg}'"
ensure_hub!
hub_remote = hub_fork_remote_name
sh "git push #{hub_remote}"
sh "hub pull-request -m '#{msg}' -r '#{github_reviewers.join(',')}' -l nochangelog"
sh 'git checkout master'
sh "git branch -D #{pr_branch}"
end
desc 'Bump the version number to the version entered interactively; pushes a commit to master'
task bump: 'ensure_git_clean' do
nextversion = UI.input "Next version will be:"
UI.user_error! "Bump version stopped by user" unless UI.confirm("Next version will be #{nextversion}. Confirm?")
U3dCode.version = nextversion
GithubChangelogGenerator.future_release = nextversion
sh 'bundle exec rspec'
sh 'git add .github_changelog_generator lib/u3d/version.rb Gemfile.lock'
sh "git commit -m 'Bump version to #{nextversion}'"
sh 'git push'
end
desc 'Update the changelog, no commit made'
task :changelog do
UI.user_error!('No CHANGELOG_GITHUB_TOKEN in the environment') unless ENV.key? 'CHANGELOG_GITHUB_TOKEN'
puts "Updating changelog #{ENV['CHANGELOG_GITHUB_TOKEN']}"
sh "github_changelog_generator" if ENV['CHANGELOG_GITHUB_TOKEN']
end
desc 'Run all rspec tests'
task :test_all do
formatter = "--format progress"
formatter += ' --pattern "./spec/**/*_spec.rb"'
rspec_args = formatter
sh "rspec #{rspec_args}"
end
def parse_changelog
releases = {}
buffer = nil
version = nil
File.readlines("CHANGELOG.md").each do |line|
if (m = line.match(/^## \[(.*)\]/))
releases[version] = buffer if buffer
version = m[1]
buffer = "#{version}\n\n"
else
next unless version # skip first lines
buffer += line
end
end
releases[version] = buffer
releases
end
desc 'Create missing Github releases from changelog'
task :create_missing_github_releases, [:force] do |_t, args|
force = args[:force] || false
releases = parse_changelog
known_releases = `hub release`.split("\n")
releases.keys.reverse.each do |version|
exist = known_releases.include? version
if exist && !force
puts "Skipping existing version #{version}"
next
end
action = exist ? "edit" : "create"
changelog = releases[version]
puts "About to #{action} version #{version}"
require "tempfile"
Tempfile.create("githubchangelog") do |changelog_file|
File.write(changelog_file, changelog)
command = "hub release #{action} #{version} --file #{changelog_file.path}"
`#{command}`
end
end
end
task default: %i[rubocop test_all]