-
Notifications
You must be signed in to change notification settings - Fork 9
/
readme.rb
70 lines (59 loc) · 2.19 KB
/
readme.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
require 'pathname'
module SchemaDev
class Readme
def self.update(config)
new(config.matrix).update
end
attr_accessor :matrix, :readme
def initialize(matrix)
self.matrix = matrix
self.readme = Pathname.new('README.md')
end
def update
return false unless readme.exist?
lines = readme.readlines
newlines = sub_matrix(lines.dup)
newlines = sub_templates(newlines)
newreadme = Gem.new(Pathname.pwd.basename.to_s).erb(newlines.join)
if newreadme != lines.join
readme.write newreadme
return true
end
end
def sub_matrix(lines)
replace_block(lines, %r{^\s*<!-- SCHEMA_DEV: MATRIX}) do |contents|
contents << "<!-- SCHEMA_DEV: MATRIX - begin -->\n"
contents << "<!-- These lines are auto-generated by schema_dev based on schema_dev.yml -->\n"
self.matrix.group_by {|e| e.slice(:ruby, :activerecord) }.each do |pair, items|
contents << "* ruby **#{pair[:ruby]}** with activerecord **#{pair[:activerecord]}**, using #{items.map{|item| "**#{item[:db]}**"}.to_sentence(last_word_connector: ' or ')}\n"
end
contents << "\n"
contents << "<!-- SCHEMA_DEV: MATRIX - end -->\n"
end
end
def sub_templates(lines)
Pathname.glob(SchemaDev::Templates.root + "README" + "*.md.erb").each do |template|
lines = sub_template(template, lines)
end
lines
end
def sub_template(template, lines)
key = template.basename(".md.erb").to_s.upcase.tr('.', ' ')
replace_block(lines, %r{^\s*<!-- SCHEMA_DEV: TEMPLATE #{key}}) do |contents|
contents << "<!-- SCHEMA_DEV: TEMPLATE #{key} - begin -->\n"
contents << "<!-- These lines are auto-inserted from a schema_dev template -->\n"
contents << template.readlines
contents << "\n"
contents << "<!-- SCHEMA_DEV: TEMPLATE #{key} - end -->\n"
end
end
def replace_block(lines, pattern)
before = lines.take_while { |e| e !~ pattern }
return lines if before == lines
after = lines.reverse.take_while { |e| e !~ pattern }.reverse
contents = []
yield contents
before + contents + after
end
end
end