File Coverage

blib/lib/App/hopen/AppUtil.pm
Criterion Covered Total %
statement 47 51 92.1
branch 13 24 54.1
condition n/a
subroutine 11 15 73.3
pod 2 2 100.0
total 73 92 79.3


line stmt bran cond sub pod time code
1             # App::hopen::AppUtil - utility routines used by App::hopen::App
2             package App::hopen::AppUtil;
3 3     3   139676 use Data::Hopen qw(:default isMYH MYH);
  3         35393  
  3         449  
4 3     3   29 use strict;
  3         6  
  3         75  
5 3     3   17 use Data::Hopen::Base;
  3         7  
  3         19  
6 3     3   4053 use parent 'Exporter';
  3         7  
  3         18  
7              
8             our $VERSION = '0.000011';
9              
10             our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
11             BEGIN {
12 3     3   402 @EXPORT = qw();
13 3         9 @EXPORT_OK = qw(find_hopen_files find_myhopen);
14 3         73 %EXPORT_TAGS = (
15             default => [@EXPORT],
16             all => [@EXPORT, @EXPORT_OK]
17             );
18             }
19              
20 3     3   19 use Cwd qw(getcwd abs_path);
  3         7  
  3         226  
21 3 50   3   22 use File::Glob $] lt '5.016' ? ':glob' : ':bsd_glob';
  3         6  
  3         689  
22             # Thanks to haukex, https://www.perlmonks.org/?node_id=1207115 -
23             # 5.14 doesn't support the ':bsd_glob' tag.
24 3     3   496 use Path::Class;
  3         37337  
  3         1556  
25              
26             # Docs {{{1
27              
28             =head1 NAME
29              
30             App::hopen::AppUtil - utility routines used by App::hopen
31              
32             =head1 FUNCTIONS
33              
34             # }}}1
35              
36             =head2 find_hopen_files
37              
38             Find the hopen files applicable to the given build directory.
39             Returns a list of hopen files, if any, to process for the given directory.
40             Hopen files match C<*.hopen.pl> or C<.hopen.pl>. Usage:
41             Also locates context files. For example, when processing C<~/foo/.hopen>,
42             Check will also find C<~/foo.hopen> if it exists.
43              
44             my $files_array = find_hopen_files(
45             [$proj_dir[, $dest_dir[, $ignore_MY_hopen]]])
46              
47             If no C<$proj_dir> is given, the current directory is used.
48              
49             If C<$ignore_MY_hopen> is truthy, C<$dest_dir> will not be checked for
50             a C<MY.hopen.pl> file.
51              
52             The returned files should be processed in left-to-right order.
53              
54             The return array will include a context file if any is present.
55             For C<$dir eq '/foo/bar'>, for example, C</foo/bar.hopen.pl> is the
56             name of the context file.
57              
58             =cut
59              
60             sub find_hopen_files {
61 5 50   5 1 932 my $proj_dir = @_ ? dir($_[0]) : dir;
62 5 100       209 my $dest_dir = dir($_[1]) if @_>=2;
63 5         139 my $ignore_MY_hopen = $_[2];
64              
65 5     10   38 local *d = sub { $proj_dir->file(shift) };
  10         999  
66              
67 5     0   50 hlog { 'Looking for hopen files in', $proj_dir->absolute };
  0         0  
68              
69             # Look for files that are included with the project
70             my @candidates = sort(
71 5 50       58 grep { !isMYH && -r } (
  6         709  
72             bsd_glob(d('*.hopen.pl'), GLOB_NOSORT),
73             bsd_glob(d('.hopen.pl'), GLOB_NOSORT),
74             )
75             );
76 5 0   0   237 hlog { 'Candidates:', @candidates ? @candidates : 'none' };
  0         0  
77 5 50       58 @candidates = $candidates[$#candidates] if @candidates;
78             # Only use the last one
79              
80             # Look in the parent dir for context files.
81             # The context file comes after the earlier candidate.
82 5         40 my $parent = $proj_dir->parent;
83 5 50       577 if($parent ne $proj_dir) { # E.g., not root dir
84 5         166 my $me = $proj_dir->absolute->basename;
85             # Absolute because dir might be `.`.
86 5         590 my $context_file = $parent->file("$me.hopen.pl");
87 5 100       389 if(-r $context_file) {
88 1         47 push @candidates, $context_file;
89 1     0   6 hlog { 'Context file', $context_file };
  0         0  
90             }
91             }
92              
93 0 0   0   0 hlog { @candidates ? ('Using hopen files', @candidates) :
94 5         241 'No hopen files found on disk' };
95 5         73 return [@candidates];
96             } #find_hopen_files()
97              
98             =head2 find_myhopen
99              
100             Find a C<MY.hopen.pl> file, if any. Returns undef if none is present.
101              
102             =cut
103              
104             sub find_myhopen {
105 4 100   4 1 19 return if $_[1]; # $ignore_MY_hopen
106 2 50       10 my $dest_dir = shift or return; # No dest dir => no MY.hopen.pl
107              
108             # Find $dest_dir/MY.hopen.pl, if there is one.
109 2         18 my $fn = $dest_dir->file(MYH);
110 2 50       145 return $fn if -r $fn;
111             } #find_myhopen
112              
113             1;
114             __END__
115             # vi: set fdm=marker: #