Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dynamic prerequisites #32

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ skip = Foo::Bar
[Git::NextVersion]
[MetaProvides::Package]

[BuildSelf]
auto_configure_requires = 1
[ModuleBuildTiny]
auto_configure_requires = 0
header = use lib 'lib';
[ConfigureSelf]

[PodSyntaxTests]

Expand Down Expand Up @@ -57,3 +59,4 @@ check_dual_life_versions = 0
skip = ExtUtils::Config
skip = ExtUtils::Helpers
skip = ExtUtils::InstallPaths
skip = CPAN::Requirements::Dynamic
31 changes: 24 additions & 7 deletions lib/Module/Build/Tiny.pm
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,23 @@ my %actions = (
},
);

sub get_arguments {
my @sources = @_;
my %opt;
GetOptionsFromArray($_, \%opt, qw/install_base=s install_path=s% installdirs=s destdir=s prefix=s config=s% uninst:1 verbose:1 dry_run:1 pureperl-only:1 create_packlist=i jobs=i/) for (@sources);
$_ = detildefy($_) for grep { defined } @opt{qw/install_base destdir prefix/}, values %{ $opt{install_path} };
$opt{config} = ExtUtils::Config->new($opt{config});
$opt{meta} = get_meta();
$opt{install_paths} = ExtUtils::InstallPaths->new(%opt, dist_name => $opt{meta}->name);
return %opt;
}

sub Build {
my $action = @ARGV && $ARGV[0] =~ /\A\w+\z/ ? shift @ARGV : 'build';
die "No such action '$action'\n" if not $actions{$action};
my($env, $bargv) = @{ decode_json(read_file('_build_params')) };
my %opt;
GetOptionsFromArray($_, \%opt, qw/install_base=s install_path=s% installdirs=s destdir=s prefix=s config=s% uninst:1 verbose:1 dry_run:1 pureperl-only:1 create_packlist=i jobs=i/) for ($env, $bargv, \@ARGV);
$_ = detildefy($_) for grep { defined } @opt{qw/install_base destdir prefix/}, values %{ $opt{install_path} };
@opt{ 'config', 'meta' } = (ExtUtils::Config->new($opt{config}), get_meta());
exit $actions{$action}->(%opt, install_paths => ExtUtils::InstallPaths->new(%opt, dist_name => $opt{meta}->name));
my %opt = get_arguments($env, $bargv, \@ARGV);
exit $actions{$action}->(%opt);
}

sub Build_PL {
Expand All @@ -180,6 +188,15 @@ sub Build_PL {
make_executable('Build');
my @env = defined $ENV{PERL_MB_OPT} ? split_like_shell($ENV{PERL_MB_OPT}) : ();
write_file('_build_params', encode_json([ \@env, \@ARGV ]));
if (my $dynamic = $meta->custom('x_dynamic_prereqs')) {
my %meta = (%{ $meta->as_struct }, dynamic_config => 0);
my %opt = get_arguments(\@env, \@ARGV);
require CPAN::Requirements::Dynamic;
my $dynamic_parser = CPAN::Requirements::Dynamic->new(%opt);
my $prereq = $dynamic_parser->evaluate($dynamic);
$meta{prereqs} = $meta->effective_prereqs->with_merged_prereqs($prereq)->as_string_hash;
$meta = CPAN::Meta->new(\%meta);
}
$meta->save(@$_) for ['MYMETA.json'], [ 'MYMETA.yml' => { version => 1.4 } ];
}

Expand Down Expand Up @@ -220,14 +237,14 @@ than 200, yet supports the features needed by most distributions.

=item * Module sharedirs

=item * Dynamic prerequisites

=back

=head2 Not Supported

=over 4

=item * Dynamic prerequisites

=item * HTML documentation generation

=item * Extending Module::Build::Tiny
Expand Down
50 changes: 33 additions & 17 deletions t/lib/DistGen.pm
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,39 @@ sub _gen_default_filedata {
ok 1;
---

$self->$add_unless('META.yml', undent(<<" ----"));
---
name: $dist_name
version: 0.001
author:
- 'David Golden <dagolden\@cpan.org>'
- 'Leon Timmermans <leont\@cpan.org>'
abstract: 'A testing dist'
license: perl
requires:
perl: 5.006
Module::Build::Tiny: 0
generated_by: Leon Timmermans
dynamic_config: 0
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: 1.4
$self->$add_unless('META.json', undent(<<" ----"));
{
"name": "Foo-Bar",
"version": 0.001,
"author": [
"David Golden <dagolden\@cpan.org>",
"Leon Timmermans <leont\@cpan.org>"
],
"abstract": "A testing dist",
"license": "perl_5",
"prereqs": {
"configure": {
"requires": {
"Module::Build::Tiny": 0
}
},
"runtime": {
"requires": {
"perl": 5.006
}
}
},
"generated_by": "Leon Timmermans",
"dynamic_config": 1,
"meta-spec": {
"url": "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
"version": 2
},
"x_dynamic_prereqs": {
"version": 1,
"expressions": [ { "condition": [ "has_perl", "$]"], "prereqs": { "Bar": 1 } } ]
}
}
----
}

Expand Down
7 changes: 7 additions & 0 deletions t/simple.t
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ sub _slurp { do { local (@ARGV,$/)=$_[0]; <> } }
ok( -d catdir(qw/blib lib auto share module Foo-Bar/), 'moduole sharedir has been made');
ok( -f catfile(qw/blib lib auto share module Foo-Bar file.txt/), 'module sharedir file has been made');

require CPAN::Meta;
my $meta = CPAN::Meta->load_file("MYMETA.json");
my $req = $meta->effective_prereqs->requirements_for('runtime', 'requires');
my $dynamic_dependency = join ',', sort $req->required_modules;

is($dynamic_dependency, 'Bar,perl', 'Dependency on Foo has been inserted');

if ($has_compiler) {
XSLoader::load('Foo::Bar');
is(Foo::Bar::foo(), "Hello World!\n", 'Can run XSub Foo::Bar::foo');
Expand Down