refactoring Makefile code
[p5sagit/Distar.git] / lib / Distar.pm
1 package Distar;
2
3 use strict;
4 use warnings FATAL => 'all';
5 use base qw(Exporter);
6 use ExtUtils::MakeMaker ();
7 use ExtUtils::MM ();
8
9 use Config;
10 use File::Spec;
11
12 our $VERSION = '0.001000';
13 $VERSION = eval $VERSION;
14
15 my $MM_VER = eval $ExtUtils::MakeMaker::VERSION;
16
17 our @EXPORT = qw(
18   author manifest_include readme_generator
19 );
20
21 sub import {
22   strict->import;
23   warnings->import(FATAL => 'all');
24   shift->export_to_level(1,@_);
25 }
26
27 sub author {
28   our $Author = shift;
29   $Author = [ $Author ]
30     if !ref $Author;
31 }
32
33 our @Manifest = (
34   'lib' => '.pm',
35   'lib' => '.pod',
36   't' => '.t',
37   't/lib' => '.pm',
38   'xt' => '.t',
39   'xt/lib' => '.pm',
40   '' => qr{[^/]*\.PL},
41   '' => qr{Changes|MANIFEST|README|META\.yml},
42   'maint' => qr{[^.].*},
43 );
44
45 sub manifest_include {
46   push @Manifest, @_;
47 }
48
49 my $readme_generator = <<'README';
50         pod2text $(VERSION_FROM) >$(DISTVNAME)/README
51         $(NOECHO) cd $(DISTVNAME) && $(ABSPERLRUN) ../Distar/helpers/add-readme-to-manifest
52 README
53 sub readme_generator {
54     $readme_generator = shift;
55 }
56
57 sub write_manifest_skip {
58   my @files = @Manifest;
59   my @parts;
60   while (my ($dir, $spec) = splice(@files, 0, 2)) {
61     my $re = ($dir ? $dir.'/' : '').
62       ((ref($spec) eq 'Regexp')
63         ? $spec
64         : !ref($spec)
65           ? ".*\Q${spec}\E"
66             # print ref as well as stringification in case of overload ""
67           : die "spec must be string or regexp, was: ${spec} (${\ref $spec})");
68     push @parts, $re;
69   }
70   my $final = '^(?!'.join('|', map "${_}\$", @parts).')';
71   open my $skip, '>', 'MANIFEST.SKIP'
72     or die "can't open MANIFEST.SKIP: $!";
73   print $skip "${final}\n";
74   close $skip;
75 }
76
77 {
78   package Distar::MM;
79   our @ISA = @MM::ISA;
80   @MM::ISA = (__PACKAGE__);
81
82   sub new {
83     my ($class, $args) = @_;
84     return $class->SUPER::new({
85       LICENSE => 'perl_5',
86       MIN_PERL_VERSION => '5.006',
87       AUTHOR => ($MM_VER >= 6.5702 ? $Distar::Author : join(', ', @$Distar::Author)),
88       %$args,
89       ABSTRACT_FROM => $args->{VERSION_FROM},
90       test => { TESTS => ($args->{test}{TESTS}||'t/*.t').' xt/*.t xt/*/*.t' },
91       realclean => { FILES => (
92         ($args->{realclean}{FILES}||'')
93         . ' Distar/ MANIFEST.SKIP MANIFEST MANIFEST.bak'
94       ) },
95     });
96   }
97
98   sub flush {
99     my $self = shift;
100     Distar::write_manifest_skip();
101     $self->SUPER::flush(@_);
102   }
103
104   sub dist_test {
105     my $self = shift;
106     my $dist_test = $self->SUPER::dist_test(@_);
107
108     $dist_test .= <<"END";
109
110 # --- Distar section:
111
112 REMAKE = \$(PERLRUN) Makefile.PL @{[ map { $self->quote_literal($_) } @ARGV ]}
113
114 END
115     $dist_test .= <<'END';
116 preflight:
117         $(ABSPERLRUN) Distar/helpers/preflight $(VERSION)
118 release: preflight
119         $(MAKE) disttest
120         $(RM_RF) $(DISTVNAME)
121         $(MAKE) $(DISTVNAME).tar$(SUFFIX)
122         git commit -a -m "Release commit for $(VERSION)"
123         git tag v$(VERSION) -m "release v$(VERSION)"
124         cpan-upload $(DISTVNAME).tar$(SUFFIX)
125         git push origin v$(VERSION) HEAD
126 distdir: readmefile
127 readmefile: create_distdir
128 END
129     . $readme_generator . <<'END';
130 disttest: distmanicheck
131 distmanicheck: create_distdir
132         cd $(DISTVNAME) && $(ABSPERLRUN) "-MExtUtils::Manifest=manicheck" -e "exit manicheck"
133 nextrelease:
134         $(ABSPERLRUN) Distar/helpers/add-changelog-heading $(VERSION) Changes
135         git add -p Changes
136 refresh:
137         cd Distar && git pull
138         rm Makefile
139         $(REMAKE)
140 END
141
142     my $include = '';
143     if (open my $fh, '<', 'maint/Makefile.include') {
144       $include = "\n# --- Makefile.include:\n" . do { local $/; <$fh> };
145     }
146
147     for my $type ('', 'minor', 'major') {
148       if ($include !~ /^bump$type:/m) {
149         my $arg = $type || '$(V)';
150         $dist_test .= <<"END"
151 bump$type:
152         \$(ABSPERLRUN) Distar/helpers/bump-version --git \$(VERSION) $arg
153         \$(RM_F) \$(FIRST_MAKEFILE)
154         \$(REMAKE)
155 END
156       }
157     }
158
159     $dist_test .= $include . "\n";
160
161     return $dist_test;
162   }
163 }
164
165 1;