| 1 | package CPAN::Maker::Bootstrapper::Role::AddPlugin; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use CLI::Simple::Constants qw(:booleans); | ||
| 7 | use CLI::Simple::Utils qw(choose slurp); | ||
| 8 | |||
| 9 | use Data::Dumper; | ||
| 10 | use English qw(-no_match_vars); | ||
| 11 | use File::ShareDir qw(dist_dir dist_file); | ||
| 12 | use File::Copy; | ||
| 13 | use File::Path qw(make_path); | ||
| 14 | use Scalar::Util qw(reftype); | ||
| 15 | use YAML::Tiny; | ||
| 16 | |||
| 17 | use Role::Tiny; | ||
| 18 | |||
| 19 | our $VERSION = '2.6.2'; | ||
| 20 | |||
| 21 | ######################################################################## | ||
| 22 | sub cmd_add_plugin { | ||
| 23 | ######################################################################## | ||
| 24 | my ($self) = @_; | ||
| 25 | |||
| 26 | my ($plugin) = $self->get_args; | ||
| 27 | |||
| 28 | die "ERROR: usage: add-plugin plugin-name\n" | ||
| 29 | if !$plugin; | ||
| 30 | |||
| 31 | my $distribution_name = sprintf 'CPAN-Maker-Bootstrapper-Role-%s', $plugin; | ||
| 32 | |||
| 33 | my $dist_dir = eval { return dist_dir($distribution_name); }; | ||
| 34 | |||
| 35 | die "ERROR: no such plugin $plugin\n" | ||
| 36 | if !$dist_dir; | ||
| 37 | |||
| 38 | my $plugin_dir = sprintf '.roles/%s', $distribution_name; | ||
| 39 | |||
| 40 | die "ERROR: plugin already exists. use --force to overwrite plugin\n" | ||
| 41 | if !$self->get_force && -d $plugin_dir; | ||
| 42 | |||
| 43 | if ( !-d $plugin_dir ) { | ||
| 44 | make_path($plugin_dir) | ||
| 45 | or die "ERROR: could not create $plugin_dir\n"; | ||
| 46 | } | ||
| 47 | |||
| 48 | die "ERROR: distribution does not contain a role.yml file\n" | ||
| 49 | if !-e "$dist_dir/role.yml"; | ||
| 50 | |||
| 51 | foreach my $file (qw(role.yml role.mk)) { | ||
| 52 | next if !-f "$plugin_dir/$file"; | ||
| 53 | |||
| 54 | rename "$plugin_dir/$file", "$plugin_dir/$file.bak" | ||
| 55 | or die "ERROR: could not create backup file: $file\n"; | ||
| 56 | } | ||
| 57 | |||
| 58 | eval { | ||
| 59 | copy "$dist_dir/role.yml", "$plugin_dir/role.yml" | ||
| 60 | or die "ERROR: could not copy role.yml to $plugin_dir/role.yml\n"; | ||
| 61 | |||
| 62 | if ( -f "$dist_dir/role.mk" ) { | ||
| 63 | copy "$dist_dir/role.mk", "$plugin_dir/role.mk" | ||
| 64 | or die "ERROR: could not copy role.mk to $plugin_dir/role.mk\n"; | ||
| 65 | } | ||
| 66 | |||
| 67 | $self->_check_roles($plugin_dir); | ||
| 68 | }; | ||
| 69 | |||
| 70 | my $err = $EVAL_ERROR; | ||
| 71 | |||
| 72 | if ($err) { | ||
| 73 | unlink "$plugin_dir/role.yml"; | ||
| 74 | unlink "$plugin_dir/role.mk"; | ||
| 75 | |||
| 76 | # if one or more of the .bak files do not exist this will fail silently - no harm, no foul | ||
| 77 | rename "$plugin_dir/role.yml.bak", "$plugin_dir/role.yml"; | ||
| 78 | rename "$plugin_dir/role.mk.bak", "$plugin_dir/role.mk"; | ||
| 79 | die "ERROR: could not install plugin\n$err"; | ||
| 80 | } | ||
| 81 | |||
| 82 | unlink "$plugin_dir/role.yml.bak"; | ||
| 83 | unlink "$plugin_dir/role.mk.bak"; | ||
| 84 | print {*STDOUT} <<"END_OF_TEXT"; | ||
| 85 | Plugin $plugin successfully installed. | ||
| 86 | |||
| 87 | Plugin targets are now available. Try 'make help' to see all targets. | ||
| 88 | |||
| 89 | To make this plugin available to others who check out the project, | ||
| 90 | consider adding its files to version control: | ||
| 91 | |||
| 92 | git add .roles/$distribution_name/ | ||
| 93 | |||
| 94 | END_OF_TEXT | ||
| 95 | return $SUCCESS; | ||
| 96 | } | ||
| 97 | |||
| 98 | ######################################################################## | ||
| 99 | sub _check_roles { | ||
| 100 | ######################################################################## | ||
| 101 | my ( $self, $plugin_dir ) = @_; | ||
| 102 | |||
| 103 | my $roles = eval { | ||
| 104 | my $yaml = YAML::Tiny->read("$plugin_dir/role.yml"); | ||
| 105 | return $yaml->[0]->{roles}; | ||
| 106 | }; | ||
| 107 | |||
| 108 | die "ERROR: unable to determine roles from role.yml\n" | ||
| 109 | if !$roles || $EVAL_ERROR; | ||
| 110 | |||
| 111 | die "ERROR: roles must be a hash of command/class pairs. Example: foo: C\n" | ||
| 112 | if !ref $roles || reftype($roles) ne 'HASH'; | ||
| 113 | |||
| 114 | foreach my $class ( values %{$roles} ) { | ||
| 115 | eval { | ||
| 116 | my $package_name = $class; | ||
| 117 | $package_name =~ s{::}{/}xmsg; | ||
| 118 | |||
| 119 | require "$package_name.pm"; | ||
| 120 | 1; | ||
| 121 | } or die "ERROR: $class cannot be loaded\n$EVAL_ERROR"; | ||
| 122 | } | ||
| 123 | |||
| 124 | return; | ||
| 125 | } | ||
| 126 | |||
| 127 | 1; |