| 1 | package Mason::App; | ||
| 2 | $Mason::App::VERSION = '2.23'; | ||
| 3 | 2 | ▶ | use Cwd qw(realpath); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use File::Basename; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use File::Temp qw(tempdir); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use Getopt::Long; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use Mason; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Mason::Util qw(json_decode); | ||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | my $usage = | ||
| 13 | "usage: $0 [--data-dir dir] [--plugins Plugin1,Plugin2] [--args json-string] [-e source] [template-file]"; | ||
| 14 | |||
| 15 | sub run { | ||
| 16 | my ( %params, $args, $source, $help ); | ||
| 17 | GetOptions( | ||
| 18 | 'args=s' => \$args, | ||
| 19 | 'e=s' => \$source, | ||
| 20 | 'h|help' => \$help, | ||
| 21 | map { dashify($_) . "=s" => \$params{$_} } qw(data_dir plugins) | ||
| 22 | ) or usage(); | ||
| 23 | if ($help) { | ||
| 24 | system("perldoc $0"); | ||
| 25 | exit; | ||
| 26 | } | ||
| 27 | %params = map { defined( $params{$_} ) ? ( $_, $params{$_} ) : () } keys(%params); | ||
| 28 | if ( $params{plugins} ) { | ||
| 29 | $params{plugins} = [ split( /\s*,\s*/, $params{plugins} ) ]; | ||
| 30 | } | ||
| 31 | my %run_args = defined($args) ? %{ json_decode($args) } : (); | ||
| 32 | |||
| 33 | my $tempdir = tempdir( 'mason-XXXX', TMPDIR => 1, CLEANUP => 1 ); | ||
| 34 | my $file; | ||
| 35 | if ($source) { | ||
| 36 | $file = "$tempdir/source.mc"; | ||
| 37 | open( my $fh, ">", $file ); | ||
| 38 | print $fh $source; | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | $file = shift(@ARGV); | ||
| 42 | usage() if @ARGV; | ||
| 43 | if ( !$file ) { | ||
| 44 | $file = "$tempdir/stdin.mc"; | ||
| 45 | open( my $fh, ">", $file ); | ||
| 46 | while (<STDIN>) { print $fh $_ } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | my $comp_root = dirname($file); | ||
| 51 | my $path = "/" . basename($file); | ||
| 52 | my $interp = Mason->new( comp_root => $comp_root, autoextend_request_path => 0, %params ); | ||
| 53 | print $interp->run( $path, %run_args )->output . "\n"; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub usage { | ||
| 57 | print "$usage\n"; | ||
| 58 | exit; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub dashify { | ||
| 62 | my $name = shift; | ||
| 63 | $name =~ s/_/-/g; | ||
| 64 | return $name; | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; | ||
| 68 | |||
| 69 | __END__ | ||
| 70 | |||
| 71 | =pod | ||
| 72 | |||
| 73 | =head1 NAME | ||
| 74 | |||
| 75 | Mason::App - Implementation of bin/mason | ||
| 76 | |||
| 77 | =head1 DESCRIPTION | ||
| 78 | |||
| 79 | See documentation for bin/mason. | ||
| 80 | |||
| 81 | =head1 SEE ALSO | ||
| 82 | |||
| 83 | L<Mason|Mason> | ||
| 84 | |||
| 85 | =head1 AUTHOR | ||
| 86 | |||
| 87 | Jonathan Swartz <swartz@pobox.com> | ||
| 88 | |||
| 89 | =head1 COPYRIGHT AND LICENSE | ||
| 90 | |||
| 91 | This software is copyright (c) 2012 by Jonathan Swartz. | ||
| 92 | |||
| 93 | This is free software; you can redistribute it and/or modify it under | ||
| 94 | the same terms as the Perl 5 programming language system itself. | ||
| 95 | |||
| 96 | =cut | ||