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