72b8c2ddbbb7c26257fbec4617ecc94d2c657dbf
[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 run_preflight
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 sub run_preflight {
78   my $version = $ARGV[0];
79
80   my $make = $Config{make};
81   my $null = File::Spec->devnull;
82
83   system("git fetch");
84   if (system("git rev-parse --quiet --verify v$version >$null") == 0) {
85     die "Tag v$version already exists!";
86   }
87
88   require File::Find;
89   File::Find::find({ no_chdir => 1, wanted => sub {
90     return
91       unless -f && /\.pm$/;
92     my $file_version = MM->parse_version($_);
93     die "Module $_ version $file_version doesn't match dist version $version"
94       unless $file_version eq 'undef' || $file_version eq $version;
95   }}, 'lib');
96
97   for (scalar `"$make" manifest 2>&1 >$null`) {
98     $_ && die "$make manifest changed:\n$_ Go check it and retry";
99   }
100
101   for (scalar `git status`) {
102     /^(?:# )?On branch master/ || die "Not on master. EEEK";
103     /Your branch is behind|Your branch and .*? have diverged/ && die "Not synced with upstream";
104   }
105
106   for (scalar `git diff`) {
107     length && die "Outstanding changes";
108   }
109   my $ymd = sprintf(
110     "%i-%02i-%02i", (gmtime)[5]+1900, (gmtime)[4]+1, (gmtime)[3]
111   );
112   my $changes_line = "$version - $ymd\n";
113   my @cached = grep /^\+/, `git diff --cached -U0`;
114   @cached > 0 or die "Please add:\n\n$changes_line\nto Changes stage Changes (git add Changes)";
115   @cached == 2 or die "Pre-commit Changes not just Changes line";
116   $cached[0] =~ /^\+\+\+ .\/Changes\n/ or die "Changes not changed";
117   $cached[1] eq "+$changes_line" or die "Changes new line should be: \n\n$changes_line ";
118
119   { no warnings 'exec'; `cpan-upload -h`; }
120   $? and die "cpan-upload not available";
121 }
122
123 {
124   package Distar::MM;
125   our @ISA = @MM::ISA;
126   @MM::ISA = (__PACKAGE__);
127
128   sub new {
129     my ($class, $args) = @_;
130     return $class->SUPER::new({
131       LICENSE => 'perl_5',
132       MIN_PERL_VERSION => '5.006',
133       AUTHOR => ($MM_VER >= 6.5702 ? $Distar::Author : join(', ', @$Distar::Author)),
134       %$args,
135       ABSTRACT_FROM => $args->{VERSION_FROM},
136       test => { TESTS => ($args->{test}{TESTS}||'t/*.t').' xt/*.t xt/*/*.t' },
137       realclean => { FILES => (
138         ($args->{realclean}{FILES}||'')
139         . ' Distar/ MANIFEST.SKIP MANIFEST MANIFEST.bak'
140       ) },
141     });
142   }
143
144   sub flush {
145     my $self = shift;
146     Distar::write_manifest_skip();
147     $self->SUPER::flush(@_);
148   }
149
150   sub dist_test {
151     my $self = shift;
152     my $dist_test = $self->SUPER::dist_test(@_);
153
154     my $include = '';
155     if (open my $fh, '<', 'maint/Makefile.include') {
156       $include = "\n# --- Makefile.include:\n" . do { local $/; <$fh> };
157     }
158
159     $dist_test .= "REMAKE = \$(PERLRUN) Makefile.PL @{[ map { $self->quote_literal($_) } @ARGV ]}";
160     $dist_test .= <<'END'
161
162 # --- Distar section:
163 preflight:
164         perl -IDistar/lib -MDistar -erun_preflight $(VERSION)
165 release: preflight
166         $(MAKE) disttest
167         rm -rf $(DISTVNAME)
168         $(MAKE) $(DISTVNAME).tar$(SUFFIX)
169         git commit -a -m "Release commit for $(VERSION)"
170         git tag v$(VERSION) -m "release v$(VERSION)"
171         cpan-upload $(DISTVNAME).tar$(SUFFIX)
172         git push origin v$(VERSION) HEAD
173 distdir: readmefile
174 readmefile: create_distdir
175 END
176     . $readme_generator . <<'END';
177 disttest: distmanicheck
178 distmanicheck: create_distdir
179         cd $(DISTVNAME) && $(ABSPERLRUN) "-MExtUtils::Manifest=manicheck" -e "exit manicheck"
180 nextrelease:
181         $(ABSPERLRUN) Distar/helpers/add-changelog-heading $(VERSION) Changes
182         git add -p Changes
183 refresh:
184         cd Distar && git pull
185         rm Makefile
186         $(REMAKE)
187 END
188
189     for my $type ('', 'minor', 'major') {
190       if ($include !~ /^bump$type:/m) {
191         my $arg = $type || '$(V)';
192         $dist_test .= <<"END"
193 bump$type:
194         Distar/helpers/bump-version --git \$(VERSION) $arg
195         rm Makefile
196         \$(REMAKE)
197 END
198       }
199     }
200
201     $dist_test .= $include . "\n";
202
203     return $dist_test;
204   }
205 }
206
207 1;