-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRakefile
475 lines (381 loc) · 12.5 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#!/usr/bin/ruby
#
# Common Rakefile for multiple elisp-projects; updates itself (if dir exists) from
#
# ../rakefile-for-elisp/Rakefile
#
# and vice versa.
#
# Configure via rake_config.yml
#
require 'fileutils'
include FileUtils
require 'yaml'
require 'set'
require 'open3'
#
# Global variables
#
# Configuration and options
$conf = YAML::load(File.open('rake_config.yml'))
$conf.transform_keys!(&:to_sym)
$conf[:file] = 'rake_config.yml'
$v = (verbose == true)
# Shared information between tasks
# from lisp-source
$version_lisp = nil
$commentary_lisp = Hash.new
$changelog_lisp = Hash.new
# from changelog
$changelog = Hash.new
$changelog_until = Hash.new
#
# Helper functions
#
def heading text, large = false, warn = false
if large
puts "\n"
print "\e[32m"
dash = "==="
puts dash * 12
puts "#{dash} #{text}"
puts dash * 12
else
print "\e[#{warn ? 35 : 33}m"
puts "--- #{text}"
end
print "\e[0m"
end
def compare_semver one,two
vs = [one, two].map do |x|
x.match(/(\d+)\.(\d+)/) || abort("Argument '#{x}' does not contain a semantic version number")
end
( 2*(vs[0][1].to_i<=>vs[1][1].to_i) + (vs[0][2].to_i<=>vs[1][2].to_i) ) <=> 0
end
def accept fname,nname
heading "Accept changes to #{fname} ?"
system "diff #{fname} #{nname}"
if $? == 0
puts "No differences between #{fname} and #{nname}."
rm nname
else
puts "\n\n"
system "ls -l #{fname} #{nname}"
heading "Changes to #{fname}.\nPlease review 'diff old new' and output of 'ls'\nType RETURN to accept or Ctrl-c Ctrl-c to reject:"
$stdin.gets
make_backup fname
mv nname,fname
end
end
def make_backup file
dir = File.dirname(file) + '/backup'
mkdir dir,{:verbose => $v} unless File.directory?(dir)
backs = [ file ] + (1..12).map {|i| dir + '/' + File.basename(file) + "_" + i.to_s}
pairs = backs[0..-2].zip(backs[1..-1]).reverse
pairs.each do |p|
next unless File.exist?(p[0])
cp p[0],p[1],{:verbose => $v}
end
end
def maybe_copy from,to
puts "Maybe copy #{from} to #{to}" if $v
[from, to].each {|f| return false unless File.exist?(f)}
return false if File.mtime(to) > File.mtime(from)
system("diff -q #{from} #{to} >/dev/null 2>&1")
return false if $?.exitstatus == 0
make_backup to
cp from,to,{:verbose => $v}
return true
end
def brushup text
text.sub!(/\A[\s\n]+/,'')
text.sub!(/[\s\n]+\Z/,'')
text.chomp!
end
def write_as_org file, level, hash, &compare
keys = hash.keys
keys = keys.sort(&compare) if compare
keys.each do |key|
file.puts '*' * level + " #{key}\n\n"
hash[key].lines.each {|l| file.puts (' ' * (level+1) + l).rstrip}
file.puts "\n"
end
pp hash if $v
end
def forward_to file, text
line = nil
begin
line = file.gets
end until !line || line.start_with?(text)
line
end
#
# Individual tasks
#
#
# Tasks, that collect information, no desc to avoid them beeing listed with -T
#
# Compare with rakefile in other dir and update
task :update_rake do
this_rf = __FILE__
parent_rf = File.expand_path('..', File.dirname(this_rf)) + '/rakefile-for-elisp/Rakefile'
system("touch -t 190001010000 #{parent_rf} >/dev/null 2>&1") unless File.exist?(parent_rf)
if maybe_copy this_rf, parent_rf
heading "Updated #{parent_rf} from #{this_rf}"
elsif maybe_copy parent_rf, this_rf
heading "This rakefile #{this_rf} has been updated from #{parent_rf}; please rerun"
exit
end
end
# Extract version from sourcefile
task :extract_version => [:update_rake] do
heading "Extract version from #{$conf[:source]}"
File.open($conf[:source]).each do |line|
if line.match(/^;; Version: (\d+\.\d+\.\d+)\s*/)
$version_lisp = Regexp.last_match[1]
puts $version_lisp
break
end
end
end
# Extract commentary from sourcefile
task :extract_commentary => [:update_rake] do
heading "Extract commentary from #{$conf[:source]}"
commentary_keys_matcher = Regexp.new('^;; (' + $conf[:required_pieces_commentary].join('|') + '):\s*$')
key = nil
File.open($conf[:source]).
drop_while { |line| !line.start_with?(';;; Commentary:') }.
drop(2).take_while { |line| line.start_with?(';;') }.each do |line|
if line.match(commentary_keys_matcher)
key = Regexp.last_match[1]
$commentary_lisp[key] = ''
puts key
else
$commentary_lisp[key] += line.sub(/^;;( )?/,'').rstrip + "\n" if key
end
end
$commentary_lisp.each_key do |key|
brushup $commentary_lisp[key]
end
pp $commentary_lisp if $v
pieces_seen = Set.new($commentary_lisp.keys)
pieces_required = Set.new($conf[:required_pieces_commentary])
abort "ERROR: Pieces of commentary seen #{pieces_seen} does not equal pieces of commentary required #{pieces_required}" unless pieces_seen == pieces_required
end
# Extract change log from sourcefile
task :extract_changelog_lisp => [:extract_version] do
heading "Extract Changelog from #{$conf[:source]}"
version = offset = nil
File.open($conf[:source]).each.
drop_while { |line| !line.start_with?(';;; Change Log:') }.
drop(2).
take_while { |line| line.start_with?(';;') }.each do |line|
if line.match(/^(;;\s+)Version (\d+\.\d+)\s*/)
version = Regexp.last_match[2]
$changelog_lisp[version] = ''
offset = Regexp.last_match[1]
puts version
elsif version
$changelog_lisp[version] += (line[offset.length..-1] || '').rstrip + "\n"
end
end
$changelog_lisp.each_key do |version|
brushup $changelog_lisp[version]
end
pp $changelog_lisp if $v
version_lisp_latest = $changelog_lisp.keys.max {|a,b| compare_semver(a,b)}
abort "Mismatch in #{$conf[:source]}: Latest version from Changelog '#{version_lisp_latest}' does not match version specified in header '#{$version_lisp}'" unless $version_lisp.sub(/\.\d+$/,'') == version_lisp_latest
end
# Extract change log from changelog
task :extract_changelog => [:extract_changelog_lisp] do
fname = 'ChangeLog.org'
heading "Extract Changelog from #{fname}"
version = nil
File.open(fname).each do |line|
if mdata = line.match(/^\* (\d+\.\d+) until (\S.*\S)/)
version = mdata[1]
$changelog[version] = ''
$changelog_until[version] = mdata[2]
puts version
else
$changelog[version] += (line[2..-1] || '').rstrip + "\n" if version
end
end
now = Time.now.strftime("%Y-%m-%d %a")[0..-2]
$changelog_lisp.each_key do |version|
$changelog[version] = $changelog_lisp[version]
$changelog_until[version] ||= now
end
version_latest = $changelog.keys.max {|a,b| compare_semver(a,b)}
$changelog_until[version_latest] = now
$changelog.each_key do |version|
brushup $changelog[version]
end
pp $changelog if $v
end
#
# Tasks, that update files with collected information
#
desc 'Update Changelog with information from sourcefile'
task :update_changelog => [:extract_changelog, :extract_version] do
fname = 'ChangeLog.org'
heading "Update #{fname}",true
nname = fname + '.new'
File.open(nname,'w') do |nfile|
$changelog.keys.sort {|a,b| compare_semver(b,a)}.each do |version|
nfile.puts "* #{version} until #{$changelog_until[version]}\n\n"
$changelog[version].lines.each {|l| nfile.puts " #{l}".rstrip}
nfile.puts "\n"
end
end
accept fname,nname
end
desc 'Update Readme with information from sourcefile'
task :update_readme => [:extract_changelog, :extract_version, :extract_commentary] do
fname = 'README.org'
heading "Update #{fname}",true
nname = fname + ".new"
toc = File.read(fname).lines.select {|l| l.start_with?('** ')}.map {|l| l[3..-1].chomp}
unseen = Set.new([:version, :toc, :about, :changelog])
tcvi = ' The current version is '
File.open(nname,'w') do |nfile|
File.open(fname) do |file|
loop do
line ||= file.gets
break unless line
# content of line will in any case written at bottom of loop, until then:
# Write something else, replace content of line or leave as is
if line.start_with?(tcvi)
heading 'Version'
line = tcvi + $version_lisp + '.'
unseen.delete(:version)
end
if line.start_with?('** Table of Contents')
heading 'Table of contents'
nfile.puts line + "\n"
toc.drop(1).each {|t| nfile.puts " - [[##{t.downcase.tr(' ','-')}][#{t}]]"}
nfile.puts "\n"
line = forward_to(file,'** ')
unseen.delete(:toc)
end
if line.start_with?('** About this Package')
heading 'Commentary'
nfile.puts line + "\n"
write_as_org nfile,3,$commentary_lisp
line = forward_to(file,'** ')
unseen.delete(:about)
end
if line.start_with?('** Latest Change Log')
heading 'Latest Change log'
nfile.puts line + "\n See ChangeLog.org for older entries.\n\n"
write_as_org nfile,3,$changelog_lisp
line = forward_to(file,'* ')
unseen.delete(:changelog)
end
# now write whatever current content of line is
nfile.puts line if line
end
end
end
abort "Did not see #{unseen.inspect} in #{fname}" if unseen.length > 0
accept fname,nname
end
desc 'Update sourcefile with information from sourcefile'
task :update_lisp => [:extract_changelog, :extract_version, :extract_commentary] do
fname = $conf[:source]
heading "Update #{fname}",true
nname = fname + ".new"
unseen = Set.new([:version, :commentary])
tiv = 'This is version'
File.open(nname,'w') do |nfile|
File.open(fname) do |file|
loop do
line ||= file.gets
break unless line
# content of line will in any case written at bottom of loop, until then:
# Write something else, replace content of line or leave as is
if line.start_with?("(defvar #{$conf[:package]}-version")
heading 'Version'
line.sub!(/\d+\.\d+\.\d+/,$version_lisp)
unseen.delete(:version)
end
if line['For Rake: Insert here']
heading 'Commentary'
nfile.write line
first = true
$conf[:pieces_for_docstring].each do |piece|
abort "ERROR: Configuration item 'pieces_for_docstring' from $conf[:file] has unkonwn docstring-piece #{piece}" unless $commentary_lisp[piece]
nfile.print first ? " \"" : "\n\n#{piece}:\n\n"
nfile.print $commentary_lisp[piece]
first = false
end
forward_to(file,tiv)
line = "\n\n#{tiv} #{$version_lisp} of #{$conf[:source]}.\n"
unseen.delete(:commentary)
end
# now write whatever current content of line is
nfile.puts line if line
end
end
end
abort "Did not see #{unseen.inspect} in #{fname}" if unseen.length > 0
accept fname,nname
end
desc 'Describe building process'
task :h => [:extract_version] do
within = false
doc = File.open(%x{git rev-parse --show-toplevel}.chomp + '/README.org').each do |line|
within = line.include?('Preparing') if line.start_with?('*')
print line if within
end
puts "Version from lisp: #{$version_lisp}"
puts "Latest tag from git: " + %x(git describe).chomp
end
desc 'Run all tests from directory test'
task :test => [:update_rake] do
heading "Run tests",true
command = <<-END.lines.map {|l| l.strip!}.join(' ')
emacs
--no-init-file
--no-site-file
--no-site-lisp
--eval "(set-variable 'make-backup-files nil)"
--eval "(set-variable 'auto-save-default nil)"
--eval "(set-variable 'create-lockfiles nil)"
--eval "(menu-bar-mode -1)"
--eval "(setq load-prefer-newer t)"
--eval "(add-to-list 'load-path \\\".\\\")"
--eval "(setq package-user-dir \\\"#{File.dirname(__FILE__)+"/elpa"}\\\")"
--eval "(setq base-dir \\\"#{File.dirname(__FILE__)}\\\")"
--eval "(package-initialize)"
--load ert
--load #{File.dirname(__FILE__)+"/test/"+$conf[:testfile]}
test/#{$conf[:testfile]}
#{$conf[:source]}
--eval "(ert-run-tests-batch-and-exit)"
--batch
END
puts command
at_summary = false
Open3.popen2e(command) do |stdin, stdout_stderr, status_thread|
stdout_stderr.each_line do |line|
at_summary = true if line.match?(/^Ran \d+ /)
if at_summary
puts line
elsif line.lstrip.start_with?('passed')
heading line
elsif line.lstrip.start_with?('FAILED')
heading line,false,true
elsif $v
puts line
end
end
abort "ERROR: Command ended with error.\nRerun with '-v' for details." unless status_thread.value.success?
end
end
desc 'Update all files'
task :update => [:update_changelog, :update_readme, :update_lisp, :test] do
end
desc 'Update all files and run tests'
task :default => [:update, :test] do
end