Alien-Xrepo

 view release on metacpan or  search on metacpan

lib/Alien/Xrepo/MM.pm  view on Meta::CPAN

    sub dist_author        { $_[0]{dist_author} }
    sub dist_version       { $_[0]{dist_version} }
    sub license            { $_[0]{license} }
    sub xrepo_snapshot     { $_[0]{xrepo_snapshot} }
    sub xrepo_share_dir    { $_[0]{xrepo_share_dir} }
    sub xrepo_cache        { $_[0]{xrepo_cache} }
    sub xrepo_update_repo  { $_[0]{xrepo_update_repo} }
    sub xrepo_verbose      { $_[0]{xrepo_verbose} }
    sub xrepo_diagnosis    { $_[0]{xrepo_diagnosis} }
    sub base_dir           { $_[0]{base_dir} }
    sub requires           { $_[0]{requires} }
    sub configure_requires { $_[0]{configure_requires} }
    sub build_requires     { $_[0]{build_requires} }
    sub test_requires      { $_[0]{test_requires} }

    # The record of facts the two builders share: this dist root, the module, and
    # the build script whose mtime counts as a freshness input.
    sub _dist ($self) {
        Alien::Xrepo::Build::Dist->new( script => 'Makefile.PL', map { $_ => $self->{$_} } qw[base_dir module_name] );
    }

    # Engine modules that count as recipe inputs when loaded into this process.
    sub _engine_mods { (qw[Alien/Xrepo/MM.pm Alien/Xrepo/Build.pm Alien/Xrepo/Runtime.pm Alien/Xrepo/Build/Recipe.pm]) }

    # The Makefile gets the xrepo freshness check as a pure_all dependency.  The
    # rule itself is a tiny perl one-liner so every `make` decision happens in
    # Perl, next to the same staleness logic Module::Build uses.  -Ilib is for the
    # source tree (some dists build the build script from a fresh checkout); the
    # config is a file, never shell-quoted.
    # make requires the recipe to start with a real tab.  ``<<~`` dedents on the
    # closing delimiter and can only strip spaces, which MakeMaker then writes
    # space-indented and GNU make rejects as a malformed rule, so the tab is
    # built explicitly instead.
    sub _postamble {
        my $tab = "\t";
        my $run = "Alien::Xrepo::MM::run(q{$CONFIG})";
        return "pure_all :: xrepo\n\nxrepo :\n$tab\$(NOECHO) \$(PERLRUN) -Ilib -MAlien::Xrepo::MM -e \"$run\";\n";
    }
    package    #
        MY {

        sub postamble ( $self = undef, @args ) {
            my $prev = '';
            if ( ref $self ) {
                my $out = eval { $self->SUPER::postamble(@args) };
                $prev = $out if defined $out;
            }
            return ( length $prev ? "$prev\n" : '' ) . Alien::Xrepo::MM::_postamble();
        }
    }

    # Generate the Makefile. The config below is written first so the pure_all hook the recipe emits has something to read when `make` runs.
    sub Write ($self) {
        my $base = path( $self->{base_dir} );
        $base->mkpath unless -d $base;
        my $cwd = getcwd();
        chdir $base or die "Alien::Xrepo::MM: chdir $base: $!";
        my $dist     = $self->_dist;
        my $snapshot = path( defined $self->{xrepo_snapshot} ? $self->{xrepo_snapshot} : $dist->snapshot_path )->absolute;
        my $share    = defined $self->{xrepo_share_dir} ? path( $self->{xrepo_share_dir} )->absolute : $snapshot->parent;
        $base->child($CONFIG)->spew_utf8(
            encode_json(
                {   module_name => $self->{module_name},
                    snapshot    => $snapshot->stringify,
                    share_dir   => $share->stringify,
                    cache       => $self->{xrepo_cache},
                    update_repo => $self->{xrepo_update_repo},
                    verbose     => $self->{xrepo_verbose},
                    diagnosis   => $self->{xrepo_diagnosis}
                }
            )
        );
        my %wm = $self->_write_makefile_args;
        require ExtUtils::MakeMaker;
        ExtUtils::MakeMaker::WriteMakefile(%wm);
        chdir $cwd;
        return;
    }

# ExtUtils::MakeMaker arguments mirror Alarm::Xrepo::MB's properties: test/build requires union into PREREQ_PM (EUMM treats PREREQ_PM as runtime), configure_requires separate, and the config cleaned with `make realclean`.
    sub _write_makefile_args ($self) {
        my %wm = (
            NAME               => $self->{module_name},
            VERSION            => $self->{dist_version},
            ABSTRACT           => ( $self->{dist_abstract} // $self->{module_name} ),
            AUTHOR             => $self->{dist_author},
            LICENSE            => $self->{license},
            PREREQ_PM          => { %{ $self->{build_requires} }, %{ $self->{test_requires} }, %{ $self->{requires} } },
            CONFIGURE_REQUIRES => { %{ $self->{configure_requires} } },
            clean              => { FILES => $CONFIG }
        );
        return %wm;
    }

# What `make` executes on the xrepo target: re-read the config written by Write(), rebuild the snapshot when stale, and do nothing (successfully) when the file is absent so a dist that merely loaded us but never used Write is unharmed.
    sub run ( $config //= $CONFIG ) {
        my $cfg = path($config);
        $cfg = $cfg->absolute unless $cfg->is_absolute;
        my $dist_root = $cfg->parent;
        return !print "Alien::Xrepo::MM: no $config found; skipping xrepo build\n" unless -e $cfg;
        my $data = decode_json( $cfg->slurp_utf8 );
        my $dist = Alien::Xrepo::Build::Dist->new( base_dir => $dist_root->stringify, module_name => $data->{module_name}, script => 'Makefile.PL' );
        my $snapshot = path( $data->{snapshot} );
        $snapshot = $dist_root->child( $data->{snapshot} ) unless $snapshot->is_absolute;
        return !print "=> xrepo: $snapshot is up to date\n" unless $dist->snapshot_stale( $snapshot, _engine_mods() );
        my $share = ( defined $data->{share_dir} && $data->{share_dir} ne '' ) ? path( $data->{share_dir} ) : $snapshot->parent;
        $share->mkpath;
        print "=> xrepo: writing $snapshot\n";
        my $build = Alien::Xrepo::Build->new(
            recipe      => $dist->recipe,
            snapshot    => $snapshot,
            share_dir   => $share->stringify,
            cache       => ( $data->{cache}       // 0 ),
            update_repo => ( $data->{update_repo} // 0 ),
            verbose     => ( $data->{verbose}     // 0 ),
            diagnosis   => ( $data->{diagnosis}   // 0 )
        );
        $build->run;
        $build->assert_installed;
        return;
    }
};
#
1;
__END__
Copyright (C) Sanko Robinson.

This library is free software; you can redistribute it and/or modify it under the terms found in
the Artistic License 2. Other copyrights, terms, and conditions may apply to data transmitted
through this module.



( run in 0.636 second using v1.01-cache-2.11-cpan-234fa220139 )