File Coverage

blib/lib/OS/Package/Factory.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1 1     1   1170 use v5.14.0;
  1         3  
  1         29  
2 1     1   3 use warnings;
  1         1  
  1         34  
3              
4             package OS::Package::Factory;
5              
6             # ABSTRACT: Initialize an OS::Package object.
7             our $VERSION = '0.2.5'; # VERSION
8              
9 1     1   3 use Config;
  1         1  
  1         24  
10 1     1   3 use Env qw( $HOME );
  1         1  
  1         3  
11 1     1   100 use File::Basename;
  1         1  
  1         40  
12 1     1   466 use Module::Load;
  1         724  
  1         4  
13 1     1   56 use OS::Package;
  0            
  0            
14             use OS::Package::Application;
15             use OS::Package::Artifact;
16             use OS::Package::Config qw( $OSPKG_CONFIG );
17             use OS::Package::Log qw( $LOGGER );
18             use OS::Package::Maintainer;
19             use OS::Package::System;
20             use Path::Tiny;
21             use YAML::Any qw( LoadFile );
22              
23             use base qw(Exporter);
24              
25             our @EXPORT = qw( vivify );
26              
27             local $YAML::UseCode = 0 if !defined $YAML::UseCode;
28             local $YAML::LoadCode = 0 if !defined $YAML::LoadCode;
29              
30             sub vivify {
31             my ($arg_ref) = @_;
32             my $name = $arg_ref->{name};
33             my $build_id = $arg_ref->{build_id};
34              
35             my $cfg_file = sprintf '%s/%s.yml', path( $OSPKG_CONFIG->dir->configs ),
36             lc($name);
37              
38             if ( !-f $cfg_file ) {
39             $LOGGER->logcroak( sprintf 'cannot find configuration file %s for %s',
40             $cfg_file, $name );
41             }
42              
43             my $config = LoadFile($cfg_file);
44              
45             my $system = OS::Package::System->new;
46              
47             my $pkg;
48              
49             if ( defined $OSPKG_CONFIG->{plugin}{ $system->os }{ $system->version } )
50             {
51             my $plugin =
52             $OSPKG_CONFIG->{plugin}{ $system->os }{ $system->version };
53              
54             load $plugin;
55              
56             my $app = OS::Package::Application->new(
57             name => $config->{name},
58             version => $config->{version}
59             );
60              
61             my $maintainer =
62             OS::Package::Maintainer->new(
63             author => $config->{maintainer}{author} );
64              
65             foreach my $method (qw( nickname email phone company )) {
66             if ( defined $config->{maintainer}{$method} ) {
67             $maintainer->$method( $config->{maintainer}{$method} );
68             }
69             }
70              
71             my $pkg_config = {
72             name => $config->{pkgname},
73             version => $config->{version},
74             prefix => $config->{prefix},
75             description => $config->{description},
76             maintainer => $maintainer,
77             application => $app,
78             };
79              
80             if ( defined $build_id ) {
81             $pkg_config->{build_id} = $build_id;
82             }
83              
84             $pkg = $plugin->new($pkg_config);
85              
86             }
87             else {
88             $LOGGER->logcroak(
89             sprintf 'cannot find plugin for %s %s',
90             ucfirst( $system->os ),
91             $system->version
92             );
93             return;
94             }
95              
96             if ( defined $config->{build} ) {
97             $pkg->install( $config->{build} );
98             }
99              
100             if ( defined $config->{prune}{directories} ) {
101             $pkg->prune_dirs( $config->{prune}{directories} );
102             }
103              
104             if ( defined $config->{prune}{files} ) {
105             $pkg->prune_files( $config->{prune}{files} );
106             }
107              
108             my $artifact = OS::Package::Artifact->new;
109              
110             if ( defined $config->{url} ) {
111              
112             $artifact->distfile( basename( $config->{url} ) );
113             $artifact->url( $config->{url} );
114             $artifact->repository( path( $OSPKG_CONFIG->dir->repository ) );
115              
116             if ( defined $config->{md5} ) {
117             $artifact->md5( $config->{md5} );
118             }
119              
120             if ( defined $config->{sha1} ) {
121             $artifact->sha1( $config->{sha1} );
122             }
123              
124             my $savefile = sprintf( '%s/%s',
125             path( $OSPKG_CONFIG->dir->repository ),
126             basename( $config->{url} ) );
127              
128             $artifact->savefile($savefile);
129              
130             }
131             elsif ( defined $config->{os}{ $pkg->system->os }{ $pkg->system->type } )
132             {
133              
134             my $artifact_cfg =
135             $config->{os}{ $pkg->system->os }{ $pkg->system->type };
136              
137             $artifact = OS::Package::Artifact->new(
138             distfile => basename( $artifact_cfg->{url} ),
139             url => $artifact_cfg->{url},
140             repository => path( $OSPKG_CONFIG->dir->repository )
141             );
142              
143             if ( defined $artifact_cfg->{md5} ) {
144             $artifact->md5( $artifact_cfg->{md5} );
145             }
146              
147             if ( defined $artifact_cfg->{sha1} ) {
148             $artifact->sha1( $artifact_cfg->{sha1} );
149             }
150              
151             $artifact->savefile(
152             sprintf( '%s/%s',
153             path( $OSPKG_CONFIG->dir->repository ),
154             basename( $artifact_cfg->{url} ) )
155             );
156              
157             }
158              
159             $pkg->artifact($artifact);
160              
161             return $pkg;
162             }
163              
164             1;
165              
166             __END__