| 1 | package Module::Starter::App; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Module::Starter::App - the code behind the command line program | ||
| 6 | |||
| 7 | =head1 VERSION | ||
| 8 | |||
| 9 | version 1.77 | ||
| 10 | |||
| 11 | =cut | ||
| 12 | |||
| 13 | use warnings; | ||
| 14 | use strict; | ||
| 15 | |||
| 16 | our $VERSION = '1.77'; | ||
| 17 | |||
| 18 | use File::Spec; | ||
| 19 | use Getopt::Long; | ||
| 20 | use Pod::Usage; | ||
| 21 | use Carp qw( croak ); | ||
| 22 | use Module::Runtime qw( require_module ); | ||
| 23 | |||
| 24 | sub _config_file { | ||
| 25 | my $self = shift; | ||
| 26 | my $configdir = $ENV{'MODULE_STARTER_DIR'} || ''; | ||
| 27 | |||
| 28 | if ( !$configdir && $ENV{'HOME'} ) { | ||
| 29 | $configdir = File::Spec->catdir( $ENV{'HOME'}, '.module-starter' ); | ||
| 30 | } | ||
| 31 | |||
| 32 | return File::Spec->catfile( $configdir, 'config' ); | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | sub _config_read { | ||
| 37 | my $self = shift; | ||
| 38 | |||
| 39 | my $filename = $self->_config_file; | ||
| 40 | return () unless -e $filename; | ||
| 41 | |||
| 42 | open( my $config_file, '<', $filename ) | ||
| 43 | or die "couldn't open config file $filename: $!\n"; | ||
| 44 | |||
| 45 | my %config; | ||
| 46 | while (my $line = <$config_file>) { | ||
| 47 | chomp $line; | ||
| 48 | next if $line =~ /\A\s*\Z/sm; | ||
| 49 | if ($line =~ /\A(\w+):\s*(.+)\Z/sm) { $config{$1} = $2; } | ||
| 50 | } | ||
| 51 | |||
| 52 | return $self->_config_multi_process(%config); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _config_multi_process { | ||
| 56 | my ( $self, %config ) = @_; | ||
| 57 | |||
| 58 | # The options that accept multiple arguments must be set to an arrayref | ||
| 59 | foreach my $key (qw( builder ignores_type modules plugins )) { | ||
| 60 | $config{$key} = [ split /(?:\s*,\s*|\s+)/, (ref $config{$key} ? join(',', @{$config{$key}}) : $config{$key}) ] if $config{$key}; | ||
| 61 | $config{$key} = [] unless exists $config{$key}; | ||
| 62 | } | ||
| 63 | |||
| 64 | return %config; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub _process_command_line { | ||
| 68 | my ( $self, %config ) = @_; | ||
| 69 | |||
| 70 | $config{'argv'} = [ @ARGV ]; | ||
| 71 | |||
| 72 | pod2usage(2) unless @ARGV; | ||
| 73 | |||
| 74 | GetOptions( | ||
| 75 | 'class=s' => \$config{class}, | ||
| 76 | 'plugin=s@' => \$config{plugins}, | ||
| 77 | 'dir=s' => \$config{dir}, | ||
| 78 | 'distro=s' => \$config{distro}, | ||
| 79 | 'module=s@' => \$config{modules}, | ||
| 80 | 'builder=s@' => \$config{builder}, | ||
| 81 | 'ignores=s@' => \$config{ignores_type}, | ||
| 82 | eumm => sub { push @{$config{builder}}, 'ExtUtils::MakeMaker' }, | ||
| 83 | mb => sub { push @{$config{builder}}, 'Module::Build' }, | ||
| 84 | mi => sub { push @{$config{builder}}, 'Module::Install' }, | ||
| 85 | |||
| 86 | 'author=s' => \$config{author}, | ||
| 87 | 'email=s' => \$config{email}, | ||
| 88 | 'license=s' => \$config{license}, | ||
| 89 | genlicense => \$config{genlicense}, | ||
| 90 | 'minperl=s' => \$config{minperl}, | ||
| 91 | 'fatalize' => \$config{fatalize}, | ||
| 92 | |||
| 93 | force => \$config{force}, | ||
| 94 | verbose => \$config{verbose}, | ||
| 95 | version => sub { | ||
| 96 | require Module::Starter; | ||
| 97 | print "module-starter v$Module::Starter::VERSION\n"; | ||
| 98 | exit 1; | ||
| 99 | }, | ||
| 100 | help => sub { pod2usage(1); }, | ||
| 101 | ) or pod2usage(2); | ||
| 102 | |||
| 103 | if (@ARGV) { | ||
| 104 | pod2usage( | ||
| 105 | -msg => "Unparseable arguments received: " . join(',', @ARGV), | ||
| 106 | -exitval => 2, | ||
| 107 | ); | ||
| 108 | } | ||
| 109 | |||
| 110 | $config{class} ||= 'Module::Starter'; | ||
| 111 | |||
| 112 | $config{builder} = ['ExtUtils::MakeMaker'] unless $config{builder}; | ||
| 113 | |||
| 114 | return %config; | ||
| 115 | } | ||
| 116 | |||
| 117 | =head2 run | ||
| 118 | |||
| 119 | Module::Starter::App->run; | ||
| 120 | |||
| 121 | This is equivalent to running F<module-starter>. Its behavior is still subject | ||
| 122 | to change. | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub run { | ||
| 127 | my $self = shift; | ||
| 128 | my %config = $self->_config_read; | ||
| 129 | |||
| 130 | %config = $self->_process_command_line(%config); | ||
| 131 | %config = $self->_config_multi_process(%config); | ||
| 132 | |||
| 133 | require_module $config{class}; | ||
| 134 | $config{class}->import( @{ $config{'plugins'} } ); | ||
| 135 | |||
| 136 | my $starter = $config{class}->new( %config ); | ||
| 137 | $starter->postprocess_config; | ||
| 138 | $starter->pre_create_distro; | ||
| 139 | $starter->create_distro; | ||
| 140 | $starter->post_create_distro; | ||
| 141 | $starter->pre_exit; | ||
| 142 | |||
| 143 | return 1; | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; |