File Coverage

blib/lib/Build/Hopen/AppUtil.pm
Criterion Covered Total %
statement 40 48 83.3
branch 6 22 27.2
condition n/a
subroutine 9 14 64.2
pod 2 2 100.0
total 57 86 66.2


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