File Coverage

blib/lib/App/hopen/AppUtil.pm
Criterion Covered Total %
statement 53 57 92.9
branch 13 24 54.1
condition n/a
subroutine 13 17 76.4
pod 2 2 100.0
total 81 100 81.0


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