File Coverage

blib/lib/Lingy/Command.pm
Criterion Covered Total %
statement 6 50 12.0
branch 0 36 0.0
condition 0 21 0.0
subroutine 2 7 28.5
pod 0 5 0.0
total 8 119 6.7


line stmt bran cond sub pod time code
1             package Lingy::Command;
2 1     1   1130 use Lingy::Base;
  1         2  
  1         6  
3              
4             has in => ();
5             has out => ();
6             has from => ();
7             has to => ();
8              
9 1     1   738 use Getopt::Long;
  1         9421  
  1         7  
10              
11             my $extension_map = {
12             pl => 'perl5',
13             pm => 'perl5',
14             pm5 => 'perl5',
15             p6 => 'perl6',
16             pl6 => 'perl6',
17             pm6 => 'perl6',
18             yml => 'yaml',
19             yaml => 'yaml',
20             };
21              
22             my $language_map = { map {($_, 1)} values %$extension_map };
23              
24             my $compiler_map = {
25             yaml => 'Lingy::Compiler::YAML',
26             perl5 => 'Lingy::Compiler::Perl5',
27             };
28              
29             my $emitter_map = {
30             perl5 => 'Lingy::Emitter::Perl5',
31             perl6 => 'Lingy::Emitter::Perl6',
32             yaml => 'Lingy::Emitter::YAML',
33             };
34              
35             sub run {
36 0     0 0   my ($self) = @_;
37              
38 0           local @ARGV = @{$self->{args}};
  0            
39 0           GetOptions(
40             'to=s' => \$self->{to},
41             'from=s' => \$self->{from},
42             'in=s' => \$self->{in},
43             'out=s' => \$self->{out},
44             );
45              
46 0 0 0       if (@ARGV and not $self->{in}) {
47 0           $self->{in} = shift @ARGV;
48             }
49 0 0         die "Unknown arguments '@ARGV'"
50             if @ARGV;
51              
52 0 0 0       if ($self->{in} and not $self->{from} and $self->{in} =~ /\.(\w+)$/) {
      0        
53 0           $self->{from} = $1;
54             }
55 0 0 0       if ($self->{out} and not $self->{to} and $self->{out} =~ /\.(\w+)$/) {
      0        
56 0           $self->{to} = $1;
57             }
58              
59 0 0         die "--from option required"
60             unless $self->{from};
61 0 0         die "--to option required"
62             unless $self->{to};
63 0 0         die "Unknown 'from' value '$self->{from}'"
64             unless exists $extension_map->{$self->{from}};
65 0 0         die "Unknown 'to' value '$self->{to}'"
66             unless exists $extension_map->{$self->{to}};
67              
68 0           my $input = $self->get_input;
69 0           my $compiler = $self->get_compiler;
70 0           my $emitter = $self->get_emitter;
71 0           my $ast = $compiler->compile($input);
72 0           my $output = $emitter->emit($ast);
73 0           $self->write_output($output);
74             }
75              
76             sub get_input {
77 0     0 0   my ($self) = @_;
78 0           local $/;
79 0 0         if (my $in = $self->{in}) {
80 0 0         open my $fh, $in
81             or die "Can't open '$in' for input";
82 0           return <$fh>;
83             }
84             else {
85 0           return <>;
86             }
87             }
88              
89             sub write_output {
90 0     0 0   my ($self, $output) = @_;
91 0 0         if (my $out = $self->{out}) {
92 0 0         open my $fh, $out
93             or die "Can't open '$out' for output";
94 0           print $fh $output;
95             }
96             else {
97 0           print $output;
98             }
99             }
100              
101             sub get_compiler {
102 0     0 0   my ($self) = @_;
103 0 0         my $from = $self->{from} or die;
104 0   0       my $lang = $extension_map->{$from} || $from;
105 0 0         my $class = $compiler_map->{$lang}
106             or die "Invalid Lingy compiler language: '$lang'";
107 0 0         eval "require $class; 1"
108             or die "$@";
109 0           $class->new;
110             }
111              
112             sub get_emitter {
113 0     0 0   my ($self) = @_;
114 0 0         my $to = $self->{to} or die;
115 0   0       my $lang = $extension_map->{$to} || $to;
116 0 0         my $class = $emitter_map->{$lang}
117             or die "Invalid Lingy emitter language: '$lang'";
118 0 0         eval "require $class; 1"
119             or die "$@";
120 0           $class->new;
121             }
122              
123              
124              
125             1;