Mojolicious/Command.pm

filter: Statementstmt 100% 56 / 56 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 22 / 22 Podpod 100% 13 / 13 totaltotal 100% 99 / 99 CC 5 SCAR 2.7
CC
5
Cov
100%
create_dir
2.0
render_data
2.0
write_file
2.0
CRAP 1.3
« Mojolicious/Command/Author/generate/dockerfile.pm ↑ Summary Mojo/BaseUtil.pm »
1 package Mojolicious::Command;
2 20 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 20 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Mojo::Loader qw(data_section);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Mojo::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use Mojo::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 20 ▶has app => sub { $_[0]{app_ref} = Mojo::Server->new->build_app('Mojo::HelloWorld') }, weak => 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
11 20 has description => 'No description';
12 20 has 'quiet';
13 5 ▶has template => sub { {vars => 1} };
Subroutine __ANON__: called CC=1 SCAR=0.0
14 20 has usage => "Usage: APPLICATION\n";
15  
16 3 ▶sub chmod_file {
Subroutine chmod_file: called CC=1 SCAR=0.0
Pod documented
17 3 my ($self, $path, $mode) = @_;
18 3 path($path)->chmod($mode);
19 3 return $self->_loud(" [chmod] $path " . sprintf('%lo', $mode));
20 }
21  
22 3 ▶sub chmod_rel_file { $_[0]->chmod_file($_[0]->rel_file($_[1]), $_[2]) }
Subroutine chmod_rel_file: called CC=1 SCAR=0.0
Pod documented
23  
24 24 ▶sub create_dir {
Subroutine create_dir: called CC=2 SCAR=6.9
Pod documented
25 24 my ($self, $path) = @_;
26 24 ▶ return $self->_loud(" [exist] $path") if -d $path;
Branch: 1 branch
BranchTrueFalseTotal
if -d $path 10 14 24
27 14 path($path)->make_path;
28 14 return $self->_loud(" [mkdir] $path");
29 }
30  
31 2 ▶sub create_rel_dir { $_[0]->create_dir($_[0]->rel_file($_[1])) }
Subroutine create_rel_dir: called CC=1 SCAR=0.0
Pod documented
32  
33 24 ▶sub extract_usage { Mojo::Util::extract_usage((caller)[1]) }
Subroutine extract_usage: called CC=1 SCAR=0.0
Pod documented
34  
35 4 ▶sub help { print shift->usage }
Subroutine help: called CC=1 SCAR=0.0
Pod documented
36  
37 50 ▶sub rel_file { path->child(split(/\//, pop)) }
Subroutine rel_file: called CC=1 SCAR=0.0
Pod documented
38  
39 20 ▶sub render_data {
Subroutine render_data: called CC=2 SCAR=6.9
Pod documented
40 20 my ($self, $name) = (shift, shift);
41 20 my $template = Mojo::Template->new($self->template)->name("template $name from DATA section");
42 20 my $output = $template->render(data_section(ref $self, $name), @_);
43 20 ▶ return ref $output ? die $output : $output;
Branch: 1 branch
BranchTrueFalseTotal
ref $output ? : 1 19 20
44 }
45  
46 19 ▶sub render_to_file {
Subroutine render_to_file: called CC=1 SCAR=0.0
Pod documented
47 19 my ($self, $data, $path) = (shift, shift, shift);
48 19 return $self->write_file($path, $self->render_data($data, @_));
49 }
50  
51 19 ▶sub render_to_rel_file {
Subroutine render_to_rel_file: called CC=1 SCAR=0.0
Pod documented
52 19 my $self = shift;
53 19 $self->render_to_file(shift, $self->rel_file(shift), @_);
54 }
55  
56 1 ▶sub run { croak 'Method "run" not implemented by subclass' }
Subroutine run: called CC=1 SCAR=0.0
Pod documented
57  
58 22 ▶sub write_file {
Subroutine write_file: called CC=2 SCAR=6.9
Pod documented
59 22 my ($self, $path, $data) = @_;
60 22 ▶ return $self->_loud(" [exist] $path") if -f $path;
Branch: 1 branch
BranchTrueFalseTotal
if -f $path 1 21 22
61 21 $self->create_dir(path($path)->dirname);
62 21 path($path)->spew($data);
63 21 return $self->_loud(" [write] $path");
64 }
65  
66 3 ▶sub write_rel_file { $_[0]->write_file($_[0]->rel_file($_[1]), $_[2]) }
Subroutine write_rel_file: called CC=1 SCAR=0.0
Pod documented
67  
68 49 ▶sub _loud {
Subroutine _loud: called CC=2 SCAR=6.9
69 49 my ($self, $msg) = @_;
70 49 ▶ say $msg unless $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->quiet 47 2 49
71 49 return $self;
72 }
73  
74 20 1;
75  
76 =encoding utf8
77  
78 =head1 NAME
79  
80 Mojolicious::Command - Command base class
81  
82 =head1 SYNOPSIS
83  
84 # Lowercase command name
85 package Mojolicious::Command::mycommand;
86 use Mojo::Base 'Mojolicious::Command', -signatures;
87  
88 # Short description
89 has description => 'My first Mojo command';
90  
91 # Usage message from SYNOPSIS
92 has usage => sub ($self) { $self->extract_usage };
93  
94 sub run ($self, @args) {
95  
96 # Magic here! :)
97 }
98  
99 =head1 SYNOPSIS
100  
101 Usage: APPLICATION mycommand [OPTIONS]
102  
103 Options:
104 -s, --something Does something
105  
106 =cut
107  
108 =head1 DESCRIPTION
109  
110 L<Mojolicious::Command> is an abstract base class for L<Mojolicious> commands.
111  
112 See L<Mojolicious::Commands/"COMMANDS"> for a list of commands that are available by default.
113  
114 =head1 ATTRIBUTES
115  
116 L<Mojolicious::Command> implements the following attributes.
117  
118 =head2 app
119  
120 my $app = $command->app;
121 $command = $command->app(Mojolicious->new);
122  
123 Application for command, defaults to a L<Mojo::HelloWorld> object. Note that this attribute is weakened.
124  
125 # Introspect
126 say "Template path: $_" for @{$command->app->renderer->paths};
127  
128 =head2 description
129  
130 my $description = $command->description;
131 $command = $command->description('Foo');
132  
133 Short description of command, used for the command list.
134  
135 =head2 quiet
136  
137 my $bool = $command->quiet;
138 $command = $command->quiet($bool);
139  
140 Limited command output.
141  
142 =head2 template
143  
144 my $template = $command->template;
145 $command = $command->template({vars => 1});
146  
147 Attribute values passed to L<Mojo::Template> objects used to render templates with L</"render_data">, defaults to
148 activating C<vars>.
149  
150 =head2 usage
151  
152 my $usage = $command->usage;
153 $command = $command->usage('Foo');
154  
155 Usage information for command, used for the help screen.
156  
157 =head1 METHODS
158  
159 L<Mojolicious::Command> inherits all methods from L<Mojo::Base> and implements the following new ones.
160  
161 =head2 chmod_file
162  
163 $command = $command->chmod_file('/home/sri/foo.txt', 0644);
164  
165 Change mode of a file.
166  
167 =head2 chmod_rel_file
168  
169 $command = $command->chmod_rel_file('foo/foo.txt', 0644);
170  
171 Portably change mode of a file relative to the current working directory.
172  
173 =head2 create_dir
174  
175 $command = $command->create_dir('/home/sri/foo/bar');
176  
177 Create a directory if it does not exist already.
178  
179 =head2 create_rel_dir
180  
181 $command = $command->create_rel_dir('foo/bar/baz');
182  
183 Portably create a directory relative to the current working directory if it does not exist already.
184  
185 =head2 extract_usage
186  
187 my $usage = $command->extract_usage;
188  
189 Extract usage message from the SYNOPSIS section of the file this method was called from with
190 L<Mojo::Util/"extract_usage">.
191  
192 =head2 help
193  
194 $command->help;
195  
196 Print usage information for command.
197  
198 =head2 rel_file
199  
200 my $path = $command->rel_file('foo/bar.txt');
201  
202 Return a L<Mojo::File> object relative to the current working directory.
203  
204 =head2 render_data
205  
206 my $data = $command->render_data('foo_bar');
207 my $data = $command->render_data('foo_bar', @args);
208 my $data = $command->render_data('foo_bar', {foo => 'bar'});
209  
210 Render a template from the C<DATA> section of the command class with L<Mojo::Loader> and L<Mojo::Template>. The
211 template can be configured with L</"template">.
212  
213 =head2 render_to_file
214  
215 $command = $command->render_to_file('foo_bar', '/home/sri/foo.txt');
216 $command = $command->render_to_file('foo_bar', '/home/sri/foo.txt', @args);
217 $command = $command->render_to_file(
218 'foo_bar', '/home/sri/foo.txt', {foo => 'bar'});
219  
220 Render a template with L</"render_data"> to a file if it does not exist already, and create the directory if necessary.
221  
222 =head2 render_to_rel_file
223  
224 $command = $command->render_to_rel_file('foo_bar', 'foo/bar.txt');
225 $command = $command->render_to_rel_file('foo_bar', 'foo/bar.txt', @args);
226 $command = $command->render_to_rel_file(
227 'foo_bar', 'foo/bar.txt', {foo => 'bar'});
228  
229 Portably render a template with L</"render_data"> to a file relative to the current working directory if it does not
230 exist already, and create the directory if necessary.
231  
232 =head2 run
233  
234 $command->run;
235 $command->run(@ARGV);
236  
237 Run command. Meant to be overloaded in a subclass.
238  
239 =head2 write_file
240  
241 $command = $command->write_file('/home/sri/foo.txt', 'Hello World!');
242  
243 Write text to a file if it does not exist already, and create the directory if necessary.
244  
245 =head2 write_rel_file
246  
247 $command = $command->write_rel_file('foo/bar.txt', 'Hello World!');
248  
249 Portably write text to a file relative to the current working directory if it does not exist already, and create the
250 directory if necessary.
251  
252 =head1 SEE ALSO
253  
254 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
255  
256 =cut
« Mojolicious/Command/Author/generate/dockerfile.pm ↑ Summary Mojo/BaseUtil.pm »