Serverseitiges Erzeugen von im Client ausgeführten JavaScript-Befehlen
[kivitendo-erp.git] / scripts / generate_client_js_actions.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use File::Slurp;
7 use List::Util qw(first max);
8
9 my $file_name = (first { -f } qw(SL/ClientJS.pm ../SL/ClientJS.pm)) || die "ClientJS.pm not found";
10 my @actions;
11
12 foreach (read_file($file_name)) {
13   chomp;
14
15   next unless (m/^my \%supported_methods/ .. m/^\);/);
16
17   push @actions, [ 'action',  $1, $2 ] if m/^\s+([a-zA-Z]+)\s*=>\s*(\d+),$/;
18   push @actions, [ 'comment', $1     ] if m/^\s+#\s+(.+)/;
19 }
20
21 my $longest   = max map { length($_->[1]) } grep { $_->[0] eq 'action' } @actions;
22 my $first     = 1;
23 my $output;
24
25 #      else if (action[0] == 'hide')        $(action[1]).hide();
26 foreach my $action (@actions) {
27   if ($action->[0] eq 'comment') {
28     print "\n" unless $first;
29     print "      // ", $action->[1], "\n";
30
31   } else {
32     my $args = $action->[2] == 1 ? '' : join(', ', map { "action[$_]" } (2..$action->[2]));
33
34     printf('      %s if (action[0] == \'%s\')%s $(action[1]).%s(%s);' . "\n",
35            $first ? '    ' : 'else',
36            $action->[1],
37            ' ' x ($longest - length($action->[1])),
38            $action->[1],
39            $args);
40     $first = 0;
41   }
42 }
43
44 printf "\n      else\%sconsole.log('Unknown action: ' + action[0]);\n", ' ' x (4 + 2 + 6 + 3 + 4 + 2 + $longest + 1);