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