| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package App::perlmv::scriptlet::according_to_containing_dir; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
355273
|
use 5.010001; |
|
|
1
|
|
|
|
|
4
|
|
|
4
|
1
|
|
|
1
|
|
10
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
33
|
|
|
5
|
1
|
|
|
1
|
|
10
|
use warnings; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
577
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
|
8
|
|
|
|
|
|
|
our $DATE = '2023-10-26'; # DATE |
|
9
|
|
|
|
|
|
|
our $DIST = 'App-perlmv-scriptlet-according_to_containing_dir'; # DIST |
|
10
|
|
|
|
|
|
|
our $VERSION = '0.001'; # VERSION |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $SCRIPTLET = { |
|
13
|
|
|
|
|
|
|
summary => q[Rename file according to its containing directory's name, e.g. foo/1.txt to foo/foo.txt, or foo/somejpeg to foo/foo.jpg], |
|
14
|
|
|
|
|
|
|
description => <<'MARKDOWN', |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
In addition to renaming the file according to the name of its container |
|
17
|
|
|
|
|
|
|
directory, if the file does not have an extension yet, an extension will be |
|
18
|
|
|
|
|
|
|
given by guessing according to its MIME type using , |
|
19
|
|
|
|
|
|
|
similar to what the `add_extension_according_to_mime_type` scriptlet does. |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
MARKDOWN |
|
22
|
|
|
|
|
|
|
code => sub { |
|
23
|
|
|
|
|
|
|
package |
|
24
|
|
|
|
|
|
|
App::perlmv::code; |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# we skip directories |
|
27
|
|
|
|
|
|
|
if (-d $_) { |
|
28
|
|
|
|
|
|
|
warn "Directory '$_' skipped\n"; |
|
29
|
|
|
|
|
|
|
return; |
|
30
|
|
|
|
|
|
|
} |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# guess file extension |
|
33
|
|
|
|
|
|
|
my ($ext) = /\.(\w+)\z/; |
|
34
|
|
|
|
|
|
|
GUESS_EXT: { |
|
35
|
|
|
|
|
|
|
if (defined $ext) { |
|
36
|
|
|
|
|
|
|
warn "DEBUG: File '$_' already has extension '$ext', skipped guessing extension\n" if $ENV{DEBUG}; |
|
37
|
|
|
|
|
|
|
last; |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
require File::MimeInfo::Magic; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $arg; |
|
43
|
|
|
|
|
|
|
if (-l $_) { open my $fh, "<", $_ or do { warn "Can't open symlink $_: $!, skipped\n"; return }; $arg = $fh } else { $arg = $_ } |
|
44
|
|
|
|
|
|
|
my $type = File::MimeInfo::Magic::mimetype($arg); |
|
45
|
|
|
|
|
|
|
unless ($type) { |
|
46
|
|
|
|
|
|
|
warn "Can't get MIME type from file '$_', skipped guessing extension\n"; |
|
47
|
|
|
|
|
|
|
last; |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
my @exts = File::MimeInfo::Magic::extensions($type) or die "Bug! extensions() does not return extensions for type '$type'"; |
|
50
|
|
|
|
|
|
|
warn "DEBUG: extensions from extensions($type) for file '$_': ".join(", ", @exts)."\n" if $ENV{DEBUG}; |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
$ext = $exts[0]; |
|
53
|
|
|
|
|
|
|
} # GUESS_EXT |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# determine the container directory's name |
|
56
|
1
|
|
|
1
|
|
11
|
no warnings 'once'; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
243
|
|
|
57
|
|
|
|
|
|
|
my $dirname = $App::perlmv::code::DIR; |
|
58
|
|
|
|
|
|
|
$dirname =~ s!/\z!!; |
|
59
|
|
|
|
|
|
|
$dirname =~ s!.+/!!; |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
defined $ext ? "$dirname.$ext" : $dirname; |
|
62
|
|
|
|
|
|
|
}, |
|
63
|
|
|
|
|
|
|
}; |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
1; |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# ABSTRACT: Rename file according to its containing directory's name, e.g. foo/1.txt to foo/foo.txt, or foo/somejpeg to foo/foo.jpg |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
__END__ |