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 51     51   949 use Mojo::Base -base;
  51         169  
  51         666  
3              
4 51     51   453 use Carp qw(croak);
  51         143  
  51         2904  
5 51     51   442 use Mojo::File qw(path);
  51         202  
  51         2578  
6 51     51   1288 use Mojo::Loader qw(data_section);
  51         190  
  51         3914  
7 51     51   1322 use Mojo::Server;
  51         205  
  51         990  
8 51     51   27097 use Mojo::Template;
  51         187  
  51         363  
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 14 my ($self, $path, $mode) = @_;
18 3         14 path($path)->chmod($mode);
19 3         16 return $self->_loud(" [chmod] $path " . sprintf('%lo', $mode));
20             }
21              
22 3     3 1 810 sub chmod_rel_file { $_[0]->chmod_file($_[0]->rel_file($_[1]), $_[2]) }
23              
24             sub create_dir {
25 24     24 1 60 my ($self, $path) = @_;
26 24 100       89 return $self->_loud(" [exist] $path") if -d $path;
27 14         83 path($path)->make_path;
28 14         77 return $self->_loud(" [mkdir] $path");
29             }
30              
31 2     2 1 1196 sub create_rel_dir { $_[0]->create_dir($_[0]->rel_file($_[1])) }
32              
33 24     24 1 154 sub extract_usage { Mojo::Util::extract_usage((caller)[1]) }
34              
35 4     4 1 14 sub help { print shift->usage }
36              
37 50     50 1 8469 sub rel_file { path->child(split(/\//, pop)) }
38              
39             sub render_data {
40 20     20 1 887 my ($self, $name) = (shift, shift);
41 20         74 my $template = Mojo::Template->new($self->template)->name("template $name from DATA section");
42 20         93 my $output = $template->render(data_section(ref $self, $name), @_);
43 20 100       241 return ref $output ? die $output : $output;
44             }
45              
46             sub render_to_file {
47 19     19 1 56 my ($self, $data, $path) = (shift, shift, shift);
48 19         66 return $self->write_file($path, $self->render_data($data, @_));
49             }
50              
51             sub render_to_rel_file {
52 19     19 1 41 my $self = shift;
53 19         63 $self->render_to_file(shift, $self->rel_file(shift), @_);
54             }
55              
56 1     1 1 2954 sub run { croak 'Method "run" not implemented by subclass' }
57              
58             sub write_file {
59 22     22 1 77 my ($self, $path, $data) = @_;
60 22 100       111 return $self->_loud(" [exist] $path") if -f $path;
61 21         127 $self->create_dir(path($path)->dirname);
62 21         86 path($path)->spew($data);
63 21         126 return $self->_loud(" [write] $path");
64             }
65              
66 3     3 1 839 sub write_rel_file { $_[0]->write_file($_[0]->rel_file($_[1]), $_[2]) }
67              
68             sub _loud {
69 49     49   136 my ($self, $msg) = @_;
70 49 100       155 say $msg unless $self->quiet;
71 49         176 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