forked from Shopify/tapioca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme.rake
196 lines (153 loc) · 4.08 KB
/
readme.rake
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
# typed: true
# frozen_string_literal: true
require "tapioca"
require "tapioca/internal"
desc("Updates the README file")
task :readme do
def assert_synchronized(path)
# Do not print diff and yield whether exit code was zero
sh("test -z \"$(git status --porcelain #{path})\"") do |outcome, _|
return if outcome
# Output diff before raising error
sh("git status --porcelain #{path}")
warn(<<~WARNING)
The `#{path}` is out of sync.
Run `bin/readme` and commit the results.
WARNING
exit!
end
end
def skip_command?(command)
ignored_commands = ["configure", "init"]
command.hidden? || command.name.start_with?("__") || ignored_commands.include?(command.name)
end
def skip_option?(option)
option.name == "auth"
end
def option_value(option)
fallback_value = case option.type
when :boolean
true
when :numeric
1
when :hash
{}
when :array
[]
when :string
""
end
if option.default.nil?
fallback_value
else
option.default
end
end
def command_options(command)
command.options.filter_map do |name, opt|
next if skip_option?(opt)
[name.to_s, option_value(opt)]
end.to_h
end
def config
Tapioca::Cli.commands.filter_map do |command_name, command|
next if skip_command?(command)
[command_name, command_options(command)]
end.to_h
end
def replace_section(contents, section_name, replacement)
contents.sub(
/(<!-- START_#{section_name} -->).+(<!-- END_#{section_name} -->)/m, <<~OUT.chomp
\\1
#{replacement.chomp}
\\2
OUT
)
end
def print_config_template(contents)
replace_section(contents, "CONFIG_TEMPLATE", <<~MARKDOWN)
```yaml
#{config.to_yaml.chomp}
```
MARKDOWN
end
class FakeShell < Thor::Shell::Basic
DEFAULT_TERMINAL_WIDTH = 120
def stdout
@stdout ||= StringIO.new
end
def contents
stdout.string
end
def clear
@stdout = StringIO.new
self
end
def terminal_width
DEFAULT_TERMINAL_WIDTH
end
end
def print_help(contents)
shell = FakeShell.new
$PROGRAM_NAME = "tapioca"
section = "HELP"
Tapioca::Cli.help(shell.clear)
contents = replace_section(contents, section, <<~MARKDOWN)
```shell
$ #{$PROGRAM_NAME} help
#{shell.contents.chomp}
```
MARKDOWN
contents
end
def print_commands_help(contents)
shell = FakeShell.new
Tapioca::Cli.commands.each_key do |command_name|
$PROGRAM_NAME = "tapioca"
section = "HELP_COMMAND_#{command_name.upcase}"
Tapioca::Cli.command_help(shell.clear, command_name)
contents = replace_section(contents, section, <<~MARKDOWN)
```shell
$ #{$PROGRAM_NAME} help #{command_name}
#{shell.contents.chomp}
```
MARKDOWN
end
contents
end
require "kramdown"
class TocConverter < Kramdown::Converter::Toc
class << self
def convert(doc, options)
result, _ = super(doc.root, options)
convert_to_list_items(result.children)
end
def convert_to_list_items(elements)
elements.flat_map do |elem|
level = elem.value.options[:level]
text = elem.value.options[:raw_text]
id = elem.attr[:id]
padding = " " * (level - 2)
["#{padding}* [#{text}](##{id})"] + convert_to_list_items(elem.children)
end
end
end
def in_toc?(el)
super && el.options[:raw_text] !~ /<!--\sno_toc\s-->/
end
end
def print_toc(contents)
section = "TOC"
doc = Kramdown::Document.new(contents)
toc = TocConverter.convert(doc, { auto_id: true, toc_levels: (2..6) })
replace_section(contents, section, toc.join("\n"))
end
path = "#{Dir.pwd}/README.md"
contents = File.read(path)
contents = print_config_template(contents)
contents = print_help(contents)
contents = print_commands_help(contents)
contents = print_toc(contents)
File.write(path, contents)
assert_synchronized(path) if ENV["CI"] == "true"
end