| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package App::Embra::App::Command::listdeps; | ||
| 5 | $App::Embra::App::Command::listdeps::VERSION = '0.001'; # TRIAL | ||
| 6 | # ABSTRACT: list plugins required to collate your site | ||
| 7 | |||
| 8 | use Class::Inspector qw<>; | ||
| 9 | use Module::Runtime qw<>; | ||
| 10 | |||
| 11 | use App::Embra::App -command; | ||
| 12 | use App::Embra::Util; | ||
| 13 | |||
| 14 | |||
| 15 | sub abstract { 'list required plugins' } | ||
| 16 | |||
| 17 | |||
| 18 | sub opt_spec { | ||
| 19 | return ( | ||
| 20 | [ 'root=s' => 'where to find embra.ini; defaults to .' ], | ||
| 21 | [ 'missing' => 'list only the missing dependencies' ], | ||
| 22 | [ 'versions' => 'include required version numbers in listing' ], | ||
| 23 | ); | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | sub execute { | ||
| 28 | my ( $self, $opt, $arg ) = @_; | ||
| 29 | require Path::Class; | ||
| 30 | |||
| 31 | my $deps = _format_deps( | ||
| 32 | _extract_deps( | ||
| 33 | Path::Class::dir(defined $opt->root ? $opt->root : '.'), | ||
| 34 | $opt->missing, | ||
| 35 | ), | ||
| 36 | $opt->versions | ||
| 37 | ); | ||
| 38 | |||
| 39 | if( $deps ) { | ||
| 40 | print $deps, "\n"; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | # straight from the mouth of App::Zilla::Util | ||
| 45 | |||
| 46 | sub _extract_deps { | ||
| 47 | my ($root, $missing) = @_; | ||
| 48 | |||
| 49 | my $ini = $root->file('embra.ini'); | ||
| 50 | |||
| 51 | die "embra listdeps only works on embra.ini files, and I couldn't find one at: $ini\n" | ||
| 52 | unless -e $ini; | ||
| 53 | |||
| 54 | my $fh = $ini->openr; | ||
| 55 | |||
| 56 | require Config::INI::Reader; | ||
| 57 | my $config = Config::INI::Reader->read_handle($fh); | ||
| 58 | |||
| 59 | require CPAN::Meta::Requirements; | ||
| 60 | my $reqs = CPAN::Meta::Requirements->new; | ||
| 61 | |||
| 62 | delete $config->{_}; | ||
| 63 | for my $sec (keys %$config) { | ||
| 64 | my ($p, $n) = split qr{ [ ]* / [ ]* }xms, $sec; | ||
| 65 | if( defined $n ) { | ||
| 66 | my $v = delete $config->{$sec}; | ||
| 67 | $v->{_name} ||= $n; | ||
| 68 | $config->{$p} = $v; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | my @packs = keys %$config; | ||
| 73 | |||
| 74 | foreach my $pack (@packs) { | ||
| 75 | |||
| 76 | my $version = 0; | ||
| 77 | if(exists $config->{$pack} && exists $config->{$pack}->{':version'}) { | ||
| 78 | $version = $config->{$pack}->{':version'}; | ||
| 79 | } | ||
| 80 | my $realname = App::Embra::Util->expand_config_package_name($pack); | ||
| 81 | $reqs->add_minimum($realname => $version); | ||
| 82 | |||
| 83 | if( not Class::Inspector->loaded( $realname ) ) { | ||
| 84 | Module::Runtime::require_module $realname; | ||
| 85 | } | ||
| 86 | if( $realname->does('App::Embra::Role::ExtraListDeps') ) { | ||
| 87 | $realname->add_extra_deps( config => $config->{$pack}, reqs => $reqs ) | ||
| 88 | } | ||
| 89 | |||
| 90 | } | ||
| 91 | |||
| 92 | seek $fh, 0, 0; | ||
| 93 | |||
| 94 | my $in_filter = 0; | ||
| 95 | while (<$fh>) { | ||
| 96 | next unless $in_filter or /^\[\s*\@Filter/; | ||
| 97 | $in_filter = 0, next if /^\[/ and ! /^\[\s*\@Filter/; | ||
| 98 | $in_filter = 1; | ||
| 99 | |||
| 100 | next unless /\A-bundle\s*=\s*([^;\s]+)/; | ||
| 101 | my $pname = $1; | ||
| 102 | chomp($pname); | ||
| 103 | $reqs->add_minimum(App::Embra::Util->expand_config_package_name($1) => 0) | ||
| 104 | } | ||
| 105 | |||
| 106 | seek $fh, 0, 0; | ||
| 107 | |||
| 108 | my @packages; | ||
| 109 | while (<$fh>) { | ||
| 110 | chomp; | ||
| 111 | next unless /\A\s*;\s*extradep\s*(\S+)\s*(=\s*(\S+))?\s*\z/; | ||
| 112 | my $ver = defined $3 ? $3 : "0"; | ||
| 113 | # Any "; extradep " is inserted at the beginning of the list | ||
| 114 | # in the file order so the user can control the order of at least a part of | ||
| 115 | # the plugin list | ||
| 116 | push @packages, $1; | ||
| 117 | # And added to the requirements so we can use it later | ||
| 118 | $reqs->add_minimum($1 => $ver); | ||
| 119 | } | ||
| 120 | |||
| 121 | my $vermap = $reqs->as_string_hash; | ||
| 122 | # Add the other requirements | ||
| 123 | push(@packages, sort keys %{ $vermap }); | ||
| 124 | |||
| 125 | # Move inc:: first in list as they may impact the loading of other | ||
| 126 | # plugins (in particular local ones). | ||
| 127 | # Also order inc:: so that thoses that want to hack @INC with inc:: plugins | ||
| 128 | # can have a consistant playground. | ||
| 129 | # We don't sort the others packages to preserve the same (random) ordering | ||
| 130 | # for the common case (no inc::, no '; authordep') as in previous dzil | ||
| 131 | # releases. | ||
| 132 | @packages = ((sort grep /^inc::/, @packages), (grep !/^inc::/, @packages)); | ||
| 133 | |||
| 134 | # Now that we have a sorted list of packages, use that to build an array of | ||
| 135 | # hashrefs for display. | ||
| 136 | require List::MoreUtils; | ||
| 137 | require Class::Load; | ||
| 138 | |||
| 139 | my @final = | ||
| 140 | map { { $_ => $vermap->{$_} } } | ||
| 141 | grep { $missing ? (! Class::Load::try_load_class($_, ($vermap->{$_} ? {-version => $vermap->{$_}} : ()))) : 1 } | ||
| 142 | List::MoreUtils::uniq( @packages ); | ||
| 143 | |||
| 144 | return \@final; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub _format_deps { | ||
| 148 | my ($reqs, $versions) = @_; | ||
| 149 | |||
| 150 | my $formatted = ''; | ||
| 151 | foreach my $rec (@{ $reqs }) { | ||
| 152 | my ($mod, $ver) = each(%{ $rec }); | ||
| 153 | $formatted .= $versions ? "$mod = $ver\n" : "$mod\n"; | ||
| 154 | } | ||
| 155 | chomp($formatted); | ||
| 156 | return $formatted; | ||
| 157 | } | ||
| 158 | |||
| 159 | 1; | ||
| 160 | |||
| 161 | __END__ |