| 1 | package Module::Starter::Plugin::App::Cmd; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use parent 'Module::Starter::Simple'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use ExtUtils::Command qw( rm_rf mkpath touch ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use File::Spec (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | # Patch Module::Starter::Simple to call create_script() after create_modules() | ||||||||||
| 11 | 1 | ▶ | use Class::Monkey qw/Module::Starter::Simple/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 0 | ▶ | after 'create_modules' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 14 | 0 | my $self = shift; | |||||||||
| 15 | |||||||||||
| 16 | 0 | $self->create_script; | |||||||||
| 17 | 1 | }, 'Module::Starter::Simple'; | |||||||||
| 18 | |||||||||||
| 19 | 1 | our $VERSION = '0.02'; | |||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | =head1 NAME | ||||||||||
| 23 | |||||||||||
| 24 | Module::Starter::Plugin::App::Cmd | ||||||||||
| 25 | |||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||
| 27 | |||||||||||
| 28 | Guts for L<App::Cmd::Starter>. | ||||||||||
| 29 | |||||||||||
| 30 | =cut | ||||||||||
| 31 | |||||||||||
| 32 | #------------------------------------------------------------------------------- | ||||||||||
| 33 | |||||||||||
| 34 | 0 | ▶ | sub postprocess_config { | ||||||||
|
Subroutine
postprocess_config: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 35 | 0 | my $self = shift; | |||||||||
| 36 | |||||||||||
| 37 | 0 | ▶ | die "Script name must be specified\n" unless $self->{script}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | ▶ | die "At least one command must be specified\n" unless $self->{commands}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | #------------------------------------------------------------------------------- | ||||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | sub pre_create_distro { | ||||||||
|
Subroutine
pre_create_distro: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 45 | 0 | my $self = shift; | |||||||||
| 46 | |||||||||||
| 47 | 0 | $self->{main_module} = @{$self->{modules}}[0]; | |||||||||
| 48 | |||||||||||
| 49 | 0 | push @{$self->{modules}}, $self->{main_module} . "::Command"; | |||||||||
| 50 | |||||||||||
| 51 | 0 | foreach my $command (split /,/,$self->{commands}) { | |||||||||
| 52 | 0 | push @{$self->{modules}}, $self->{main_module} . "::Command::" . ucfirst $command; | |||||||||
| 53 | } | ||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | #------------------------------------------------------------------------------- | ||||||||||
| 58 | |||||||||||
| 59 | 0 | ▶ | sub module_guts { | ||||||||
|
Subroutine
module_guts: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 60 | 0 | my ($self, $module, $rtname) = @_; | |||||||||
| 61 | |||||||||||
| 62 | 0 | my $main_module = $self->{main_module}; | |||||||||
| 63 | |||||||||||
| 64 | 0 | given ($module) { | |||||||||
| 65 | 0 | when ($main_module) { | |||||||||
| 66 | 0 | $self->main_module_guts($module, $rtname); | |||||||||
| 67 | } | ||||||||||
| 68 | 0 | when ("$main_module"."::Command") { | |||||||||
| 69 | 0 | $self->command_pm_guts($module); | |||||||||
| 70 | } | ||||||||||
| 71 | 0 | when (/($main_module\b::Command)::(\w+)/) { | |||||||||
| 72 | 0 | $self->command_module_guts($module, $1, lc $2); | |||||||||
| 73 | } | ||||||||||
| 74 | 0 | default { | |||||||||
| 75 | 0 | $self->SUPER::module_guts($module, $rtname); | |||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | |||||||||||
| 81 | #------------------------------------------------------------------------------- | ||||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | sub main_module_guts { | ||||||||
|
Subroutine
main_module_guts: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 84 | 0 | my $self = shift; | |||||||||
| 85 | 0 | my $module = shift; | |||||||||
| 86 | 0 | my $rtname = shift; | |||||||||
| 87 | |||||||||||
| 88 | # Sub-templates | ||||||||||
| 89 | 0 | my $header = $self->_module_header($module, $rtname); | |||||||||
| 90 | 0 | my $bugs = $self->_module_bugs($module, $rtname); | |||||||||
| 91 | 0 | my $support = $self->_module_support($module, $rtname); | |||||||||
| 92 | 0 | my $license = $self->_module_license($module, $rtname); | |||||||||
| 93 | |||||||||||
| 94 | 0 | my $script = $self->{script}; | |||||||||
| 95 | 0 | my $env_var = uc($script =~ s/-/_/gr) . '_CONFIG'; | |||||||||
| 96 | |||||||||||
| 97 | 0 | my $content = <<"HERE"; | |||||||||
| 98 | $header | ||||||||||
| 99 | |||||||||||
| 100 | use 5.010; | ||||||||||
| 101 | use App::Cmd::Setup -app; | ||||||||||
| 102 | use Config::General qw/ParseConfig/; | ||||||||||
| 103 | use File::HomeDir; | ||||||||||
| 104 | use File::Spec::Functions qw/catfile/; | ||||||||||
| 105 | |||||||||||
| 106 | sub config { | ||||||||||
| 107 | state \$config = {ParseConfig(config_file())}; | ||||||||||
| 108 | return \$config; | ||||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | sub config_file { | ||||||||||
| 112 | my \@files = ( | ||||||||||
| 113 | \$ENV{$env_var}, | ||||||||||
| 114 | catfile(File::HomeDir->my_home, '.$script'), | ||||||||||
| 115 | '/usr/local/etc/$script', | ||||||||||
| 116 | '/etc/$script', | ||||||||||
| 117 | ); | ||||||||||
| 118 | |||||||||||
| 119 | foreach my \$file (grep {defined \$_} \@files) { | ||||||||||
| 120 | return \$file if -r \$file; | ||||||||||
| 121 | } | ||||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | \=head1 SYNOPSIS | ||||||||||
| 125 | |||||||||||
| 126 | Quick summary of what the module does. | ||||||||||
| 127 | |||||||||||
| 128 | Perhaps a little code snippet. | ||||||||||
| 129 | |||||||||||
| 130 | use $module; | ||||||||||
| 131 | |||||||||||
| 132 | my \$foo = $module->new(); | ||||||||||
| 133 | ... | ||||||||||
| 134 | |||||||||||
| 135 | \=head1 AUTHOR | ||||||||||
| 136 | |||||||||||
| 137 | $self->{author}, C<< <$self->{email_obfuscated}> >> | ||||||||||
| 138 | |||||||||||
| 139 | $bugs | ||||||||||
| 140 | |||||||||||
| 141 | $support | ||||||||||
| 142 | |||||||||||
| 143 | $license | ||||||||||
| 144 | |||||||||||
| 145 | \=cut | ||||||||||
| 146 | |||||||||||
| 147 | 1; # End of $module | ||||||||||
| 148 | HERE | ||||||||||
| 149 | 0 | return $content; | |||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | |||||||||||
| 153 | #------------------------------------------------------------------------------- | ||||||||||
| 154 | |||||||||||
| 155 | 0 | ▶ | sub command_pm_guts { | ||||||||
|
Subroutine
command_pm_guts: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 156 | 0 | my ($self, $module) = @_; | |||||||||
| 157 | |||||||||||
| 158 | 0 | return <<EOT; | |||||||||
| 159 | package $module; | ||||||||||
| 160 | |||||||||||
| 161 | use App::Cmd::Setup -command; | ||||||||||
| 162 | |||||||||||
| 163 | sub opt_spec { | ||||||||||
| 164 | my ( \$class, \$app ) = \@_; | ||||||||||
| 165 | |||||||||||
| 166 | # Example options | ||||||||||
| 167 | # | ||||||||||
| 168 | # return ( | ||||||||||
| 169 | # [ 'name=s' => "Name", {default => \$SUPER::config->{name} || undef} ], | ||||||||||
| 170 | # ); | ||||||||||
| 171 | return (); | ||||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | sub validate_args { | ||||||||||
| 175 | my ( \$self, \$opt, \$args ) = \@_; | ||||||||||
| 176 | |||||||||||
| 177 | # Example validation | ||||||||||
| 178 | # | ||||||||||
| 179 | # \$self->usage_message('Your error here') unless (\$some_condition); | ||||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | 1; | ||||||||||
| 183 | EOT | ||||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | |||||||||||
| 187 | #------------------------------------------------------------------------------- | ||||||||||
| 188 | |||||||||||
| 189 | 0 | ▶ | sub command_module_guts { | ||||||||
|
Subroutine
command_module_guts: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 190 | 0 | my ($self, $module, $base, $command) = @_; | |||||||||
| 191 | |||||||||||
| 192 | 0 | return <<EOT; | |||||||||
| 193 | package $module; | ||||||||||
| 194 | |||||||||||
| 195 | use strict; | ||||||||||
| 196 | use warnings; | ||||||||||
| 197 | use parent '$base'; | ||||||||||
| 198 | |||||||||||
| 199 | # Documentation | ||||||||||
| 200 | |||||||||||
| 201 | sub abstract { | ||||||||||
| 202 | return "Abstract for the $command command"; | ||||||||||
| 203 | } | ||||||||||
| 204 | |||||||||||
| 205 | sub usage_desc { | ||||||||||
| 206 | return "%c $command %o"; | ||||||||||
| 207 | } | ||||||||||
| 208 | |||||||||||
| 209 | sub description { | ||||||||||
| 210 | return "Description for the $command command\\nOptions:"; | ||||||||||
| 211 | } | ||||||||||
| 212 | |||||||||||
| 213 | |||||||||||
| 214 | # Command specific options | ||||||||||
| 215 | |||||||||||
| 216 | sub opt_spec { | ||||||||||
| 217 | my (\$class, \$app) = \@_; | ||||||||||
| 218 | |||||||||||
| 219 | return ( | ||||||||||
| 220 | # Example options | ||||||||||
| 221 | # | ||||||||||
| 222 | # [ "familiar" => "Use an informal greeting", {default => \$SUPER::config->{familiar} || undef} ], | ||||||||||
| 223 | |||||||||||
| 224 | \$class->SUPER::opt_spec, # Include global options | ||||||||||
| 225 | ); | ||||||||||
| 226 | } | ||||||||||
| 227 | |||||||||||
| 228 | |||||||||||
| 229 | # The command itself | ||||||||||
| 230 | |||||||||||
| 231 | sub execute { | ||||||||||
| 232 | my (\$self, \$opt, \$args) = \@_; | ||||||||||
| 233 | |||||||||||
| 234 | # require 'My::Dependency'; | ||||||||||
| 235 | # Tip: Using 'require' instead of 'use' will save memory and make startup faster | ||||||||||
| 236 | |||||||||||
| 237 | # Command code goes here | ||||||||||
| 238 | } | ||||||||||
| 239 | |||||||||||
| 240 | 1; | ||||||||||
| 241 | EOT | ||||||||||
| 242 | } | ||||||||||
| 243 | |||||||||||
| 244 | |||||||||||
| 245 | #------------------------------------------------------------------------------- | ||||||||||
| 246 | |||||||||||
| 247 | 0 | ▶ | sub create_script { | ||||||||
|
Subroutine
create_script: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 248 | 0 | my $self = shift; | |||||||||
| 249 | |||||||||||
| 250 | 0 | my $script_dir = File::Spec->catdir($self->{basedir}, 'script'); | |||||||||
| 251 | 0 | ▶ | unless (-d $script_dir) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 252 | 0 | local @ARGV = $script_dir; | |||||||||
| 253 | 0 | mkpath @ARGV; | |||||||||
| 254 | 0 | $self->progress("Created $script_dir"); | |||||||||
| 255 | } | ||||||||||
| 256 | |||||||||||
| 257 | 0 | my $script_file = File::Spec->catfile($script_dir, $self->{script}); | |||||||||
| 258 | 0 | $self->create_file($script_file, <<EOT); | |||||||||
| 259 | #! /usr/bin/env perl | ||||||||||
| 260 | |||||||||||
| 261 | use $self->{main_module}; | ||||||||||
| 262 | $self->{main_module}->run; | ||||||||||
| 263 | EOT | ||||||||||
| 264 | 0 | $self->progress("Created $script_file"); | |||||||||
| 265 | } | ||||||||||
| 266 | |||||||||||
| 267 | |||||||||||
| 268 | #------------------------------------------------------------------------------- | ||||||||||
| 269 | |||||||||||
| 270 | 0 | ▶ | sub post_create_distro { | ||||||||
|
Subroutine
post_create_distro: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 271 | 0 | my $self = shift; | |||||||||
| 272 | } | ||||||||||
| 273 | |||||||||||
| 274 | |||||||||||
| 275 | #------------------------------------------------------------------------------- | ||||||||||
| 276 | |||||||||||
| 277 | =head1 AUTHOR | ||||||||||
| 278 | |||||||||||
| 279 | Jon Allen (JJ), C<< <jj at jonallen.info> >> | ||||||||||
| 280 | |||||||||||
| 281 | =head1 BUGS | ||||||||||
| 282 | |||||||||||
| 283 | Please report any bugs or feature requests to C<bug-module-starter-plugin-app-cmd at rt.cpan.org>, or through | ||||||||||
| 284 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Module-Starter-Plugin-App-Cmd>. I will be notified, and then you'll | ||||||||||
| 285 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 286 | |||||||||||
| 287 | |||||||||||
| 288 | |||||||||||
| 289 | |||||||||||
| 290 | =head1 SUPPORT | ||||||||||
| 291 | |||||||||||
| 292 | You can find documentation for this module with the perldoc command. | ||||||||||
| 293 | |||||||||||
| 294 | perldoc Module::Starter::Plugin::App::Cmd | ||||||||||
| 295 | |||||||||||
| 296 | |||||||||||
| 297 | You can also look for information at: | ||||||||||
| 298 | |||||||||||
| 299 | =over 4 | ||||||||||
| 300 | |||||||||||
| 301 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 302 | |||||||||||
| 303 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Module-Starter-Plugin-App-Cmd> | ||||||||||
| 304 | |||||||||||
| 305 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 306 | |||||||||||
| 307 | L<http://annocpan.org/dist/Module-Starter-Plugin-App-Cmd> | ||||||||||
| 308 | |||||||||||
| 309 | =item * CPAN Ratings | ||||||||||
| 310 | |||||||||||
| 311 | L<http://cpanratings.perl.org/d/Module-Starter-Plugin-App-Cmd> | ||||||||||
| 312 | |||||||||||
| 313 | =item * Search CPAN | ||||||||||
| 314 | |||||||||||
| 315 | L<http://search.cpan.org/dist/Module-Starter-Plugin-App-Cmd/> | ||||||||||
| 316 | |||||||||||
| 317 | =back | ||||||||||
| 318 | |||||||||||
| 319 | |||||||||||
| 320 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 321 | |||||||||||
| 322 | |||||||||||
| 323 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 324 | |||||||||||
| 325 | Copyright 2012 Jon Allen (JJ). | ||||||||||
| 326 | |||||||||||
| 327 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 328 | under the terms of either: the GNU General Public License as published | ||||||||||
| 329 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 330 | |||||||||||
| 331 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 332 | |||||||||||
| 333 | |||||||||||
| 334 | =cut | ||||||||||
| 335 | |||||||||||
| 336 | 1 | 1; # End of Module::Starter::Plugin::App::Cmd | |||||||||