File Coverage

blib/lib/Ado/Command/generate/adoplugin.pm
Criterion Covered Total %
statement 42 42 100.0
branch 8 8 100.0
condition 6 12 50.0
subroutine 7 7 100.0
pod 1 1 100.0
total 64 70 91.4


line stmt bran cond sub pod time code
1             package Ado::Command::generate::adoplugin;
2 3     3   2499 use Mojo::Base 'Ado::Command::generate';
  3         6  
  3         23  
3 3     3   392 use Mojo::Util qw(camelize class_to_path decamelize);
  3         12  
  3         235  
4 3     3   15 use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
  3         5  
  3         24  
5 3     3   1834 use Time::Piece ();
  3         12515  
  3         109  
6 3     3   25 use Carp;
  3         5  
  3         267  
7 3     3   17 use Cwd;
  3         6  
  3         2168  
8             File::Spec::Functions->import(qw(catfile catdir));
9              
10              
11             has description => "Generates directory structures for Ado-specific plugins..\n";
12             has usage => sub { shift->extract_usage };
13             has crud => sub {
14             require Ado::Command::generate::crud;
15             Ado::Command::generate::crud->new(app => shift->app);
16             };
17              
18             sub run {
19 5     5 1 4596 my ($self, @args) = @_;
20 5         43 my $args = $self->args;
21             GetOptionsFromArray \@args,
22             'n|name=s' => \$args->{name},
23             'c|crud' => \$args->{crud},
24              
25             # CRUD options
26             'C|controller_namespace=s' => \$args->{controller_namespace},
27             'L|lib=s' => \$args->{lib},
28             'M|model_namespace=s' => \$args->{model_namespace},
29             'O|overwrite' => \$args->{overwrite},
30             'T|templates_root=s' => \$args->{templates_root},
31             't|tables=s@' => \$args->{tables},
32             'H|home_dir=s' => \$args->{home_dir},
33 5         92 ;
34              
35 5 100       4454 unless ($$args{name}) {
36 1         11 croak $self->usage;
37             }
38 4 100 100     44 if ($args->{crud} && !$args->{tables}) {
39 1         346 croak 'Option --tables is mandatory when option --crud is passed!' . $/;
40             }
41              
42             # Class
43 3 100       42 my $class = $$args{name} =~ /^[a-z]/ ? camelize($$args{name}) : $$args{name};
44 3         49 $class = "Ado::Plugin::$class";
45 3         23 my $path = class_to_path $class;
46 3         93 my $dir = join '-', split '::', $class;
47 3         68 $self->render_to_rel_file('class', "$dir/lib/$path", $class, $$args{name});
48 3         30515 my $decamelized = decamelize($$args{name});
49              
50 3 100       103 if ($args->{crud}) {
51 1         4 $args->{tables} = join(',', @{$args->{tables}});
  1         6  
52 1   33     26 $args->{home_dir} //= catdir(getcwd(), $dir);
53 1   33     14 $args->{templates_root} //= catdir($args->{home_dir}, 'templates');
54 1   33     13 $args->{lib} //= catdir($args->{home_dir}, 'lib');
55             $self->crud->run(
56             '-C' => $args->{controller_namespace},
57             '-L' => $args->{lib},
58             '-M' => $args->{model_namespace},
59             '-O' => $args->{overwrite},
60             '-T' => $args->{templates_root},
61             '-t' => $args->{tables},
62             '-H' => $args->{home_dir},
63 1         7 );
64             }
65              
66             # Test
67             $self->render_to_rel_file('test', "$dir/t/plugin/$decamelized-00.t",
68 3         29 $class, $$args{name}, $decamelized);
69              
70             # Build.PL
71 3         8122 $self->render_to_rel_file('build_file', "$dir/Build.PL", $class, $path, $dir);
72              
73             # Configuration
74 3         7953 $self->render_to_rel_file('config_file', "$dir/etc/plugins/$decamelized.conf",
75             $decamelized, $self->crud, $args);
76              
77 3         3376 return $self;
78             }
79             1;
80              
81             =pod
82              
83             =encoding utf8
84              
85             =head1 NAME
86              
87             Ado::Command::generate::adoplugin - Generates an Ado::Plugin
88              
89             =head1 SYNOPSIS
90              
91             On the command-line:
92              
93             $ cd ~/opt/public_dev
94             # Ado is "globally" installed for the current perlbrew Perl
95             $ ado generate adoplugin --name MyBlog
96             $ ado generate adoplugin --name MyBlog --crud -t 'articles,news'
97              
98             Programmatically:
99              
100             use Ado::Command::generate::adoplugin;
101             my $vhost = Ado::Command::generate::adoplugin->new;
102             $vhost->run(-n => 'MyBlog', -c => 1, -t => 'articles,news');
103              
104             =head1 DESCRIPTION
105              
106             L generates directory structures for
107             fully functional L-specific plugins with optional
108             L in the newly created plugin directory.
109             The new plugin is generated in the current directory.
110              
111             This is a core command, that means it is always enabled and its code a
112             more complex example for learning to build new commands. You're welcome to fork it.
113              
114             =head1 OPTIONS
115              
116             Below are the options this command accepts, described in L notation.
117              
118             =head2 n|name=s
119              
120             Mandatory. String. The name of the plugin. The resulting full class name is
121             the camelized version of C.
122              
123             =head2 c|crud
124              
125             Boolean. When set you can pass in addition all the arguments accepted by
126             L. It is mandatory to pass at least the
127             C<--tables> option so the controllers can be generated.
128              
129             When generating a plugin:
130             C<--controller_namespace>
131             defaults to Croutes-Enamespaces-E[0]>;
132             C<--home_dir> defaults to the plugin base directory;
133             C<--lib> defaults to C in the plugin base directory;
134             C<--model_namespace> defaults to L;
135             C<--templates_root> defaults to C in the plugin base directory.
136              
137             =head1 ATTRIBUTES
138              
139             L inherits all attributes from
140             L and implements the following new ones.
141              
142             =head2 crud
143              
144             #returns $self.
145             $self->crud(Ado::Command::generate::crud->new(app => $self->app))
146             #returns Ado::Command::generate::crud instance.
147             my $crud = $self->crud->run(%options);
148              
149             An instance of L.
150             Used by L to generate routes for controllers
151             and possibly others.
152              
153             =head2 description
154              
155             my $description = $command->description;
156             $command = $command->description('Foo!');
157              
158             Short description of this command, used for the command list.
159              
160             =head2 usage
161              
162             my $usage = $command->usage;
163             $command = $command->usage('Foo!');
164              
165             Usage information for this command, used for the help screen.
166              
167             =head1 METHODS
168              
169             L inherits all methods from
170             L and implements the following new ones.
171              
172             =head2 run
173              
174             $plugin->run(@ARGV);
175              
176             Run this command.
177              
178             =head1 SEE ALSO
179              
180             L,
181             L,
182             L,
183             L, L,
184             L, L,
185             L L,
186             L, L
187              
188             =head1 AUTHOR
189              
190             Красимир Беров (Krasimir Berov)
191              
192             =head1 COPYRIGHT AND LICENSE
193              
194             Copyright 2014 Красимир Беров (Krasimir Berov).
195              
196             This program is free software, you can redistribute it and/or
197             modify it under the terms of the
198             GNU Lesser General Public License v3 (LGPL-3.0).
199             You may copy, distribute and modify the software provided that
200             modifications are open source. However, software that includes
201             the license may release under a different license.
202              
203             See http://opensource.org/licenses/lgpl-3.0.html for more information.
204              
205             =cut
206              
207              
208             __DATA__