blib/lib/Lingy/Main.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 164 CC 20 SCAR 38.6
CC
20
Cov
0%
getopt
110.0
run
56.0
do_eval
20.0
CRAP 47.2
1 use strict; use warnings;
2 package Lingy::Main;
3  
4 use Lingy::RT;
5 use Lingy::Common;
6  
7 use Getopt::Long;
8  
9 use constant default => '--repl';
10 use constant options => +{
11 'clj|C' => 'bool',
12 'dev|D' => 'bool',
13 'eval|e' => 'str',
14 ppp => 'bool',
15 repl => 'bool',
16 run => 'arg',
17 version => 'bool',
18 xxx => 'bool',
19 };
20  
21 sub new {
22 my $class = shift;
23  
24 bless {
25 map( ($_, ''), keys %{$class->options} ),
26 @_,
27 }, $class;
28 }
29  
30 sub run {
31 my ($self, @args) = @_;
32  
33 $self->getopt(@args);
34  
35 my ($repl, $run, $eval, $version, $clj, $dev, $args) =
36 @{$self}{qw<repl run eval version clj dev args>};
37 local @ARGV = @$args;
38  
39 RT->init;
40 RT->rep(qq<(clojure-repl-on)>) if $clj;
41 RT->rep(qq<(use 'lingy.devel)>) if $dev;
42  
43 $version ? $self->do_version() :
44 $eval ? $self->do_eval() :
45 $repl ? $self->do_repl() :
46 $run ? $self->do_run() :
47 $self->do_repl();
48 }
49  
50 sub do_version {
51 RT->rep(
52 '(println (str "Lingy [" *HOST* "] version " (lingy-version)))',
53 );
54 }
55  
56 sub do_eval {
57 my ($self) = @_;
58 my ($repl, $eval, $ppp, $xxx) =
59 @{$self}{qw<repl eval ppp xxx>};
60  
61 if ($repl) {
62 RT->rep(qq<(do $eval\n)>);
63 RT->repl;
64 } else {
65 if ($ppp) {
66 RT->rep(qq<(use 'lingy.devel) (PPP (quote $eval\n))>);
67 } elsif ($xxx) {
68 RT->rep(qq<(use 'lingy.devel) (XXX (quote $eval\n))>);
69 } else {
70 unshift @ARGV, '-';
71 map print("$_\n"),
72 grep $_ ne 'nil',
73 RT->rep($eval);
74 }
75 }
76 }
77  
78 sub do_repl {
79 RT->repl;
80 }
81  
82 sub do_run {
83 my ($self) = @_;
84 my $run = $self->{run};
85 if ($run ne '/dev/stdin') {
86 -f $run or err "No such file '$run'";
87 }
88 RT->rep(qq<(load-file "$run")>);
89 }
90  
91 sub from_stdin {
92 not -t STDIN or exists $ENV{LINGY_TEST_STDIN};
93 }
94  
95 sub getopt {
96 my ($self, @args) = @_;
97  
98 my $default = $self->default;
99  
100 if ($default and not(@args or $self->from_stdin)) {
101 @args = ($default);
102 }
103  
104 local @ARGV = @args;
105  
106 my $spec = {};
107 my $opts = $self->options;
108 for my $key (keys %$opts) {
109 (my $name = $key) =~ s/\|.*//;
110 my $type = $opts->{$key};
111 if ($type eq 'bool') {
112 $spec->{$key} = \$self->{$name};
113 }
114 elsif ($type eq 'str') {
115 $spec->{"$key=s"} = \$self->{$name};
116 }
117 elsif ($type eq 'arg') {
118 }
119 else {
120 err "Option type '$type' not supported";
121 }
122 }
123  
124 $spec->{help} = sub {
125 print $ENV{LINGY_USAGE};
126 exit 0;
127 };
128  
129 Getopt::Long::Configure(qw(
130 gnu_getopt
131 no_auto_abbrev
132 no_ignore_case
133 ));
134 eval {
135 GetOptions (%$spec) or
136 err "Error in command line arguments";
137 };
138 die "$@$ENV{LINGY_USAGE}" if $@;
139  
140 if (@ARGV) {
141 if ($self->{repl}) {
142 unshift @ARGV, 'NO_SOURCE_PATH';
143 } else {
144 $self->{run} = $ARGV[0];
145 $self->{run} = '/dev/stdin'
146 if $self->{run} eq '-';
147 }
148  
149 } else {
150 if ($self->from_stdin) {
151 $self->{run} = '/dev/stdin';
152 unshift @ARGV, '<stdin>';
153 } else {
154 unshift @ARGV, 'NO_SOURCE_PATH';
155 }
156 }
157  
158 $self->{args} = [@ARGV];
159 }
160  
161 1;