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