File Coverage

blib/lib/Beam/Runner/Util.pm
Criterion Covered Total %
statement 39 39 100.0
branch 10 12 83.3
condition 3 5 60.0
subroutine 6 6 100.0
pod 2 2 100.0
total 60 64 93.7


line stmt bran cond sub pod time code
1             package Beam::Runner::Util;
2             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 classes.
15             #pod
16             #pod =head1 SEE ALSO
17             #pod
18             #pod L, L, L
19             #pod
20             #pod =cut
21              
22 4     4   71292 use strict;
  4         18  
  4         120  
23 4     4   20 use warnings;
  4         8  
  4         116  
24 4     4   24 use Exporter 'import';
  4         7  
  4         183  
25 4     4   805 use Path::Tiny qw( path );
  4         13403  
  4         2057  
26              
27             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             our @EXTS = ( "", qw( .yml .yaml .json .xml .pl ) );
32             # A regex to use to remove the container's name
33             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             our $PATHS_SEP = $^O eq 'MSWin32' ? ';' : ':';
38              
39             #pod =sub find_containers
40             #pod
41             #pod my %container = find_containers();
42             #pod
43             #pod Returns a list of C and C pairs pointing to all the containers
44             #pod in the C paths.
45             #pod
46             #pod =cut
47              
48             sub find_containers {
49 3     3 1 3919 my %containers;
50 3   100     42 for my $dir ( split /$PATHS_SEP/, $ENV{BEAM_PATH} // '' ) {
51 2         14 my $p = path( $dir );
52 2         90 my $i = $p->iterator( { recurse => 1, follow_symlinks => 1 } );
53 2         75 while ( my $file = $i->() ) {
54 6 50       750 next unless $file->is_file;
55 6 50       125 next unless $file =~ $EXT_RE;
56 6         63 my $name = $file->relative( $p );
57 6         1244 $name =~ s/$EXT_RE//;
58 6   33     136 $containers{ $name } ||= $file;
59             }
60             }
61 3         132 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 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             sub find_container_path {
79 15     15 1 37 my ( $container ) = @_;
80 15         26 my $path;
81 15 100       47 if ( path( $container )->is_file ) {
82 7         369 return path( $container );
83             }
84              
85 8         431 my @dirs = ( "." );
86 8 100       49 if ( $ENV{BEAM_PATH} ) {
87 6         62 push @dirs, split /$PATHS_SEP/, $ENV{BEAM_PATH};
88             }
89              
90 8         27 DIR: for my $dir ( @dirs ) {
91 14         111 my $d = path( $dir );
92 14         364 for my $ext ( @EXTS ) {
93 60         827 my $f = $d->child( $container . $ext );
94 60 100       1970 if ( $f->exists ) {
95 6         152 $path = $f;
96 6         25 last DIR;
97             }
98             }
99             }
100              
101 8 100       97 die sprintf qq{Could not find container "%s" in directories: %s\n},
102             $container, join( $PATHS_SEP, @dirs )
103             unless $path;
104              
105 6         23 return $path;
106             }
107              
108             1;
109              
110             __END__