c58915065968d8ce7454d499c8b213c1d6d342da
[p5sagit/Distar.git] / lib / Distar.pm
1 package Distar;
2
3 use strictures 1;
4 use base qw(Exporter);
5
6 our @EXPORT = qw(
7   author manifest_include run_preflight
8 );
9
10 sub import {
11   strictures->import;
12   shift->export_to_level(1,@_);
13 }
14
15 sub author { our $Author = shift }
16
17 our @Manifest = (
18   'lib' => '.pm',
19   't' => '.t',
20   't/lib' => '.pm',
21   'xt' => '.t',
22   'xt/lib' => '.pm',
23   '' => '.PL',
24   '' => qr{Changes|MANIFEST|README|META\.yml},
25   'maint' => qr{[^.].*},
26 );
27
28 sub manifest_include {
29   push @Manifest, @_;
30 }
31
32 sub write_manifest_skip {
33   use autodie;
34   my @files = @Manifest;
35   my @parts;
36   while (my ($dir, $spec) = splice(@files, 0, 2)) {
37     my $re = ($dir ? $dir.'/' : '').
38       ((ref($spec) eq 'Regexp')
39         ? $spec
40         : !ref($spec)
41           ? ".*\Q${spec}\E"
42           : die "spec must be string or regexp, was: ${spec} (${\ref $spec})");
43     push @parts, $re;
44   }
45   my $final = '^(?!'.join('|', map "${_}\$", @parts).')';
46   open my $skip, '>', 'MANIFEST.SKIP';
47   print $skip "${final}\n";
48   close $skip;
49 }
50
51 sub run_preflight {
52   system("git fetch");
53
54   for (scalar `git status`) {
55     /Your branch is (behind|ahead of)/ && die "Not synced with upstream";
56   }
57
58   for (scalar `git diff`) {
59     length && die "Oustanding changes";
60   }
61   my @cached = grep /^\+/, `git diff --cached -U0`;
62   @cached == 2 or die "Pre-commit Changes not just Changes line";
63   $cached[0] eq "+++ b/Changes\n" or die "Changes not changed";
64   my $ymd = sprintf(
65     "%i-%02i-%02i", (localtime)[5]+1900, (localtime)[4]+1, (localtime)[3]
66   );
67   $cached[1] eq "+$ARGV[0] - $ymd\n" or die "Changes new line should be: \n\n$ARGV[0] - $ymd\n ";
68 }
69
70 sub MY::postamble { <<'END'; }
71 preflight:
72         perl -Idistar/lib -MDistar -erun_preflight $(VERSION)
73 upload: preflight disttest $(DISTVNAME).tar$(SUFFIX)
74         cpan-upload $(DISTVNAME).tar$(SUFFIX)
75 release: upload
76         git commit -a -m "Release commit for $(VERSION)"
77         git tag release_$(VERSION)
78         git push
79         git push --tags
80 distdir: readmefile
81 readmefile: create_distdir
82         pod2text $(VERSION_FROM) >$(DISTVNAME)/README
83 END
84
85 {
86   no warnings 'redefine';
87   sub main::WriteMakefile {
88     my %args = @_;
89     ExtUtils::MakeMaker::WriteMakefile(
90       @_, AUTHOR => our $Author, ABSTRACT_FROM => $args{VERSION_FROM},
91       test => { TESTS => ($args{test}{TESTS}||'').' xt/*.t' },
92     );
93   }
94 }
95
96 END {
97   write_manifest_skip()
98 }
99
100 1;