File Coverage

blib/lib/PLS/Util.pm
Criterion Covered Total %
statement 21 26 80.7
branch 5 12 41.6
condition 1 6 16.6
subroutine 3 3 100.0
pod 1 1 100.0
total 31 48 64.5


line stmt bran cond sub pod time code
1             package PLS::Util;
2              
3 13     13   97 use strict;
  13         24  
  13         620  
4 13     13   74 use warnings;
  13         136  
  13         5241  
5              
6             =head1 NAME
7              
8             PLS::Util
9              
10             =head1 DESCRIPTION
11              
12             Utility functions for PLS
13              
14             =head1 FUNCTIONS
15              
16             =head2 resolve_workspace_relative_path
17              
18             Given a path, potentially with ${workspaceFolder} or $ROOT_PATH,
19             returns a list of paths with those variables resolved to an actual workspace folder.
20             Additionally, any needed globbing will be performed.
21              
22             The returned list will only contain paths that exist.
23              
24             =head3 PARAMETERS
25              
26             =over
27              
28             =item path
29              
30             The path from user configuration that needs to be resolved.
31              
32             =item workspace_folders
33              
34             An array of the workspace folders. If empty or not passed, this list will
35             be queried from L<PLS::Parser::Index>.
36              
37             =item no_glob
38              
39             If true, no globbing will be performed against the path.
40              
41             =back
42              
43             =cut
44              
45             sub resolve_workspace_relative_path
46             {
47 4     4 1 57 my ($path, $workspace_folders, $no_glob) = @_;
48              
49 4 50       103 if (not length $path)
50             {
51 0         0 return;
52             }
53              
54 4 50 33     104 if (ref $workspace_folders ne 'ARRAY' or not scalar @{$workspace_folders})
  0         0  
55             {
56 4         122 require PLS::Parser::Index;
57 4         193 $workspace_folders = PLS::Parser::Index->new->workspace_folders;
58             }
59              
60 4         50 my @resolved;
61              
62 4         43 foreach my $workspace_folder (@{$workspace_folders})
  4         98  
63             {
64 4         103 my $resolved = $path =~ s/\$ROOT_PATH/$workspace_folder/r;
65 4         69 $resolved =~ s/\$\{workspaceFolder\}/$workspace_folder/;
66              
67 4 50       71 if (not length $resolved)
68             {
69 0         0 next;
70             }
71              
72 4 50       43 if ($no_glob)
73             {
74 0 0 0     0 if (length $resolved and -e $resolved)
75             {
76 0         0 push @resolved, $resolved;
77             }
78             } ## end if ($no_glob)
79             else
80             {
81 4 50       489 push @resolved, grep { length and -e } glob $resolved;
  4         205  
82             }
83             } ## end foreach my $workspace_folder...
84              
85 4         106 return @resolved;
86             } ## end sub resolve_workspace_relative_path
87              
88             1;