9f6d14284243e45b72b79b50fd7f448e52f4a6c1
[p5sagit/Distar.git] / lib / Distar.pm
1 package Distar;
2
3 use strictures 1;
4 use base qw(Exporter);
5
6 use Config;
7 use File::Spec;
8
9 our @EXPORT = qw(
10   author manifest_include run_preflight
11 );
12
13 sub import {
14   strictures->import;
15   shift->export_to_level(1,@_);
16 }
17
18 sub author { our $Author = shift }
19
20 our $Ran_Preflight;
21
22 our @Manifest = (
23   'lib' => '.pm',
24   't' => '.t',
25   't/lib' => '.pm',
26   'xt' => '.t',
27   'xt/lib' => '.pm',
28   '' => qr{[^/]*\.PL},
29   '' => qr{Changes|MANIFEST|README|META\.yml},
30   'maint' => qr{[^.].*},
31 );
32
33 sub manifest_include {
34   push @Manifest, @_;
35 }
36
37 sub write_manifest_skip {
38   use autodie;
39   my @files = @Manifest;
40   my @parts;
41   while (my ($dir, $spec) = splice(@files, 0, 2)) {
42     my $re = ($dir ? $dir.'/' : '').
43       ((ref($spec) eq 'Regexp')
44         ? $spec
45         : !ref($spec)
46           ? ".*\Q${spec}\E"
47             # print ref as well as stringification in case of overload ""
48           : die "spec must be string or regexp, was: ${spec} (${\ref $spec})");
49     push @parts, $re;
50   }
51   my $final = '^(?!'.join('|', map "${_}\$", @parts).')';
52   open my $skip, '>', 'MANIFEST.SKIP';
53   print $skip "${final}\n";
54   close $skip;
55 }
56
57 sub run_preflight {
58   $Ran_Preflight = 1;
59   my $version = $ARGV[0];
60
61   system("git fetch");
62
63   my $make = $Config{make};
64   my $null = File::Spec->devnull;
65
66   require ExtUtils::MakeMaker;
67   require File::Find;
68   File::Find::find({ no_chdir => 1, wanted => sub {
69     return
70       unless -f && /\.pm$/;
71     my $file_version = MM->parse_version($_);
72     die "Module $_ version $file_version doesn't match dist version $version"
73       unless $file_version eq 'undef' || $file_version eq $version;
74   }}, 'lib');
75
76   for (scalar `"$make" manifest 2>&1 >$null`) {
77     $_ && die "$make manifest changed:\n$_ Go check it and retry";
78   }
79
80   for (scalar `git status`) {
81     /^# On branch master/ || die "Not on master. EEEK";
82     /Your branch is behind|Your branch and .*? have diverged/ && die "Not synced with upstream";
83   }
84
85   for (scalar `git diff`) {
86     length && die "Outstanding changes";
87   }
88   my $ymd = sprintf(
89     "%i-%02i-%02i", (localtime)[5]+1900, (localtime)[4]+1, (localtime)[3]
90   );
91   my @cached = grep /^\+/, `git diff --cached -U0`;
92   @cached > 0 or die "Please add:\n\n$version - $ymd\n\nto Changes stage Changes (git add Changes)";
93   @cached == 2 or die "Pre-commit Changes not just Changes line";
94   $cached[0] =~ /^\+\+\+ .\/Changes\n/ or die "Changes not changed";
95   $cached[1] eq "+$version - $ymd\n" or die "Changes new line should be: \n\n$version - $ymd\n ";
96 }
97
98 sub MY::postamble {
99     my ($self, %extra) = @_;
100
101     my $post = <<'END';
102 preflight:
103         perl -IDistar/lib -MDistar -erun_preflight $(VERSION)
104 release: preflight
105         $(MAKE) disttest
106         rm -rf $(DISTVNAME)
107         $(MAKE) $(DISTVNAME).tar$(SUFFIX)
108         git commit -a -m "Release commit for $(VERSION)"
109         git tag v$(VERSION) -m "release v$(VERSION)"
110         cpan-upload $(DISTVNAME).tar$(SUFFIX)
111         git push origin v$(VERSION) HEAD
112 distdir: readmefile
113 readmefile: create_distdir
114         pod2text $(VERSION_FROM) >$(DISTVNAME)/README
115         $(NOECHO) cd $(DISTVNAME) && $(ABSPERLRUN) ../Distar/helpers/add-readme-to-manifest
116 END
117     if (open my $fh, '<', 'maint/Makefile.include') {
118         $post .= do { local $/; <$fh> };
119     }
120
121     # add on any extra args that WriteMakefile chose to pass us
122     # (note that Devel::Declare, and possibly others, use this)
123     $post .= "\n" . join('', %extra) if keys %extra;
124
125     return $post;
126 }
127
128 {
129   no warnings 'redefine';
130   sub main::WriteMakefile {
131     my %args = @_;
132     ExtUtils::MakeMaker::WriteMakefile(
133       LICENSE => 'perl',
134       @_, AUTHOR => our $Author, ABSTRACT_FROM => $args{VERSION_FROM},
135       test => { TESTS => ($args{test}{TESTS}||'t/*.t').' xt/*.t' },
136     );
137   }
138 }
139
140 END {
141   write_manifest_skip() unless $Ran_Preflight
142 }
143
144 1;