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