use subclassing to avoid interfering with a MY::postamble
[p5sagit/Distar.git] / lib / Distar.pm
CommitLineData
42e08a83 1package Distar;
2
3use strictures 1;
4use base qw(Exporter);
ca3b3b8a 5use ExtUtils::MakeMaker ();
6use ExtUtils::MM ();
42e08a83 7
ca313d5a 8use Config;
28268771 9use File::Spec;
ca313d5a 10
42e08a83 11our @EXPORT = qw(
5154970c 12 author manifest_include run_preflight
42e08a83 13);
14
15sub import {
16 strictures->import;
17 shift->export_to_level(1,@_);
18}
19
20sub author { our $Author = shift }
21
d7998bfa 22our $Ran_Preflight;
23
42e08a83 24our @Manifest = (
25 'lib' => '.pm',
26 't' => '.t',
27 't/lib' => '.pm',
28 'xt' => '.t',
29 'xt/lib' => '.pm',
1d950b4a 30 '' => qr{[^/]*\.PL},
42e08a83 31 '' => qr{Changes|MANIFEST|README|META\.yml},
42e08a83 32 'maint' => qr{[^.].*},
33);
34
35sub manifest_include {
36 push @Manifest, @_;
37}
38
39sub 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"
a3e39afd 49 # print ref as well as stringification in case of overload ""
42e08a83 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
5154970c 59sub run_preflight {
d7998bfa 60 $Ran_Preflight = 1;
12b4d7c3 61 my $version = $ARGV[0];
d7998bfa 62
5154970c 63 system("git fetch");
64
ca313d5a 65 my $make = $Config{make};
28268771 66 my $null = File::Spec->devnull;
ca313d5a 67
12b4d7c3 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
4df609ef 77 for (scalar `"$make" manifest 2>&1 >$null`) {
ca313d5a 78 $_ && die "$make manifest changed:\n$_ Go check it and retry";
41c39fda 79 }
80
5154970c 81 for (scalar `git status`) {
50d3c4ad 82 /^# On branch master/ || die "Not on master. EEEK";
325f6231 83 /Your branch is behind|Your branch and .*? have diverged/ && die "Not synced with upstream";
5154970c 84 }
85
86 for (scalar `git diff`) {
7374f43f 87 length && die "Outstanding changes";
5154970c 88 }
5154970c 89 my $ymd = sprintf(
90 "%i-%02i-%02i", (localtime)[5]+1900, (localtime)[4]+1, (localtime)[3]
91 );
59fcfa65 92 my @cached = grep /^\+/, `git diff --cached -U0`;
12b4d7c3 93 @cached > 0 or die "Please add:\n\n$version - $ymd\n\nto Changes stage Changes (git add Changes)";
59fcfa65 94 @cached == 2 or die "Pre-commit Changes not just Changes line";
c373c51b 95 $cached[0] =~ /^\+\+\+ .\/Changes\n/ or die "Changes not changed";
12b4d7c3 96 $cached[1] eq "+$version - $ymd\n" or die "Changes new line should be: \n\n$version - $ymd\n ";
5154970c 97}
98
ca3b3b8a 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';
b0401762 118
ca3b3b8a 119# --- Distar section:
5154970c 120preflight:
584b890c 121 perl -IDistar/lib -MDistar -erun_preflight $(VERSION)
72932296 122release: preflight
123 $(MAKE) disttest
27dbd26e 124 rm -rf $(DISTVNAME)
125 $(MAKE) $(DISTVNAME).tar$(SUFFIX)
42e08a83 126 git commit -a -m "Release commit for $(VERSION)"
401ece0b 127 git tag v$(VERSION) -m "release v$(VERSION)"
65a1f7d9 128 cpan-upload $(DISTVNAME).tar$(SUFFIX)
2c636792 129 git push origin v$(VERSION) HEAD
5154970c 130distdir: readmefile
131readmefile: create_distdir
132 pod2text $(VERSION_FROM) >$(DISTVNAME)/README
046ee554 133 $(NOECHO) cd $(DISTVNAME) && $(ABSPERLRUN) ../Distar/helpers/add-readme-to-manifest
ca3b3b8a 134
42e08a83 135END
a2f6bc7b 136 if (open my $fh, '<', 'maint/Makefile.include') {
ca3b3b8a 137 $dist_test .= do { local $/; <$fh> };
a2f6bc7b 138 }
ca3b3b8a 139 return $dist_test;
42e08a83 140 }
141}
142
143END {
d7998bfa 144 write_manifest_skip() unless $Ran_Preflight
42e08a83 145}
146
1471;