-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgenerated_features.rb
More file actions
329 lines (293 loc) · 10.3 KB
/
generated_features.rb
File metadata and controls
329 lines (293 loc) · 10.3 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
require 'httparty'
require 'json'
require 'time'
module Generated
class FeaturesGenerator < Jekyll::Generator
safe true
def getBugTrackerLabel(url)
if url.include?('crbug.com') || url.include?('bugs.chromium.org')
"Chromium issue tracker"
elsif url.include?('webkit.org') || url.include?('bugs.webkit.org')
"WebKit issue tracker"
elsif url.include?('bugzil.la') || url.include?('bugzilla.mozilla.org')
"Firefox issue tracker"
else
"Issue tracker"
end
end
def getImplUrls(feature)
platforms = ['webview_ios', 'webview_android', 'chrome_android', 'safari_ios']
urls = []
platforms.each do |platform|
next unless feature['__compat']['support'].key?(platform)
support = feature['__compat']['support'][platform]
entries = support.kind_of?(Array) ? support : [support]
entries.each do |entry|
next unless entry.kind_of?(Hash) && entry.key?('impl_url')
url_list = entry['impl_url'].kind_of?(Array) ? entry['impl_url'] : [entry['impl_url']]
urls.concat(url_list)
end
end
urls.uniq!
tracker_counts = Hash.new(0)
result = {}
urls.each do |url|
base_label = getBugTrackerLabel(url)
tracker_counts[base_label] += 1
count = tracker_counts[base_label]
label = count > 1 ? "#{base_label} (#{count})" : base_label
result[label] = url
end
result
end
def getVersions(feature, platform)
if !feature['__compat']["support"].key?(platform) then
return {
"*" => "n"
}
end
if (feature['__compat']["support"][platform].kind_of?(Array)) then
return feature['__compat']["support"][platform].map { |version|
if version["version_added"].kind_of?(String) then
[version["version_added"], "y"]
else
["unknown", "y"]
end
}.to_h
end
version = feature['__compat']["support"][platform]["version_added"]
if version.class == FalseClass then
return {
"*" => "n"
}
end
if version.class == TrueClass then
return {
"*" => "y"
}
end
if version.nil? || version.empty? then
version = "unkonwn"
end
return {
version => "y"
}
end
def parse_wpe_results(parsed)
bcd_map = {}
return bcd_map unless parsed.key?('results')
parsed['results'].each do |bcd_key, data|
next unless data.kind_of?(Hash) && data.key?('pass')
bcd_map[bcd_key] = data['pass'].include?('Window') ? 'y' : 'n'
end
bcd_map
end
def parse_servo_results(parsed)
bcd_map = {}
return bcd_map unless parsed.key?('results')
parsed['results'].each do |_url, tests|
next unless tests.kind_of?(Array)
tests.each do |test|
next unless test['exposure'] == 'Window'
key = test['name']
next if bcd_map.key?(key)
bcd_map[key] = test['result'] == true ? 'y' : 'n'
end
end
bcd_map
end
def extract_version_from_results(parsed, file_name)
# WPE format: testrun_timestamp_end (Unix seconds) in metadata
if parsed.is_a?(Hash) && parsed.key?('metadata') &&
parsed['metadata'].key?('testrun_timestamp_end')
return Time.at(parsed['metadata']['testrun_timestamp_end']).utc.strftime('%Y-%m-%d')
end
# Date embedded in filename (e.g. latest-wpe...-2026-03-27T...)
if file_name =~ /(\d{4}-\d{2}-\d{2})/
return $1
end
nil
end
def try_fix_truncated_json(content)
# If a file is truncated mid-JSON, find the last complete entry (ends with },\n)
# and close the results object and root object after it.
last_complete = content.rindex(/\},\s*\n/)
return nil unless last_complete
fixed = content[0...last_complete + 1] + "\n }\n}"
JSON.parse(fixed)
rescue JSON::ParserError
nil
end
def fetch_latest_webview_results
api_url = "https://api.github.com/repos/WebView-CG/webview-bcd-results/contents/results"
response = HTTParty.get(api_url, headers: { "User-Agent" => "CanIWebView-build" })
files = JSON.parse(response.body)
results = {}
files.each do |file|
name = file['name']
next unless name.start_with?('latest-')
next if name == 'latest-android.json'
begin
content = HTTParty.get(file['download_url']).body
parsed = begin
JSON.parse(content)
rescue JSON::ParserError
Jekyll.logger.warn "webview-bcd-results:", "#{name} appears truncated, attempting partial parse"
try_fix_truncated_json(content)
end
next unless parsed
version = extract_version_from_results(parsed, name) || 'latest'
source_url = file['html_url']
if name.start_with?('latest-wpewebkit')
results['wpe_minibrowser'] = {
'results' => parse_wpe_results(parsed),
'version' => version,
'source_url' => source_url,
}
elsif name.start_with?('latest-servo')
results['servo'] = {
'results' => parse_servo_results(parsed),
'version' => version,
'source_url' => source_url,
}
end
rescue => e
Jekyll.logger.warn "webview-bcd-results:", "Failed to process #{name}: #{e.message}"
end
end
results
rescue => e
Jekyll.logger.warn "webview-bcd-results:", "Failed to fetch latest results: #{e.message}"
{}
end
def generate_bcd_from_section(site, section, timestamp, category, appended_title = "", bcd_prefix = "", latest_results = {})
section.keys.each do |key|
# We skip potential special keys since we can iterate over sub sections
next unless key.index("__") != 0
feature = section[key]
bcd_key = bcd_prefix.empty? ? key : "#{bcd_prefix}.#{key}"
title = "#{appended_title}#{key}"
slug = "mdn-#{title.downcase.strip.gsub('-', '').gsub(/[\_|\s]/, '-').gsub(':', '')}"
path = site.in_source_dir("_generated_features/#{slug}.md")
doc = Jekyll::Document.new(path, {
:site => site,
:collection => site.collections['generated_features'],
})
data_source = "Support data provided by: [](https://github.com/mdn/browser-compat-data)"
doc.data['title'] = title
doc.data['slug'] = slug
doc.data['category'] = category
doc.data['keywords'] = 'todo'
doc.data['last_test_date'] = timestamp
doc.data['notes'] = data_source
links = feature["__compat"].key?("mdn_url") ? {
"MDN reference" => feature["__compat"]["mdn_url"]
} : {}
impl_urls = getImplUrls(feature)
doc.data['links'] = links.merge(impl_urls)
doc.data['has_impl_urls'] = !impl_urls.empty?
stats = {
"wkwebview" => {
"macos" => {
"*" => "u"
},
"ios" => getVersions(feature, "webview_ios"),
},
"androidwebview" => {
"android" => getVersions(feature, "webview_android")
},
"webview2" => {
"windows" => {
"*" => "u"
}
},
"chrome_android" => {
"android" => getVersions(feature, "chrome_android")
},
"safari_ios" => {
"ios" => getVersions(feature, "safari_ios")
},
}
latest_results.each do |client, data|
platform = client == "wpe_minibrowser" ? "linux" : "android"
support = data['results'].fetch(bcd_key, "u")
version = data['version']
stats[client] = { platform => { version => support } }
end
doc.data['stats'] = stats
site.collections['generated_features'].docs << doc
end
end
def generate_bcd(site)
version = File.read("bcd_version").strip
bcd = HTTParty.get("http://unpkg.com/@mdn/browser-compat-data@#{version}/data.json").body
parsed_bcd = JSON.parse(bcd)
timestamp = parsed_bcd['__meta']['timestamp']
site.data['bcd_version'] = version
latest_results = fetch_latest_webview_results()
site.data['bcd_test_meta'] = {}
latest_results.each do |client, data|
site.data['bcd_test_meta'][client] = {
'version' => data['version'],
'source_url' => data['source_url'],
}
end
generate_bcd_from_section(site, parsed_bcd['api'],
timestamp, "js", "", "api", latest_results)
generate_bcd_from_section(site, parsed_bcd['javascript']['builtins'],
timestamp, "js", "JavaScript built-in: ", "javascript.builtins", latest_results)
generate_bcd_from_section(site, parsed_bcd['html']['elements'],
timestamp, "html", "HTML element: ", "html.elements", latest_results)
generate_bcd_from_section(site, parsed_bcd['html']['global_attributes'],
timestamp, "html", "HTML attribute: ", "html.global_attributes", latest_results)
generate_bcd_from_section(site, parsed_bcd['manifests']['webapp'],
timestamp, "html", "HTML manifest: ", "manifests.webapp", latest_results)
generate_bcd_from_section(site, parsed_bcd['css']['selectors'],
timestamp, "css", "CSS selector: ", "css.selectors", latest_results)
generate_bcd_from_section(site, parsed_bcd['css']['properties'],
timestamp, "css", "CSS property: ", "css.properties", latest_results)
generate_bcd_from_section(site, parsed_bcd['http']['headers'],
timestamp, "http", "HTTP header: ", "http.headers", latest_results)
generate_bcd_from_section(site, parsed_bcd['http']['status'],
timestamp, "http", "HTTP status code: ", "http.status", latest_results)
# The css types can have sub fields so we iterate over these and then
# generate sections.
parsed_bcd['css']['types'].keys.each do |type|
generate_bcd_from_section(site, parsed_bcd['css']['types'][type],
timestamp, "css", "CSS type: #{type}: ", "css.types.#{type}", latest_results)
end
end
def generate_baseline(site)
# The web features "computeBaseline" code is distributed via npm
# so it's easier to run this generation code in node
# and then process the results back in this plugin for jekyll
script_out = `npx tsx tools/baseline`
baseline = JSON.parse(script_out)
baseline.each do |feature|
path = site.in_source_dir("_generated_features/#{feature['slug']}.md")
doc = Jekyll::Document.new(path, {
:site => site,
:collection => site.collections['generated_features'],
})
# TODO:
data_source = "Support data provided by: [](https://github.com/mdn/browser-compat-data)"
doc.data['title'] = "web-feature: " + feature['title']
doc.data['description'] = feature['description']
doc.data['slug'] = feature['slug']
doc.data['category'] = feature['category']
doc.data['keywords'] = 'baseline'
doc.data['last_test_date'] = feature['last_test_date']
doc.data['notes'] = data_source
doc.data['links'] = feature['links']
doc.data['stats'] = feature['stats']
doc.data['notes_by_num'] = feature['notes_by_num']
doc.data['baseline'] = feature['baseline']
site.collections['generated_features'].docs << doc
end
end
def generate(site)
generate_bcd(site)
generate_baseline(site)
end
end
end