blib/lib/Exherbo/Packager.pm

filter: Statementstmt 92.9% 92 / 99 Branchbran 53.3% 16 / 30 Conditioncond 66.6% 6 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 18 / 18 Podpod 50.0% 2 / 4 totaltotal 81.9% 136 / 166 CC 21 SCAR 13.6
CC
21
Cov
83%
init_config
8.3
_gen_deps
5.0
_get_config
4.8
CRAP 3.9
↑ Summary
1 package Exherbo::Packager;
2  
3 # ABSTRACT: Generates exheres for perl modules
4  
5 =pod
6  
7 =head1 NAME
8  
9 Exherbo::Packager
10  
11 =head1 SYNOPSIS
12  
13 use Exherbo::Packager qw/init_config gen_template/;
14 my $config_loc = "/etc/exherbo-packager.yml"
15 init_config($config_loc);
16 gen_template("Exherbo::Packager");
17  
18 =head1 DESCRIPTION
19  
20 This module exports two functions, one to initialize the configuration of the
21 packager, and the other to generate the exheres. Currently, this package only
22 generates Exheres for Perl modules, but support for other languages is coming
23 soon.
24  
25 An OO version of this module is also planned, since exporting things into the
26 global namespace is icky.
27  
28 =head2 gen_template($modname)
29  
30 gen_template takes one argument, and that is the name of the perl module you
31 wish to generate. It will output the exheres in your current directory, in a
32 subdirectory named by the category it chooses.
33  
34 This will B<die> with an error if the exheres already exists.
35  
36 =head2 init_config
37  
38 =head2 init_config($config_loc)
39  
40 init_config can optionally take one argument, that being the location of the
41 config file you wish to use for this run of the packager. Once run, it will get
42 all of the configuration information for calls to C<gen_template()>.
43  
44 =head1 BUGS
45  
46 =over 1
47 =item No OO interface
48 =item Not very generic or extendable
49 =item Little error checking
50 =back
51  
52 =head1 AUTHOR
53  
54 William Orr <will@worrbase.com>
55  
56 =cut
57  
58  
59 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 ▶use MetaCPAN::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 ▶use YAML::Any qw/LoadFile DumpFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 2 our @ISA = qw/Exporter/;
70 2 our @EXPORT_OK = qw/init_config gen_template/;
71  
72 2 ▶use constant CONFIG_LOC => $ENV{HOME}."/.exherbo-packager.yml";
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 2 my $mcpan;
75 my $config;
76  
77  
78 1 ▶sub gen_template {
Subroutine gen_template: called CC=4 SCAR=14.5
Pod documented
79 1 my ($name, $fh) = @_;
80  
81 1 my $mod = _get_module_info($name);
82 1 my $release = _get_release_info($mod);
83 1 my $dt = DateTime->now();
84
85 0 ▶ if (not $config) { $config = _get_config(); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($config) 0 1 1
86 1 my $year = $dt->year;
87  
88 1 ▶ unless ($mod->{description}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mod->{"description"}) 0 1 1
89 0 bleep("No description available");
90 0 $mod->{description} = "Describe me!";
91 0 $mod->{abstract} = "A nifty little abstract should go here!";
92 }
93  
94 1 $mod->{description} = sanitize($mod->{description});
95 1 $mod->{abstract} = sanitize($mod->{abstract});
96  
97 1 print $fh <<EOF
98 # Copyright $year $config->{name} <$config->{email}>
99 # Distributed under the terms of the GNU General Public License v2
100  
101 require perl-module [ module_author=$mod->{author} ]
102  
103 SUMMARY="$mod->{abstract}"
104 DESCRIPTION="
105 $mod->{description}
106 "
107  
108 SLOT="0"
109 PLATFORMS="$config->{platforms}"
110 MYOPTIONS=""
111  
112 DEPENDENCIES="
113 build+run:
114 EOF
115 ;
116 1 my $deps = _gen_deps($release->{dependency});
117 1 foreach my $k (sort { uc $a cmp uc $b } keys %$deps) {
118 21 say $fh " dev-perl/$deps->{$k}"
119 }
120 1 print $fh <<EOF
121 "
122  
123 BUGS_TO="$config->{email}"
124  
125 EOF
126 ;
127 }
128  
129 40 ▶sub _get_module_info {
Subroutine _get_module_info: called CC=3 SCAR=11.2
130 40 my ($name) = @_;
131  
132 40 ▶ $mcpan //= MetaCPAN::API->new();
Condition66%2/3
$mcpan //= "MetaCPAN::API"->new
l!l&&r!l&&!r
3910
MC/DC0%0/2
$mcpan //= "MetaCPAN::API"->new
$mcpan "MetaCPAN::API"->new
Truth table66%2/3
A //= B
A= $mcpanB= "MetaCPAN::API"->new
ABresult
1X1
011
000
133 40 my $mod = $mcpan->module($name);
134  
135 40 ▶ ouch(404, "Module $name not found") if (not $mod);
Branch: 1 branch
BranchTrueFalseTotal
unless $mod 0 40 40
136 40 return $mod;
137 }
138  
139 39 ▶sub _get_release_info {
Subroutine _get_release_info: called CC=2 SCAR=7.0
140 39 my ($mod) = @_;
141  
142 39 my $rel = $mcpan->release(distribution => $mod->{distribution}, release => $mod->{release});
143 39 ▶ barf("Release $mod->{distribution} not found") if (not $rel);
Branch: 1 branch
BranchTrueFalseTotal
unless $rel 0 39 39
144 39 return $rel;
145 }
146  
147 1 ▶sub _get_config {
Subroutine _get_config: called CC=4 SCAR=15.6
148 1 my $lconfig = CONFIG_LOC;
149 1 ▶ $lconfig = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
150 1 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { return $config //= LoadFile($lconfig) } } or barf("Could not read config") 1 0 1
151 1 ▶ return $config //= LoadFile($lconfig);
Condition33%1/3
$config //= LoadFile($lconfig)
l!l&&r!l&&!r
010
MC/DC0%0/2
$config //= LoadFile($lconfig)
$config LoadFile($lconfig)
Truth table33%1/3
A //= B
A= $configB= LoadFile($lconfig)
ABresult
1X1
011
000
152 } or barf("Could not read config");
153 }
154  
155 2 ▶sub get_outfile_name {
Subroutine get_outfile_name: called CC=2 SCAR=7.6
Pod undocumented
156 2 my $mod = shift;
157  
158 2 ▶ if (ref($mod) ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mod ne "HASH") 0 2 2
159 0 $mod = _get_module_info($mod);
160 }
161  
162 2 return "$mod->{release}.exheres-0";
163  
164 }
165  
166 2 ▶sub _gen_deps {
Subroutine _gen_deps: called CC=5 SCAR=16.1
167 2 my ($deps) = @_;
168 2 my $rel_deps = {};
169  
170 2 foreach my $dep (@{$deps}) {
171 55 ▶ if ($dep->{relationship} eq 'requires' and $dep->{module} ne 'perl') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dep->{"relationship"} eq "requires" and $dep->{"module"} ne "perl") 37 18 55
Condition100%3/3
$dep->{"relationship"} eq "requires" and $dep->{"module"} ne "perl"
!ll&&!rl&&r
16237
MC/DC100%2/2
$dep->{"relationship"} eq "requires" and $dep->{"module"} ne "perl"
$dep->{"relationship"} eq "requires" $dep->{"module"} ne "perl"
Truth table100%3/3
A and B
A= $dep->{"relationship"} eq "requires"B= $dep->{"module"} ne "perl"
ABresult
0X0
100
111
172 37 my $rel = _get_release_info(_get_module_info($dep->{module}));
173 37 ▶ next if ($rel->{distribution} eq 'perl');
Branch: 1 branch
BranchTrueFalseTotal
if $rel->{"distribution"} eq "perl" 4 33 37
174 33 $rel_deps->{$rel->{distribution}} = $rel->{name};
175 }
176 }
177  
178 2 return $rel_deps;
179 }
180  
181 1 ▶sub init_config {
Subroutine init_config: called CC=7 SCAR=21.2
Pod documented
182 1 my $lconfig = CONFIG_LOC;
183 1 ▶ $lconfig = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
184  
185 1 ▶ if ( -f $lconfig ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $lconfig) 1 0 1
186 1 print "Are you sure you want to overwrite your config? ";
187 1 ▶ return if (*STDIN->getline !~ /^y$/i);
Branch: 1 branch
BranchTrueFalseTotal
unless *STDIN->getline =~ /^y$/i 0 1 1
188 }
189  
190 1 my $conf_info = { };
191 1 print "What's your name? ";
192 1 $conf_info->{name} = _sane_chomp(*STDIN->getline);
193  
194 1 print "What's your email address? ";
195 1 $conf_info->{email} = _sane_chomp(*STDIN->getline);
196  
197 1 print "Give me a valid arch string to use by default for new packages: ";
198 1 $conf_info->{platforms} = _sane_chomp(*STDIN->getline);
199 1 print "\n";
200  
201 1 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { unless (-f $lconfig) { open my $fh, ">", $lconfig or die; close $fh; } ; DumpFile($lconfig, $conf_info) } } or ouch(400, "Could not open config file for writing") 1 0 1
202 1 ▶ if ( not -f $lconfig ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $lconfig) 0 1 1
203 0 ▶ open(my $fh, '>', $lconfig) or die;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $lconfig or die 0 0 0
204 0 close($fh);
205 }
206 1 DumpFile($lconfig, $conf_info)
207 } or ouch 400, "Could not open config file for writing";
208 }
209  
210 2 ▶sub sanitize {
Subroutine sanitize: called CC=1 SCAR=0.0
Pod undocumented
211 2 my $in = shift;
212 2 $in =~ s/"/\"/g;
213 2 return $in;
214 }
215  
216 3 ▶sub _sane_chomp {
Subroutine _sane_chomp: called CC=1 SCAR=0.0
217 3 my $str = shift;
218 3 chomp $str;
219 3 return $str;
220 }
221  
222 2 1;
↑ Summary