1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
package Dist::Zilla::Plugin::ModuleBuildTiny;
$Dist::Zilla::Plugin::ModuleBuildTiny::VERSION = '0.020';
use 5.020;
use Moose;
with qw/
Dist::Zilla::Role::BuildPL
Dist::Zilla::Role::TextTemplate
Dist::Zilla::Role::PrereqSource
Dist::Zilla::Role::FileGatherer
Dist::Zilla::Role::MetaProvider
/;
use Dist::Zilla 4.300039;
use Module::Metadata;
use Moose::Util::TypeConstraints 'enum';
use MooseX::Types::Perl qw/StrictVersionStr/;
use MooseX::Types::Moose qw/Bool Str ArrayRef/;
use List::Util 1.33 qw/first any/;
use experimental qw/signatures postderef/;
sub mvp_multivalue_args { qw(header_strs footer_strs) }
sub mvp_aliases {
+{
header => 'header_strs',
footer => 'footer_strs',
}
}
around BUILDARGS => sub($orig, $class, @args) {
my $args = $class->$orig(@args);
my $delimiter = delete $args->{delimiter};
if (defined $delimiter and length($delimiter)) {
foreach my $arg (grep exists $args->{$_}, qw(header_strs footer_strs)) {
s/^\Q$delimiter\E// foreach $args->{$arg}->@*;
}
}
return $args;
};
has version_method => (
is => 'ro',
isa => enum(['installed', 'conservative']),
default => 'conservative',
);
has has_pl => (
is => 'ro',
isa => Bool,
lazy => 1,
default => sub($self) {
return any { $_->name =~ /^lib\/.*\.PL$/ } $self->zilla->files->@*;
},
);
has has_xs => (
is => 'ro',
isa => Bool,
lazy => 1,
default => sub($self) {
return any { $_->name =~ /^lib\/.*\.xs$/ } $self->zilla->files->@*;
},
);
has static => (
is => 'ro',
isa => enum([qw/no yes auto/]),
default => 'no',
);
has version => (
is => 'ro',
lazy => 1,
isa => StrictVersionStr,
default => sub($self) {
if ($self->version_method eq 'installed') {
return Module::Metadata->new_from_module('Module::Build::Tiny')->version->stringify;
}
elsif (-e 'include/' or any { $_->name =~ /^src\/.*\.c$/} $self->zilla->files->@*) {
return '0.044';
}
elsif (-e 'module-share') {
return '0.044';
}
elsif ($self->has_pl) {
return '0.039';
}
elsif ($self->has_xs) {
return '0.036';
}
return '0.034'; # _build_params format
},
);
has minimum_perl => (
is => 'ro',
isa => StrictVersionStr,
lazy => 1,
default => sub($self) {
my $prereqs = $self->zilla->prereqs->cpan_meta_prereqs;
my $reqs = $prereqs->merged_requirements([ qw/configure build test runtime/ ], ['requires']);
return $reqs->requirements_for_module('perl') || '5.006';
},
);
has header_strs => (
is => 'ro',
isa => ArrayRef[Str],
traits => ['Array'],
lazy => 1,
default => sub { [] },
documentation => "Additional code lines to include at the beginning of Makefile.PL",
);
has header_file => (
is => 'ro', isa => Str,
documentation => 'Additional header content to include from a file',
);
has header => (
is => 'ro',
isa => Str,
lazy => 1,
builder => '_build_header',
documentation => "A string included at the beginning of Makefile.PL",
);
sub _build_header($self) {
join "\n",
$self->header_strs->@*,
( $self->header_file
? do {
my $abs_file = path($self->zilla->root, $self->header_file);
$self->log_fatal([ 'header_file %s does not exist!', $self->header_file ])
if not $abs_file->exists;
$abs_file->slurp_utf8
}
: () );
}
has footer_strs => (
is => 'ro',
isa => ArrayRef[Str],
traits => ['Array'],
lazy => 1,
default => sub { [] },
documentation => "Additional code lines to include at the end of Makefile.PL",
);
has footer_file => (
is => 'ro', isa => Str,
documentation => 'Additional footer content to include from a file',
);
has footer => (
is => 'ro',
isa => Str,
lazy => 1,
builder => '_build_footer',
documentation => "A string included at the end of Makefile.PL",
);
sub _build_footer($self) {
join "\n",
$self->footer_strs->@*,
( $self->footer_file
? do {
my $abs_file = path($self->zilla->root, $self->footer_file);
$self->log_fatal([ 'footer_file %s does not exist!', $self->footer_file ])
if not $abs_file->exists;
$abs_file->slurp_utf8
}
: () );
}
has auto_configure_requires => (
is => 'ro',
isa => Bool,
default => 1,
);
my $template = <<'BUILD_PL';
# This Build.PL for {{ $dist_name }} was generated by {{ $plugin_title }}.
use strict;
use warnings;
{{ $header }}
use {{ $minimum_perl }};
use Module::Build::Tiny{{ $version ne 0 && " $version" }};
Build_PL();
{{ $footer }}
BUILD_PL
sub register_prereqs($self) {
if ($self->auto_configure_requires) {
$self->zilla->register_prereqs({ phase => 'configure' }, 'Module::Build::Tiny' => $self->version);
}
return;
}
sub can_static($self) {
return !$self->has_pl && !$self->has_xs;
}
sub metadata($self) {
my $static = $self->static eq 'yes' || $self->static eq 'auto' && $self->can_static;
return $static ? { x_static_install => 1 } : ();
}
sub gather_files($self) {
if (my $file = first { $_->name eq 'Build.PL' } $self->zilla->files->@*)
{
# if it's another type, some other plugin added it, so it's better to
# error out and let the developer sort out what went wrong.
if ($file->isa('Dist::Zilla::File::OnDisk')) {
$self->log('replacing existing Build.PL found in repository');
$self->zilla->prune_file($file);
}
}
require Dist::Zilla::File::InMemory;
my $file = Dist::Zilla::File::InMemory->new({
name => 'Build.PL',
content => $template, # template evaluated later
});
$self->add_file($file);
return;
}
sub setup_installer($self) {
for my $map (map { $_->share_dir_map } $self->zilla->plugins_with(-ShareDir)->@*) {
for my $module (keys $map->{module}->%*) {
my $expected = "module-share/$module" =~ s/::/-/gr;
if ($map->{module}{$module} ne $expected) {
$self->log_fatal("Sharedir location for module $module sharedir should be '$expected'");
}
}
$self->log_fatal('Sharedir location must be share/') if defined $map->{dist} and $map->{dist} ne 'share';
}
my $file = first { $_->name eq 'Build.PL' } @{$self->zilla->files};
my $content = $file->content;
$content = $self->fill_in_string($content, {
version => $self->version,
minimum_perl => $self->minimum_perl,
dist_name => $self->zilla->name,
plugin_title => ref($self) . ' ' . ($self->VERSION || '<self>'),
header => $self->header,
footer => $self->footer,
});
$self->log_debug([ 'updating contents of Build.PL in memory' ]);
$file->content($content);
return;
}
__PACKAGE__->meta->make_immutable;
no Moose::Util::TypeConstraints;
no Moose;
1;
# ABSTRACT: Build a Build.PL that uses Module::Build::Tiny
# vim: set ts=4 sw=4 noet nolist :
__END__
=pod
=encoding UTF-8
=head1 NAME
Dist::Zilla::Plugin::ModuleBuildTiny - Build a Build.PL that uses Module::Build::Tiny
=head1 VERSION
version 0.020
=head1 DESCRIPTION
This plugin will create a F<Build.PL> for installing the dist using L<Module::Build::Tiny|Module::Build::Tiny>.
=head1 ATTRIBUTES
=head2 version
B<Optional:> Specify the minimum version of L<Module::Build::Tiny|Module::Build::Tiny> to depend on.
Defaults to the version determined by C<version_method>.
=head2 version_method
This attribute determines how the default minimum perl is detected. It has two possible values:
=over 4
=item * installed
This will give the version installed on the author's perl installation.
=item * conservative
This will return a heuristically determined minimum version of MBT.
=back
=head2 minimum_perl
B<Optional:> Specify the minimum version of perl to require in the F<Build.PL>.
This is normally taken from dzil's prereq metadata.
=head2 static
This is an option to set the B<HIGHLY EXPERIMENTAL> C<x_static_install>
metadata field. B<DO NOT USE THIS OPTION> if you are not involved in its
testing with the Perl Toolchain Gang.
It has three possible values:
=over 4
=item * no
No extra metadata is added. This is the default setting.
=item * yes
Sets C<x_static_install = 1> in metadata.
=item * auto
Sets C<x_static_install = 1> in metadata if the distribution appears to be
compatible - presently only the existence of F<.PL> and F<.xs> files are
checked.
=back
=head2 header
A line of code which is included near the top of F<Build.PL>. Can be used more than once.
=head2 footer
A line of code which is included at the bottom of F<Build.PL>. Can be used more than once.
=head2 delimiter
A string, usually a single character, which is stripped from the beginning of
all C<header>, and C<footer> lines. This is because the
INI file format strips all leading whitespace from option values, so including
this character at the front allows you to use leading whitespace in an option
string. This is helpful for the formatting of F<Build.PL>s, but a nice thing
to have when inserting any block of code.
=head1 AUTHOR
Leon Timmermans <fawaka@gmail.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Leon Timmermans.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|