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