-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRakefile
278 lines (234 loc) · 7.94 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# frozen_string_literal: true
## --- BEGIN LICENSE BLOCK ---
# Copyright (c) 2018-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 'logger'
require 'colored'
require 'highline/import'
module UI
# raised from crash!
class UICrash < StandardError
end
class EPipeIgnorerLogDevice < Logger::LogDevice
def initialize(logdev)
super
@logdev = logdev
end
def write(message)
@logdev.write(message)
rescue Errno::EPIPE
# ignored
end
end
class << self
def log
return @log if @log
$stdout.sync = true
@log ||= Logger.new(EPipeIgnorerLogDevice.new($stdout))
@log.formatter = proc do |severity, datetime, _progname, msg|
"#{format_string(datetime, severity)}#{msg}\n"
end
@log
end
def verbose?
false
end
def format_string(datetime = Time.now, severity = "")
timestamp ||= if verbose?
'%Y-%m-%d %H:%M:%S.%2N'
else
'%H:%M:%S'
end
s = []
s << "#{severity} " if verbose? && severity && !severity.empty?
s << "[#{datetime.strftime(timestamp)}] " if timestamp
s.join('')
end
def confirm(message)
verify_interactive!(message)
agree("#{format_string}#{message.to_s.yellow} (y/n)", true)
end
def user_error!(message)
raise StandardError, message.to_s.red
end
def input(message)
verify_interactive!(message)
ask("#{format_string}#{message.to_s.yellow}").to_s.strip
end
def error(message)
log.error(message.to_s.red)
end
def important(message)
log.error(message.to_s.yellow)
end
def success(message)
log.error(message.to_s.green)
end
def message(message)
log.info(message.to_s)
end
def deprecated(message)
log.error(message.to_s.bold.blue)
end
def command(message)
log.info("$ #{message}".cyan.underline)
end
def interactive?
interactive = true
interactive = false if $stdout.isatty == false
# interactive = false if Helper.ci?
return interactive
end
private
def verify_interactive!(message)
return if interactive?
important(message)
crash!("Could not retrieve response as the program runs in non-interactive mode")
end
def crash!(exception)
raise UICrash.new, exception.to_s
end
end
end
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 FreshsalesCode
PATH = 'lib/freshsales/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
require 'English'
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
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
# ensure 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 'freshsales/version'
nextversion = Freshsales::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}'"
sh "git push lacostej" # FIXME: hardcoded
# FIXME: check hub present
sh "hub pull-request -m '#{msg}'" # requires hub pre-release " -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?")
FreshsalesCode.version = nextversion
GithubChangelogGenerator.future_release = nextversion
sh 'bundle exec rspec'
sh 'git add .github_changelog_generator lib/freshsales/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
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"
if ENV["CIRCLECI"]
Dir.mkdir("/tmp/rspec/")
formatter += " -r rspec_junit_formatter --format RspecJunitFormatter -o /tmp/rspec/rspec.xml"
TEST_FILES = `(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)`.tr!("\n", ' ')
rspec_args = "#{formatter} #{TEST_FILES}"
else
formatter += ' --pattern "./spec/**/*_spec.rb"'
rspec_args = formatter
end
sh "bundle exec rspec #{rspec_args}"
end
task default: %i[rubocop test_all]