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