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