| 1 | |||||||||||
| 2 | =head1 NAME | ||||||||||
| 3 | |||||||||||
| 4 | File::Corresponding::File::Profile - The definition of what matches | ||||||||||
| 5 | and translates to corresponding files | ||||||||||
| 6 | |||||||||||
| 7 | =cut | ||||||||||
| 8 | |||||||||||
| 9 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | package File::Corresponding::File::Profile; | ||||||||||
| 11 | 6 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 6 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 6 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 6 | ▶ | use Path::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 6 | ▶ | use File::Corresponding::File::Found; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | =head1 PROPERTIES | ||||||||||
| 22 | |||||||||||
| 23 | =head2 name | ||||||||||
| 24 | |||||||||||
| 25 | Name/description of this file profile. | ||||||||||
| 26 | |||||||||||
| 27 | =cut | ||||||||||
| 28 | 6 | has 'name' => (is => 'ro', isa => 'Str', default => ""); | |||||||||
| 29 | |||||||||||
| 30 | |||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | =head2 sprintf | ||||||||||
| 34 | |||||||||||
| 35 | sprintf string to construct a file name. It should contain at least | ||||||||||
| 36 | one % command to insert a relative file name. | ||||||||||
| 37 | |||||||||||
| 38 | Only used if defined. | ||||||||||
| 39 | |||||||||||
| 40 | =cut | ||||||||||
| 41 | 6 | has 'sprintf' => (is => 'ro', isa => 'Maybe[Str]'); | |||||||||
| 42 | |||||||||||
| 43 | |||||||||||
| 44 | |||||||||||
| 45 | =head2 regex : RegexRef | ||||||||||
| 46 | |||||||||||
| 47 | Regex matching a file. The first capture parens are used to extract | ||||||||||
| 48 | the local file name. | ||||||||||
| 49 | |||||||||||
| 50 | If coerced from a string, define as qr$regex, i.e. specify the | ||||||||||
| 51 | delimiters and any needed flags. | ||||||||||
| 52 | |||||||||||
| 53 | =cut | ||||||||||
| 54 | |||||||||||
| 55 | subtype RegexRef | ||||||||||
| 56 | => as RegexpRef | ||||||||||
| 57 | 34 | ▶ | => where { ref($_) eq "Regexp" }; #print "JPL: where: ($_) (" . ref($_) . ")\n"; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 58 | coerce RegexRef | ||||||||||
| 59 | => from 'Str' | ||||||||||
| 60 | 6 | ▶ | => via { regex_from_qr($_) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 61 | |||||||||||
| 62 | 6 | has 'regex' => ( | |||||||||
| 63 | is => 'rw', | ||||||||||
| 64 | isa => 'RegexRef', | ||||||||||
| 65 | coerce => 1, | ||||||||||
| 66 | required => 1, | ||||||||||
| 67 | ); | ||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | |||||||||||
| 71 | =head1 METHODS | ||||||||||
| 72 | |||||||||||
| 73 | =head2 matching_file_fragment($file) : ($file_base, $file_fragment) | () | ||||||||||
| 74 | |||||||||||
| 75 | Return two item list with (the base filename, the captured file name | ||||||||||
| 76 | fragment) from matching $file against regex, or () if nothing matched. | ||||||||||
| 77 | |||||||||||
| 78 | The $file_base is the $file, but with the whole matching regex | ||||||||||
| 79 | removed, forming the basis for looking up corresponding files. | ||||||||||
| 80 | |||||||||||
| 81 | =cut | ||||||||||
| 82 | |||||||||||
| 83 | sub matching_file_fragment { | ||||||||||
| 84 | 17 | ▶ | my $self = shift; | ||||||||
|
Subroutine
matching_file_fragment: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 85 | 17 | my ($file) = @_; | |||||||||
| 86 | 17 | my $regex = $self->regex; | |||||||||
| 87 | |||||||||||
| 88 | 17 | my $file_base = $file; | |||||||||
| 89 | 17 | ▶ | $file_base =~ s/$regex// and return ($file_base, $1); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | |||||||||||
| 91 | 8 | return (); | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | |||||||||||
| 96 | =head2 new_found_if_file_exists($matching_profile, $file_base, $fragment) : File::Found | () | ||||||||||
| 97 | |||||||||||
| 98 | Return a new File::Corresponding::File::Found object if a file made up | ||||||||||
| 99 | of $file_base, this profile, and $fragment exists in the filesystem. | ||||||||||
| 100 | |||||||||||
| 101 | If not, return (). | ||||||||||
| 102 | |||||||||||
| 103 | =cut | ||||||||||
| 104 | |||||||||||
| 105 | sub new_found_if_file_exists { | ||||||||||
| 106 | 16 | ▶ | my $self = shift; | ||||||||
|
Subroutine
new_found_if_file_exists: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 107 | 16 | my ($matching_profile, $file_base, $fragment) = @_; | |||||||||
| 108 | 16 | ▶ | my $sprintf = $self->sprintf or return (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | |||||||||||
| 110 | 15 | my $file = file($file_base, sprintf($sprintf, $fragment)); | |||||||||
| 111 | |||||||||||
| 112 | 15 | ▶ | -e $file or return (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | |||||||||||
| 114 | 11 | return File::Corresponding::File::Found->new({ | |||||||||
| 115 | file => $file, | ||||||||||
| 116 | matching_profile => $matching_profile, | ||||||||||
| 117 | found_profile => $self, | ||||||||||
| 118 | }); | ||||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | |||||||||||
| 122 | |||||||||||
| 123 | =head1 SUBROUTINES | ||||||||||
| 124 | |||||||||||
| 125 | =head2 rex_from_qr($rex_string) : RegexRef | ||||||||||
| 126 | |||||||||||
| 127 | Convert $rex_string to a proper Regex ref, or die with a useful error | ||||||||||
| 128 | message. | ||||||||||
| 129 | |||||||||||
| 130 | =cut | ||||||||||
| 131 | sub regex_from_qr { | ||||||||||
| 132 | 6 | ▶ | my ($rex_string) = @_; | ||||||||
|
Subroutine
regex_from_qr: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 133 | 6 | my $rex = eval "qr $rex_string"; | |||||||||
| 134 | 6 | ▶ | $@ and die("Could not parse regexp ($rex_string): | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | $@ | ||||||||||
| 136 | Correct regex syntax is e.g. '/ prove [.] bat /x' | ||||||||||
| 137 | "); | ||||||||||
| 138 | 6 | return $rex; | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | |||||||||||
| 142 | |||||||||||
| 143 | 6 | 1; | |||||||||
| 144 | |||||||||||
| 145 | |||||||||||
| 146 | |||||||||||
| 147 | __END__ | ||||||||||