| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 71 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 71 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 71 | ▶ | use autodie qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 71 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 71 | ▶ | use Getopt::Long qw(GetOptions); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 71 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 71 | ▶ | use Text::Diff; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 71 | ▶ | use App::makefilepl2cpanfile; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | makefilepl2cpanfile - Convert a Makefile.PL to a cpanfile | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 VERSION | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | This document describes makefilepl2cpanfile as shipped with | ||||||||||||||||||||||||||||
| 20 | App::makefilepl2cpanfile version 0.07. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | bin/makefilepl2cpanfile [options] | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | Options: | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | --with-develop Include author/development dependencies (default) | ||||||||||||||||||||||||||||
| 29 | --no-develop Exclude author/development dependencies | ||||||||||||||||||||||||||||
| 30 | --dry-run Print output to STDOUT instead of writing cpanfile | ||||||||||||||||||||||||||||
| 31 | --diff Show a unified diff against the existing cpanfile | ||||||||||||||||||||||||||||
| 32 | --check Verify all Makefile.PL dependencies appear in output | ||||||||||||||||||||||||||||
| 33 | --help Show this usage message | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | Reads a F<Makefile.PL> in the current directory and generates a corresponding | ||||||||||||||||||||||||||||
| 38 | F<cpanfile> using L<App::makefilepl2cpanfile/generate>. Existing C<develop> | ||||||||||||||||||||||||||||
| 39 | blocks in a pre-existing F<cpanfile> are preserved and merged. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | By default the result is written to F<cpanfile> in the current directory and | ||||||||||||||||||||||||||||
| 42 | C<cpanfile written successfully.> is printed. The file is replaced in one | ||||||||||||||||||||||||||||
| 43 | step: if anything fails (reading, generating, a full disk, the final | ||||||||||||||||||||||||||||
| 44 | rename), the program exits with a non-zero status, the previous | ||||||||||||||||||||||||||||
| 45 | F<cpanfile> is left exactly as it was, and no temporary files remain. A | ||||||||||||||||||||||||||||
| 46 | newly created F<cpanfile> gets the permissions a new file normally would | ||||||||||||||||||||||||||||
| 47 | (C<0666> less the umask). | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =over 4 | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =item * C<--dry-run> prints the result to STDOUT and writes nothing. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =item * C<--diff> prints a unified diff from the existing F<cpanfile> (or | ||||||||||||||||||||||||||||
| 54 | from an empty file when there is none) to the result, and writes nothing. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =item * C<--check> additionally verifies that every module found by | ||||||||||||||||||||||||||||
| 57 | L<App::makefilepl2cpanfile/parse_prereqs> appears in the result, printing | ||||||||||||||||||||||||||||
| 58 | C<All Makefile.PL prerequisites are present in the output.> or warning with | ||||||||||||||||||||||||||||
| 59 | the list of missing modules: | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | WARNING: The following modules from Makefile.PL did not appear in the output: | ||||||||||||||||||||||||||||
| 62 | Some::Module | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | (one module per line, indented by two spaces). It does not change what | ||||||||||||||||||||||||||||
| 65 | is written, but when modules are missing the program exits with status | ||||||||||||||||||||||||||||
| 66 | 1, so a CI job can fail on it. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =item * C<--with-develop> and C<--no-develop> select the C<with_develop> | ||||||||||||||||||||||||||||
| 69 | argument; giving both is an error (C<--with-develop and --no-develop are | ||||||||||||||||||||||||||||
| 70 | mutually exclusive>) and the program exits with a non-zero status. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =item * C<--help> prints the usage message and exits with status 0. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =item * If F<cpanfile> exists but is not a regular file (a directory, a | ||||||||||||||||||||||||||||
| 75 | FIFO, a device) the program stops with C<Refusing to use 'cpanfile': it is | ||||||||||||||||||||||||||||
| 76 | not a regular file> and a non-zero exit status. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =item * If F<cpanfile> is a symbolic link the program stops with | ||||||||||||||||||||||||||||
| 79 | C<Refusing to use 'cpanfile': it is a symbolic link> and a non-zero exit | ||||||||||||||||||||||||||||
| 80 | status, without reading or writing it. Writing would otherwise replace | ||||||||||||||||||||||||||||
| 81 | whatever file the link points to, which in a cloned repository could be | ||||||||||||||||||||||||||||
| 82 | any file of yours. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =back | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 56 | my ($with_dev, $no_dev, $dry, $diff, $check, $help); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 56 | ▶ | GetOptions( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 'with-develop' => \$with_dev, | ||||||||||||||||||||||||||||
| 92 | 'no-develop' => \$no_dev, | ||||||||||||||||||||||||||||
| 93 | 'dry-run' => \$dry, | ||||||||||||||||||||||||||||
| 94 | 'diff' => \$diff, | ||||||||||||||||||||||||||||
| 95 | 'check' => \$check, | ||||||||||||||||||||||||||||
| 96 | 'help' => \$help, | ||||||||||||||||||||||||||||
| 97 | ) or _usage(); | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 56 | ▶ | _usage() if $help; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 55 | ▶ | die "--with-develop and --no-develop are mutually exclusive\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$with_dev and $no_dev
MC/DC100%2/2
$with_dev and $no_dev
$with_dev
$no_dev
Truth table100%3/3
A and B
A= $with_devB= $no_dev
| |||||||||||||||||||||||||||||
| 102 | if $with_dev && $no_dev; | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 53 | ▶ | $with_dev //= !$no_dev; | ||||||||||||||||||||||||||
|
Condition100%2/2
$with_dev //= !$no_dev
MC/DC100%1/1
$with_dev //= !$no_dev
$with_dev
Truth table100%2/2
A
A= $with_dev
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # Read existing cpanfile so its develop block can be preserved during merge. | ||||||||||||||||||||||||||||
| 107 | 53 | my $cpanfile_path = path('cpanfile'); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # SECURITY: a repository controls its own files, symlinks included, and | ||||||||||||||||||||||||||||
| 110 | # Path::Tiny's spew follows a symlink and replaces its target. A 'cpanfile' | ||||||||||||||||||||||||||||
| 111 | # pointing at, say, ~/.bashrc would be overwritten with generated text. | ||||||||||||||||||||||||||||
| 112 | # Refuse to read or write through a symlink; checked again before writing. | ||||||||||||||||||||||||||||
| 113 | 53 | my $symlink_refusal = "Refusing to use '$cpanfile_path': it is a symbolic link"; | |||||||||||||||||||||||||||
| 114 | 53 | ▶ | croak $symlink_refusal if -l $cpanfile_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Anything else in the way that is not a plain file (a directory, a FIFO, a | ||||||||||||||||||||||||||||
| 117 | # device) is refused too: File::Copy::move, used below, would otherwise move | ||||||||||||||||||||||||||||
| 118 | # the new file INTO a directory named cpanfile and report success. | ||||||||||||||||||||||||||||
| 119 | 50 | my $irregular_refusal = "Refusing to use '$cpanfile_path': it is not a regular file"; | |||||||||||||||||||||||||||
| 120 | 50 | ▶ | croak $irregular_refusal if -e $cpanfile_path && !-f _; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
-e $cpanfile_path and not -f _
MC/DC100%2/2
-e $cpanfile_path and not -f _
-e $cpanfile_path
not -f _
Truth table100%3/3
A and not(B)
A= -e $cpanfile_pathB= not -f _
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 49 | ▶ | my $existing = $cpanfile_path->is_file ? $cpanfile_path->slurp_utf8 : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Read Makefile.PL once: the same text is converted and, with --check, | ||||||||||||||||||||||||||||
| 125 | # verified, so the check can never compare against a different version of | ||||||||||||||||||||||||||||
| 126 | # the file. | ||||||||||||||||||||||||||||
| 127 | 49 | my $content = App::makefilepl2cpanfile::read_makefile('Makefile.PL'); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 49 | my $out = App::makefilepl2cpanfile::generate( | |||||||||||||||||||||||||||
| 130 | content => $content, | ||||||||||||||||||||||||||||
| 131 | existing => $existing, | ||||||||||||||||||||||||||||
| 132 | with_develop => $with_dev, | ||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # Exit status: 1 when --check finds missing modules (so CI can fail on it), | ||||||||||||||||||||||||||||
| 136 | # otherwise 0. The requested output is still produced either way. | ||||||||||||||||||||||||||||
| 137 | 48 | my $status = 0; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # --check: reuse the library's parser to verify nothing was dropped. | ||||||||||||||||||||||||||||
| 140 | # This avoids duplicating the extraction regex in the script. | ||||||||||||||||||||||||||||
| 141 | 48 | ▶ | if ($check) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 5 | my $expected = App::makefilepl2cpanfile::parse_prereqs($content); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # Collect the module named on every dependency line in one pass, then | ||||||||||||||||||||||||||||
| 145 | # look each expected module up. Searching the output once per module | ||||||||||||||||||||||||||||
| 146 | # with /\bModule\b/ was O(modules x output), and wrong: '\b' treats | ||||||||||||||||||||||||||||
| 147 | # '::' as a boundary, so 'Moo' was "found" inside 'Moo::Role'. | ||||||||||||||||||||||||||||
| 148 | 5 | my %present = map { $_ => 1 } $out =~ / | |||||||||||||||||||||||||||
| 149 | ^ [ \t]* | ||||||||||||||||||||||||||||
| 150 | (?: requires | recommends | suggests | conflicts ) [ \t]+ | ||||||||||||||||||||||||||||
| 151 | '([^'\n]++)' # the module name | ||||||||||||||||||||||||||||
| 152 | /mgx; | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 5 | my @missing; | |||||||||||||||||||||||||||
| 155 | 5 | for my $phase (keys %{$expected}) { | |||||||||||||||||||||||||||
| 156 | 10 | for my $rel (keys %{ $expected->{$phase} }) { | |||||||||||||||||||||||||||
| 157 | 14 | push @missing, grep { !$present{$_} } sort keys %{ $expected->{$phase}{$rel} }; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 5 | ▶ | if (@missing) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | warn "WARNING: The following modules from Makefile.PL did not appear in the output:\n"; | |||||||||||||||||||||||||||
| 163 | 0 | warn " $_\n" for @missing; | |||||||||||||||||||||||||||
| 164 | 0 | $status = 1; | |||||||||||||||||||||||||||
| 165 | } else { | ||||||||||||||||||||||||||||
| 166 | 5 | print "All Makefile.PL prerequisites are present in the output.\n"; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # --diff: show what would change, then exit without writing. With no | ||||||||||||||||||||||||||||
| 171 | # existing cpanfile every generated line appears as an addition. | ||||||||||||||||||||||||||||
| 172 | 48 | ▶ | if ($diff) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 5 | print diff(\$existing, \$out, { STYLE => 'Unified' }); | |||||||||||||||||||||||||||
| 174 | 5 | exit $status; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | # --dry-run: print to STDOUT instead of writing to disk. | ||||||||||||||||||||||||||||
| 178 | 43 | ▶ | if ($dry) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 10 | print $out; | |||||||||||||||||||||||||||
| 180 | 10 | exit $status; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 33 | ▶ | croak $symlink_refusal if -l $cpanfile_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 33 | ▶ | croak $irregular_refusal if -e $cpanfile_path && !-f _; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-e $cpanfile_path and not -f _
MC/DC0%0/2
-e $cpanfile_path and not -f _
-e $cpanfile_path
not -f _
Truth table66%2/3
A and not(B)
A= -e $cpanfile_pathB= not -f _
| |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | # Write atomically and leave no debris. The text goes to a temporary file | ||||||||||||||||||||||||||||
| 187 | # in this directory, renamed over cpanfile only once it is complete, so a | ||||||||||||||||||||||||||||
| 188 | # failed run leaves the previous cpanfile exactly as it was. Path::Tiny's | ||||||||||||||||||||||||||||
| 189 | # spew does the same but leaves its temporary file behind when the write or | ||||||||||||||||||||||||||||
| 190 | # rename fails; this one belongs to a File::Temp object and is removed when | ||||||||||||||||||||||||||||
| 191 | # $tmp goes out of scope, whatever happened. rename() replaces the | ||||||||||||||||||||||||||||
| 192 | # directory entry itself, so it never follows a symlink either. | ||||||||||||||||||||||||||||
| 193 | { | ||||||||||||||||||||||||||||
| 194 | 33 | my $tmp = Path::Tiny->tempfile(TEMPLATE => '.cpanfile-XXXXXXXX', DIR => '.'); | |||||||||||||||||||||||||||
| 195 | 32 | my $fh = $tmp->filehandle('>', ':raw:encoding(UTF-8)'); | |||||||||||||||||||||||||||
| 196 | 32 | ▶ | print {$fh} $out or croak "Cannot write '$tmp': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 32 | close $fh; # autodie: reports a failed flush, e.g. disk full | |||||||||||||||||||||||||||
| 198 | 32 | chmod 0666 & ~umask, "$tmp"; # the permissions a new file would get | |||||||||||||||||||||||||||
| 199 | 32 | $tmp->move("$cpanfile_path"); | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | 32 | print "cpanfile written successfully.\n"; | |||||||||||||||||||||||||||
| 202 | 32 | exit $status; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 1 | ▶ | sub _usage { | ||||||||||||||||||||||||||
|
Subroutine
_usage: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 205 | 1 | print <<"END_USAGE"; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Usage: $0 [options] | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | --with-develop Include author/development dependencies (default) | ||||||||||||||||||||||||||||
| 210 | --no-develop Exclude author/development dependencies | ||||||||||||||||||||||||||||
| 211 | --dry-run Print output instead of writing cpanfile | ||||||||||||||||||||||||||||
| 212 | --diff Show diff against existing cpanfile (writes nothing) | ||||||||||||||||||||||||||||
| 213 | --check Verify all Makefile.PL dependencies appear in output | ||||||||||||||||||||||||||||
| 214 | --help Show this help message | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | END_USAGE | ||||||||||||||||||||||||||||
| 217 | 1 | exit 0; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | __END__ | ||||||||||||||||||||||||||||