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