From 1e4e571c17b655c38118b509f786df33d9372dde Mon Sep 17 00:00:00 2001 From: Leon Timmermans Date: Mon, 1 May 2023 16:18:41 +0200 Subject: [PATCH] Implement dynamic prerequisites --- dist.ini | 1 + lib/Module/Build/Tiny.pm | 13 +++++++++++-- t/simple.t | 11 +++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/dist.ini b/dist.ini index 85bad53..2a6de76 100644 --- a/dist.ini +++ b/dist.ini @@ -59,3 +59,4 @@ check_dual_life_versions = 0 skip = ExtUtils::Config skip = ExtUtils::Helpers skip = ExtUtils::InstallPaths +skip = CPAN::Requirements::Dynamic diff --git a/lib/Module/Build/Tiny.pm b/lib/Module/Build/Tiny.pm index 4f594b6..18636f5 100644 --- a/lib/Module/Build/Tiny.pm +++ b/lib/Module/Build/Tiny.pm @@ -188,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->parse($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 } ]; } @@ -228,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 diff --git a/t/simple.t b/t/simple.t index f6920e9..af6d59c 100644 --- a/t/simple.t +++ b/t/simple.t @@ -81,6 +81,10 @@ if ($has_compiler) { "meta-spec": { "url": "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version": 2 + }, + "x_dynamic_prereqs": { + "version": 1, + "expressions": [ { "condition": [ "has_perl", "$]"] , "prereqs": { "runtime": { "requires": { "Bar": 1 } } } } ] } } ---- @@ -167,6 +171,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');