| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | wrt-render-all - utility for rendering static HTML files from wrt entries | ||
| 8 | |||
| 9 | =head1 USAGE | ||
| 10 | |||
| 11 | wrt render-all | ||
| 12 | wrt render-all --config ./wrt.json ... | ||
| 13 | wrt render-all --help | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | Renders all entries in the current wrt archive to the C<publish_dir> specified | ||
| 18 | in the configuration file (normally F<wrt.json>). By default, this is | ||
| 19 | F<./public>. | ||
| 20 | |||
| 21 | Detailed documentation can be found in the L<App::WRT> man page or at | ||
| 22 | L<https://code.p1k3.com/gitea/brennen/wrt>. | ||
| 23 | |||
| 24 | =head1 LICENSE | ||
| 25 | |||
| 26 | wrt is free software; you can redistribute it and/or modify | ||
| 27 | it under the terms of the GNU General Public License as published by | ||
| 28 | the Free Software Foundation; either version 2 of the License, or | ||
| 29 | (at your option) any later version. | ||
| 30 | |||
| 31 | =head1 AUTHOR | ||
| 32 | |||
| 33 | Brennen Bearnes <code@p1k3.com> | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | 1 | ▶ | use 5.10.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 38 | |||
| 39 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 40 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 41 | 1 | ▶ | no warnings 'uninitialized'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | |||
| 43 | 1 | ▶ | use Getopt::Long; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 44 | 1 | ▶ | use Pod::Usage; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 45 | 1 | ▶ | use Time::HiRes; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 46 | 0 | ▶ | use App::WRT; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 47 | use App::WRT::FileIO; | ||
| 48 | use App::WRT::Renderer; | ||
| 49 | |||
| 50 | my $start_time = [Time::HiRes::gettimeofday()]; | ||
| 51 | |||
| 52 | # If invoked directly from the command-line, caller() will return undef. | ||
| 53 | # Execute main() with a callback to print output directly, a FileIO object, | ||
| 54 | # and a copy of our real @ARGV: | ||
| 55 | if (not caller()) { | ||
| 56 | my $output = sub { say @_; }; | ||
| 57 | my $io = App::WRT::FileIO->new(); | ||
| 58 | main($output, $io, @ARGV); | ||
| 59 | exit(0); | ||
| 60 | } | ||
| 61 | |||
| 62 | # main() takes an output callback, a FileIO object or equivalent, and an @ARGV | ||
| 63 | # to pass in to GetOptionsFromArray(). This allows relatively simple | ||
| 64 | # integration tests to be written. See also: t/bin-wrt-render-all.t | ||
| 65 | sub main { | ||
| 66 | my ($output, $io, @local_argv) = @_; | ||
| 67 | |||
| 68 | # Handle options, including help generated from the POD above. See: | ||
| 69 | # - http://perldoc.perl.org/Getopt/Long.html#User-defined-subroutines-to-handle-options | ||
| 70 | # - https://metacpan.org/pod/Pod::Usage | ||
| 71 | # - http://michael.thegrebs.com/2014/06/08/Pod-Usage/ | ||
| 72 | my $config_file = 'wrt.json'; | ||
| 73 | GetOptions( | ||
| 74 | 'config=s' => \$config_file, | ||
| 75 | help => sub { pod2usage(0) }, | ||
| 76 | ) or pod2usage(2); | ||
| 77 | |||
| 78 | unless (-e $config_file) { | ||
| 79 | die "No wrt config file found. Tried: $config_file"; | ||
| 80 | } | ||
| 81 | |||
| 82 | my $wrt = App::WRT::new_from_file($config_file); | ||
| 83 | |||
| 84 | # This expects a callback to handle logging output and a callback to handle | ||
| 85 | # file writing: | ||
| 86 | |||
| 87 | my $renderer = App::WRT::Renderer->new( | ||
| 88 | $wrt, | ||
| 89 | $output, | ||
| 90 | $io | ||
| 91 | ); | ||
| 92 | |||
| 93 | $renderer->render(); | ||
| 94 | |||
| 95 | $output->( | ||
| 96 | "elapsed: " . Time::HiRes::tv_interval($start_time) . " seconds" | ||
| 97 | ); | ||
| 98 | } | ||
| 99 | |||
| 100 | 1; | ||