refactoring Makefile code
[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(
be032607 18 author manifest_include readme_generator
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
ca3b3b8a 77{
78 package Distar::MM;
9af7ff2e 79 our @ISA = @MM::ISA;
80 @MM::ISA = (__PACKAGE__);
ca3b3b8a 81
82 sub new {
83 my ($class, $args) = @_;
84 return $class->SUPER::new({
180e908e 85 LICENSE => 'perl_5',
53e1282f 86 MIN_PERL_VERSION => '5.006',
0cc25268 87 AUTHOR => ($MM_VER >= 6.5702 ? $Distar::Author : join(', ', @$Distar::Author)),
c12b6d29 88 %$args,
ca3b3b8a 89 ABSTRACT_FROM => $args->{VERSION_FROM},
c194baba 90 test => { TESTS => ($args->{test}{TESTS}||'t/*.t').' xt/*.t xt/*/*.t' },
0a3fdb23 91 realclean => { FILES => (
92 ($args->{realclean}{FILES}||'')
93 . ' Distar/ MANIFEST.SKIP MANIFEST MANIFEST.bak'
94 ) },
ca3b3b8a 95 });
96 }
97
eddb1ce6 98 sub flush {
99 my $self = shift;
100 Distar::write_manifest_skip();
101 $self->SUPER::flush(@_);
102 }
103
ca3b3b8a 104 sub dist_test {
105 my $self = shift;
8ab5ea70 106 my $dist_test = $self->SUPER::dist_test(@_);
107
81d518f6 108 $dist_test .= <<"END";
b0401762 109
ca3b3b8a 110# --- Distar section:
81d518f6 111
112REMAKE = \$(PERLRUN) Makefile.PL @{[ map { $self->quote_literal($_) } @ARGV ]}
113
114END
115 $dist_test .= <<'END';
5154970c 116preflight:
be032607 117 $(ABSPERLRUN) Distar/helpers/preflight $(VERSION)
72932296 118release: preflight
119 $(MAKE) disttest
81d518f6 120 $(RM_RF) $(DISTVNAME)
27dbd26e 121 $(MAKE) $(DISTVNAME).tar$(SUFFIX)
42e08a83 122 git commit -a -m "Release commit for $(VERSION)"
401ece0b 123 git tag v$(VERSION) -m "release v$(VERSION)"
65a1f7d9 124 cpan-upload $(DISTVNAME).tar$(SUFFIX)
2c636792 125 git push origin v$(VERSION) HEAD
5154970c 126distdir: readmefile
127readmefile: create_distdir
6e83c113 128END
129 . $readme_generator . <<'END';
de048fa8 130disttest: distmanicheck
792c9e91 131distmanicheck: create_distdir
132 cd $(DISTVNAME) && $(ABSPERLRUN) "-MExtUtils::Manifest=manicheck" -e "exit manicheck"
e7a78651 133nextrelease:
134 $(ABSPERLRUN) Distar/helpers/add-changelog-heading $(VERSION) Changes
135 git add -p Changes
0edb27b8 136refresh:
137 cd Distar && git pull
138 rm Makefile
139 $(REMAKE)
8ab5ea70 140END
ca3b3b8a 141
81d518f6 142 my $include = '';
143 if (open my $fh, '<', 'maint/Makefile.include') {
144 $include = "\n# --- Makefile.include:\n" . do { local $/; <$fh> };
145 }
146
8ab5ea70 147 for my $type ('', 'minor', 'major') {
148 if ($include !~ /^bump$type:/m) {
149 my $arg = $type || '$(V)';
150 $dist_test .= <<"END"
151bump$type:
81d518f6 152 \$(ABSPERLRUN) Distar/helpers/bump-version --git \$(VERSION) $arg
153 \$(RM_F) \$(FIRST_MAKEFILE)
ece2756d 154 \$(REMAKE)
42e08a83 155END
8ab5ea70 156 }
a2f6bc7b 157 }
8ab5ea70 158
159 $dist_test .= $include . "\n";
160
ca3b3b8a 161 return $dist_test;
42e08a83 162 }
163}
164
42e08a83 1651;