File Coverage

blib/lib/Bundler/MultiGem/Command/initialize.pm
Criterion Covered Total %
statement 23 51 45.1
branch 0 8 0.0
condition 0 4 0.0
subroutine 8 15 53.3
pod 7 7 100.0
total 38 85 44.7


line stmt bran cond sub pod time code
1             package Bundler::MultiGem::Command::initialize;
2              
3 1     1   66404 use 5.006;
  1         12  
4 1     1   5 use strict;
  1         2  
  1         32  
5 1     1   6 use warnings;
  1         2  
  1         31  
6              
7 1     1   398 use Bundler::MultiGem -command;
  1         4  
  1         9  
8 1     1   4847 use Cwd qw(realpath);
  1         2  
  1         59  
9 1     1   491 use Bundler::MultiGem::Utl::InitConfig qw(merge_configuration);
  1         3  
  1         68  
10 1     1   8 use File::Spec::Functions qw(catfile);
  1         2  
  1         55  
11 1     1   608 use YAML::Tiny;
  1         5752  
  1         571  
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             our $VERSION = '0.02';
24              
25             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...]
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 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
57              
58             =cut
59              
60             sub command_names {
61 0     0 1   qw(initialize init bootstrap b)
62             }
63              
64             =head2 execute
65              
66             =cut
67              
68             sub execute {
69 0     0 1   my ($self, $opt, $args) = @_;
70              
71 0           foreach my $k (keys %{$opt}) {
  0            
72 0           my ($k1, $k2) = split(/_/, $k, 2);
73 0           my $new_key = opt_prefix($k1);
74 0 0         next unless $new_key;
75 0           $app->{config}->{$new_key}->{$k2} = $opt->{$k};
76             }
77              
78 0           $app->{config} = merge_configuration($app->{config});
79              
80 0   0       my $output_file = $opt->{'conf-file'} || ".bundle-multigem.yml";
81 0           my $yaml = YAML::Tiny->new( $app->{config} );
82              
83 0 0         if (! -f $output_file ) {
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     0 1   sub usage_desc { "bundle-multigem %o " }
97              
98             =head2 opt_spec
99              
100             =cut
101              
102             sub opt_spec {
103             return (
104 0     0 1   [ "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             sub validate_args {
121 0     0 1   my ($self, $opt, $args) = @_;
122              
123 0 0         if (scalar @$args != 1) {
124 0           $self->usage_error("You should provide exactly one argument ()");
125             }
126              
127 0           my $root_path = realpath($args->[0]);
128              
129 0 0         if (! -e $root_path) {
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             sub config {
143 0     0 1   my $app = shift;
144 0   0       $app->{config} ||= {};
145             }
146              
147             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             sub opt_prefix {
166 0     0 1   my $k = shift;
167 0           $OPT_PREFIX->{$k};
168             }
169              
170             =head1 AUTHOR
171              
172             Mauro Berlanda, C<< >>
173              
174             =head1 BUGS
175              
176             Please report any bugs or feature requests to L, or through
177             the web interface at L. 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
194              
195             =item * Github Repository
196              
197             L
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
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;