serge icon indicating copy to clipboard operation
serge copied to clipboard

run_command for jobs with several destination_languages dont change %OUTFILE% variable

Open mackushev opened this issue 2 years ago • 1 comments

simple run_command plugin like this:


    destination_languages                   de fr es ja ko zh-Hans
    callback_plugins
    {
        :out
        {
            plugin         run_command

            data
            {
                command echo '%OUTFILE%' | sed "s|^$(pwd)/||" >> /root/out.log
            }
        }
    }    

produce output:

res/example/German/dlg/example_German.rc/root/res/example/German/dlg/ res/example/German/dlg/example_German.rc/root/res/example/German/dlg/ res/example/German/dlg/example_German.rc/root/res/example/German/dlg/ res/example/German/dlg/example_German.rc/root/res/example/German/dlg/ res/example/German/dlg/example_German.rc/root/res/example/German/dlg/ res/example/German/dlg/example_German.rc/root/res/example/German/dlg/

mackushev avatar Dec 23 '22 22:12 mackushev

In the run_command.pm file, the $command variable is overwritten. You can fix this by changing

sub process_then_block {
    my ($self, $phase, $block, $file, $lang, $strref) = @_;

    die "This plugin should only be used in 'after_save_localized_file' phase (current phase: '$phase')" unless $phase eq 'after_save_localized_file';

    my $outfile = $self->{parent}->{engine}->get_full_output_path($file, $lang);
    ($_, my $outpath, $_) = fileparse($outfile); # this way $outpath will include the trailing delimiter

    foreach my $command (@{$block->{command}}) {
        # substitute %FILE% and target language-based macros
        # with the full path to the saved file
        $command = subst_macros($command, $file, $lang);

        # substitute %OUTFILE% macro with the full path to the saved file
        $command =~ s/%OUTFILE%/$outfile/sg;
        # substitute %OUTPATH% macro with the full directory path
        $command =~ s/%OUTPATH%/$outpath/sg;

        die "After macro substitution, 'command' parameter evaluates to an empty string" if $command eq '';

        print "RUN: $command\n";
        system($command);

        my $error_code = unpack 'c', pack 'C', $? >> 8; # error code
        die "Exit code: $error_code\n" if $error_code != 0;
    }

    return (shift @_)->SUPER::process_then_block(@_);
}

to

sub process_then_block {
    my ($self, $phase, $block, $file, $lang, $strref) = @_;

    die "This plugin should only be used in 'after_save_localized_file' phase (current phase: '$phase')" unless $phase eq 'after_save_localized_file';

    my $outfile = $self->{parent}->{engine}->get_full_output_path($file, $lang);
    ($_, my $outpath, $_) = fileparse($outfile); # this way $outpath will include the trailing delimiter

    foreach my $command (@{$block->{command}}) {
        # substitute %FILE% and target language-based macros
        # with the full path to the saved file
        my $runcmd = subst_macros($command, $file, $lang);

        # substitute %OUTFILE% macro with the full path to the saved file
        $runcmd =~ s/%OUTFILE%/$outfile/sg;
        # substitute %OUTPATH% macro with the full directory path
        $runcmd =~ s/%OUTPATH%/$outpath/sg;

        die "After macro substitution, 'command' parameter evaluates to an empty string" if $runcmd eq '';

        print "RUN: $runcmd\n";
        system($runcmd);

        my $error_code = unpack 'c', pack 'C', $? >> 8; # error code
        die "Exit code: $error_code\n" if $error_code != 0;
    }

    return (shift @_)->SUPER::process_then_block(@_);
}

jimmymcpeter avatar May 24 '23 12:05 jimmymcpeter