correctly apply fatal warnings to caller
[p5sagit/Distar.git] / lib / Distar.pm
CommitLineData
42e08a83 1package Distar;
2
362ec4af 3use strict;
4use warnings FATAL => 'all';
42e08a83 5use base qw(Exporter);
ca3b3b8a 6use ExtUtils::MakeMaker ();
7use ExtUtils::MM ();
42e08a83 8
ca313d5a 9use Config;
28268771 10use File::Spec;
ca313d5a 11
37e295d8 12our $VERSION = '0.001000';
13$VERSION = eval $VERSION;
14
42e08a83 15our @EXPORT = qw(
5154970c 16 author manifest_include run_preflight
42e08a83 17);
18
19sub import {
2852faf3 20 strict->import;
21 warnings->import(FATAL => 'all');
42e08a83 22 shift->export_to_level(1,@_);
23}
24
25sub author { our $Author = shift }
26
d7998bfa 27our $Ran_Preflight;
28
42e08a83 29our @Manifest = (
30 'lib' => '.pm',
31 't' => '.t',
32 't/lib' => '.pm',
33 'xt' => '.t',
34 'xt/lib' => '.pm',
1d950b4a 35 '' => qr{[^/]*\.PL},
42e08a83 36 '' => qr{Changes|MANIFEST|README|META\.yml},
42e08a83 37 'maint' => qr{[^.].*},
38);
39
40sub manifest_include {
41 push @Manifest, @_;
42}
43
44sub write_manifest_skip {
42e08a83 45 my @files = @Manifest;
46 my @parts;
47 while (my ($dir, $spec) = splice(@files, 0, 2)) {
48 my $re = ($dir ? $dir.'/' : '').
49 ((ref($spec) eq 'Regexp')
50 ? $spec
51 : !ref($spec)
52 ? ".*\Q${spec}\E"
a3e39afd 53 # print ref as well as stringification in case of overload ""
42e08a83 54 : die "spec must be string or regexp, was: ${spec} (${\ref $spec})");
55 push @parts, $re;
56 }
57 my $final = '^(?!'.join('|', map "${_}\$", @parts).')';
19916679 58 open my $skip, '>', 'MANIFEST.SKIP'
59 or die "can't open MANIFEST.SKIP: $!";
42e08a83 60 print $skip "${final}\n";
61 close $skip;
62}
63
5154970c 64sub run_preflight {
d7998bfa 65 $Ran_Preflight = 1;
12b4d7c3 66 my $version = $ARGV[0];
d7998bfa 67
ca313d5a 68 my $make = $Config{make};
28268771 69 my $null = File::Spec->devnull;
ca313d5a 70
a973b068 71 system("git fetch");
72 if (system("git rev-parse --quiet --verify v$version >$null") == 0) {
73 die "Tag v$version already exists!";
74 }
75
12b4d7c3 76 require File::Find;
77 File::Find::find({ no_chdir => 1, wanted => sub {
78 return
79 unless -f && /\.pm$/;
80 my $file_version = MM->parse_version($_);
81 die "Module $_ version $file_version doesn't match dist version $version"
82 unless $file_version eq 'undef' || $file_version eq $version;
83 }}, 'lib');
84
4df609ef 85 for (scalar `"$make" manifest 2>&1 >$null`) {
ca313d5a 86 $_ && die "$make manifest changed:\n$_ Go check it and retry";
41c39fda 87 }
88
5154970c 89 for (scalar `git status`) {
cc86b8ea 90 /^(?:# )?On branch master/ || die "Not on master. EEEK";
325f6231 91 /Your branch is behind|Your branch and .*? have diverged/ && die "Not synced with upstream";
5154970c 92 }
93
94 for (scalar `git diff`) {
7374f43f 95 length && die "Outstanding changes";
5154970c 96 }
5154970c 97 my $ymd = sprintf(
98 "%i-%02i-%02i", (localtime)[5]+1900, (localtime)[4]+1, (localtime)[3]
99 );
0887b8ff 100 my $changes_line = "$version - $ymd\n";
59fcfa65 101 my @cached = grep /^\+/, `git diff --cached -U0`;
e2d04df8 102 @cached > 0 or die "Please add:\n\n$changes_line\nto Changes stage Changes (git add Changes)";
59fcfa65 103 @cached == 2 or die "Pre-commit Changes not just Changes line";
c373c51b 104 $cached[0] =~ /^\+\+\+ .\/Changes\n/ or die "Changes not changed";
e2d04df8 105 $cached[1] eq "+$changes_line" or die "Changes new line should be: \n\n$changes_line ";
c789d7d6 106
107 { no warnings 'exec'; `cpan-upload -h`; }
108 $? and die "cpan-upload not available";
5154970c 109}
110
ca3b3b8a 111{
112 package Distar::MM;
113 our @ISA = @ExtUtils::MM::ISA;
114 @ExtUtils::MM::ISA = (__PACKAGE__);
115
116 sub new {
117 my ($class, $args) = @_;
118 return $class->SUPER::new({
119 LICENSE => 'perl',
120 %$args,
121 AUTHOR => $Distar::Author,
122 ABSTRACT_FROM => $args->{VERSION_FROM},
c194baba 123 test => { TESTS => ($args->{test}{TESTS}||'t/*.t').' xt/*.t xt/*/*.t' },
ca3b3b8a 124 });
125 }
126
127 sub dist_test {
128 my $self = shift;
f504bfb4 129 my $manicheck = '$(PERLRUN) "-MExtUtils::Manifest=manicheck" -e "exit manicheck"';
130 my $test = $self->can('cd') ? $self->cd('$(DISTVNAME)', $manicheck)
131 : 'cd $(DISTVNAME) && ' . $manicheck;
de048fa8 132 my $dist_test = $self->SUPER::dist_test(@_) . sprintf(<<'END', $test);
b0401762 133
ca3b3b8a 134# --- Distar section:
5154970c 135preflight:
584b890c 136 perl -IDistar/lib -MDistar -erun_preflight $(VERSION)
72932296 137release: preflight
138 $(MAKE) disttest
27dbd26e 139 rm -rf $(DISTVNAME)
140 $(MAKE) $(DISTVNAME).tar$(SUFFIX)
42e08a83 141 git commit -a -m "Release commit for $(VERSION)"
401ece0b 142 git tag v$(VERSION) -m "release v$(VERSION)"
65a1f7d9 143 cpan-upload $(DISTVNAME).tar$(SUFFIX)
2c636792 144 git push origin v$(VERSION) HEAD
5154970c 145distdir: readmefile
146readmefile: create_distdir
147 pod2text $(VERSION_FROM) >$(DISTVNAME)/README
046ee554 148 $(NOECHO) cd $(DISTVNAME) && $(ABSPERLRUN) ../Distar/helpers/add-readme-to-manifest
de048fa8 149disttest: distmanicheck
150distmanicheck: distdir
151 %s
ca3b3b8a 152
42e08a83 153END
a2f6bc7b 154 if (open my $fh, '<', 'maint/Makefile.include') {
ca3b3b8a 155 $dist_test .= do { local $/; <$fh> };
a2f6bc7b 156 }
ca3b3b8a 157 return $dist_test;
42e08a83 158 }
159}
160
161END {
d7998bfa 162 write_manifest_skip() unless $Ran_Preflight
42e08a83 163}
164
1651;