Repository navigation
Expand file tree
/
Copy pathgenerate.rb
More file actions
executable file
·308 lines (248 loc) · 6.8 KB
/
Copy pathgenerate.rb
File metadata and controls
executable file
·308 lines (248 loc) · 6.8 KB
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
#!/usr/bin/env ruby
# generate.rb
#
# SUMMARY
#
# A simple script that generates files across the Vector repo. This is used
# for documentation, config examples, etc. The source templates are located
# in /scripts/generate/templates/* and the results are placed in their
# respective root directories.
#
# See the README.md in the generate folder for more details.
#
# Setup
#
require 'uri'
require_relative "setup"
#
# Requires
#
require_relative "generate/post_processors/component_importer"
require_relative "generate/post_processors/link_definer"
require_relative "generate/post_processors/option_linker"
require_relative "generate/post_processors/section_referencer"
require_relative "generate/post_processors/section_sorter"
require_relative "generate/templates"
#
# Flags
#
dry_run = ARGV.include?("--dry-run")
#
# Functions
#
def doc_valid?(url_path)
parts = url_path.split("#", 2)
file_or_dir_path = WEBSITE_ROOT + parts[0][0..-2]
anchor = parts[1]
file_path =
if File.directory?(file_or_dir_path) && File.file?("#{file_or_dir_path}/README.md")
"#{file_or_dir_path}/README.md"
else
"#{file_or_dir_path}.md"
end
if File.exists?(file_path)
if !anchor.nil?
content = File.read(file_path)
headings = content.scan(/\n###?#?#? (.*)\n/).flatten.uniq
anchors = headings.collect(&:parameterize)
anchors.include?(anchor)
else
true
end
else
false
end
end
def link_valid?(value)
if value.start_with?(DOCS_BASE_PATH)
doc_valid?(value)
elsif value.start_with?("/")
page_valid?(value)
else
url_valid?(value)
end
end
def page_valid?(path)
uri = URI::parse(path)
path =
if uri.path == "/"
"/index"
elsif uri.path.end_with?("/")
uri.path[0..-2]
else
uri.path
end
File.exists?("#{PAGES_ROOT}#{path}.js")
end
def post_process(content, doc, links)
if doc.end_with?(".md")
content = content.clone
content = PostProcessors::ComponentImporter.import!(content)
content = PostProcessors::SectionSorter.sort!(content)
content = PostProcessors::SectionReferencer.reference!(content)
content = PostProcessors::LinkDefiner.define!(content, doc, links)
content = PostProcessors::OptionLinker.link!(content)
end
content
end
def url_valid?(url)
case url
# We add an exception for paths on packages.timber.io because the
# index.html file we use also serves as the error page. This is how
# it serves directories.
when /^https:\/\/packages\.timber\.io\/vector[^.]*$/
true
else
uri = URI.parse(url)
req = Net::HTTP.new(uri.host, uri.port)
req.open_timeout = 500
req.read_timeout = 1000
req.ssl_timeout = 1000
req.use_ssl = true if uri.scheme == 'https'
path = uri.path == "" ? "/" : uri.path
begin
res = req.request_head(path)
res.code.to_i != 404
rescue Errno::ECONNREFUSED
return false
end
end
end
#
# Header
#
title("Generating files...")
#
# Setup
#
metadata = Metadata.load!(META_ROOT, DOCS_ROOT, PAGES_ROOT)
templates = Templates.new(ROOT_DIR, metadata)
#
# Create missing release pages
#
metadata.releases_list.each do |release|
template_path = "#{PAGES_ROOT}/releases/#{release.version}/download.js"
if !File.exists?(template_path)
dirname = File.dirname(template_path)
unless File.directory?(dirname)
FileUtils.mkdir_p(dirname)
end
contents =
<<~EOF
import React from 'react';
import ReleaseDownload from '@site/src/components/ReleaseDownload';
function Download() {
return <ReleaseDownload version="#{release.version}" />
}
export default Download;
EOF
File.open(template_path, 'w+') { |file| file.write(contents) }
end
template_path = "#{PAGES_ROOT}/releases/#{release.version}.js"
if !File.exists?(template_path)
contents =
<<~EOF
import React from 'react';
import ReleaseNotes from '@site/src/components/ReleaseNotes';
function ReleaseNotesPage() {
const version = "#{release.version}";
return <ReleaseNotes version={version} />;
}
export default ReleaseNotesPage;
EOF
File.open(template_path, 'w+') { |file| file.write(contents) }
end
end
#
# Create missing component templates
#
metadata.components.each do |component|
template_path = "#{REFERENCE_ROOT}/#{component.type.pluralize}/#{component.name}.md.erb"
if !File.exists?(template_path)
contents = templates.component_default(component)
File.open(template_path, 'w+') { |file| file.write(contents) }
end
end
erb_paths =
Dir.glob("#{ROOT_DIR}/**/*.erb", File::FNM_DOTMATCH).
to_a.
filter { |path| !path.start_with?("#{ROOT_DIR}/scripts") }.
filter { |path| !path.start_with?("#{ROOT_DIR}/distribution/nix") }
#
# Create missing .md files
#
erb_paths.each do |erb_path|
md_path = erb_path.gsub(/\.erb$/, "")
if !File.exists?(md_path)
File.open(md_path, "w") {}
end
end
#
# Render templates
#
metadata = Metadata.load!(META_ROOT, DOCS_ROOT, PAGES_ROOT)
templates = Templates.new(ROOT_DIR, metadata)
erb_paths.
select { |path| !templates.partial?(path) }.
each do |template_path|
target_file = template_path.gsub(/^#{ROOT_DIR}\//, "").gsub(/\.erb$/, "")
target_path = "#{ROOT_DIR}/#{target_file}"
content = templates.render(target_file)
content = post_process(content, target_path, metadata.links)
current_content = File.read(target_path)
if current_content != content
action = dry_run ? "Will be changed" : "Changed"
say("#{action} - #{target_file}", color: :green)
File.write(target_path, content) if !dry_run
else
action = dry_run ? "Will not be changed" : "Not changed"
say("#{action} - #{target_file}", color: :blue)
end
end
if dry_run
return
end
#
# Post process individual docs
#
title("Post processing generated files...")
docs =
Dir.glob("#{DOCS_ROOT}/**/*.md").to_a +
Dir.glob("#{POSTS_ROOT}/**/*.md").to_a +
["#{ROOT_DIR}/README.md"]
docs.each do |doc|
path = doc.gsub(/^#{ROOT_DIR}\//, "")
original_content = File.read(doc)
new_content = post_process(original_content, doc, metadata.links)
if original_content != new_content
File.write(doc, new_content)
say("Processed - #{path}", color: :green)
else
say("Not changed - #{path}", color: :blue)
end
end
#
# Check URLs
#
check_urls =
if ENV.key?("CHECK_URLS")
ENV.fetch("CHECK_URLS") == "true"
else
title("Checking URLs...")
get("Would you like to check & verify URLs?", ["y", "n"]) == "y"
end
if check_urls
Parallel.map(metadata.links.values.to_a.sort, in_threads: 50) do |id, value|
if !link_valid?(value)
error!(
<<~EOF
Link `#{id}` invalid!
#{value}
Please make sure this path or URL exists.
EOF
)
else
say("Valid - #{id} - #{value}", color: :green)
end
end
end