File Coverage

blib/lib/App/hopen/AppUtil.pm
Criterion Covered Total %
statement 50 54 92.5
branch 13 24 54.1
condition n/a
subroutine 12 16 75.0
pod 2 2 100.0
total 77 96 80.2


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   139949 use Data::Hopen qw(:default isMYH MYH);
  3         34730  
  3         372  
4 3     3   24 use strict; use warnings;
  3     3   6  
  3         71  
  3         15  
  3         6  
  3         76  
5 3     3   14 use Data::Hopen::Base;
  3         6  
  3         18  
6 3     3   3961 use parent 'Exporter';
  3         6  
  3         19  
7              
8             our $VERSION = '0.000012'; # TRIAL
9              
10             our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
11             BEGIN {
12 3     3   378 @EXPORT = qw();
13 3         9 @EXPORT_OK = qw(find_hopen_files find_myhopen);
14 3         113 %EXPORT_TAGS = (
15             default => [@EXPORT],
16             all => [@EXPORT, @EXPORT_OK]
17             );
18             }
19              
20 3     3   19 use Cwd qw(getcwd abs_path);
  3         6  
  3         229  
21 3 50   3   21 use File::Glob $] lt '5.016' ? ':glob' : ':bsd_glob';
  3         5  
  3         629  
22             # Thanks to haukex, https://www.perlmonks.org/?node_id=1207115 -
23             # 5.14 doesn't support the ':bsd_glob' tag.
24 3     3   474 use Path::Class;
  3         37917  
  3         1646  
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 993 my $proj_dir = @_ ? dir($_[0]) : dir;
62 5 100       209 my $dest_dir = dir($_[1]) if @_>=2;
63 5         199 my $ignore_MY_hopen = $_[2];
64              
65 5     10   40 local *d = sub { $proj_dir->file(shift) };
  10         1008  
66              
67 5     0   43 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       56 grep { !isMYH && -r } (
  6         684  
72             bsd_glob(d('*.hopen.pl'), GLOB_NOSORT),
73             bsd_glob(d('.hopen.pl'), GLOB_NOSORT),
74             )
75             );
76 5 0   0   239 hlog { 'Candidates:', @candidates ? @candidates : 'none' };
  0         0  
77 5 50       68 @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         28 my $parent = $proj_dir->parent;
83 5 50       557 if($parent ne $proj_dir) { # E.g., not root dir
84 5         160 my $me = $proj_dir->absolute->basename;
85             # Absolute because dir might be `.`.
86 5         579 my $context_file = $parent->file("$me.hopen.pl");
87 5 100       340 if(-r $context_file) {
88 1         63 push @candidates, $context_file;
89 1     0   10 hlog { 'Context file', $context_file };
  0         0  
90             }
91             }
92              
93 0 0   0   0 hlog { @candidates ? ('Using hopen files', @candidates) :
94 5         221 'No hopen files found on disk' };
95 5         69 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 16 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         20 my $fn = $dest_dir->file(MYH);
110 2 50       141 return $fn if -r $fn;
111             } #find_myhopen
112              
113             1;
114             __END__
115             # vi: set fdm=marker: #