| 1 | package Dist::Inkt::Role::WriteMakefilePL; | ||
| 2 | |||
| 3 | our $AUTHORITY = 'cpan:TOBYINK'; | ||
| 4 | our $VERSION = '0.026'; | ||
| 5 | |||
| 6 | use Moose::Role; | ||
| 7 | use Types::Standard -types; | ||
| 8 | use Data::Dump 'pp'; | ||
| 9 | use namespace::autoclean; | ||
| 10 | |||
| 11 | sub DYNAMIC_CONFIG_PATH () { 'meta/DYNAMIC_CONFIG.PL' }; | ||
| 12 | |||
| 13 | has has_shared_files => ( | ||
| 14 | is => 'ro', | ||
| 15 | isa => Bool, | ||
| 16 | lazy => 1, | ||
| 17 | builder => '_build_has_shared_files', | ||
| 18 | ); | ||
| 19 | |||
| 20 | sub _build_has_shared_files | ||
| 21 | { | ||
| 22 | my $self = shift; | ||
| 23 | !! $self->sourcefile('share')->is_dir; | ||
| 24 | } | ||
| 25 | |||
| 26 | has directories_containing_tests => ( | ||
| 27 | is => 'ro', | ||
| 28 | isa => ArrayRef, | ||
| 29 | lazy => 1, | ||
| 30 | builder => '_build_directories_containing_tests', | ||
| 31 | ); | ||
| 32 | |||
| 33 | sub _build_directories_containing_tests | ||
| 34 | { | ||
| 35 | my $self = shift; | ||
| 36 | my $rule = Path::Iterator::Rule->new->file->name('*.t'); | ||
| 37 | my %dirs; | ||
| 38 | $dirs{ Path::Tiny->new($_)->relative($self->rootdir)->dirname }++ | ||
| 39 | for $rule->all( $self->sourcefile('t') ); | ||
| 40 | [ sort keys %dirs ]; | ||
| 41 | } | ||
| 42 | |||
| 43 | has needs_conflict_check_code => ( | ||
| 44 | is => 'ro', | ||
| 45 | isa => Bool, | ||
| 46 | lazy => 1, | ||
| 47 | builder => '_build_needs_conflict_check_code', | ||
| 48 | ); | ||
| 49 | |||
| 50 | sub _build_needs_conflict_check_code | ||
| 51 | { | ||
| 52 | my $self = shift; | ||
| 53 | !!grep { | ||
| 54 | exists $self->metadata->{prereqs}{$_} | ||
| 55 | and exists $self->metadata->{prereqs}{$_}{conflicts} | ||
| 56 | and !!scalar keys %{ $self->metadata->{prereqs}{$_}{conflicts} } | ||
| 57 | } qw( configure build runtime test develop ); | ||
| 58 | } | ||
| 59 | |||
| 60 | has needs_optional_features_code => ( | ||
| 61 | is => 'ro', | ||
| 62 | isa => Bool, | ||
| 63 | lazy => 1, | ||
| 64 | builder => '_build_needs_optional_features_code', | ||
| 65 | ); | ||
| 66 | |||
| 67 | sub _build_needs_optional_features_code | ||
| 68 | { | ||
| 69 | my $self = shift; | ||
| 70 | !! %{ $self->metadata->{optional_features} || {} }; | ||
| 71 | } | ||
| 72 | |||
| 73 | has needs_fix_makefile_code => ( | ||
| 74 | is => 'ro', | ||
| 75 | isa => Bool, | ||
| 76 | lazy => 1, | ||
| 77 | builder => '_build_needs_fix_makefile_code', | ||
| 78 | ); | ||
| 79 | |||
| 80 | sub _build_needs_fix_makefile_code | ||
| 81 | { | ||
| 82 | my $self = shift; | ||
| 83 | !! $self->sourcefile('inc')->is_dir; | ||
| 84 | } | ||
| 85 | |||
| 86 | after PopulateMetadata => sub { | ||
| 87 | my $self = shift; | ||
| 88 | $self->metadata->{prereqs}{configure}{requires}{'ExtUtils::MakeMaker'} = '6.17' | ||
| 89 | if !defined $self->metadata->{prereqs}{configure}{requires}{'ExtUtils::MakeMaker'}; | ||
| 90 | $self->metadata->{prereqs}{configure}{requires}{'File::ShareDir::Install'} = '0.02' | ||
| 91 | if $self->has_shared_files | ||
| 92 | && !defined $self->metadata->{prereqs}{configure}{requires}{'File::ShareDir::Install'}; | ||
| 93 | $self->metadata->{prereqs}{configure}{recommends}{'CPAN::Meta::Requirements'} = '2.000' | ||
| 94 | if $self->needs_conflict_check_code; | ||
| 95 | $self->metadata->{dynamic_config} = 1 | ||
| 96 | if $self->sourcefile(DYNAMIC_CONFIG_PATH)->exists; | ||
| 97 | $self->metadata->{dynamic_config} = 1 | ||
| 98 | if $self->needs_optional_features_code; | ||
| 99 | }; | ||
| 100 | |||
| 101 | after BUILD => sub { | ||
| 102 | my $self = shift; | ||
| 103 | unshift @{ $self->targets }, 'MakefilePL'; | ||
| 104 | }; | ||
| 105 | |||
| 106 | sub Build_MakefilePL | ||
| 107 | { | ||
| 108 | my $self = shift; | ||
| 109 | my $file = $self->targetfile('Makefile.PL'); | ||
| 110 | $file->exists and return $self->log('Skipping %s; it already exists', $file); | ||
| 111 | $self->log('Writing %s', $file); | ||
| 112 | |||
| 113 | chomp( | ||
| 114 | my $dump = pp( $self->metadata->as_struct({version => '2'}) ) | ||
| 115 | ); | ||
| 116 | |||
| 117 | my $dynamic_config = do | ||
| 118 | { | ||
| 119 | my $dc = $self->sourcefile(DYNAMIC_CONFIG_PATH); | ||
| 120 | $dc->exists ? "\ndo {\n${\ $dc->slurp_utf8 }\n};" : ''; | ||
| 121 | }; | ||
| 122 | |||
| 123 | $self->rights_for_generated_files->{'Makefile.PL'} ||= [ | ||
| 124 | 'Copyright 2020 Toby Inkster.', | ||
| 125 | "Software::License::Perl_5"->new({ holder => 'Toby Inkster', year => '2020' }), | ||
| 126 | ] if !$dynamic_config; | ||
| 127 | |||
| 128 | my $share = ''; | ||
| 129 | if ($self->has_shared_files) | ||
| 130 | { | ||
| 131 | $share = "\nuse File::ShareDir::Install;\n" | ||
| 132 | . "install_share 'share';\n" | ||
| 133 | . "{ package MY; use File::ShareDir::Install qw(postamble) };\n"; | ||
| 134 | } | ||
| 135 | |||
| 136 | my $conflict_check = $self->needs_conflict_check_code ? $self->conflict_check_code : ''; | ||
| 137 | my $optional_features = $self->needs_optional_features_code ? $self->optional_features_code : ''; | ||
| 138 | my $fix_makefile = $self->needs_fix_makefile_code ? $self->fix_makefile_code : ''; | ||
| 139 | my $tests = join(q[ ], map "$_\*.t", @{ $self->directories_containing_tests }); | ||
| 140 | |||
| 141 | my $makefile = do { local $/ = <DATA> }; | ||
| 142 | $makefile =~ s/%%%METADATA%%%/$dump/; | ||
| 143 | $makefile =~ s/%%%SHARE%%%/$share/; | ||
| 144 | $makefile =~ s/%%%TESTS%%%/$tests/; | ||
| 145 | $makefile =~ s/%%%DYNAMIC_CONFIG%%%/$dynamic_config/; | ||
| 146 | $makefile =~ s/%%%CONFLICT_CHECK%%%/$conflict_check/; | ||
| 147 | $makefile =~ s/%%%OPTIONAL_FEATURES%%%/$optional_features/; | ||
| 148 | $makefile =~ s/%%%FIX_MAKEFILE%%%/$fix_makefile/; | ||
| 149 | $file->spew_utf8($makefile); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub conflict_check_code | ||
| 153 | { | ||
| 154 | <<'CODE' | ||
| 155 | for my $stage (keys %{$meta->{prereqs}}) | ||
| 156 | { | ||
| 157 | my $conflicts = $meta->{prereqs}{$stage}{conflicts} or next; | ||
| 158 | eval { require CPAN::Meta::Requirements } or last; | ||
| 159 | $conflicts = 'CPAN::Meta::Requirements'->from_string_hash($conflicts); | ||
| 160 | |||
| 161 | for my $module ($conflicts->required_modules) | ||
| 162 | { | ||
| 163 | eval "require $module" or next; | ||
| 164 | my $installed = eval(sprintf('$%s::VERSION', $module)); | ||
| 165 | $conflicts->accepts_module($module, $installed) or next; | ||
| 166 | |||
| 167 | my $message = "\n". | ||
| 168 | "** This version of $meta->{name} conflicts with the version of\n". | ||
| 169 | "** module $module ($installed) you have installed.\n"; | ||
| 170 | die($message . "\n" . "Bailing out") | ||
| 171 | if $stage eq 'build' || $stage eq 'configure'; | ||
| 172 | |||
| 173 | $message .= "**\n". | ||
| 174 | "** It's strongly recommended that you update it after\n". | ||
| 175 | "** installing this version of $meta->{name}.\n"; | ||
| 176 | warn("$message\n"); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | CODE | ||
| 180 | } | ||
| 181 | |||
| 182 | sub optional_features_code | ||
| 183 | { | ||
| 184 | <<'CODE' | ||
| 185 | |||
| 186 | if ($ENV{MM_INSTALL_FEATURES}) | ||
| 187 | { | ||
| 188 | my %features = %{ $meta->{optional_features} }; | ||
| 189 | my @features = sort { | ||
| 190 | $features{$b}{x_default} <=> $features{$a}{x_default} or $a cmp $b | ||
| 191 | } keys %features; | ||
| 192 | |||
| 193 | for my $feature_id (@features) | ||
| 194 | { | ||
| 195 | my %feature = %{ $features{$feature_id} }; | ||
| 196 | |||
| 197 | next unless prompt( | ||
| 198 | sprintf('Install %s (%s)?', $feature_id, $feature{description} || 'no description'), | ||
| 199 | $feature{x_default} ? 'Y' : 'N', | ||
| 200 | ) =~ /^Y/i; | ||
| 201 | |||
| 202 | $features{$feature_id}{x_selected} = 1; | ||
| 203 | |||
| 204 | for my $stage (keys %{$feature{prereqs}}) | ||
| 205 | { | ||
| 206 | for my $level (keys %{$feature{prereqs}{$stage}}) | ||
| 207 | { | ||
| 208 | for my $module (keys %{$feature{prereqs}{$stage}{$level}}) | ||
| 209 | { | ||
| 210 | $meta->{prereqs}{$stage}{$level}{$module} | ||
| 211 | = $feature{prereqs}{$stage}{$level}{$module}; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | } | ||
| 215 | } | ||
| 216 | } | ||
| 217 | else | ||
| 218 | { | ||
| 219 | print <<'MM_INSTALL_FEATURES'; | ||
| 220 | |||
| 221 | ** Setting the MM_INSTALL_FEATURES environment variable to true | ||
| 222 | ** would allow you to choose additional features. | ||
| 223 | |||
| 224 | MM_INSTALL_FEATURES | ||
| 225 | } | ||
| 226 | CODE | ||
| 227 | } | ||
| 228 | |||
| 229 | sub fix_makefile_code | ||
| 230 | { | ||
| 231 | <<'CODE' | ||
| 232 | |||
| 233 | sub FixMakefile | ||
| 234 | { | ||
| 235 | return unless -d 'inc'; | ||
| 236 | my $file = shift; | ||
| 237 | |||
| 238 | local *MAKEFILE; | ||
| 239 | open MAKEFILE, "< $file" or die "FixMakefile: Couldn't open $file: $!; bailing out"; | ||
| 240 | my $makefile = do { local $/; <MAKEFILE> }; | ||
| 241 | close MAKEFILE or die $!; | ||
| 242 | |||
| 243 | $makefile =~ s/\b(test_harness\(\$\(TEST_VERBOSE\), )/$1'inc', /; | ||
| 244 | $makefile =~ s/( -I\$\(INST_ARCHLIB\))/ -Iinc$1/g; | ||
| 245 | $makefile =~ s/( "-I\$\(INST_LIB\)")/ "-Iinc"$1/g; | ||
| 246 | $makefile =~ s/^(FULLPERL = .*)/$1 "-Iinc"/m; | ||
| 247 | $makefile =~ s/^(PERL = .*)/$1 "-Iinc"/m; | ||
| 248 | |||
| 249 | open MAKEFILE, "> $file" or die "FixMakefile: Couldn't open $file: $!; bailing out"; | ||
| 250 | print MAKEFILE $makefile or die $!; | ||
| 251 | close MAKEFILE or die $!; | ||
| 252 | } | ||
| 253 | |||
| 254 | FixMakefile($mm->{FIRST_MAKEFILE} || 'Makefile'); | ||
| 255 | CODE | ||
| 256 | } | ||
| 257 | |||
| 258 | 1; | ||
| 259 | |||
| 260 | __DATA__ |