blib/lib/Dallycot/CLI.pm untested

filter: Statementstmt 0.0% 0 / 271 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 19 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 391 CC 30 SCAR 42.6
CC
30
Cov
0%
run_file
240.0
run
20.0
process_line
20.0
CRAP 70.7
1 package Dallycot::CLI;
2 our $AUTHORITY = 'cpan:JSMITH';
3  
4 # ABSTRACT: command line interface application
5  
6 use utf8;
7 use Moose;
8 with 'MooseX::Getopt';
9  
10 use Mojo;
11 use AnyEvent;
12 use Promises qw(deferred), backend => ['AnyEvent'];
13  
14 use Dallycot;
15 use Dallycot::Compiler;
16 use Dallycot::Parser;
17 use Dallycot::Processor;
18 use Dallycot::Channel::Terminal;
19 use MooseX::Types::Moose qw(ArrayRef);
20  
21 my $has_tangle = eval {
22 require Dallycot::Tangle;
23 1;
24 };
25  
26 if ($@) { print STDERR "$@\n"; }
27  
28 BEGIN {
29 require Dallycot::Library;
30 Dallycot::Library->libraries;
31 }
32  
33 has 'c' => (
34 is => 'ro',
35 isa => 'Bool',
36 traits => ['Getopt'],
37 documentation => 'check syntax only (parses but does not execute)',
38 );
39  
40 has 'o' => (
41 is => 'ro',
42 isa => 'Str',
43 traits => ['Getopt'],
44 documentation => 'output RDF/XML to provided file (does not execute)',
45 );
46  
47 #has 'O' => (
48 # is => 'ro',
49 # isa => 'Str',
50 # traits => ['Getopt'],
51 # documentation => 'output result of execution as RDF/XML to provided file',
52 # cmd_flag => 'O'
53 #);
54  
55 has 'S' => (
56 is => 'ro',
57 isa => 'Bool',
58 documentation => 'look for programfile using PATH environment variable'
59 );
60  
61 has 'v' => (
62 is => 'ro',
63 isa => 'Bool',
64 traits => ['Getopt'],
65 documentation => 'print version and licensing banner and exit',
66 cmd_flag => 'v',
67 );
68  
69 has 'V' => (
70 is => 'ro',
71 isa => 'Bool',
72 traits => ['Getopt'],
73 documentation => 'print installed namespaces and exit',
74 cmd_flag => 'V',
75 );
76  
77 has '_parser' => (
78 accessor => 'parser',
79 default => sub { Dallycot::Parser->new }
80 );
81  
82 has '_engine' => (
83 accessor => 'engine',
84 default => sub {
85 Dallycot::Processor->new(
86 max_cost => 1_000_000,
87 ignore_cost => 1
88 );
89 }
90 );
91  
92 has '_channel' => ( accessor => 'channel', );
93  
94 has '_prompt' => (
95 accessor => 'prompt',
96 default => 'in[%d] := ',
97 );
98  
99 has '_deep_prompt' => (
100 accessor => 'deep_prompt',
101 default => '>>> ',
102 );
103  
104 has '_statement_counter' => (
105 accessor => 'statement_counter',
106 default => 1,
107 isa => 'Int'
108 );
109  
110 has '_done' => ( accessor => 'done', );
111  
112 has '_in' => (
113 accessor => 'in',
114 default => sub { Dallycot::Value::Vector->new }
115 );
116  
117 has '_out' => (
118 accessor => 'out',
119 default => sub { Dallycot::Value::Vector->new }
120 );
121  
122 sub check {
123 my ($self) = @_;
124 return $self->c;
125 }
126  
127 sub run {
128 my ($app) = @_;
129  
130 $app->channel( Dallycot::Channel::Terminal->new );
131  
132 if ( $app->v ) {
133 $app->print_banner;
134 my $d = deferred;
135 $d->resolve(undef);
136 return $d->promise;
137 }
138  
139 if ( $app->V ) {
140 $app->print_library_namespaces;
141 my $d = deferred;
142 $d->resolve(undef);
143 return $d->promise;
144 }
145  
146 $app->engine->create_channel( '$OUTPUT', $app->channel );
147  
148 $app->engine->create_channel( '$INPUT', $app->channel );
149  
150 my @args = @{ $app->extra_argv };
151  
152 if (@args) {
153 return $app->run_files(@args);
154 }
155  
156 my $d = deferred;
157  
158 $app->done($d);
159  
160 $app->print_banner;
161  
162 # load .dallycot - but no error if it doesn't exist
163 $app->run_file( $ENV{'HOME'} . "/.dallycot", 1 )->done(
164 sub {
165 my ( $in, $out );
166  
167 $app->engine->append_namespace_search_path(Dallycot::Library::CLI->namespace);
168  
169 $app->engine->add_assignment( 'in', $app->in );
170  
171 $app->engine->add_assignment( 'out', $app->out );
172  
173 $app->primary_prompt;
174 },
175 sub {
176 print STDERR "*** ", @_, "\n";
177 }
178 );
179  
180 return $d->promise;
181 }
182  
183 sub print_banner {
184 my ($app) = @_;
185  
186 my $out = $app->channel;
187  
188 $Dallycot::VERSION //= 'm.xxyyyz';
189 $out->send_data(
190 "Dallycot, version $Dallycot::VERSION.\n",
191 "Copyright (C) 2014-2015 James Smith.\n",
192 "This is free software licensed under the same terms as Perl 5.\n",
193 "There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or\n",
194 "FITNESS FOR A PARTICULAR PURPOSE.\n\n",
195 "Additional information about Dallycot is available at http://www.dallycot.net/.\n\n",
196 "Please contribute if you find this software useful.\n",
197 "For more information, visit http://www.dallycot.net/get-involved/.\n"
198 );
199 return;
200 }
201  
202 sub print_library_namespaces {
203 my ($app) = @_;
204  
205 my %namespaces;
206  
207 foreach my $lib ( Dallycot::Library->libraries ) {
208 $namespaces{ $lib->instance->namespace } = $lib;
209 }
210  
211 my $out = $app->channel;
212  
213 if ( keys %namespaces ) {
214 $out->send_data("The following namespaces are installed:\n");
215 }
216 else {
217 $out->send_data("No namespaces are installed\n");
218 }
219  
220 foreach my $ns ( sort keys %namespaces ) {
221 $out->send_data(" $ns\n");
222 }
223 return;
224 }
225  
226 sub run_files {
227 my ( $app, @files ) = @_;
228  
229 my $d = deferred;
230  
231 $app->_run_files( $d, @files );
232  
233 return $d->promise;
234 }
235  
236 sub _run_files {
237 my ( $app, $d, $file, @files ) = @_;
238  
239 $app->run_file($file)->done(
240 sub {
241 if (@files) {
242 $app->_run_files( $d, @files );
243 }
244 else {
245 $d->resolve();
246 }
247 },
248 sub {
249 $d->reject(@_);
250 }
251 );
252 return;
253 }
254  
255 sub primary_prompt {
256 my ($app) = @_;
257  
258 $app->channel->receive_data(
259 prompt => Dallycot::Value::String->new( sprintf( "\n" . $app->prompt, $app->statement_counter ) ) )
260 ->done(
261 sub {
262 my ($line) = @_;
263 if ( $line->isa('Dallycot::Value::String') ) {
264 if ( $line->value =~ m{^\s*$} ) {
265 $app->add_history($line);
266 $app->primary_prompt;
267 }
268 else {
269 $app->check_parse($line);
270 }
271 }
272 else {
273 $app->channel->send_data("\n");
274 $app->done->resolve(undef);
275 }
276 },
277 sub {
278 my ($err) = @_;
279 $app->channel->send_data("*** $err\n");
280 $app->done->resolve(undef);
281 }
282 );
283 return;
284 }
285  
286 sub check_parse {
287 my ( $app, $line ) = @_;
288  
289 my $parse = $app->parser->parse( $line->value );
290 if ( !defined($parse)
291 || @$parse == 1 && $parse->[0]->isa('Dallycot::AST::Expr') && !$app->parser->error )
292 {
293 $app->secondary_prompt($line);
294 }
295 else {
296 $app->process_line( $line, $parse );
297 }
298 return;
299 }
300  
301 sub secondary_prompt {
302 my ( $app, $line ) = @_;
303  
304 $app->channel->receive_data( prompt => Dallycot::Value::String->new( $app->deep_prompt ) )->done(
305 sub {
306 my ($next_line) = @_;
307 if ( $next_line->is_defined ) {
308 $line = Dallycot::Value::String->new( $line->value . "\n" . $next_line->value );
309 $app->check_parse($line);
310 }
311 else {
312 $app->process_line( $line, undef );
313 }
314 }
315 );
316 return;
317 }
318  
319 sub add_history {
320 my ( $app, $line ) = @_;
321  
322 my $in = $app->in;
323 my $stmt_counter = $app->statement_counter;
324 $app->statement_counter( $app->statement_counter + 1 );
325 $app->channel->add_history($line);
326  
327 ${$in}[ $stmt_counter - 1 ] = $line;
328 return $stmt_counter;
329 }
330  
331 sub process_line {
332 my ( $app, $line, $parse ) = @_;
333  
334 my $stmt_counter = $app->add_history($line);
335  
336 if ( $app->parser->error ) {
337 $app->channel->send_data( $app->parser->error );
338 $app->primary_prompt;
339 }
340 elsif ( defined $parse ) {
341 $app->execute($parse)->then(
342 sub {
343 my ($ret) = @_;
344 if ( defined $ret ) {
345 $app->channel->send_data( "\nout[$stmt_counter] := ", $ret->as_text );
346 my $out = $app->out;
347 ${$out}[ $stmt_counter - 1 ] = $ret;
348 }
349 },
350 sub {
351 my ($error) = @_;
352 $app->channel->send_data("*** $error\n");
353 }
354 )->finally(
355 sub {
356 $app->channel->send_data("\n");
357 $app->primary_prompt;
358 }
359 )->catch( sub {
360 print STDERR "Uh oh: @_\n";
361 } )->done( sub { } );
362 }
363 else {
364 $app->channel->send_data("*** Unable to parse\n");
365 $app->primary_prompt;
366 }
367 return;
368 }
369  
370 sub run_file {
371 my ( $app, $filename, $ignore_existance ) = @_;
372  
373 my $file;
374 if ( $app->S ) {
375 my @dirs = ( split( /:/, $ENV{PATH} ), '.' );
376 $file = shift(@dirs) . '/' . $filename;
377 while ( !-f $file && @dirs ) {
378 $file = shift(@dirs) . '/' . $filename;
379 }
380 }
381 else {
382 $file = $filename;
383 }
384 if ( -f $file ) {
385 open my $file, "<", $file or do {
386 my $d = deferred;
387 $d->reject("Unable to read $filename");
388 return $d->promise;
389 };
390 local ($/) = undef;
391 my $source = <$file>;
392 close $file;
393 if ( $filename =~ m{\.md$} ) {
394 if ($has_tangle) {
395 $source = Dallycot::Tangle->new->parse($source);
396 }
397 else {
398 my $d = deferred;
399 $d->reject("Unable to parse markdown: Markdent or its prerequisites are not installed");
400 return $d->promise;
401 }
402 }
403 my $parse = $app->parser->parse($source);
404 if ( $app->parser->warnings ) {
405 $app->channel->send_data( "Warnings:\n " . join( "\n ", $app->parser->warnings ) . "\n" );
406 }
407 if ( !$parse ) {
408 my $err = $app->parser->error;
409 my $d = deferred;
410 if ($err) {
411 $d->reject("In $filename:\n$err");
412 }
413 else {
414 $d->reject("Unable to parse $filename");
415 }
416 return $d->promise;
417 }
418 elsif ( $app->check ) {
419 my $d = deferred;
420 $d->resolve();
421 return $d->promise;
422 }
423 elsif ( $app->o ) {
424 my $model = Dallycot::Compiler -> new;
425 my $root = $model -> compile(@$parse);
426 my $xml;
427 if($app->o =~ /\.ttl$/) {
428 $xml = $model -> as_turtle;
429 }
430 elsif($app->o =~ /\.n3/) {
431 $xml = $model -> as_ntriples;
432 }
433 elsif($app->o =~ /\.tsv/) {
434 $xml = $model -> as_tsv;
435 }
436 elsif($app->o =~ /\.dot/) {
437 $xml = $model -> as_dot;
438 }
439 else {
440 $xml = $model -> as_xml;
441 }
442 open my $fh, ">", $app->o or die "Unable to open " . $app->o . " for writing\n";
443 print $fh $xml;
444 close $fh;
445 my $d = deferred;
446 $d -> resolve;
447 return $d -> promise;
448 }
449 else {
450 return $app->execute($parse);
451 }
452 }
453 elsif ( !$ignore_existance ) {
454 my $d = deferred;
455 $d->reject("Unable to read $filename");
456 return $d->promise;
457 }
458 else {
459 my $d = deferred;
460 $d->resolve(undef);
461 return $d->promise;
462 }
463 }
464  
465 sub execute {
466 my ( $app, $parse ) = @_;
467  
468 my $d = eval {
469 if ( !is_ArrayRef($parse) ) {
470 $parse = [$parse];
471 }
472 $app->engine->add_cost( -$app->engine->cost );
473 return $app->engine->execute( @{$parse} )->catch(
474 sub {
475 my ($err) = @_;
476 while ( $err =~ s{\s+at\s.+?\sline\s+\d+.*?$}{}x ) {
477  
478 # noop
479 }
480  
481 $app->channel->send_data( '*** ' . $err . "\n" );
482 }
483 );
484 };
485 if ($d) {
486 return $d;
487 }
488 elsif ($@) {
489 my $err = $@;
490 while ( $err =~ s{\s+at\s.+?\sline\s+\d+.*?$}{}x ) {
491  
492 # noop
493 }
494  
495 $app->channel->send_data( '*** ' . $err . "\n" );
496 }
497 else {
498 $app->channel->send_data('*** Unable to execute\n');
499 }
500 $d = deferred;
501 $d->resolve();
502 return $d->promise;
503 }
504  
505 __PACKAGE__ -> meta -> make_immutable;
506  
507 1;