File Coverage

blib/lib/Win32/Vcpkg/List.pm
Criterion Covered Total %
statement 60 64 93.7
branch 19 26 73.0
condition 10 15 66.6
subroutine 10 10 100.0
pod 4 4 100.0
total 103 119 86.5


line stmt bran cond sub pod time code
1             package Win32::Vcpkg::List;
2              
3 1     1   509 use strict;
  1         3  
  1         25  
4 1     1   5 use warnings;
  1         1  
  1         21  
5 1     1   21 use 5.008001;
  1         3  
6 1     1   4 use Win32::Vcpkg;
  1         2  
  1         13  
7 1     1   4 use Path::Tiny ();
  1         2  
  1         28  
8 1     1   734 use Storable qw( dclone );
  1         3008  
  1         687  
9              
10             # ABSTRACT: Interface to Microsoft Vcpkg List of Packages
11             our $VERSION = '0.04'; # VERSION
12              
13              
14             sub new
15             {
16 1     1 1 5684 my($class, %args) = @_;
17              
18 1 50       8 my $root = defined $args{root} ? Path::Tiny->new($args{root}) : Win32::Vcpkg->root;
19              
20 1         175 my $vcpkg_exe = $root->child('vcpkg.exe');
21 1 50       31 if(-x $vcpkg_exe)
22             {
23             # TODO: this is a bit flaky, the root directory might
24             # not have vcpkg.exe.
25 0         0 `$vcpkg_exe list`; # force a rebuild of the status file
26             }
27              
28 1         55 my $status = $root->child('installed', 'vcpkg', 'status');
29              
30 1         34 my @status;
31             my %arch;
32             {
33 1         2 my %entry;
  1         2  
34              
35 1         4 foreach my $line ($status->lines)
36             {
37 219         505 chomp $line;
38 219 100       565 if($line =~ /^(.*?):\s*(.*)$/)
    50          
39             {
40 195         350 my($k,$v) = ($1, $2);
41 195         328 $entry{$k} = $v;
42 195 100       305 $arch{$v}++ if $k eq 'Architecture';
43             }
44             elsif($line =~ /^\s*$/)
45             {
46 24 50       37 if(%entry)
47             {
48 24         359 push @status, dclone(\%entry);
49 24         64 %entry = ();
50             }
51             }
52             }
53              
54 1 50       11 if(%entry)
55             {
56 0         0 push @status, \%entry;
57             }
58             }
59              
60             bless {
61 1         16 root => $root,
62             status => \@status,
63             triplets => [sort keys %arch],
64             }, $class;
65             }
66              
67              
68 8     8 1 2748 sub root { shift->{root} }
69              
70              
71 1     1 1 487 sub triplets { @{ shift->{triplets} } }
  1         3  
72              
73              
74             sub search
75             {
76 4     4 1 979 my($self, $name, %options) = @_;
77 4   66     20 my $triplet = $options{triplet} || Win32::Vcpkg->perl_triplet;
78 4 100       19 my $debug = defined $options{debug} ? $options{debug} : $ENV{PERL_WIN32_VCPKG_DEBUG};
79              
80 4         5 foreach my $status (@{ $self->{status} })
  4         12  
81             {
82             next unless $status->{Architecture} eq $triplet
83             && $status->{Package} eq $name
84             && !defined $status->{Feature}
85             && $status->{Version}
86 50 50 100     136 && $status->{Status} eq 'install ok installed';
      66        
      66        
      33        
87              
88 3         6 my $version = $status->{Version};
89 3         6 my $file_list = $self->root->child('installed','vcpkg','info',sprintf("%s_%s_%s.list", $name, $version, $triplet));
90 3 50       113 if($file_list->is_file)
91             {
92 3         83 my @lib;
93 3         10 my $libpath = Path::Tiny->new($triplet, 'lib');
94 3 100       103 $libpath = $libpath->parent->child('debug','lib') if $debug;
95 3         97 foreach my $line ($file_list->lines)
96             {
97 72         1703 chomp $line;
98 72         126 my $path = Path::Tiny->new($line);
99 72 100       1476 if($path->basename =~ /^(.*?)\.lib$/)
100             {
101 6 100       109 if($path->parent->stringify eq $libpath->stringify)
102             {
103 3         108 push @lib, "$1";
104             }
105             }
106             }
107 3         531 require Win32::Vcpkg::Package;
108             return Win32::Vcpkg::Package->new(
109             _name => $name,
110             _version => $version,
111             root => $self->root,
112             triplet => $triplet,
113             debug => $debug,
114             include => $options{include},
115 3         9 lib => \@lib,
116             );
117             }
118             else
119             {
120 0         0 require Carp;
121 0         0 Carp::croak("unable to find file list for $name $version $triplet");
122             }
123             }
124              
125 1         3 return undef;
126             }
127              
128              
129             1;
130              
131             __END__