-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate_perl_modules.pl
executable file
·192 lines (146 loc) · 4.26 KB
/
generate_perl_modules.pl
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
#!/usr/bin/env perl
# The point of this is to build skeleton classes which are then fleshed out at runtime
# so that people can wrap a mop around it
use strict;
use warnings;
use FindBin;
use File::Slurper;
use JSON;
use lib "$FindBin::Bin/lib";
use Playwright::Util;
my $module_source = '';
while (<DATA>) {
$module_source .= $_;
}
# Next, grab the API JSON and iterate to build classes.
our $raw = File::Slurper::read_binary("$FindBin::Bin/api.json");
our $spec = JSON::decode_json($raw);
$spec = Playwright::Util::arr2hash($spec,'name');
our %mapper = (
mouse => "
=head2 mouse()
Returns a Playwright::Mouse object.
=cut
sub mouse {
my ( \$self ) = \@_;
return Playwright::Mouse->new(
handle => \$self,
parent => \$self,
id => \$self->{guid},
);
}\n\n",
keyboard => "
=head2 keyboard()
Returns a Playwright::Keyboard object.
=cut
sub keyboard {
my ( \$self ) = \@_;
return Playwright::Keyboard->new(
handle => \$self,
parent => \$self,
id => \$self->{guid},
);
}\n\n",
);
our %methods_to_rename = (
'$' => 'select',
'$$' => 'selectMulti',
'$eval' => 'eval',
'$$eval' => 'evalMulti',
);
our %bogus_methods = (
'querySelector' => '$',
'querySelectorAll' => '$$',
'evalOnSelector' => '$eval',
'evalOnSelectorAll' => '$$eval',
);
# Playwright methods we can't actually have work here
our @banned = ('_api_request');
my $ncallback = sub {
$_[0] =~ s/#.*$//;
return shift;
};
my @modules;
foreach my $class ( keys(%$spec), 'Mouse', 'Keyboard' ) {
next if $class eq 'Playwright';
my $pkg = "Playwright::$class";
my $subs = '';
push(@modules,$pkg);
my @seen;
my $members = Playwright::Util::arr2hash($spec->{$class}{members},'name', $ncallback);
foreach my $method ( ( keys( %$members ), 'on', 'evaluate', 'evaluateHandle' ) ) {
next if grep { $_ eq $method } @banned;
my $renamed = $method;
$method = $bogus_methods{$method} if exists $bogus_methods{$method};
$renamed = $methods_to_rename{$method} if exists $methods_to_rename{$method};
next if grep { $method eq $_ } @seen;
if (exists $mapper{$method}) {
$subs .= $mapper{$method};
} else {
$subs .= "
=head2 $renamed\(\@args)
Execute the $class\:\:$renamed playwright routine.
See L<https://playwright.dev/docs/api/class-$class#$class-$method> for more information.
=cut
sub $renamed {
my \$self = shift;
return \$self->_api_request(
args => [\@_],
command => '$method',
object => \$self->{guid},
type => \$self->{type}
);
}\n\n";
}
push(@seen,$method);
}
my $local_source = $module_source;
$local_source =~ s/\%REPLACEME\%/$pkg/gm;
$local_source =~ s/\%CLASSNAME\%/$class/gm;
$local_source =~ s/\%SUBROUTINES\%/$subs/gm;
open(my $fh, '>', "$FindBin::Bin/lib/Playwright/$class.pm");
print $fh $local_source;
close $fh;
}
# Now overwrite the list of modules in Playwright.pm
open(my $fh, '>', "$FindBin::Bin/lib/Playwright/ModuleList.pm");
print $fh "#ABSTRACT: Playwright sub classes.
#PODNAME: Playwright::ModuleList
# You should not use this directly; use Playwright instead.
package Playwright::ModuleList;
use strict;
use warnings;
";
foreach my $mod (@modules) {
print $fh "use $mod;\n";
}
print $fh "\n1;\n";
close($fh);
1;
__DATA__
# ABSTRACT: Automatically generated class for %REPLACEME%
# PODNAME: %REPLACEME%
# These classes used to be generated at runtime, but are now generated when the module is built.
# Don't send patches against these modules, they will be ignored.
# See generate_perl_modules.pl in the repository for generating this.
use strict;
use warnings;
package %REPLACEME%;
use parent 'Playwright::Base';
=head1 CONSTRUCTOR
=head2 new(%options)
You shouldn't have to call this directly.
Instead it should be returned to you as the result of calls on Playwright objects, or objects it returns.
=cut
sub new {
my ($self,%options) = @_;
$options{type} = '%CLASSNAME%';
return $self->SUPER::new(%options);
}
=head1 METHODS
=cut
sub spec {
return $Playwright::spec->{'%CLASSNAME%'}{members};
}
%SUBROUTINES%
1;