From 295c182d70fa4cfe58d33002a17bd264810b574a Mon Sep 17 00:00:00 2001 From: Leon Timmermans Date: Sat, 20 Apr 2024 20:12:42 +0200 Subject: [PATCH 1/4] Switch away from [BuildSelf] --- dist.ini | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dist.ini b/dist.ini index cc10048..85bad53 100644 --- a/dist.ini +++ b/dist.ini @@ -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] From 6b8c922fac945518d31aa280455577fe21d99660 Mon Sep 17 00:00:00 2001 From: Leon Timmermans Date: Mon, 1 May 2023 14:28:29 +0200 Subject: [PATCH 2/4] Split out get_arguments function --- lib/Module/Build/Tiny.pm | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/Module/Build/Tiny.pm b/lib/Module/Build/Tiny.pm index a9bc09e..4f594b6 100644 --- a/lib/Module/Build/Tiny.pm +++ b/lib/Module/Build/Tiny.pm @@ -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 { From a8704bfb9fe971820ed0d88235fa7a037b707c68 Mon Sep 17 00:00:00 2001 From: Leon Timmermans Date: Thu, 25 Apr 2024 16:36:44 +0200 Subject: [PATCH 3/4] Switch to using META.json in test --- t/lib/DistGen.pm | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/t/lib/DistGen.pm b/t/lib/DistGen.pm index ae1ed53..277cd52 100644 --- a/t/lib/DistGen.pm +++ b/t/lib/DistGen.pm @@ -153,23 +153,35 @@ sub _gen_default_filedata { ok 1; --- - $self->$add_unless('META.yml', undent(<<" ----")); - --- - name: $dist_name - version: 0.001 - author: - - 'David Golden ' - - 'Leon Timmermans ' - 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 ", + "Leon Timmermans " + ], + "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 + } + } ---- } From cee8ac75e0bfadf3829ff76763d8b5a9660a5fe5 Mon Sep 17 00:00:00 2001 From: Leon Timmermans Date: Mon, 1 May 2023 16:18:41 +0200 Subject: [PATCH 4/4] Implement dynamic prerequisites --- dist.ini | 1 + lib/Module/Build/Tiny.pm | 13 +++++++++++-- t/lib/DistGen.pm | 8 ++++++-- t/simple.t | 7 +++++++ 4 files changed, 25 insertions(+), 4 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..3ac98fa 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->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 } ]; } @@ -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/lib/DistGen.pm b/t/lib/DistGen.pm index 277cd52..248316a 100644 --- a/t/lib/DistGen.pm +++ b/t/lib/DistGen.pm @@ -180,8 +180,12 @@ sub _gen_default_filedata { "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 } } ] + } + } ---- } diff --git a/t/simple.t b/t/simple.t index 9aaf982..3d7cfc1 100644 --- a/t/simple.t +++ b/t/simple.t @@ -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');