move preflight to separate script
[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     my $include = '';
109     if (open my $fh, '<', 'maint/Makefile.include') {
110       $include = "\n# --- Makefile.include:\n" . do { local $/; <$fh> };
111     }
112
113     $dist_test .= "REMAKE = \$(PERLRUN) Makefile.PL @{[ map { $self->quote_literal($_) } @ARGV ]}";
114     $dist_test .= <<'END'
115
116 # --- Distar section:
117 preflight:
118         $(ABSPERLRUN) Distar/helpers/preflight $(VERSION)
119 release: preflight
120         $(MAKE) disttest
121         rm -rf $(DISTVNAME)
122         $(MAKE) $(DISTVNAME).tar$(SUFFIX)
123         git commit -a -m "Release commit for $(VERSION)"
124         git tag v$(VERSION) -m "release v$(VERSION)"
125         cpan-upload $(DISTVNAME).tar$(SUFFIX)
126         git push origin v$(VERSION) HEAD
127 distdir: readmefile
128 readmefile: create_distdir
129 END
130     . $readme_generator . <<'END';
131 disttest: distmanicheck
132 distmanicheck: create_distdir
133         cd $(DISTVNAME) && $(ABSPERLRUN) "-MExtUtils::Manifest=manicheck" -e "exit manicheck"
134 nextrelease:
135         $(ABSPERLRUN) Distar/helpers/add-changelog-heading $(VERSION) Changes
136         git add -p Changes
137 refresh:
138         cd Distar && git pull
139         rm Makefile
140         $(REMAKE)
141 END
142
143     for my $type ('', 'minor', 'major') {
144       if ($include !~ /^bump$type:/m) {
145         my $arg = $type || '$(V)';
146         $dist_test .= <<"END"
147 bump$type:
148         Distar/helpers/bump-version --git \$(VERSION) $arg
149         rm Makefile
150         \$(REMAKE)
151 END
152       }
153     }
154
155     $dist_test .= $include . "\n";
156
157     return $dist_test;
158   }
159 }
160
161 1;