Commits

Anonymous committed ed69840

Added the --make flag with some rudimentary tests.

Comments (0)

Files changed (2)

modules/App-XML-DocBook-Docmake/lib/App/XML/DocBook/Docmake.pm

 
 __PACKAGE__->mk_accessors(qw(
     _input_path
+    _make_like
     _mode
     _output_path
     _stylesheet
     my $verbose = 0;
     my $stylesheet;
     my @in_stringparams;
+    my $make_like = 0;
 
     my $ret = GetOptionsFromArray($argv,
         "o=s" => \$output_path,
         "v|verbose" => \$verbose,
         "x|stylesheet=s" => \$stylesheet,
         "stringparam=s" => \@in_stringparams,
+        "make" => \$make_like,
     );
 
     my @stringparams;
     $self->_verbose($verbose);
     $self->_stylesheet($stylesheet);
     $self->_xslt_stringparams(\@stringparams);
+    $self->_make_like($make_like);
 
     my $mode = shift(@$argv);
 
 EOF
 }
 
+sub _is_older
+{
+    my $self = shift;
+
+    my $file1 = shift;
+    my $file2 = shift;
+
+    my @stat1 = stat($file1);
+    my @stat2 = stat($file2);
+
+    return ($stat1[9] <= $stat2[9]);
+}
+
+sub _should_update_output
+{
+    my $self = shift;
+    my $args = shift;
+
+    return $self->_is_older($args->{output}, $args->{input});
+}
+
 sub _run_mode_fo
 {
     my $self = shift;
         }
     }
 
-    return ["-o", $output_path];
+    return $output_path;
+}
+
+sub _pre_proc_command
+{
+    my ($self, $args) = @_;
+
+    my $input_file = $args->{input};
+    my $output_file = $args->{output};
+    my $template = $args->{template};
+    
+    my @cmd;
+    foreach my $arg (@$template)
+    {
+        # If it's a string
+        if (ref($arg) eq "")
+        {
+            push @cmd, $arg;
+        }
+        elsif ($arg->is_output())
+        {
+            push @cmd, $output_file;
+        }
+        elsif ($arg->is_input())
+        {
+            push @cmd, $input_file;
+        }
+        else
+        {
+            die "Unknown Argument in Command Template.";
+        }
+    }
+    return \@cmd;
+}
+
+sub _run_input_output_cmd
+{
+    my $self = shift;
+    my $args = shift;
+
+    my $input_file = $args->{input};
+    my $output_file = $args->{output};
+
+    if (
+        (!$self->_make_like())
+            ||
+        $self->_should_update_output(
+            {
+                input => $input_file,
+                output => $output_file
+            }
+        )
+    )
+    {
+        $self->_exec_command(
+            $self->_pre_proc_command($args),
+        );
+    }
 }
 
 sub _run_xslt
     {
         @stylesheet_params = ($self->_stylesheet());
     }
-    
-    return $self->_exec_command(
-        [
-            "xsltproc",
-            @{$self->_calc_output_param_for_xslt($args)},
-            (map { ("--stringparam", @$_ ) } @{$self->_xslt_stringparams()}),
-            @stylesheet_params,
-            $self->_input_path(),
-        ],
+ 
+    return $self->_run_input_output_cmd(
+        {
+            input => $self->_input_path(),
+            output => $self->_calc_output_param_for_xslt($args),
+            template =>
+            [
+                "xsltproc",
+                "-o", $self->_output_cmd_comp(),
+                (map { ("--stringparam", @$_ ) } @{$self->_xslt_stringparams()}),
+                @stylesheet_params,
+                $self->_input_cmd_comp(),
+            ],
+        },
     );
 }
 
 
     my $xslt_output_path = $self->_output_path();
 
+    # TODO : do something meaningful if a period (".") is not present
     $xslt_output_path =~ s{\.([^\.]*)\z}{\.fo}ms;
 
     $self->_run_xslt({output_path => $xslt_output_path});
 
-    return $self->_exec_command(
-        [
-            "fop",
-            ("-".$args->{fo_out_format}),
-            $self->_output_path(),
-            $xslt_output_path,
-        ],
+    return $self->_run_input_output_cmd(
+        {
+            input => $xslt_output_path,
+            output => $self->_output_path(),
+            template =>
+            [
+                "fop",
+                ("-".$args->{fo_out_format}),
+                $self->_output_cmd_comp(),
+                $self->_input_cmd_comp(),
+            ],
+        },
     );
 }
 
     );
 }
 
+sub _input_cmd_comp
+{
+    my $self = shift;
+
+    return App::XML::DocBook::Docmake::CmdComponent->new(
+        {
+            is_input => 1,
+            is_output => 0,
+        }
+    );
+}
+
+sub _output_cmd_comp
+{
+    my $self = shift;
+
+    return App::XML::DocBook::Docmake::CmdComponent->new(
+        {
+            is_input => 0,
+            is_output => 1,
+        }
+    );
+}
+
+package App::XML::DocBook::Docmake::CmdComponent;
+
+use base 'Class::Accessor';
+
+__PACKAGE__->mk_accessors(qw(
+    is_input
+    is_output
+    ));
+
+1;
+
 =head1 AUTHOR
 
 Shlomi Fish, C<< <shlomif at cpan.org> >>

modules/App-XML-DocBook-Docmake/t/01-use.t

 use strict;
 use warnings;
 
-use Test::More tests => 4;
+use Test::More tests => 7;
 use Test::Trap qw( trap $trap :flow:stderr(systemsafe):stdout(systemsafe):warn );
 
 use App::XML::DocBook::Docmake;
     return \@ret;
 }
 
+package MyTest::DocmakeAppDebug::Newer;
+
+use vars qw($should_update);
+
+use vars qw(@ISA);
+
+@ISA=('MyTest::DocmakeAppDebug');
+
+sub _should_update_output
+{
+    return $should_update->(@_);
+}
+
 package main;
 
 {
     );
 }
 
+{
+    my @should_update;
+    local $MyTest::DocmakeAppDebug::Newer::should_update = sub {
+        my $self = shift;
+        my $args = shift;
+        push @should_update,
+            [ map { $_ => $args->{$_} } 
+             sort { $a cmp $b }
+             keys(%$args)
+            ]
+            ;
+        return 1;
+    };
+    my $docmake = MyTest::DocmakeAppDebug::Newer->new({argv => [
+            "-v",
+            "--make",
+            "-o", "GOTO-THE-output.pdf",
+            "pdf",
+            "MYMY-input.xml",
+            ]});
+
+    # TEST
+    ok ($docmake, "Docmake was constructed successfully");
+
+    $docmake->run();
+
+    # TEST
+    is_deeply(MyTest::DocmakeAppDebug->debug_commands(),
+        [
+            [
+                "xsltproc",
+                "-o", "GOTO-THE-output.fo",
+                "http://docbook.sourceforge.net/release/xsl/current/fo/docbook.xsl",
+                "MYMY-input.xml",
+            ],
+            [
+                "fop",
+                "-pdf",
+                "GOTO-THE-output.pdf",
+                "GOTO-THE-output.fo",
+            ],
+        ],
+        "Making sure all commands got run",
+    );
+
+    # TEST
+    is_deeply(\@should_update,
+        [
+            [ "input", "MYMY-input.xml", "output", "GOTO-THE-output.fo"],
+            [ "input", "GOTO-THE-output.fo", "output", "GOTO-THE-output.pdf" ],
+        ],
+        "should update is OK.",
+    );
+}
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.