generate_perl_modules.pl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/perl
  2. # The point of this is to build skeleton classes which are then fleshed out at runtime
  3. # so that people can wrap a mop around it
  4. use strict;
  5. use warnings;
  6. use FindBin;
  7. use File::Slurper;
  8. use JSON;
  9. use lib "$FindBin::Bin/lib";
  10. use Playwright::Util;
  11. my $module_source = '';
  12. while (<DATA>) {
  13. $module_source .= $_;
  14. }
  15. # Next, grab the API JSON and iterate to build classes.
  16. our $raw = File::Slurper::read_binary("$FindBin::Bin/api.json");
  17. our $spec = JSON::decode_json($raw);
  18. $spec = Playwright::Util::arr2hash($spec,'name');
  19. our %mapper = (
  20. mouse => "
  21. =head2 mouse()
  22. Returns a Playwright::Mouse object.
  23. =cut
  24. sub mouse {
  25. my ( \$self ) = \@_;
  26. return Playwright::Mouse->new(
  27. handle => \$self,
  28. parent => \$self,
  29. id => \$self->{guid},
  30. );
  31. }\n\n",
  32. keyboard => "
  33. =head2 keyboard()
  34. Returns a Playwright::Keyboard object.
  35. =cut
  36. sub keyboard {
  37. my ( \$self ) = \@_;
  38. return Playwright::Keyboard->new(
  39. handle => \$self,
  40. parent => \$self,
  41. id => \$self->{guid},
  42. );
  43. }\n\n",
  44. );
  45. our %methods_to_rename = (
  46. '$' => 'select',
  47. '$$' => 'selectMulti',
  48. '$eval' => 'eval',
  49. '$$eval' => 'evalMulti',
  50. );
  51. our %bogus_methods = (
  52. 'querySelector' => '$',
  53. 'querySelectorAll' => '$$',
  54. 'evalOnSelector' => '$eval',
  55. 'evalOnSelectorAll' => '$$eval',
  56. );
  57. my @modules;
  58. foreach my $class ( keys(%$spec), 'Mouse', 'Keyboard' ) {
  59. next if $class eq 'Playwright';
  60. my $pkg = "Playwright::$class";
  61. my $subs = '';
  62. push(@modules,$pkg);
  63. my @seen;
  64. my $members = Playwright::Util::arr2hash($spec->{$class}{members},'name');
  65. foreach my $method ( ( keys( %$members ), 'on', 'evaluate', 'evaluateHandle' ) ) {
  66. my $renamed = $method;
  67. $method = $bogus_methods{$method} if exists $bogus_methods{$method};
  68. $renamed = $methods_to_rename{$method} if exists $methods_to_rename{$method};
  69. next if grep { $method eq $_ } @seen;
  70. if (exists $mapper{$method}) {
  71. $subs .= $mapper{$method};
  72. } else {
  73. $subs .= "
  74. =head2 $renamed\(\@args)
  75. Execute the $class\:\:$renamed playwright routine.
  76. See L<https://playwright.dev/api/class-$class#$class-$method> for more information.
  77. =cut
  78. sub $renamed {
  79. my \$self = shift;
  80. return \$self->_request(
  81. args => [\@_],
  82. command => '$method',
  83. object => \$self->{guid},
  84. type => \$self->{type}
  85. );
  86. }\n\n";
  87. }
  88. push(@seen,$method);
  89. }
  90. my $local_source = $module_source;
  91. $local_source =~ s/\%REPLACEME\%/$pkg/gm;
  92. $local_source =~ s/\%CLASSNAME\%/$class/gm;
  93. $local_source =~ s/\%SUBROUTINES\%/$subs/gm;
  94. open(my $fh, '>', "$FindBin::Bin/lib/Playwright/$class.pm");
  95. print $fh $local_source;
  96. close $fh;
  97. }
  98. # Now overwrite the list of modules in Playwright.pm
  99. open(my $fh, '>', "$FindBin::Bin/lib/Playwright/ModuleList.pm");
  100. print $fh "#ABSTRACT: Playwright sub classes.
  101. #PODNAME: Playwright::ModuleList
  102. # You should not use this directly; use Playwright instead.
  103. package Playwright::ModuleList;
  104. use strict;
  105. use warnings;
  106. ";
  107. foreach my $mod (@modules) {
  108. print $fh "use $mod;\n";
  109. }
  110. print $fh "\n1;\n";
  111. close($fh);
  112. 1;
  113. __DATA__
  114. # ABSTRACT: Automatically generated class for %REPLACEME%
  115. # PODNAME: %REPLACEME%
  116. # These classes used to be generated at runtime, but are now generated when the module is built.
  117. # Don't send patches against these modules, they will be ignored.
  118. # See generate_perl_modules.pl in the repository for generating this.
  119. use strict;
  120. use warnings;
  121. package %REPLACEME%;
  122. use parent 'Playwright::Base';
  123. =head1 CONSTRUCTOR
  124. =head2 new(%options)
  125. You shouldn't have to call this directly.
  126. Instead it should be returned to you as the result of calls on Playwright objects, or objects it returns.
  127. =cut
  128. sub new {
  129. my ($self,%options) = @_;
  130. $options{type} = '%CLASSNAME%';
  131. return $self->SUPER::new(%options);
  132. }
  133. =head1 METHODS
  134. =cut
  135. %SUBROUTINES%
  136. 1;