MultiGem/Command/initialize.pm

filter: Statementstmt 49.0% 27 / 55 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 53.3% 8 / 15 Podpod 100% 7 / 7 totaltotal 46.1% 42 / 91 CC 8 SCAR 22.7
CC
8
Cov
40%
execute
30.0
validate_args
12.0
config
6.0
CRAP 9.7
« MultiGem/Model/Gem.pm ↑ Summary MultiGem/Utl/Directories.pm »
1 package Bundler::MultiGem::Command::initialize;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Bundler::MultiGem -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Bundler::MultiGem::Utl::InitConfig qw(merge_configuration);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Bundler::MultiGem::Command::initialize - Generate a configuration file (alias: init bootstrap b)
16  
17 =head1 VERSION
18  
19 Version 0.02
20  
21 =cut
22  
23 1 our $VERSION = '0.02';
24  
25 1 our $app = {};
26  
27 =head1 SYNOPSIS
28  
29 This module includes the commands to initialize a yml configuration file for installing multiple versions of the same gem
30  
31 bundle-multigem [-f] [long options...] <path>
32  
33 --gm --gem-main-module provide the gem main module (default:
34 constantize --gem-name)
35 --gn --gem-name provide the gem name
36 --gs --gem-source provide the gem source (default:
37 https://rubygems.org)
38 --gv --gem-versions provide the gem versions to install (e.g:
39 --gem-versions 0.0.1 --gem-versions 0.0.2)
40 --dp --dir-pkg directory for downloaded gem pkg (default:
41 pkg)
42 --dt --dir-target directory for extracted versions (default:
43 versions)
44 --cp --cache-pkg keep cache of pkg directory (default: 1)
45 --ct --cache-target keep cache of target directory (default: 0)
46 -f --conf-file choose config file name (default:
47 .bundle-multigem.yml)
48  
49  
50 Please note that the C<path> passed as argument will be considered as the root path for the project
51  
52 =head1 SUBROUTINES
53  
54 =head2 command_names
55  
56 Command aliases: C<initialize, init, bootstrap, b>
57  
58 =cut
59  
60 0 ▶sub command_names {
Subroutine command_names: not called CC=1 SCAR=6.9
Pod documented
61 0 qw(initialize init bootstrap b)
62 }
63  
64 =head2 execute
65  
66 =cut
67  
68 0 ▶sub execute {
Subroutine execute: not called CC=5 SCAR=34.0
Pod documented
69 0 my ($self, $opt, $args) = @_;
70  
71 0 foreach my $k (keys %{$opt}) {
72 0 my ($k1, $k2) = split(/_/, $k, 2);
73 0 my $new_key = opt_prefix($k1);
74 0 ▶ next unless $new_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $new_key 0 0 0
75 0 $app->{config}->{$new_key}->{$k2} = $opt->{$k};
76 }
77  
78 0 $app->{config} = merge_configuration($app->{config});
79  
80 0 ▶ my $output_file = $opt->{'conf-file'} || ".bundle-multigem.yml";
Condition0%0/2
$opt->{'conf-file'} || '.bundle-multigem.yml'
l!l
00
MC/DC0%0/1
$opt->{'conf-file'} || '.bundle-multigem.yml'
$opt->{'conf-file'}
Truth table0%0/2
A
A= $opt->{'conf-file'}
Aresult
11
00
81 0 my $yaml = YAML::Tiny->new( $app->{config} );
82  
83 0 ▶ if (! -f $output_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $output_file) 0 0 0
84 0 $output_file = catfile($app->{config}->{directories}->{root}, ".bundle-multigem.yml")
85 }
86  
87 0 $yaml->write($output_file);
88  
89 0 print "Configuration generated at: ${output_file}\n";
90 }
91  
92 =head2 usage_desc
93  
94 =cut
95  
96 0 ▶sub usage_desc { "bundle-multigem %o <path>" }
Subroutine usage_desc: not called CC=1 SCAR=6.9
Pod documented
97  
98 =head2 opt_spec
99  
100 =cut
101  
102 0 ▶sub opt_spec {
Subroutine opt_spec: not called CC=1 SCAR=6.9
Pod documented
103 return (
104 0 [ "gem-main-module|gm=s", "provide the gem main module (default: constantize --gem-name)" ],
105 [ "gem-name|gn=s", "provide the gem name" ],
106 [ "gem-source|gs=s", "provide the gem source (default: https://rubygems.org)" ],
107 [ "gem-versions|gv=s@", "provide the gem versions to install (e.g: --gem-versions 0.0.1 --gem-versions 0.0.2)" ],
108 [ "dir-pkg|dp=s", "directory for downloaded gem pkg (default: pkg)" ],
109 [ "dir-target|dt=s", "directory for extracted versions (default: versions)" ],
110 [ "cache-pkg|cp=s", "keep cache of pkg directory (default: 1)" ],
111 [ "cache-target|ct=s", "keep cache of target directory (default: 0)" ],
112 [ "conf-file|f=s", "choose config file name (default: .bundle-multigem.yml)" ],
113 );
114 }
115  
116 =head2 validate_args
117  
118 =cut
119  
120 0 ▶sub validate_args {
Subroutine validate_args: not called CC=3 SCAR=24.8
Pod documented
121 0 my ($self, $opt, $args) = @_;
122  
123 0 ▶ if (scalar @$args != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$args != 1) 0 0 0
124 0 $self->usage_error("You should provide exactly one argument (<path>)");
125 }
126  
127 0 my $root_path = realpath($args->[0]);
128  
129 0 ▶ if (! -e $root_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $root_path) 0 0 0
130 0 $self->usage_error("You should provide a valid path ($root_path does not exists)");
131 }
132  
133 $app->{config}->{directories} = {
134 0 'root' => $root_path
135 };
136 }
137  
138 =head2 config
139  
140 =cut
141  
142 0 ▶sub config {
Subroutine config: not called CC=2 SCAR=17.9
Pod documented
143 0 my $app = shift;
144 0 ▶ $app->{config} ||= {};
Condition0%0/2
$app->{"config"} ||= {}
l!l
00
MC/DC0%0/1
$app->{"config"} ||= {}
$app->{"config"}
Truth table0%0/2
A
A= $app->{"config"}
Aresult
11
00
145 }
146  
147 1 our $OPT_PREFIX = {
148 'gem' => 'gem',
149 'dir' => 'directories',
150 'cache' => 'cache'
151 };
152  
153 =head2 opt_prefix
154  
155 Internal. Apply $OPT_PREFIX when collecting arguments
156  
157 our $OPT_PREFIX = {
158 'gem' => 'gem',
159 'dir' => 'directories',
160 'cache' => 'cache'
161 };
162  
163 =cut
164  
165 0 ▶sub opt_prefix {
Subroutine opt_prefix: not called CC=1 SCAR=6.9
Pod documented
166 0 my $k = shift;
167 0 $OPT_PREFIX->{$k};
168 }
169  
170 =head1 AUTHOR
171  
172 Mauro Berlanda, C<< <kupta at cpan.org> >>
173  
174 =head1 BUGS
175  
176 Please report any bugs or feature requests to L<https://github.com/mberlanda/Bundler-MultiGem/issues>, or through
177 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=.>. I will be notified, and then you'll
178 automatically be notified of progress on your bug as I make changes.
179  
180 =head1 SUPPORT
181  
182 You can find documentation for this module with the perldoc command.
183  
184 perldoc Bundler::MultiGem::Directories
185  
186  
187 You can also look for information at:
188  
189 =over 2
190  
191 =item * RT: CPAN's request tracker (report bugs here)
192  
193 L<https://rt.cpan.org/Public/Dist/Display.html?Name=Bundler-MultiGem>
194  
195 =item * Github Repository
196  
197 L<https://github.com/mberlanda/Bundler-MultiGem>
198  
199 =back
200  
201  
202 =head1 ACKNOWLEDGEMENTS
203  
204  
205 =head1 LICENSE AND COPYRIGHT
206  
207 Copyright 2018 Mauro Berlanda.
208  
209 This program is free software; you can redistribute it and/or modify it
210 under the terms of the the Artistic License (2.0). You may obtain a
211 copy of the full license at:
212  
213 L<http://www.perlfoundation.org/artistic_license_2_0>
214  
215 Any use, modification, and distribution of the Standard or Modified
216 Versions is governed by this Artistic License. By using, modifying or
217 distributing the Package, you accept this license. Do not use, modify,
218 or distribute the Package, if you do not accept this license.
219  
220 If your Modified Version has been derived from a Modified Version made
221 by someone other than you, you are nevertheless required to ensure that
222 your Modified Version complies with the requirements of this license.
223  
224 This license does not grant you the right to use any trademark, service
225 mark, tradename, or logo of the Copyright Holder.
226  
227 This license includes the non-exclusive, worldwide, free-of-charge
228 patent license to make, have made, use, offer to sell, sell, import and
229 otherwise transfer the Package with respect to any patent claims
230 licensable by the Copyright Holder that are necessarily infringed by the
231 Package. If you institute patent litigation (including a cross-claim or
232 counterclaim) against any party alleging that the Package constitutes
233 direct or contributory patent infringement, then this Artistic License
234 to you shall terminate on the date that such litigation is filed.
235  
236 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
237 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
238 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
239 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
240 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
241 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
242 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
243 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
244  
245  
246 =cut
247  
248 1 1;
« MultiGem/Model/Gem.pm ↑ Summary MultiGem/Utl/Directories.pm »