| 1 | package ExtUtils::Command::MM; | ||
| 2 | |||
| 3 | require 5.006; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | require Exporter; | ||
| 9 | our @ISA = qw(Exporter); | ||
| 10 | |||
| 11 | our @EXPORT = qw(test_harness pod2man perllocal_install uninstall | ||
| 12 | warn_if_old_packlist test_s cp_nonempty); | ||
| 13 | our $VERSION = '7.13_01'; | ||
| 14 | $VERSION = eval $VERSION; | ||
| 15 | |||
| 16 | my $Is_VMS = $^O eq 'VMS'; | ||
| 17 | |||
| 18 | eval { require Time::HiRes; die unless Time::HiRes->can("stat"); }; | ||
| 19 | *mtime = $@ ? | ||
| 20 | sub { [ stat($_[0])]->[9] } : | ||
| 21 | sub { [Time::HiRes::stat($_[0])]->[9] } ; | ||
| 22 | |||
| 23 | =head1 NAME | ||
| 24 | |||
| 25 | ExtUtils::Command::MM - Commands for the MM's to use in Makefiles | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | perl "-MExtUtils::Command::MM" -e "function" "--" arguments... | ||
| 30 | |||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | B<FOR INTERNAL USE ONLY!> The interface is not stable. | ||
| 35 | |||
| 36 | ExtUtils::Command::MM encapsulates code which would otherwise have to | ||
| 37 | be done with large "one" liners. | ||
| 38 | |||
| 39 | Any $(FOO) used in the examples are make variables, not Perl. | ||
| 40 | |||
| 41 | =over 4 | ||
| 42 | |||
| 43 | =item B<test_harness> | ||
| 44 | |||
| 45 | test_harness($verbose, @test_libs); | ||
| 46 | |||
| 47 | Runs the tests on @ARGV via Test::Harness passing through the $verbose | ||
| 48 | flag. Any @test_libs will be unshifted onto the test's @INC. | ||
| 49 | |||
| 50 | @test_libs are run in alphabetical order. | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub test_harness { | ||
| 55 | require Test::Harness; | ||
| 56 | require File::Spec; | ||
| 57 | |||
| 58 | $Test::Harness::verbose = shift; | ||
| 59 | |||
| 60 | # Because Windows doesn't do this for us and listing all the *.t files | ||
| 61 | # out on the command line can blow over its exec limit. | ||
| 62 | require ExtUtils::Command; | ||
| 63 | my @argv = ExtUtils::Command::expand_wildcards(@ARGV); | ||
| 64 | |||
| 65 | local @INC = @INC; | ||
| 66 | unshift @INC, map { File::Spec->rel2abs($_) } @_; | ||
| 67 | Test::Harness::runtests(sort { lc $a cmp lc $b } @argv); | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | =item B<pod2man> | ||
| 73 | |||
| 74 | pod2man( '--option=value', | ||
| 75 | $podfile1 => $manpage1, | ||
| 76 | $podfile2 => $manpage2, | ||
| 77 | ... | ||
| 78 | ); | ||
| 79 | |||
| 80 | # or args on @ARGV | ||
| 81 | |||
| 82 | pod2man() is a function performing most of the duties of the pod2man | ||
| 83 | program. Its arguments are exactly the same as pod2man as of 5.8.0 | ||
| 84 | with the addition of: | ||
| 85 | |||
| 86 | --perm_rw octal permission to set the resulting manpage to | ||
| 87 | |||
| 88 | And the removal of: | ||
| 89 | |||
| 90 | --verbose/-v | ||
| 91 | --help/-h | ||
| 92 | |||
| 93 | If no arguments are given to pod2man it will read from @ARGV. | ||
| 94 | |||
| 95 | If Pod::Man is unavailable, this function will warn and return undef. | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | sub pod2man { | ||
| 100 | local @ARGV = @_ ? @_ : @ARGV; | ||
| 101 | |||
| 102 | { | ||
| 103 | local $@; | ||
| 104 | if( !eval { require Pod::Man } ) { | ||
| 105 | warn "Pod::Man is not available: $@". | ||
| 106 | "Man pages will not be generated during this install.\n"; | ||
| 107 | return 0; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | require Getopt::Long; | ||
| 111 | |||
| 112 | # We will cheat and just use Getopt::Long. We fool it by putting | ||
| 113 | # our arguments into @ARGV. Should be safe. | ||
| 114 | my %options = (); | ||
| 115 | Getopt::Long::config ('bundling_override'); | ||
| 116 | Getopt::Long::GetOptions (\%options, | ||
| 117 | 'section|s=s', 'release|r=s', 'center|c=s', | ||
| 118 | 'date|d=s', 'fixed=s', 'fixedbold=s', 'fixeditalic=s', | ||
| 119 | 'fixedbolditalic=s', 'official|o', 'quotes|q=s', 'lax|l', | ||
| 120 | 'name|n=s', 'perm_rw=i', 'utf8|u' | ||
| 121 | ); | ||
| 122 | delete $options{utf8} unless $Pod::Man::VERSION >= 2.17; | ||
| 123 | |||
| 124 | # If there's no files, don't bother going further. | ||
| 125 | return 0 unless @ARGV; | ||
| 126 | |||
| 127 | # Official sets --center, but don't override things explicitly set. | ||
| 128 | if ($options{official} && !defined $options{center}) { | ||
| 129 | $options{center} = q[Perl Programmer's Reference Guide]; | ||
| 130 | } | ||
| 131 | |||
| 132 | # This isn't a valid Pod::Man option and is only accepted for backwards | ||
| 133 | # compatibility. | ||
| 134 | delete $options{lax}; | ||
| 135 | my $count = scalar @ARGV / 2; | ||
| 136 | my $plural = $count == 1 ? 'document' : 'documents'; | ||
| 137 | print "Manifying $count pod $plural\n"; | ||
| 138 | |||
| 139 | do {{ # so 'next' works | ||
| 140 | my ($pod, $man) = splice(@ARGV, 0, 2); | ||
| 141 | |||
| 142 | next if ((-e $man) && | ||
| 143 | (mtime($man) > mtime($pod)) && | ||
| 144 | (mtime($man) > mtime("Makefile"))); | ||
| 145 | |||
| 146 | my $parser = Pod::Man->new(%options); | ||
| 147 | $parser->parse_from_file($pod, $man) | ||
| 148 | or do { warn("Could not install $man\n"); next }; | ||
| 149 | |||
| 150 | if (exists $options{perm_rw}) { | ||
| 151 | chmod(oct($options{perm_rw}), $man) | ||
| 152 | or do { warn("chmod $options{perm_rw} $man: $!\n"); next }; | ||
| 153 | } | ||
| 154 | }} while @ARGV; | ||
| 155 | |||
| 156 | return 1; | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | =item B<warn_if_old_packlist> | ||
| 161 | |||
| 162 | perl "-MExtUtils::Command::MM" -e warn_if_old_packlist <somefile> | ||
| 163 | |||
| 164 | Displays a warning that an old packlist file was found. Reads the | ||
| 165 | filename from @ARGV. | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | sub warn_if_old_packlist { | ||
| 170 | my $packlist = $ARGV[0]; | ||
| 171 | |||
| 172 | return unless -f $packlist; | ||
| 173 | print <<"PACKLIST_WARNING"; | ||
| 174 | WARNING: I have found an old package in | ||
| 175 | $packlist. | ||
| 176 | Please make sure the two installations are not conflicting | ||
| 177 | PACKLIST_WARNING | ||
| 178 | |||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | =item B<perllocal_install> | ||
| 183 | |||
| 184 | perl "-MExtUtils::Command::MM" -e perllocal_install | ||
| 185 | <type> <module name> <key> <value> ... | ||
| 186 | |||
| 187 | # VMS only, key|value pairs come on STDIN | ||
| 188 | perl "-MExtUtils::Command::MM" -e perllocal_install | ||
| 189 | <type> <module name> < <key>|<value> ... | ||
| 190 | |||
| 191 | Prints a fragment of POD suitable for appending to perllocal.pod. | ||
| 192 | Arguments are read from @ARGV. | ||
| 193 | |||
| 194 | 'type' is the type of what you're installing. Usually 'Module'. | ||
| 195 | |||
| 196 | 'module name' is simply the name of your module. (Foo::Bar) | ||
| 197 | |||
| 198 | Key/value pairs are extra information about the module. Fields include: | ||
| 199 | |||
| 200 | installed into which directory your module was out into | ||
| 201 | LINKTYPE dynamic or static linking | ||
| 202 | VERSION module version number | ||
| 203 | EXE_FILES any executables installed in a space seperated | ||
| 204 | list | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | sub perllocal_install { | ||
| 209 | my($type, $name) = splice(@ARGV, 0, 2); | ||
| 210 | |||
| 211 | # VMS feeds args as a piped file on STDIN since it usually can't | ||
| 212 | # fit all the args on a single command line. | ||
| 213 | my @mod_info = $Is_VMS ? split /\|/, <STDIN> | ||
| 214 | : @ARGV; | ||
| 215 | |||
| 216 | my $pod; | ||
| 217 | $pod = sprintf <<'POD', scalar(localtime), $type, $name; | ||
| 218 | =head2 %s: C<%s> L<%3$s|%3$s> | ||
| 219 | |||
| 220 | =over 4 | ||
| 221 | |||
| 222 | POD | ||
| 223 | |||
| 224 | do { | ||
| 225 | my($key, $val) = splice(@mod_info, 0, 2); | ||
| 226 | |||
| 227 | $pod .= <<POD | ||
| 228 | =item * | ||
| 229 | |||
| 230 | C<$key: $val> | ||
| 231 | |||
| 232 | POD | ||
| 233 | |||
| 234 | } while(@mod_info); | ||
| 235 | |||
| 236 | $pod .= "=back\n\n"; | ||
| 237 | $pod =~ s/^ //mg; | ||
| 238 | print $pod; | ||
| 239 | |||
| 240 | return 1; | ||
| 241 | } | ||
| 242 | |||
| 243 | =item B<uninstall> | ||
| 244 | |||
| 245 | perl "-MExtUtils::Command::MM" -e uninstall <packlist> | ||
| 246 | |||
| 247 | A wrapper around ExtUtils::Install::uninstall(). Warns that | ||
| 248 | uninstallation is deprecated and doesn't actually perform the | ||
| 249 | uninstallation. | ||
| 250 | |||
| 251 | =cut | ||
| 252 | |||
| 253 | sub uninstall { | ||
| 254 | my($packlist) = shift @ARGV; | ||
| 255 | |||
| 256 | require ExtUtils::Install; | ||
| 257 | |||
| 258 | print <<'WARNING'; | ||
| 259 | |||
| 260 | Uninstall is unsafe and deprecated, the uninstallation was not performed. | ||
| 261 | We will show what would have been done. | ||
| 262 | |||
| 263 | WARNING | ||
| 264 | |||
| 265 | ExtUtils::Install::uninstall($packlist, 1, 1); | ||
| 266 | |||
| 267 | print <<'WARNING'; | ||
| 268 | |||
| 269 | Uninstall is unsafe and deprecated, the uninstallation was not performed. | ||
| 270 | Please check the list above carefully, there may be errors. | ||
| 271 | Remove the appropriate files manually. | ||
| 272 | Sorry for the inconvenience. | ||
| 273 | |||
| 274 | WARNING | ||
| 275 | |||
| 276 | } | ||
| 277 | |||
| 278 | =item B<test_s> | ||
| 279 | |||
| 280 | perl "-MExtUtils::Command::MM" -e test_s <file> | ||
| 281 | |||
| 282 | Tests if a file exists and is not empty (size > 0). | ||
| 283 | I<Exits> with 0 if it does, 1 if it does not. | ||
| 284 | |||
| 285 | =cut | ||
| 286 | |||
| 287 | sub test_s { | ||
| 288 | exit(-s $ARGV[0] ? 0 : 1); | ||
| 289 | } | ||
| 290 | |||
| 291 | =item B<cp_nonempty> | ||
| 292 | |||
| 293 | perl "-MExtUtils::Command::MM" -e cp_nonempty <srcfile> <dstfile> <perm> | ||
| 294 | |||
| 295 | Tests if the source file exists and is not empty (size > 0). If it is not empty | ||
| 296 | it copies it to the given destination with the given permissions. | ||
| 297 | |||
| 298 | =back | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | sub cp_nonempty { | ||
| 303 | my @args = @ARGV; | ||
| 304 | return 0 unless -s $args[0]; | ||
| 305 | require ExtUtils::Command; | ||
| 306 | { | ||
| 307 | local @ARGV = @args[0,1]; | ||
| 308 | ExtUtils::Command::cp(@ARGV); | ||
| 309 | } | ||
| 310 | { | ||
| 311 | local @ARGV = @args[2,1]; | ||
| 312 | ExtUtils::Command::chmod(@ARGV); | ||
| 313 | } | ||
| 314 | } | ||
| 315 | |||
| 316 | |||
| 317 | 1; |