use dist-checkconflicts
[gitmo/Package-Stash.git] / inc / MMPackageStash.pm
1 package inc::MMPackageStash;
2 use Moose;
3
4 extends 'Dist::Zilla::Plugin::MakeMaker::Awesome';
5
6 # XXX: this is pretty gross, it should be possible to clean this up later
7 around _build_MakeFile_PL_template => sub {
8     my $orig = shift;
9     my $self = shift;
10
11     # copied from M::I
12     my $can_cc = <<'CAN_CC';
13 use Config ();
14 use File::Spec ();
15
16 # check if we can run some command
17 sub can_run {
18         my ($cmd) = @_;
19
20         my $_cmd = $cmd;
21         return $_cmd if (-x $_cmd or $_cmd = MM->maybe_command($_cmd));
22
23         for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') {
24                 next if $dir eq '';
25                 my $abs = File::Spec->catfile($dir, $_[1]);
26                 return $abs if (-x $abs or $abs = MM->maybe_command($abs));
27         }
28
29         return;
30 }
31
32 # can we locate a (the) C compiler
33 sub can_cc {
34         my @chunks = split(/ /, $Config::Config{cc}) or return;
35
36         # $Config{cc} may contain args; try to find out the program part
37         while (@chunks) {
38                 return can_run("@chunks") || (pop(@chunks), next);
39         }
40
41         return;
42 }
43 CAN_CC
44
45     # copied out of moose
46     my $check_conflicts = <<'CHECK_CONFLICTS';
47 sub check_conflicts {
48     if (eval { require 'lib/Package/Stash/Conflicts.pm'; 1; }) {
49         if (eval { Package::Stash::Conflicts->check_conflicts; 1 }) {
50             return;
51         }
52         else {
53             my $err = $@;
54             $err =~ s/^/    /mg;
55             warn "***\n$err***\n";
56         }
57     }
58     else {
59         print <<'EOF';
60 ***
61     Your toolchain doesn't support configure_requires, so Dist::CheckConflicts
62     hasn't been installed yet. You should check for conflicting modules
63     manually using the 'package-stash-conflicts' script that is installed with
64     this distribution once the installation finishes.
65 ***
66 EOF
67     }
68
69     # More or less copied from Module::Build
70     return if $ENV{PERL_MM_USE_DEFAULT};
71     return unless -t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT));
72
73     sleep 4;
74 }
75 CHECK_CONFLICTS
76
77     my $template = $self->$orig(@_);
78
79     $template =~ s/(use ExtUtils::MakeMaker.*)/$1\n\ncheck_conflicts();/;
80     $template =~ s/(WriteMakefile\()/delete \$WriteMakefileArgs{PREREQ_PM}{'Package::Stash::XS'}\n  unless can_cc();\n\n$1/;
81
82     return $template . $can_cc . $check_conflicts;
83 };
84
85 __PACKAGE__->meta->make_immutable;
86 no Moose;
87
88 1;