-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrake_extensions.rb
254 lines (220 loc) · 5.85 KB
/
rake_extensions.rb
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
# frozen_string_literal: true
require 'benchmark'
require 'fileutils'
require 'rake'
# os detection
module OS
def self.windows?
(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
end
def self.mac?
(/darwin/ =~ RUBY_PLATFORM) != nil
end
def self.unix?
!OS.windows?
end
def self.linux?
OS.unix? && !OS.mac?
end
def self.jruby?
RUBY_ENGINE == 'jruby'
end
end
# benchmarking tasks
$task_benchmarks = []
# add some benchmark logging to Task
class Rake::Task
def execute_with_benchmark(*args)
puts "***************** executing #{name}"
bm = Benchmark.realtime { execute_without_benchmark(*args) }
$task_benchmarks << [name, bm]
end
alias execute_without_benchmark execute
alias execute execute_with_benchmark
end
at_exit do
total_time = $task_benchmarks.reduce(0) { |acc, x| acc + x[1] }
$task_benchmarks
.sort { |a, b| b[1] <=> a[1] }
.each do |res|
percentage = res[1] / total_time * 100
next unless percentage.round > 0
percentage_bar = ''
percentage.round.times { percentage_bar += '|' }
puts "#{percentage_bar} (#{'%.1f' % percentage} %) #{res[0]} ==> #{'%.1f' % res[1]}s"
end
puts "total time was: #{'%.1f' % total_time}"
end
# version management
class Versioner
def self.for(type, version_dir)
case type
when :gemspec
GemspecVersioner.new(version_dir)
when :package_json
JsonVersioner.new(version_dir)
when :cargo_toml
TomlVersioner.new(version_dir)
end
end
end
def current_version_with_regex(filelist, r)
current_version = nil
FileUtils.cd @version_dir, :verbose => false do
filelist.each do |file|
text = File.read(file)
if match = text.match(r)
current_version = match.captures[1]
end
end
end
current_version
end
class Versioner
def initialize(version_dir)
@version_dir = version_dir
end
def get_next_version(jump)
current_version = get_current_version
v = Version.new(current_version)
v.send(jump)
end
def increment_version(jump)
next_version = get_next_version(jump)
puts "increment version from #{get_current_version} ==> #{next_version}"
update_version(next_version)
end
end
class TomlVersioner < Versioner
VERSION_REGEX = /^(version\s=\s['\"])(\d+\.\d+\.\d+)(['\"])/i.freeze
def get_current_version
current_version_with_regex(['Cargo.toml'], VERSION_REGEX)
end
def update_version(new_version)
FileUtils.cd @version_dir, :verbose => false do
['Cargo.toml'].each do |file|
text = File.read(file)
new_contents = text.gsub(VERSION_REGEX, "\\1#{new_version}\\3")
File.open(file, 'w') { |f| f.puts new_contents }
end
end
end
end
class GemspecVersioner < Versioner
VERSION_REGEX = /^(\s*.*?\.version\s*?=\s*['\"])(.*)(['\"])/i.freeze
DATE_REGEX = /^(\s*.*?\.date\s*?=\s*['\"])(.*)(['\"])/.freeze
def get_current_version
current_version_with_regex(FileList['*.gemspec'], VERSION_REGEX)
end
def update_version(new_version)
FileUtils.cd @version_dir, :verbose => false do
FileList['*.gemspec'].each do |file|
text = File.read(file)
today = Time.now.strftime('%Y-%m-%d')
correct_date_contents = text.gsub(DATE_REGEX, "\\1#{today}\\3")
new_contents = correct_date_contents.gsub(VERSION_REGEX, "\\1#{new_version}\\3")
File.open(file, 'w') { |f| f.write new_contents }
end
end
end
end
class JsonVersioner < Versioner
VERSION_REGEX = /^(\s+['\"]version['\"]:\s['\"])(.*)(['\"])/i.freeze
def get_current_version
current_version_with_regex(['package.json'], VERSION_REGEX)
end
def update_version(new_version)
FileUtils.cd @version_dir, :verbose => false do
['package.json'].each do |file|
text = File.read(file)
new_contents = text.gsub(VERSION_REGEX, "\\1#{new_version}\\3")
File.open(file, 'w') { |f| f.puts new_contents }
end
end
end
end
class Version < Array
def initialize(s)
super(s.split('.').map(&:to_i))
end
def as_version_code
get_major * 1000 * 1000 + get_minor * 1000 + get_patch
end
def <(x)
(self <=> x) < 0
end
def >(x)
(self <=> x) > 0
end
def ==(x)
(self <=> x) == 0
end
def patch
patch = last
self[0...-1].concat([patch + 1])
end
def minor
self[1] = self[1] + 1
self[2] = 0
self
end
def major
self[0] = self[0] + 1
self[1] = 0
self[2] = 0
self
end
def get_major
self[0]
end
def get_minor
self[1]
end
def get_patch
self[2]
end
def to_s
join('.')
end
end
## git related utilities
desc 'push tag to github'
task :push do
sh 'git push origin'
current_version = get_current_version
sh "git push origin #{current_version}"
end
def assert_tag_exists(version)
raise "tag #{version} missing" if `git tag -l #{version}`.empty?
end
def create_changelog(current_version, next_version)
sha1s = `git log #{current_version}..HEAD --oneline`.strip.split(/\n/).collect { |line| line.split(' ').first }
log_entries = []
sha1s.each do |sha1|
raw_log = `git log --format=%B -n 1 #{sha1}`.strip
log_lines = raw_log.split(/\n/)
first_line = true
entry = log_lines
.reject { |x| x.strip == '' }
.collect do |line|
if line =~ /^\s*\*/
line.sub(/\s*\*/, ' *').to_s
else
res = first_line ? "* #{line}" : " #{line}"
first_line = false
res
end
end
log_entries << entry
end
log = log_entries.join("\n")
date = Time.now.strftime('%m/%d/%Y')
log_entry = "### [#{next_version}] - #{date}\n#{log}"
puts "logmessages:\n#{log}"
['CHANGELOG.md'].each do |file|
File.open(file, 'w') { |f| f.write('# Changelog') } unless File.exist?(file)
text = File.read(file)
new_contents = text.gsub(/^#\sChangelog/, "# Changelog\n\n#{log_entry}")
File.open(file, 'w') { |f| f.puts new_contents }
end
end