File Coverage

blib/lib/Mojolicious/Command.pm
Criterion Covered Total %
statement 48 48 100.0
branch 8 8 100.0
condition n/a
subroutine 20 20 100.0
pod 13 13 100.0
total 89 89 100.0


line stmt bran cond sub pod time code
1             package Mojolicious::Command;
2 50     50   930 use Mojo::Base -base;
  50         142  
  50         421  
3              
4 50     50   418 use Carp qw(croak);
  50         176  
  50         2999  
5 50     50   408 use Mojo::File qw(path);
  50         176  
  50         2579  
6 50     50   1208 use Mojo::Loader qw(data_section);
  50         188  
  50         2475  
7 50     50   1127 use Mojo::Server;
  50         164  
  50         1333  
8 50     50   24763 use Mojo::Template;
  50         166  
  50         335  
9              
10             has app => sub { $_[0]{app_ref} = Mojo::Server->new->build_app('Mojo::HelloWorld') }, weak => 1;
11             has description => 'No description';
12             has 'quiet';
13             has template => sub { {vars => 1} };
14             has usage => "Usage: APPLICATION\n";
15              
16             sub chmod_file {
17 3     3 1 12 my ($self, $path, $mode) = @_;
18 3         13 path($path)->chmod($mode);
19 3         14 return $self->_loud(" [chmod] $path " . sprintf('%lo', $mode));
20             }
21              
22 3     3 1 737 sub chmod_rel_file { $_[0]->chmod_file($_[0]->rel_file($_[1]), $_[2]) }
23              
24             sub create_dir {
25 24     24 1 71 my ($self, $path) = @_;
26 24 100       89 return $self->_loud(" [exist] $path") if -d $path;
27 14         69 path($path)->make_path;
28 14         70 return $self->_loud(" [mkdir] $path");
29             }
30              
31 2     2 1 984 sub create_rel_dir { $_[0]->create_dir($_[0]->rel_file($_[1])) }
32              
33 24     24 1 125 sub extract_usage { Mojo::Util::extract_usage((caller)[1]) }
34              
35 4     4 1 15 sub help { print shift->usage }
36              
37 50     50 1 6928 sub rel_file { path->child(split(/\//, pop)) }
38              
39             sub render_data {
40 20     20 1 824 my ($self, $name) = (shift, shift);
41 20         72 my $template = Mojo::Template->new($self->template)->name("template $name from DATA section");
42 20         89 my $output = $template->render(data_section(ref $self, $name), @_);
43 20 100       250 return ref $output ? die $output : $output;
44             }
45              
46             sub render_to_file {
47 19     19 1 60 my ($self, $data, $path) = (shift, shift, shift);
48 19         80 return $self->write_file($path, $self->render_data($data, @_));
49             }
50              
51             sub render_to_rel_file {
52 19     19 1 74 my $self = shift;
53 19         63 $self->render_to_file(shift, $self->rel_file(shift), @_);
54             }
55              
56 1     1 1 2587 sub run { croak 'Method "run" not implemented by subclass' }
57              
58             sub write_file {
59 22     22 1 65 my ($self, $path, $data) = @_;
60 22 100       104 return $self->_loud(" [exist] $path") if -f $path;
61 21         114 $self->create_dir(path($path)->dirname);
62 21         83 path($path)->spurt($data);
63 21         109 return $self->_loud(" [write] $path");
64             }
65              
66 3     3 1 725 sub write_rel_file { $_[0]->write_file($_[0]->rel_file($_[1]), $_[2]) }
67              
68             sub _loud {
69 49     49   128 my ($self, $msg) = @_;
70 49 100       144 say $msg unless $self->quiet;
71 49         185 return $self;
72             }
73              
74             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 is an abstract base class for L commands.
111              
112             See L for a list of commands that are available by default.
113              
114             =head1 ATTRIBUTES
115              
116             L 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 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 objects used to render templates with L, defaults to
148             activating C.
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 inherits all methods from L 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.
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 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 section of the command class with L and L. The
211             template can be configured with L.
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 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 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, L, L.
255              
256             =cut