| 1 | package App::cpm::Builder::Static; | ||
| 2 | use v5.24; | ||
| 3 | use warnings; | ||
| 4 | use experimental qw(lexical_subs signatures); | ||
| 5 | |||
| 6 | use ExtUtils::Config; | ||
| 7 | use ExtUtils::Helpers qw(make_executable man1_pagename man3_pagename); | ||
| 8 | use ExtUtils::Install qw(pm_to_blib); | ||
| 9 | use File::Basename qw(dirname); | ||
| 10 | use File::Find (); | ||
| 11 | use File::Path qw(mkpath); | ||
| 12 | use File::Spec::Functions qw(abs2rel catdir catfile rel2abs); | ||
| 13 | |||
| 14 | use parent 'App::cpm::Builder::Base'; | ||
| 15 | |||
| 16 | my sub find_files ($pattern, $dir) { | ||
| 17 | my @files; | ||
| 18 | File::Find::find(sub { | ||
| 19 | return if !-f; | ||
| 20 | push @files, $File::Find::name if /$pattern/; | ||
| 21 | }, $dir) if -d $dir; | ||
| 22 | return @files; | ||
| 23 | } | ||
| 24 | |||
| 25 | my sub read_file ($filename) { | ||
| 26 | open my $fh, '<', $filename or die "Could not open $filename: $!\n"; | ||
| 27 | return do { local $/; <$fh> }; | ||
| 28 | } | ||
| 29 | |||
| 30 | my sub contains_pod ($file) { | ||
| 31 | return if !-T $file; | ||
| 32 | return read_file($file) =~ /^\=(?:head|pod|item)/m; | ||
| 33 | } | ||
| 34 | |||
| 35 | my sub manify ($input_file, $output_file, $section) { | ||
| 36 | return if -e $output_file && -M $input_file <= -M $output_file; | ||
| 37 | my $dirname = dirname($output_file); | ||
| 38 | mkpath($dirname) if !-d $dirname; | ||
| 39 | require Pod::Man; | ||
| 40 | Pod::Man->new(section => $section)->parse_from_file($input_file, $output_file); | ||
| 41 | return; | ||
| 42 | } | ||
| 43 | |||
| 44 | my sub script_files () { | ||
| 45 | return ( | ||
| 46 | grep { !/\.pod\z/ } find_files(qr/.+/, 'script'), | ||
| 47 | ); | ||
| 48 | } | ||
| 49 | |||
| 50 | my sub script_doc_files () { | ||
| 51 | return ( | ||
| 52 | grep { /\.pod\z/ } find_files(qr/.+/, 'script'), | ||
| 53 | ); | ||
| 54 | } | ||
| 55 | |||
| 56 | my sub share_files ($dist_name) { | ||
| 57 | return ( | ||
| 58 | map { $_ => catfile(qw(blib lib auto share dist), $dist_name, abs2rel($_, 'share')) } | ||
| 59 | find_files(qr/.+/, 'share'), | ||
| 60 | map { $_ => catfile(qw(blib lib auto share module), abs2rel($_, 'module-share')) } | ||
| 61 | find_files(qr/.+/, 'module-share'), | ||
| 62 | ); | ||
| 63 | } | ||
| 64 | |||
| 65 | my sub build_manpages ($install_paths, $config, $modules, $scripts) { | ||
| 66 | if ($install_paths->install_destination('bindoc') && $install_paths->is_default_installable('bindoc')) { | ||
| 67 | my $section = $config->get('man1ext'); | ||
| 68 | for my $input ($scripts->@*, script_doc_files()) { | ||
| 69 | next if !contains_pod($input); | ||
| 70 | manify($input, catfile('blib', 'bindoc', man1_pagename($input)), $section); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | if ($install_paths->install_destination('libdoc') && $install_paths->is_default_installable('libdoc')) { | ||
| 74 | my $section = $config->get('man3ext'); | ||
| 75 | for my $input ($modules->@*) { | ||
| 76 | next if !contains_pod($input); | ||
| 77 | manify($input, catfile('blib', 'libdoc', man3_pagename($input)), $section); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | sub supports ($class, $meta) { | ||
| 83 | return $meta->{x_static_install} && $meta->{x_static_install} == 1; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub configure ($self, $ctx, $dependency_libs, $dependency_paths) { | ||
| 87 | $self->meta->save(@$_) for ['MYMETA.json'], [ 'MYMETA.yml' => { version => 1.4 } ]; | ||
| 88 | return 1; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub build ($self, $ctx, $dependency_libs, $dependency_paths) { | ||
| 92 | my $ok = $self->run_build($ctx, sub { | ||
| 93 | my @module = find_files(qr/\.(?:pm|pod)\z/, 'lib'); | ||
| 94 | my @script = script_files(); | ||
| 95 | my %file = ( | ||
| 96 | (map { $_ => catfile('blib', $_) } @module), | ||
| 97 | (map { $_ => catfile('blib', $_) } @script), | ||
| 98 | share_files($self->meta->name), | ||
| 99 | ); | ||
| 100 | pm_to_blib(\%file, catdir(qw(blib lib auto))); | ||
| 101 | make_executable($_) for grep { m{\Ablib/script/} } values %file; | ||
| 102 | mkpath(catdir(qw(blib arch))); | ||
| 103 | build_manpages($self->_install_paths, ExtUtils::Config->new, \@module, \@script) if $self->{man_pages}; | ||
| 104 | return 1; | ||
| 105 | }, $dependency_libs, $dependency_paths); | ||
| 106 | return if !$ok; | ||
| 107 | $self->_prepare_paths_cache; | ||
| 108 | $self->_write_blib_meta($ctx); | ||
| 109 | return 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub test ($self, $ctx, $dependency_libs, $dependency_paths) { | ||
| 113 | $self->run_test($ctx, sub { | ||
| 114 | return 1 if !-d 't'; | ||
| 115 | require TAP::Harness::Env; | ||
| 116 | my $tester = TAP::Harness::Env->create({ | ||
| 117 | color => -t STDOUT ? 1 : 0, | ||
| 118 | lib => [ map { rel2abs(catdir(qw(blib), $_)) } qw(arch lib) ], | ||
| 119 | }); | ||
| 120 | return !$tester->runtests(sort(find_files(qr/\.t\z/, 't')))->has_errors; | ||
| 121 | }, $dependency_libs, $dependency_paths); | ||
| 122 | } | ||
| 123 | |||
| 124 | 1; | ||
| 125 | |||
| 126 | =head1 COPYRIGHT AND LICENSE | ||
| 127 | |||
| 128 | This module is based on L<Module::Build::Tiny|https://metacpan.org/dist/Module-Build-Tiny>. | ||
| 129 | Its COPYRIGHT AND LICENSE is: | ||
| 130 | |||
| 131 | This software is copyright (c) 2011 by Leon Timmermans, David Golden. | ||
| 132 | |||
| 133 | This is free software; you can redistribute it and/or modify it under | ||
| 134 | the same terms as the Perl 5 programming language system itself. | ||
| 135 | |||
| 136 | =cut |