| 1 | package Beam::Runner::Util; | ||||||||||||||||||||
| 2 | 8 | our $VERSION = '0.016'; | |||||||||||||||||||
| 3 | # ABSTRACT: Utilities for Beam::Runner command classes | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||||||||||||
| 6 | #pod | ||||||||||||||||||||
| 7 | #pod use Beam::Runner::Util qw( find_container_path ); | ||||||||||||||||||||
| 8 | #pod | ||||||||||||||||||||
| 9 | #pod my $path = find_container_path( $container_name ); | ||||||||||||||||||||
| 10 | #pod | ||||||||||||||||||||
| 11 | #pod =head1 DESCRIPTION | ||||||||||||||||||||
| 12 | #pod | ||||||||||||||||||||
| 13 | #pod This module has some shared utility functions for creating | ||||||||||||||||||||
| 14 | #pod L<Beam::Runner::Command> classes. | ||||||||||||||||||||
| 15 | #pod | ||||||||||||||||||||
| 16 | #pod =head1 SEE ALSO | ||||||||||||||||||||
| 17 | #pod | ||||||||||||||||||||
| 18 | #pod L<Beam::Runner>, L<beam>, L<Exporter> | ||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||
| 20 | #pod =cut | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 8 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 8 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 8 | ▶ | use Exporter 'import'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | 8 | ▶ | use Path::Tiny qw( path ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 8 | our @EXPORT_OK = qw( find_container_path find_containers ); | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | # File extensions to try to find, starting with no extension (which is | ||||||||||||||||||||
| 30 | # to say the extension is given by the user's input) | ||||||||||||||||||||
| 31 | 8 | our @EXTS = ( "", qw( .yml .yaml .json .xml .pl ) ); | |||||||||||||||||||
| 32 | # A regex to use to remove the container's name | ||||||||||||||||||||
| 33 | 8 | my $EXT_RE = qr/(?:@{[ join '|', @EXTS ]})$/; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # The "BEAM_PATH" separator value. Windows uses ';' to separate | ||||||||||||||||||||
| 36 | # PATH-like variables, everything else uses ':' | ||||||||||||||||||||
| 37 | 8 | ▶ | our $PATHS_SEP = $^O eq 'MSWin32' ? ';' : ':'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | #pod =sub find_containers | ||||||||||||||||||||
| 40 | #pod | ||||||||||||||||||||
| 41 | #pod my %container = find_containers(); | ||||||||||||||||||||
| 42 | #pod | ||||||||||||||||||||
| 43 | #pod Returns a list of C<name> and C<path> pairs pointing to all the containers | ||||||||||||||||||||
| 44 | #pod in the C<BEAM_PATH> paths. | ||||||||||||||||||||
| 45 | #pod | ||||||||||||||||||||
| 46 | #pod =cut | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 3 | ▶ | sub find_containers { | ||||||||||||||||||
|
Subroutine
find_containers: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||||||||||||
| 49 | 3 | my %containers; | |||||||||||||||||||
| 50 | 3 | ▶ | for my $dir ( split /$PATHS_SEP/, $ENV{BEAM_PATH} // '' ) { | ||||||||||||||||||
|
Condition100%2/2
$ENV{"BEAM_PATH"} // ""
MC/DC100%1/1
$ENV{"BEAM_PATH"} // ""
$ENV{"BEAM_PATH"}
Truth table100%2/2
A
A= $ENV{"BEAM_PATH"}
| |||||||||||||||||||||
| 51 | 2 | my $p = path( $dir ); | |||||||||||||||||||
| 52 | 2 | my $i = $p->iterator( { recurse => 1, follow_symlinks => 1 } ); | |||||||||||||||||||
| 53 | 2 | ▶ | while ( my $file = $i->() ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 6 | ▶ | next unless $file->is_file; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 6 | ▶ | next unless $file =~ $EXT_RE; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 6 | my $name = $file->relative( $p ); | |||||||||||||||||||
| 57 | 6 | $name =~ s/$EXT_RE//; | |||||||||||||||||||
| 58 | 6 | ▶ | $containers{ $name } ||= $file; | ||||||||||||||||||
|
Condition33%1/3
$containers{$name} ||= $file
MC/DC0%0/2
$containers{$name} ||= $file
$containers{$name}
$file
Truth table33%1/3
A ||= B
A= $containers{$name}B= $file
| |||||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | 3 | return %containers; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | #pod =sub find_container_path | ||||||||||||||||||||
| 65 | #pod | ||||||||||||||||||||
| 66 | #pod my $path = find_container_path( $container_name ); | ||||||||||||||||||||
| 67 | #pod | ||||||||||||||||||||
| 68 | #pod Find the path to the given container. If the given container is already | ||||||||||||||||||||
| 69 | #pod an absolute path, it is simply returned. Otherwise, the container is | ||||||||||||||||||||
| 70 | #pod searched for in the directories defined by the C<BEAM_PATH> environment | ||||||||||||||||||||
| 71 | #pod variable. | ||||||||||||||||||||
| 72 | #pod | ||||||||||||||||||||
| 73 | #pod If the container cannot be found, throws an exception with a user-friendly | ||||||||||||||||||||
| 74 | #pod error message. | ||||||||||||||||||||
| 75 | #pod | ||||||||||||||||||||
| 76 | #pod =cut | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 15 | ▶ | sub find_container_path { | ||||||||||||||||||
|
Subroutine
find_container_path: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||
| 79 | 15 | my ( $container ) = @_; | |||||||||||||||||||
| 80 | 15 | my $path; | |||||||||||||||||||
| 81 | 15 | ▶ | if ( path( $container )->is_file ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 7 | return path( $container ); | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 8 | my @dirs = ( "." ); | |||||||||||||||||||
| 86 | 8 | ▶ | if ( $ENV{BEAM_PATH} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | 6 | push @dirs, split /$PATHS_SEP/, $ENV{BEAM_PATH}; | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 8 | DIR: for my $dir ( @dirs ) { | |||||||||||||||||||
| 91 | 14 | my $d = path( $dir ); | |||||||||||||||||||
| 92 | 14 | for my $ext ( @EXTS ) { | |||||||||||||||||||
| 93 | 60 | my $f = $d->child( $container . $ext ); | |||||||||||||||||||
| 94 | 60 | ▶ | if ( $f->exists ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 6 | $path = $f; | |||||||||||||||||||
| 96 | 6 | last DIR; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 8 | ▶ | die sprintf qq{Could not find container "%s" in directories: %s\n}, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | $container, join( $PATHS_SEP, @dirs ) | ||||||||||||||||||||
| 103 | unless $path; | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 6 | return $path; | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 8 | 1; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||