blib/lib/Module/Setup/Helper.pm

filter: Statementstmt 97.1% 34 / 35 Branchbran n/a Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 92.3% 12 / 13 Podpod 66.6% 6 / 9 totaltotal 88.3% 53 / 60 CC 2 SCAR 1.9
CC
2
Cov
95%
run
2.0
helper_option_prefix
2.0
BEGIN
1.0
CRAP 1.2
« blib/lib/Module/Setup/Plugin/Template.pm ↑ Summary blib/lib/Module/Setup/Path.pm »
1 package Module::Setup::Helper;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base 'Module::Setup::Flavor';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Module::Setup;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
9 2 my($class, %args) = @_;
10 2 my $self = bless {
11 argv => [],
12 options => {},
13 %args,
14 }, $class;
15  
16 2 $self->{options}->{flavor_class} = "+$class";
17  
18 2 $self->helper_base_init;
19 2 $self->helper_flavor_init;
20 2 $self;
21 }
22 2 ▶sub helper_base_init {
Subroutine helper_base_init: called CC=1 SCAR=0.0
Pod documented
23 2 my $self = shift;
24 2 $self->{options}->{module} = shift @{ $self->{argv} };
25 }
26 2 ▶sub helper_flavor_init {}
Subroutine helper_flavor_init: called CC=1 SCAR=0.0
Pod documented
27 1 ▶sub helper_default_plugins { qw/ Template Helper / }
Subroutine helper_default_plugins: called CC=1 SCAR=0.0
Pod documented
28  
29 0 ▶sub helper_option_prefix { die "$_[0] class is not implement helper_option_prefix method" }
Subroutine helper_option_prefix: not called CC=1 SCAR=6.9
Pod documented
30 1 ▶sub generate_path_option_names { qw// }
Subroutine generate_path_option_names: called CC=1 SCAR=0.0
Pod undocumented
31 1 ▶sub is_append_files { 0 }
Subroutine is_append_files: called CC=1 SCAR=0.0
Pod documented
32  
33 1 ▶sub run {
Subroutine run: called CC=2 SCAR=6.9
Pod undocumented
34 1 my $self = shift;
35  
36 # set the direct run mode
37 1 $self->{options}->{direct} = 1;
38  
39 # add default plugins
40 1 ▶ $self->{options}->{plugins} ||= [];
Condition50%1/2
$self->{"options"}{"plugins"} ||= []
l!l
01
MC/DC0%0/1
$self->{"options"}{"plugins"} ||= []
$self->{"options"}{"plugins"}
Truth table50%1/2
A
A= $self->{"options"}{"plugins"}
Aresult
11
00
41 1 push @{ $self->{options}->{plugins} }, $self->helper_default_plugins;
42  
43 1 $self->before_run;
44  
45 my $setup = Module::Setup->new(
46 1 argv => [ $self->{options}->{module}, @{ $self->{argv} } ],
47 options => $self->{options},
48 helper => $self->{helper},
49 1 );
50 1 $setup->run;
51 }
52  
53 1 ▶sub before_run {}
Subroutine before_run: called CC=1 SCAR=0.0
Pod documented
54  
55 1 1;
56  
57 =head1 NAME
58  
59 Module::Setup::Helper - build in your application helper support
60  
61 =head1 SYNOPSIS
62  
63 script/helper.pl is your helper script
64  
65 use strict;
66 use warnings;
67  
68 my $type = shift @ARGV;
69 my $helper = "YourApp::Helper::$type";
70 eval "use $helper";
71  
72 $helper->new(
73 argv => [ @ARGV ],
74 options => {
75 # Module::Setup's options
76 },
77 helper => {
78 option => 'FOO_OPTION',
79 },
80 )->run;
81  
82 lib/YourApp/Helper.pm is your application helper base class.
83  
84 package YourApp::Helper;
85 use strict;
86 use warnings;
87 use base 'Module::Setup::Helper';
88  
89 # make your application's options name prefix
90 sub helper_option_prefix { 'yourapp' }
91  
92 sub helper_base_init {
93 my $self = shift;
94  
95 # auto detect app module name by Makefile.PL
96 open my $fh, '<', 'Makefile.PL' or die $!;
97 local $/;
98 my $makefile = <$fh>;
99 my($module) = $makefile =~ /all_from 'lib/(.+).pm'/;
100 unless ($module) {
101 return $self->{options}->{module} = shift @{ $self->{argv} };
102 }
103 $module =~ s{/}{::}g;
104 $self->{options}->{module} = $module;
105 }
106  
107  
108  
109 1;
110  
111 lib/YourApp/Helper/Controller.pm is your application helper for controller
112  
113 package YourApp::Helper::Controller;
114 use strict;
115 use warnings;
116 use base 'YourApp::Helper';
117  
118 sub generate_path_option_names { qw/ target / }
119 sub is_append_files { 1 } # append type helper
120  
121 sub helper_flavor_init {
122 my $self = shift;
123  
124 # auto detect target name by @ARGV
125 my $target = $self->{argv}->[0];
126 die 'required target name' unless $target;
127 $self->{helper}->{target} = $target;
128 }
129  
130 1;
131
132 __DATA__
133 ---
134 file: lib/____var-module_path-var____/Controller/____var-yourapp_target_path-var____.pm
135 template: |
136 package [% module %]::Controller::[% yourapp_target %];
137 use base 'YourApp::Controller';
138  
139 my $option = '[% yourapp_option %]';
140  
141 ... some code
142  
143 1;
144 file: t/100_controller/____var-yourapp_target_path-var____.t
145 template: |
146 use Test::More tests => 1;
147 use_ok '[% yourapp_app %]::Controller::[% yourapp_target %]';
148  
149 run the script/helper.pl
150  
151 $ perl ./script/helper.pl Controller Search
152 $ cat lib/Foo/Controller/Search.pm
153 package Foo::Controller::Search;
154 use base 'YourApp::Controller';
155  
156 my $option = 'FOO_OPTION';
157  
158 ... some code
159  
160 1;
161 $ cat t/100_controller/Search.t
162 use Test::More tests => 1;
163 use_ok 'Foo::Controller::Search';
164  
165 =head1 Helper Hook Point
166  
167 =head2 helper_option_prefix (required)
168  
169 When using config of helper by template, prefix specified here is used.
170  
171 sub helper_option_prefix { 'foo' }
172  
173 in your flavor's template
174  
175 [% foo_bar %]
176 # use the $self->{helper}->{bar}
177  
178 =head2 is_append_files
179  
180 When this function returns truth, flavor is added to the existing application.
181  
182 sub is_append_files { 1 } # default is false
183  
184 It is the same work as the time helper of adding Controller of Catalyst.
185  
186 =head2 helper_base_init
187  
188 helper initialize phase for your application helper base class
189  
190 # default
191 sub helper_base_init {
192 my $self = shift;
193 $self->{options}->{module} = shift @{ $self->{argv} };
194 }
195  
196 you can set the base module name
197  
198 =head2 helper_flavor_init
199  
200 helper initialize phase for flavor class
201  
202 sub helper_flavor_init {
203 my $self = shift;
204 $self->{helper} = {
205 # some options for helper
206 };
207 }
208  
209 you can set the target module name, or more options for helper
210  
211 =head3 $helper->{helper}->{setup_template_vars_callback}
212  
213 you can set the callback for setup_template_vars phase
214  
215 sub helper_flavor_init {
216 my $self = shift;
217 # set callback handler for setup_template_vars hook point
218 $self->{helper}->{setup_template_vars_callback} = sub {
219 my($setup, $vars) = @_;
220 $vars->{callback_var} = 'this is callback var';
221 $vars->{callback_path} = 'callback/path';
222 };
223 }
224  
225 example is C<t/Helper/Basic/Callback.pm>
226  
227 =head2 helper_default_plugins
228  
229 Plugin which helper uses is listed.
230  
231 # default
232 sub helper_default_plugins { qw/ Template Helper / }
233  
234 =head2 before_run
235  
236 It is a phase just before helper actually starts Module::Setup.
237  
238 =cut
« blib/lib/Module/Setup/Plugin/Template.pm ↑ Summary blib/lib/Module/Setup/Path.pm »