-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild-bcd-for-firefox
executable file
·449 lines (393 loc) · 15.3 KB
/
build-bcd-for-firefox
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
#! /usr/bin/perl
use strict;
use warnings;
use Getopt::Long qw(:config bundling);
use Cwd();
use File::Spec();
use File::Find();
use JSON();
use English qw( -no_match_vars );
use Fcntl();
use FileHandle();
use File::HomeDir();
use File::Temp();
use JSON();
use Carp();
use Firefox::Marionette();
local $ENV{PATH} = '/usr/bin:/bin:/usr/sbin:/sbin';
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
sub _BUFFER_SIZE { return 8192 }
sub _MAX_KEYS { return 3 }
our $VERSION = '1.62';
MAIN: {
my %options;
Getopt::Long::GetOptions( \%options, 'help|h', 'version|v', 'path|p', );
my $bcd_path = Firefox::Marionette::BCD_PATH(1);
_parse_options( $bcd_path, %options );
my ( $volume, $directories, $file ) = File::Spec->splitpath($bcd_path);
my $firefox_marionette_directory =
File::Spec->catdir( $volume, $directories );
my $browser_compat_data_directory =
File::Spec->catdir( $firefox_marionette_directory,
'browser-compat-data' );
_setup_git_repos($browser_compat_data_directory);
my $summary = {};
my $api_directory =
File::Spec->catdir( $browser_compat_data_directory, 'api' );
my $builtins_directory = File::Spec->catdir( $browser_compat_data_directory,
'javascript', 'builtins' );
my $debug = 0;
my $firefox = Firefox::Marionette->new( debug => $debug )->content()
->go('https://duckduckgo.com');
File::Find::find(
{
wanted => sub {
if ( $File::Find::name =~ /[.]json$/smx ) {
if ($debug) { Carp::carp("Looking at $File::Find::name\n") }
my $path = $File::Find::name;
my $handle = FileHandle->new( $path, Fcntl::O_RDONLY() )
or Carp::croak(
"Failed to open $path for reading:$EXTENDED_OS_ERROR");
my $content;
my $result;
while ( $result = sysread $handle,
my $buffer, _BUFFER_SIZE() )
{
$content .= $buffer;
}
defined $result
or Carp::croak(
"Failed to read from $path:$EXTENDED_OS_ERROR");
close $handle
or
Carp::croak("Failed to close $path:$EXTENDED_OS_ERROR");
my $json = JSON::decode_json($content);
my $root_element = _get_root_element( $json, $path );
foreach my $class_name (
sort { $a cmp $b }
keys %{$root_element}
)
{
foreach
my $browser (qw(firefox chrome edge safari ie opera))
{
my $class_reference =
$root_element->{$class_name}->{__compat}
->{support}->{$browser};
my $mirror_reference =
$root_element->{$class_name}->{__compat}
->{support}->{chrome};
my @versions = _get_versions( $class_reference,
$mirror_reference, $class_name );
foreach my $version (
sort {
$a->{version_added} <=> $b->{version_added}
} @versions
)
{
next if ( $version->{partial_implementation} );
_process_version(
$summary, $class_name, $browser,
$version, $path
);
}
foreach my $function_name (
sort { $a cmp $b }
keys %{ $root_element->{$class_name} }
)
{
next if ( $function_name eq '__compat' );
next if ( $function_name eq 'worker_support' );
_process_function(
$summary,
root_element => $root_element,
class_name => $class_name,
function_name => $function_name,
browser => $browser,
path => $path
);
}
}
}
}
},
follow => 1
},
$api_directory,
$builtins_directory
);
my ( $bcd_handle, $tmp_path ) = File::Temp::tempfile(
'firefox-marionette-bcd-XXXXXXXXXXX',
DIR => $firefox_marionette_directory
);
$bcd_handle->print( JSON->new()->pretty()->encode($summary) )
or Carp::croak("Failed to write to $tmp_path:$EXTENDED_OS_ERROR");
$bcd_handle->close()
or Carp::croak("Failed to close $tmp_path:$EXTENDED_OS_ERROR");
rename $tmp_path, $bcd_path
or
Carp::croak("Failed to rename $tmp_path to $bcd_path:$EXTENDED_OS_ERROR");
}
sub _parse_options {
my ( $bcd_path, %options ) = @_;
if ( $options{help} ) {
require Pod::Simple::Text;
my $parser = Pod::Simple::Text->new();
$parser->parse_from_file($PROGRAM_NAME);
exit 0;
}
elsif ( $options{version} ) {
print "$VERSION\n"
or die "Failed to print to STDOUT:$EXTENDED_OS_ERROR\n";
exit 0;
}
elsif ( $options{path} ) {
print "$bcd_path\n"
or die "Failed to print to STDOUT:$EXTENDED_OS_ERROR\n";
exit 0;
}
return;
}
sub _setup_git_repos {
my ($browser_compat_data_directory) = @_;
if ( -d $browser_compat_data_directory ) {
my $cwd = Cwd::cwd();
chdir $browser_compat_data_directory
or Carp::croak(
"Failed to chdir $browser_compat_data_directory:$EXTENDED_OS_ERROR"
);
system {'git'} 'git', 'pull', '--quiet'
and Carp::croak(
"Failed to git pull from $browser_compat_data_directory\n");
chdir $cwd or Carp::croak("Failed to chdir $cwd:$EXTENDED_OS_ERROR");
}
else {
my $mdn_browser_repo = 'https://github.com/mdn/browser-compat-data.git';
system {'git'} 'git', 'clone', $mdn_browser_repo,
$browser_compat_data_directory
and Carp::croak(
"Failed to git clone $mdn_browser_repo $browser_compat_data_directory\n"
);
}
return;
}
sub _process_version {
my ( $summary, $class_name, $browser, $version, $path ) = @_;
if ( $version->{version_added} ) {
if ( $version->{version_added} ne 'preview' ) {
$summary->{$class_name}->{type} = 'class';
my %extra = _get_extra_from_flags( $version, $path );
push @{ $summary->{$class_name}->{browsers}->{$browser} },
{ add => $version->{version_added}, %extra };
}
}
if ( $version->{version_removed} ) {
push @{ $summary->{$class_name}->{browsers}->{$browser} },
{ rm => $version->{version_removed} + 0 };
}
return;
}
sub _process_function {
my ( $summary, %parameters ) = @_;
my $root_element = $parameters{root_element};
my $function_name = $parameters{function_name};
my $class_name = $parameters{class_name};
my $browser = $parameters{browser};
my $path = $parameters{path};
my $cleaned_function_name = $function_name;
my $static;
if ( $cleaned_function_name =~ s/_static$//smx ) {
$static = 1;
}
my $function_reference =
$root_element->{$class_name}->{$function_name}->{__compat}->{support}
->{$browser};
my $mirror_reference =
$root_element->{$class_name}->{$function_name}->{__compat}->{support}
->{chrome};
my @versions =
_get_versions( $function_reference, $mirror_reference, $function_name );
foreach my $version ( sort { $a->{version_added} <=> $b->{version_added} }
@versions )
{
# Example of partial_implementation to allow property to exist HTMLAnchorElement.ping
# next if ( $version->{partial_implementation} );
# Example of note to allow property to exist - AudioBufferSourceNode.buffer
# next if ( $version->{notes} );
if ( ( $version->{version_added} )
&& ( $version->{version_added} ne 'preview' ) )
{
$summary->{"$class_name.$cleaned_function_name"}->{type} =
'function';
$summary->{"$class_name.$cleaned_function_name"}->{static} =
$static ? \1 : \0;
my %extra = _get_extra_from_flags( $version, $path );
push @{ $summary->{"$class_name.$cleaned_function_name"}->{browsers}
->{$browser} }, { add => $version->{version_added}, %extra };
}
if ( ( $version->{version_removed} )
&& ( $version->{version_removed} ne 'preview' ) )
{
push @{ $summary->{"$class_name.$cleaned_function_name"}->{browsers}
->{$browser} }, { rm => $version->{version_removed} + 0 };
}
}
return;
}
sub _get_extra_from_flags {
my ( $version, $path ) = @_;
my %extra;
if ( $version->{flags} ) {
foreach my $flag ( @{ $version->{flags} } ) {
if ( ( defined $flag->{type} )
&& ( defined $flag->{name} )
&& ( defined $flag->{value_to_set} )
&& ( ( keys %{$flag} ) == _MAX_KEYS() ) )
{
}
elsif (( defined $flag->{type} )
&& ( defined $flag->{name} )
&& ( !defined $flag->{value_to_set} )
&& ( ( keys %{$flag} ) == 2 ) )
{
next;
}
else {
Carp::croak("Unknown flag for $path");
}
if ( $flag->{type} eq 'preference' ) {
%extra = (
pref_name => $flag->{name},
pref_value => $flag->{value_to_set}
);
}
else {
Carp::croak("Unknown type of '$flag->{type}' in $path");
}
}
}
return %extra;
}
sub _get_root_element {
my ( $json, $path ) = @_;
my $root_element;
my @parts;
while ( !$root_element ) {
my $missed;
foreach my $key ( keys %{$json} ) {
if ( exists $json->{$key}->{__compat} ) {
my $full_key = join q[.], @parts, $key;
$full_key =~ s/^javascript[.]builtins[.]//smx;
$full_key =~ s/^api[.]//smx;
$root_element->{$full_key} = $json->{$key};
}
else {
$json = $json->{$key};
push @parts, $key;
last;
}
}
if ($root_element) {
if ($missed) {
Carp::croak("Failed to navigate JSON in $path for key $missed");
}
}
}
return $root_element;
}
sub _get_versions {
my ( $object, $mirror, $name ) = @_;
my @versions;
if ($object) {
if ( $object eq 'mirror' ) {
$object = $mirror;
}
if ( ( ref $object ) eq 'HASH' ) {
push @versions, _strip_version($object);
}
else {
push @versions, _strip_version( @{$object} );
}
}
return @versions;
}
sub _strip_version {
my (@possible) = @_;
my @approved;
foreach my $version (@possible) {
if ( $version->{version_added} ) {
if ( $version->{version_added} eq 'preview' ) {
next;
}
elsif ( $version->{version_added} =~
s/^\x{2264}(\d+(?:[.]\d+)?)$/$1/smx )
{
}
}
push @approved, $version;
}
return @approved;
}
__END__
=head1 NAME
build-bcd-for-firefox - build user agent data from the @mdn/browser-compat-data repo
=head1 VERSION
Version 1.62
=head1 USAGE
$ build-bcd-for-firefox
$ build-bcd-for-firefox --path
=head1 DESCRIPTION
This program is intended to build a database for the agent method of the Firefox::Marionette class. It builds this database by cloning the L<@mdn/browser-compat-data|https://github.com/mdn/browser-compat-data> repository on github.com and then summarising this data for the agent method.
The path where the database is stored varies by user and operating system, and can be shown with the --path option.
=head1 REQUIRED ARGUMENTS
None
=head1 OPTIONS
Option names can be abbreviated to uniqueness and can be stated with singe or double dashes, and option values can be separated from the option name by a space or '=' (as with Getopt::Long). Option names are also case-
sensitive.
=over 4
=item * --help - This page.
=item * --version - Print the current version of this binary
=item * --path - Print the path of the local database that is getting built.
=back
=head1 CONFIGURATION
build-bcd-for-firefox requires no configuration files or environment variables.
=head1 DEPENDENCIES
build-bcd-for-firefox requires the following non-core Perl modules
=over
=item *
L<Pod::Simple::Text|Pod::Simple::Text>
=back
=head1 DIAGNOSTICS
None.
=head1 INCOMPATIBILITIES
None known.
=head1 EXIT STATUS
This program will exit with a zero after successfully completing.
=head1 BUGS AND LIMITATIONS
To report a bug, or view the current list of bugs, please visit L<https://github.com/david-dick/firefox-marionette/issues>
=head1 AUTHOR
David Dick C<< <[email protected]> >>
=head1 LICENSE AND COPYRIGHT
Copyright (c) 2024, David Dick C<< <[email protected]> >>. All rights reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic/perlartistic>.
=head1 DISCLAIMER OF WARRANTY
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.