X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=scripts%2Fgenerate_client_js_actions.pl;h=f7f9a16ef7475e8b8ceeb8671c9ca347b8db94dc;hb=7bacc2aae8c0bf72a8fe1eaf757890184e6950f8;hp=b2598b39e4af0085ef8b031b9bf9c683d390d479;hpb=49556f01cfe6a395cf5554e1a00dc61d6da0493e;p=kivitendo-erp.git diff --git a/scripts/generate_client_js_actions.pl b/scripts/generate_client_js_actions.pl index b2598b39e..f7f9a16ef 100755 --- a/scripts/generate_client_js_actions.pl +++ b/scripts/generate_client_js_actions.pl @@ -4,41 +4,61 @@ use strict; use warnings; use File::Slurp; +use FindBin; use List::Util qw(first max); +use Template; -my $file_name = (first { -f } qw(SL/ClientJS.pm ../SL/ClientJS.pm)) || die "ClientJS.pm not found"; +my $rel_dir = $FindBin::Bin . '/..'; my @actions; -foreach (read_file($file_name)) { +foreach (read_file("${rel_dir}/SL/ClientJS.pm")) { chomp; next unless (m/^my \%supported_methods/ .. m/^\);/); - push @actions, [ 'action', $1, $2 ] if m/^\s+([a-zA-Z]+)\s*=>\s*(\d+),$/; - push @actions, [ 'comment', $1 ] if m/^\s+#\s+(.+)/; + push @actions, [ 'action', $1, $2, $3 ] if m/^ \s+ '? ([a-zA-Z_:]+) '? \s*=>\s* (-? \d+) , (?: \s* \# \s+ (.+))? $/x; + push @actions, [ 'comment', $1, $2 ] if m/^ \s+\# \s+ (.+?) (?: \s* pattern: \s+ (.+))? $/x; } -my $longest = max map { length($_->[1]) } grep { $_->[0] eq 'action' } @actions; -my $first = 1; -my $output; +my $longest = max map { length($_->[1]) } grep { $_->[0] eq 'action' } @actions; +my $first = 1; +my $default_pattern = '$().()'; +my $pattern = $default_pattern; +my $output = ''; -# else if (action[0] == 'hide') $(action[1]).hide(); foreach my $action (@actions) { if ($action->[0] eq 'comment') { - print "\n" unless $first; - print " // ", $action->[1], "\n"; + $output .= "\n" unless $first; + $output .= " // " . $action->[1] . "\n"; + + $pattern = $action->[2] eq '' ? $default_pattern : $action->[2] if $action->[2]; } else { - my $args = $action->[2] == 1 ? '' : join(', ', map { "action[$_]" } (2..$action->[2])); - - printf(' %s if (action[0] == \'%s\')%s $(action[1]).%s(%s);' . "\n", - $first ? ' ' : 'else', - $action->[1], - ' ' x ($longest - length($action->[1])), - $action->[1], - $args); - $first = 0; + my $args = $action->[2] == 1 ? '' + : $action->[2] < 0 ? 'action.slice(2, action.length)' + : join(', ', map { "action[$_]" } (2..$action->[2])); + + $output .= sprintf(' %s if (action[0] == \'%s\')%s ', + $first ? ' ' : 'else', + $action->[1], + ' ' x ($longest - length($action->[1]))); + + my $function = $action->[1]; + $function =~ s/.*://; + + my $call = $action->[3] || $pattern; + $call =~ s//'action[1]'/eg; + $call =~ s//$function/eg; + $call =~ s//$args/eg; + $call =~ s//'action[' . ($1 + 1) . ']'/eg; + + $output .= $call . ";\n"; + $first = 0; } } -printf "\n else\%sconsole.log('Unknown action: ' + action[0]);\n", ' ' x (4 + 2 + 6 + 3 + 4 + 2 + $longest + 1); +$output .= sprintf "\n else\%sconsole.log('Unknown action: ' + action[0]);\n", ' ' x (4 + 2 + 6 + 3 + 4 + 2 + $longest + 1); + +my $template = Template->new({ ABSOLUTE => 1 }); +$template->process($rel_dir . '/scripts/generate_client_js_actions.tpl', { actions => $output }, $rel_dir . '/js/client_js.js') || die $template->error(), "\n"; +print "js/client_js.js generated automatically.\n";