source: main/trunk/greenstone2/perllib/cpan/Mojolicious/Commands.pm

Last change on this file was 32205, checked in by ak19, 6 years ago

First set of commits to do with implementing the new 'paged_html' output option of PDFPlugin that uses using xpdftools' new pdftohtml. So far tested only on Linux (64 bit), but things work there so I'm optimistically committing the changes since they work. 2. Committing the pre-built Linux binaries of XPDFtools for both 32 and 64 bit built by the XPDF group. 2. To use the correct bitness variant of xpdftools, setup.bash now exports the BITNESS env var, consulted by gsConvert.pl. 3. All the perl code changes to do with using xpdf tools' pdftohtml to generate paged_html and feed it in the desired form into GS(3): gsConvert.pl, PDFPlugin.pm and its parent ConvertBinaryPFile.pm have been modified to make it all work. xpdftools' pdftohtml generates a folder containing an html file and a screenshot for each page in a PDF (as well as an index.html linking to each page's html). However, we want a single html file that contains each individual 'page' html's content in a div, and need to do some further HTML style, attribute and structure modifications to massage the xpdftool output to what we want for GS. In order to parse and manipulate the HTML 'DOM' to do this, we're using the Mojo::DOM package that Dr Bainbridge found and which he's compiled up. Mojo::DOM is therefore also committed in this revision. Some further changes and some display fixes are required, but need to check with the others about that.

File size: 7.9 KB
Line 
1package Mojolicious::Commands;
2use Mojo::Base 'Mojolicious::Command';
3
4use Mojo::Loader qw(find_modules find_packages load_class);
5use Mojo::Server;
6use Mojo::Util qw(getopt tablify);
7
8has hint => <<EOF;
9
10See 'APPLICATION help COMMAND' for more information on a specific command.
11EOF
12has message => sub { shift->extract_usage . "\nCommands:\n" };
13has namespaces => sub { ['Mojolicious::Command'] };
14
15sub detect {
16
17 # PSGI (Plack only for now)
18 return 'psgi' if defined $ENV{PLACK_ENV};
19
20 # CGI
21 return 'cgi' if defined $ENV{PATH_INFO} || defined $ENV{GATEWAY_INTERFACE};
22
23 # Nothing
24 return undef;
25}
26
27sub run {
28 my ($self, $name, @args) = @_;
29
30 # Application loader
31 return $self->app if defined $ENV{MOJO_APP_LOADER};
32
33 # Try to detect environment
34 if (!$ENV{MOJO_NO_DETECT} && (my $env = $self->detect)) { $name = $env }
35
36 # Run command
37 if ($name && $name =~ /^\w+$/ && ($name ne 'help' || $args[0])) {
38
39 # Help
40 $name = shift @args if my $help = $name eq 'help';
41 $help = $ENV{MOJO_HELP} ||= $help;
42
43 # Remove options shared by all commands before loading the command
44 _args(\@args);
45 my $module;
46 $module = _command("${_}::$name", 1) and last for @{$self->namespaces};
47
48 # Unknown command
49 die qq{Unknown command "$name", maybe you need to install it?\n}
50 unless $module;
51
52 # Run command
53 my $command = $module->new(app => $self->app);
54 return $help ? $command->help(@args) : $command->run(@args);
55 }
56
57 # Hide list for tests
58 return 1 if $ENV{HARNESS_ACTIVE};
59
60 # Find all available commands
61 my %all;
62 for my $ns (@{$self->namespaces}) {
63 $all{substr $_, length "${ns}::"} //= $_->new->description
64 for grep { _command($_) } find_modules($ns), find_packages($ns);
65 }
66
67 my @rows = map { [" $_", $all{$_}] } sort keys %all;
68 return print $self->message, tablify(\@rows), $self->hint;
69}
70
71sub start_app { shift; Mojo::Server->new->build_app(shift)->start(@_) }
72
73# Command line options for MOJO_HELP, MOJO_HOME and MOJO_MODE
74sub _args {
75 getopt shift, ['pass_through'],
76 'h|help' => \$ENV{MOJO_HELP},
77 'home=s' => \$ENV{MOJO_HOME},
78 'm|mode=s' => \$ENV{MOJO_MODE}
79 unless __PACKAGE__->detect;
80}
81
82# Do not remove options from @ARGV
83BEGIN { _args([@ARGV]) }
84
85sub _command {
86 my ($module, $fatal) = @_;
87 return $module->isa('Mojolicious::Command') ? $module : undef
88 unless my $e = load_class $module;
89 $fatal && ref $e ? die $e : return undef;
90}
91
921;
93
94=encoding utf8
95
96=head1 NAME
97
98Mojolicious::Commands - Command line interface
99
100=head1 SYNOPSIS
101
102 Usage: APPLICATION COMMAND [OPTIONS]
103
104 mojo version
105 mojo generate lite_app
106 ./myapp.pl daemon -m production -l http://*:8080
107 ./myapp.pl get /foo
108 ./myapp.pl routes -v
109
110 Tip: CGI and PSGI environments can be automatically detected very often and
111 work without commands.
112
113 Options (for all commands):
114 -h, --help Get more information on a specific command
115 --home <path> Path to home directory of your application, defaults to
116 the value of MOJO_HOME or auto-detection
117 -m, --mode <name> Operating mode for your application, defaults to the
118 value of MOJO_MODE/PLACK_ENV or "development"
119
120=head1 DESCRIPTION
121
122L<Mojolicious::Commands> is the interactive command line interface for the
123L<Mojolicious> framework. It will automatically detect available commands in
124the C<Mojolicious::Command> namespace.
125
126=head1 COMMANDS
127
128These commands are available by default.
129
130=head2 cgi
131
132 $ ./myapp.pl cgi
133
134Use L<Mojolicious::Command::cgi> to start application with CGI backend, usually
135auto detected.
136
137=head2 cpanify
138
139 $ mojo cpanify -u sri -p secr3t Mojolicious-Plugin-Fun-0.1.tar.gz
140
141Use L<Mojolicious::Command::cpanify> for uploading files to CPAN.
142
143=head2 daemon
144
145 $ ./myapp.pl daemon
146
147Use L<Mojolicious::Command::daemon> to start application with standalone HTTP
148and WebSocket server.
149
150=head2 eval
151
152 $ ./myapp.pl eval 'say app->home'
153
154Use L<Mojolicious::Command::eval> to run code against application.
155
156=head2 generate
157
158 $ mojo generate
159 $ mojo generate help
160 $ ./myapp.pl generate help
161
162List available generator commands with short descriptions.
163
164 $ mojo generate help <generator>
165 $ ./myapp.pl generate help <generator>
166
167List available options for generator command with short descriptions.
168
169=head2 generate app
170
171 $ mojo generate app <AppName>
172
173Use L<Mojolicious::Command::generate::app> to generate application directory
174structure for a fully functional L<Mojolicious> application.
175
176=head2 generate lite_app
177
178 $ mojo generate lite_app
179
180Use L<Mojolicious::Command::generate::lite_app> to generate a fully functional
181L<Mojolicious::Lite> application.
182
183=head2 generate makefile
184
185 $ mojo generate makefile
186 $ ./myapp.pl generate makefile
187
188Use L<Mojolicious::Command::generate::makefile> to generate C<Makefile.PL> file
189for application.
190
191=head2 generate plugin
192
193 $ mojo generate plugin <PluginName>
194
195Use L<Mojolicious::Command::generate::plugin> to generate directory structure
196for a fully functional L<Mojolicious> plugin.
197
198=head2 get
199
200 $ mojo get https://mojolicious.org
201 $ ./myapp.pl get /foo
202
203Use L<Mojolicious::Command::get> to perform requests to remote host or local
204application.
205
206=head2 help
207
208 $ mojo
209 $ mojo help
210 $ ./myapp.pl help
211
212List available commands with short descriptions.
213
214 $ mojo help <command>
215 $ ./myapp.pl help <command>
216
217List available options for the command with short descriptions.
218
219=head2 inflate
220
221 $ ./myapp.pl inflate
222
223Use L<Mojolicious::Command::inflate> to turn templates and static files
224embedded in the C<DATA> sections of your application into real files.
225
226=head2 prefork
227
228 $ ./myapp.pl prefork
229
230Use L<Mojolicious::Command::prefork> to start application with standalone
231pre-forking HTTP and WebSocket server.
232
233=head2 psgi
234
235 $ ./myapp.pl psgi
236
237Use L<Mojolicious::Command::psgi> to start application with PSGI backend,
238usually auto detected.
239
240=head2 routes
241
242 $ ./myapp.pl routes
243
244Use L<Mojolicious::Command::routes> to list application routes.
245
246=head2 test
247
248 $ ./myapp.pl test
249 $ ./myapp.pl test t/fun.t
250
251Use L<Mojolicious::Command::test> to run application tests from the C<t>
252directory.
253
254=head2 version
255
256 $ mojo version
257 $ ./myapp.pl version
258
259Use L<Mojolicious::Command::version> to show version information for available
260core and optional modules, very useful for debugging.
261
262=head1 ATTRIBUTES
263
264L<Mojolicious::Commands> inherits all attributes from L<Mojolicious::Command>
265and implements the following new ones.
266
267=head2 hint
268
269 my $hint = $commands->hint;
270 $commands = $commands->hint('Foo');
271
272Short hint shown after listing available commands.
273
274=head2 message
275
276 my $msg = $commands->message;
277 $commands = $commands->message('Hello World!');
278
279Short usage message shown before listing available commands.
280
281=head2 namespaces
282
283 my $namespaces = $commands->namespaces;
284 $commands = $commands->namespaces(['MyApp::Command']);
285
286Namespaces to load commands from, defaults to C<Mojolicious::Command>.
287
288 # Add another namespace to load commands from
289 push @{$commands->namespaces}, 'MyApp::Command';
290
291=head1 METHODS
292
293L<Mojolicious::Commands> inherits all methods from L<Mojolicious::Command> and
294implements the following new ones.
295
296=head2 detect
297
298 my $env = $commands->detect;
299
300Try to detect environment, or return C<undef> if none could be detected.
301
302=head2 run
303
304 $commands->run;
305 $commands->run(@ARGV);
306
307Load and run commands. Automatic deployment environment detection can be
308disabled with the C<MOJO_NO_DETECT> environment variable.
309
310=head2 start_app
311
312 Mojolicious::Commands->start_app('MyApp');
313 Mojolicious::Commands->start_app(MyApp => @ARGV);
314
315Load application from class and start the command line interface for it. Note
316that the options C<-h>/C<--help>, C<--home> and C<-m>/C<--mode>, which are
317shared by all commands, will be parsed from C<@ARGV> during compile time.
318
319 # Always start daemon for application
320 Mojolicious::Commands->start_app('MyApp', 'daemon', '-l', 'http://*:8080');
321
322=head1 SEE ALSO
323
324L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
325
326=cut
Note: See TracBrowser for help on using the repository browser.