check that cpan-upload is available before starting release
[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 our @EXPORT = qw(
16   author manifest_include run_preflight
17 );
18
19 sub import {
20   strictures->import;
21   shift->export_to_level(1,@_);
22 }
23
24 sub author { our $Author = shift }
25
26 our $Ran_Preflight;
27
28 our @Manifest = (
29   'lib' => '.pm',
30   't' => '.t',
31   't/lib' => '.pm',
32   'xt' => '.t',
33   'xt/lib' => '.pm',
34   '' => qr{[^/]*\.PL},
35   '' => qr{Changes|MANIFEST|README|META\.yml},
36   'maint' => qr{[^.].*},
37 );
38
39 sub manifest_include {
40   push @Manifest, @_;
41 }
42
43 sub write_manifest_skip {
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     or die "can't open MANIFEST.SKIP: $!";
59   print $skip "${final}\n";
60   close $skip;
61 }
62
63 sub run_preflight {
64   $Ran_Preflight = 1;
65   my $version = $ARGV[0];
66
67   my $make = $Config{make};
68   my $null = File::Spec->devnull;
69
70   system("git fetch");
71   if (system("git rev-parse --quiet --verify v$version >$null") == 0) {
72     die "Tag v$version already exists!";
73   }
74
75   require File::Find;
76   File::Find::find({ no_chdir => 1, wanted => sub {
77     return
78       unless -f && /\.pm$/;
79     my $file_version = MM->parse_version($_);
80     die "Module $_ version $file_version doesn't match dist version $version"
81       unless $file_version eq 'undef' || $file_version eq $version;
82   }}, 'lib');
83
84   for (scalar `"$make" manifest 2>&1 >$null`) {
85     $_ && die "$make manifest changed:\n$_ Go check it and retry";
86   }
87
88   for (scalar `git status`) {
89     /^(?:# )?On branch master/ || die "Not on master. EEEK";
90     /Your branch is behind|Your branch and .*? have diverged/ && die "Not synced with upstream";
91   }
92
93   for (scalar `git diff`) {
94     length && die "Outstanding changes";
95   }
96   my $ymd = sprintf(
97     "%i-%02i-%02i", (localtime)[5]+1900, (localtime)[4]+1, (localtime)[3]
98   );
99   my $changes_line = "$version - $ymd\n";
100   my @cached = grep /^\+/, `git diff --cached -U0`;
101   @cached > 0 or die "Please add:\n\n$changes_line\nto Changes stage Changes (git add Changes)";
102   @cached == 2 or die "Pre-commit Changes not just Changes line";
103   $cached[0] =~ /^\+\+\+ .\/Changes\n/ or die "Changes not changed";
104   $cached[1] eq "+$changes_line" or die "Changes new line should be: \n\n$changes_line ";
105
106   { no warnings 'exec'; `cpan-upload -h`; }
107   $? and die "cpan-upload not available";
108 }
109
110 {
111   package Distar::MM;
112   our @ISA = @ExtUtils::MM::ISA;
113   @ExtUtils::MM::ISA = (__PACKAGE__);
114
115   sub new {
116     my ($class, $args) = @_;
117     return $class->SUPER::new({
118       LICENSE => 'perl',
119       %$args,
120       AUTHOR => $Distar::Author,
121       ABSTRACT_FROM => $args->{VERSION_FROM},
122       test => { TESTS => ($args->{test}{TESTS}||'t/*.t').' xt/*.t xt/*/*.t' },
123     });
124   }
125
126   sub dist_test {
127     my $self = shift;
128     my $manicheck = '$(PERLRUN) "-MExtUtils::Manifest=manicheck" -e "exit manicheck"';
129     my $test = $self->can('cd') ? $self->cd('$(DISTVNAME)', $manicheck)
130                                 : 'cd $(DISTVNAME) && ' . $manicheck;
131     my $dist_test = $self->SUPER::dist_test(@_) . sprintf(<<'END', $test);
132
133 # --- Distar section:
134 preflight:
135         perl -IDistar/lib -MDistar -erun_preflight $(VERSION)
136 release: preflight
137         $(MAKE) disttest
138         rm -rf $(DISTVNAME)
139         $(MAKE) $(DISTVNAME).tar$(SUFFIX)
140         git commit -a -m "Release commit for $(VERSION)"
141         git tag v$(VERSION) -m "release v$(VERSION)"
142         cpan-upload $(DISTVNAME).tar$(SUFFIX)
143         git push origin v$(VERSION) HEAD
144 distdir: readmefile
145 readmefile: create_distdir
146         pod2text $(VERSION_FROM) >$(DISTVNAME)/README
147         $(NOECHO) cd $(DISTVNAME) && $(ABSPERLRUN) ../Distar/helpers/add-readme-to-manifest
148 disttest: distmanicheck
149 distmanicheck: distdir
150         %s
151
152 END
153     if (open my $fh, '<', 'maint/Makefile.include') {
154       $dist_test .= do { local $/; <$fh> };
155     }
156     return $dist_test;
157   }
158 }
159
160 END {
161   write_manifest_skip() unless $Ran_Preflight
162 }
163
164 1;