| 1 | package Module::Install::Template; | ||
| 2 | use 5.006; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Cwd; | ||
| 6 | use File::Temp 'tempfile'; | ||
| 7 | use Data::Dumper; | ||
| 8 | our $VERSION = '0.08'; | ||
| 9 | use base qw(Module::Install::Base); | ||
| 10 | |||
| 11 | sub is_author { | ||
| 12 | my $author = $^O eq 'VMS' ? './inc/_author' : './inc/.author'; | ||
| 13 | -d $author; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub tag { | ||
| 17 | my $self = shift; | ||
| 18 | (my $name = lc $self->name) =~ s/-//g; | ||
| 19 | $name; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub rt_email { | ||
| 23 | my $self = shift; | ||
| 24 | sprintf '<bug-%s@rt.cpan.org>', lc $self->name; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub year_str { | ||
| 28 | my ($self, $first_year) = @_; | ||
| 29 | my $this_year = ((localtime)[5] + 1900); | ||
| 30 | return $this_year if (!defined $first_year) || $first_year == $this_year; | ||
| 31 | die "first year ($first_year) is after this year ($this_year)?\n" | ||
| 32 | if $first_year > $this_year; | ||
| 33 | return "$first_year-$this_year"; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub process_templates { | ||
| 37 | my ($self, %args) = @_; | ||
| 38 | |||
| 39 | # only module authors should process templates; if you're not the original | ||
| 40 | # author, you won't have the templates anyway, only the generated files. | ||
| 41 | return unless $self->is_author; | ||
| 42 | $::WANTS_MODULE_INSTALL_TEMPLATE = 1; | ||
| 43 | my @other_authors; | ||
| 44 | if (defined $args{other_authors}) { | ||
| 45 | @other_authors = | ||
| 46 | ref $args{other_authors} eq 'ARRAY' | ||
| 47 | ? @{ $args{other_authors} } | ||
| 48 | : ($args{other_authors}); | ||
| 49 | } | ||
| 50 | my $config = { | ||
| 51 | template => { | ||
| 52 | INCLUDE_PATH => "$ENV{HOME}/.mitlib", | ||
| 53 | (defined $args{start_tag} ? (START_TAG => $args{start_tag}) : ()), | ||
| 54 | (defined $args{end_tag} ? (END_TAG => $args{end_tag}) : ()), | ||
| 55 | }, | ||
| 56 | vars => { | ||
| 57 | name => $self->name, | ||
| 58 | year => $self->year_str($args{first_year}), | ||
| 59 | tag => $self->tag, | ||
| 60 | rt_email => $self->rt_email, | ||
| 61 | base_dir => getcwd(), | ||
| 62 | (@other_authors ? (other_authors => \@other_authors) : ()), | ||
| 63 | }, | ||
| 64 | }; | ||
| 65 | my ($fh, $filename) = tempfile(); | ||
| 66 | print $fh Data::Dumper->Dump([$config], ['config']); | ||
| 67 | close $fh or die "can't close $filename: $!\n"; | ||
| 68 | $self->makemaker_args(PM_FILTER => "tt_pm_to_blib $filename"); | ||
| 69 | |||
| 70 | # Some of the following may not have been available in the template; the | ||
| 71 | # module author can specify that they should come from somewhere else. | ||
| 72 | if (defined $args{rest_from}) { | ||
| 73 | |||
| 74 | # try to get all values that haven't been defined yet from the | ||
| 75 | # indicated source | ||
| 76 | for my $key (qw(version perl_version author license abstract)) { | ||
| 77 | next if defined($self->$key) && length($self->$key); | ||
| 78 | my $method = "${key}_from"; | ||
| 79 | $self->$method($args{rest_from}); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | $self->all_from($args{all_from}) | ||
| 83 | if defined $args{all_from}; | ||
| 84 | $self->version_from($args{version_from}) | ||
| 85 | if defined $args{version_from}; | ||
| 86 | $self->perl_version_from($args{perl_version_from}) | ||
| 87 | if defined $args{perl_version_from}; | ||
| 88 | $self->author_from($args{author_from}) | ||
| 89 | if defined $args{author_from}; | ||
| 90 | $self->license_from($args{license_from}) | ||
| 91 | if defined $args{license_from}; | ||
| 92 | $self->abstract_from($args{abstract_from}) | ||
| 93 | if defined $args{abstract_from}; | ||
| 94 | } | ||
| 95 | |||
| 96 | # 'make dist' uses ExtUtils::Manifest's maniread() and manicopy() to determine | ||
| 97 | # what should be copied into the dist dir. This is fine for most purposes, but | ||
| 98 | # with Moduile::Install::Template we really want the finished files, not the | ||
| 99 | # templates. So we override the create_distdir rule here, but only if we're | ||
| 100 | # the author - the end user shouldn't be bothered with any of this | ||
| 101 | # kludge^Wmagic. | ||
| 102 | # | ||
| 103 | # We still use maniread() to get the file names, but then change those | ||
| 104 | # filenames pointing into lib/ to point into blib/lib instead. Now the right | ||
| 105 | # files get copied, but they end up in the wrong place - in the dist dir's | ||
| 106 | # blib/lib/. So at the end we move them to the right place and delete the dist | ||
| 107 | # dir's blib/ directory. | ||
| 108 | # | ||
| 109 | # And 'make disttest' needs to be modified as well; we need to have the blib/ | ||
| 110 | # files before we can test the distribution. So I've added a 'pm_to_blib' | ||
| 111 | # requirement to the 'disttest' target. | ||
| 112 | sub MY::postamble { | ||
| 113 | my $self = shift; | ||
| 114 | no warnings 'once'; | ||
| 115 | return '' if defined $::IS_MODULE_INSTALL_TEMPLATE; | ||
| 116 | |||
| 117 | # for some reason, Module::Install runs this subroutine even if the | ||
| 118 | # Makefile.PL doesn't specify process_template(). So here we check whether | ||
| 119 | # process_template() has been run. | ||
| 120 | return '' unless defined $::WANTS_MODULE_INSTALL_TEMPLATE; | ||
| 121 | return '' unless Module::Install::Template->is_author; | ||
| 122 | return <<'EOPOSTAMBLE'; | ||
| 123 | create_distdir : pm_to_blib | ||
| 124 | $(RM_RF) $(DISTVNAME) | ||
| 125 | $(PERLRUN) "-MExtUtils::Manifest=manicopy,maniread" \ | ||
| 126 | -e '$$m = maniread(); while (($$k, $$v) = each %$$m) { next if $$k !~ m!^lib/!; delete $$m->{$$k}; $$m->{"blib/$$k"} = $$v; }; manicopy($$m, "$(DISTVNAME)", "$(DIST_CP)"); ' | ||
| 127 | $(MV) $(DISTVNAME)/blib/lib $(DISTVNAME)/lib | ||
| 128 | $(RM_RF) $(DISTVNAME)/blib | ||
| 129 | |||
| 130 | disttest : pm_to_blib distdir | ||
| 131 | cd $(DISTVNAME) && $(ABSPERLRUN) Makefile.PL | ||
| 132 | cd $(DISTVNAME) && $(MAKE) $(PASTHRU) | ||
| 133 | cd $(DISTVNAME) && $(MAKE) test $(PASTHRU) | ||
| 134 | EOPOSTAMBLE | ||
| 135 | } | ||
| 136 | 1; | ||
| 137 | __END__ |