| 1 | package Menlo::Builder::Static; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use CPAN::Meta; | ||
| 6 | use ExtUtils::Config 0.003; | ||
| 7 | use ExtUtils::Helpers 0.020 qw/make_executable split_like_shell man1_pagename man3_pagename detildefy/; | ||
| 8 | use ExtUtils::Install qw/pm_to_blib install/; | ||
| 9 | use ExtUtils::InstallPaths 0.002; | ||
| 10 | use File::Basename qw/dirname/; | ||
| 11 | use File::Find (); | ||
| 12 | use File::Path qw/mkpath/; | ||
| 13 | use File::Spec::Functions qw/catfile catdir rel2abs abs2rel splitdir curdir/; | ||
| 14 | use Getopt::Long 2.36 qw/GetOptionsFromArray/; | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my($class, %args) = @_; | ||
| 18 | bless { | ||
| 19 | meta => $args{meta}, | ||
| 20 | }, $class; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub meta { | ||
| 24 | my $self = shift; | ||
| 25 | $self->{meta}; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub manify { | ||
| 29 | my ($input_file, $output_file, $section, $opts) = @_; | ||
| 30 | return if -e $output_file && -M $input_file <= -M $output_file; | ||
| 31 | my $dirname = dirname($output_file); | ||
| 32 | mkpath($dirname, $opts->{verbose}) if not -d $dirname; | ||
| 33 | require Pod::Man; | ||
| 34 | Pod::Man->new(section => $section)->parse_from_file($input_file, $output_file); | ||
| 35 | print "Manifying $output_file\n" if $opts->{verbose} && $opts->{verbose} > 0; | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub find { | ||
| 40 | my ($pattern, $dir) = @_; | ||
| 41 | my @ret; | ||
| 42 | File::Find::find(sub { push @ret, $File::Find::name if /$pattern/ && -f }, $dir) if -d $dir; | ||
| 43 | return @ret; | ||
| 44 | } | ||
| 45 | |||
| 46 | my %actions = ( | ||
| 47 | build => sub { | ||
| 48 | my %opt = @_; | ||
| 49 | my %modules = map { $_ => catfile('blib', $_) } find(qr/\.p(?:m|od)$/, 'lib'); | ||
| 50 | my %scripts = map { $_ => catfile('blib', $_) } find(qr//, 'script'); | ||
| 51 | my %shared = map { $_ => catfile(qw/blib lib auto share dist/, $opt{meta}->name, abs2rel($_, 'share')) } find(qr//, 'share'); | ||
| 52 | pm_to_blib({ %modules, %scripts, %shared }, catdir(qw/blib lib auto/)); | ||
| 53 | make_executable($_) for values %scripts; | ||
| 54 | mkpath(catdir(qw/blib arch/), $opt{verbose}); | ||
| 55 | |||
| 56 | if ($opt{install_paths}->install_destination('bindoc') && $opt{install_paths}->is_default_installable('bindoc')) { | ||
| 57 | manify($_, catfile('blib', 'bindoc', man1_pagename($_)), $opt{config}->get('man1ext'), \%opt) for keys %scripts; | ||
| 58 | } | ||
| 59 | if ($opt{install_paths}->install_destination('libdoc') && $opt{install_paths}->is_default_installable('libdoc')) { | ||
| 60 | manify($_, catfile('blib', 'libdoc', man3_pagename($_)), $opt{config}->get('man3ext'), \%opt) for keys %modules; | ||
| 61 | } | ||
| 62 | 1; | ||
| 63 | }, | ||
| 64 | test => sub { | ||
| 65 | my %opt = @_; | ||
| 66 | die "Must run `./Build build` first\n" if not -d 'blib'; | ||
| 67 | require TAP::Harness::Env; | ||
| 68 | my %test_args = ( | ||
| 69 | (verbosity => $opt{verbose}) x!! exists $opt{verbose}, | ||
| 70 | (jobs => $opt{jobs}) x!! exists $opt{jobs}, | ||
| 71 | (color => 1) x !!-t STDOUT, | ||
| 72 | lib => [ map { rel2abs(catdir(qw/blib/, $_)) } qw/arch lib/ ], | ||
| 73 | ); | ||
| 74 | my $tester = TAP::Harness::Env->create(\%test_args); | ||
| 75 | $tester->runtests(sort +find(qr/\.t$/, 't'))->has_errors and return; | ||
| 76 | 1; | ||
| 77 | }, | ||
| 78 | install => sub { | ||
| 79 | my %opt = @_; | ||
| 80 | die "Must run `./Build build` first\n" if not -d 'blib'; | ||
| 81 | install($opt{install_paths}->install_map, @opt{qw/verbose dry_run uninst/}); | ||
| 82 | 1; | ||
| 83 | }, | ||
| 84 | ); | ||
| 85 | |||
| 86 | sub build { | ||
| 87 | my $self = shift; | ||
| 88 | my $action = @_ && $_[0] =~ /\A\w+\z/ ? shift @_ : 'build'; | ||
| 89 | die "No such action '$action'\n" if not $actions{$action}; | ||
| 90 | my %opt; | ||
| 91 | 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 ($self->{env}, $self->{configure_args}, \@_); | ||
| 92 | $_ = detildefy($_) for grep { defined } @opt{qw/install_base destdir prefix/}, values %{ $opt{install_path} }; | ||
| 93 | @opt{ 'config', 'meta' } = (ExtUtils::Config->new($opt{config}), $self->meta); | ||
| 94 | $actions{$action}->(%opt, install_paths => ExtUtils::InstallPaths->new(%opt, dist_name => $opt{meta}->name)); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub configure { | ||
| 98 | my $self = shift; | ||
| 99 | $self->{env} = defined $ENV{PERL_MB_OPT} ? [split_like_shell($ENV{PERL_MB_OPT})] : []; | ||
| 100 | $self->{configure_args} = [@_]; | ||
| 101 | $self->meta->save(@$_) for ['MYMETA.json'], [ 'MYMETA.yml' => { version => 1.4 } ]; | ||
| 102 | } | ||
| 103 | |||
| 104 | 1; | ||
| 105 | |||
| 106 | =head1 COPYRIGHT AND LICENSE | ||
| 107 | |||
| 108 | This software is copyright (c) 2011 by Leon Timmermans, David Golden. | ||
| 109 | |||
| 110 | This is free software; you can redistribute it and/or modify it under | ||
| 111 | the same terms as the Perl 5 programming language system itself. | ||
| 112 | |||
| 113 | =cut |