| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use HTML::Mason '1.11'; | ||
| 5 | use File::Basename qw(dirname basename); | ||
| 6 | use File::Spec (); | ||
| 7 | use Cwd (); | ||
| 8 | |||
| 9 | my ( $params, $component, $args ) = parse_command_line(@ARGV); | ||
| 10 | |||
| 11 | # Set a default comp_root | ||
| 12 | unless ( exists $params->{comp_root} ) { | ||
| 13 | if ( File::Spec->file_name_is_absolute($component) ) { | ||
| 14 | $params->{comp_root} = dirname($component); | ||
| 15 | $component = '/' . basename($component); | ||
| 16 | } | ||
| 17 | else { | ||
| 18 | $params->{comp_root} = Cwd::cwd; | ||
| 19 | |||
| 20 | # Convert local path syntax to slashes | ||
| 21 | my ( $dirs, $file ) = ( File::Spec->splitpath($component) )[ 1, 2 ]; | ||
| 22 | $component = '/' . join '/', File::Spec->splitdir($dirs), $file; | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | my $interp = HTML::Mason::Interp->new(%$params); | ||
| 27 | $interp->exec( $component, @$args ); | ||
| 28 | |||
| 29 | ####################################################################################### | ||
| 30 | sub parse_command_line { | ||
| 31 | die usage() unless @_; | ||
| 32 | |||
| 33 | my %params; | ||
| 34 | while (@_) { | ||
| 35 | if ( $_[0] eq '--config_file' ) { | ||
| 36 | shift; | ||
| 37 | my $file = shift; | ||
| 38 | eval { require YAML; 1 } | ||
| 39 | or die | ||
| 40 | "--config_file requires the YAML Perl module to be installed.\n"; | ||
| 41 | my $href = YAML::LoadFile($file); | ||
| 42 | @params{ keys %$href } = values %$href; | ||
| 43 | |||
| 44 | } | ||
| 45 | elsif ( $_[0] =~ /^--/ ) { | ||
| 46 | my ( $k, $v ) = ( shift, shift ); | ||
| 47 | $k =~ s/^--//; | ||
| 48 | $params{$k} = $v; | ||
| 49 | |||
| 50 | } | ||
| 51 | else { | ||
| 52 | my $comp = shift; | ||
| 53 | return ( \%params, $comp, \@_ ); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | die usage(); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub usage { | ||
| 61 | return <<EOF; | ||
| 62 | |||
| 63 | Usage: $0 [--param1 value1 ...] [--config_file file] component [arg1 arg2 ...] | ||
| 64 | e.g.: $0 --comp_root /mason/comps component.mas | ||
| 65 | or: $0 --config_file /mason/config.yaml component.mas foo 5 bar 3 | ||
| 66 | |||
| 67 | Use --config_file to specify any additional parameters. | ||
| 68 | |||
| 69 | 'comp_root' defaults to current directory if component has a | ||
| 70 | relative path, or to dirname(component) otherwise. | ||
| 71 | |||
| 72 | See `perldoc HTML::Mason::Params` for a list of valid parameters. | ||
| 73 | |||
| 74 | EOF | ||
| 75 | } |