Reply/App.pm

filter: Statementstmt 25.7% 18 / 70 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 5 / 15 Podpod 100% 5 / 5 totaltotal 26.4% 28 / 106 CC 13 SCAR 37.5
CC
13
Cov
21%
run
110.0
generate_default_config
6.0
usage
6.0
CRAP 42.7
« Reply/Plugin/ReadLine.pm ↑ Summary Reply/Plugin/Autocomplete/Functions.pm »
1 package Reply::App;
2 1 our $AUTHORITY = 'cpan:DOY';
3 1 $Reply::App::VERSION = '0.42';
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: command line app runner for Reply
7  
8 1 ▶use Getopt::Long 2.36 'GetOptionsFromArray';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Reply;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Reply::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14  
15 0 ▶sub new { bless {}, shift }
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
16  
17  
18 0 ▶sub run {
Subroutine run: not called CC=10 SCAR=47.0
Pod documented
19 0 my $self = shift;
20 0 my @argv = @_;
21  
22 0 Getopt::Long::Configure("gnu_getopt");
23  
24 0 my $cfgfile = '.replyrc';
25 0 my $exitcode;
26 0 my (@modules, @script_lines, @files);
27 my $parsed = GetOptionsFromArray(
28 \@argv,
29 'cfg:s' => \$cfgfile,
30 0 ▶ 'l|lib' => sub { unshift @INC, 'lib' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 0 ▶ 'b|blib' => sub { unshift @INC, 'blib/lib', 'blib/arch' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 ▶ 'I:s@' => sub { unshift @INC, $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 'M:s@' => \@modules,
34 'e:s@' => \@script_lines,
35 0 ▶ 'version' => sub { $exitcode = 0; version() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 0 ▶ 'help' => sub { $exitcode = 0; usage() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 0 );
38  
39 0 @files = @argv;
40 0 for my $file (@files) {
41 0 ▶ if (!stat $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (stat $file) 0 0 0
42 0 die "Can't read $file: $!";
43 }
44 }
45  
46 0 ▶ if (!$parsed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed) 0 0 0
47 0 usage(1);
48 0 $exitcode = 1;
49 }
50  
51 0 ▶ return $exitcode if defined $exitcode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $exitcode 0 0 0
52  
53 0 my $cfg = Reply::Config->new(file => $cfgfile);
54  
55 0 my %args = (config => $cfg);
56 0 my $file = $cfg->file;
57 0 ▶ if (!-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 0 0
58 0 print("$file not found. Generating a default...\n");
59 0 ▶ unless ($self->generate_default_config($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->generate_default_config($file)) 0 0 0
60 0 %args = ();
61 }
62 }
63  
64 0 my $reply = Reply->new(%args);
65 0 $reply->step("use $_") for @modules;
66 0 $reply->step($_, 1) for @script_lines;
67 0 $reply->step('do "' . quotemeta($_) . '"', 1) for @files;
68 0 $reply->run;
69  
70 0 return 0;
71 }
72  
73  
74 0 ▶sub generate_default_config {
Subroutine generate_default_config: not called CC=2 SCAR=17.9
Pod documented
75 0 my $self = shift;
76 0 my ($file) = @_;
77  
78 0 ▶ if (open my $fh, '>', $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '>', $file) { } 0 0 0
79 0 my $contents = do {
80 0 local $/;
81 <DATA>
82 0 };
83 0 $contents =~ s/use 5.XXX/use $]/;
84 0 print $fh $contents;
85 0 close $fh;
86  
87 0 return 1;
88 }
89 else {
90 0 warn "Couldn't write to $file";
91  
92 0 return 0;
93 }
94 }
95  
96  
97 0 ▶sub usage {
Subroutine usage: not called CC=2 SCAR=17.9
Pod documented
98 0 ▶ my $fh = $_[0] ? *STDERR : *STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
99 0 print $fh " reply [-lb] [-I dir] [-M mod] [--version] [--help] [--cfg file]\n";
100 }
101  
102  
103 0 ▶sub version {
Subroutine version: not called CC=2 SCAR=17.9
Pod documented
104 0 ▶ my $fh = $_[0] ? *STDERR : *STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
105 0 print $fh "Reply version $Reply::VERSION\n";
106 }
107  
108 1 1;
109  
110 =pod
111  
112 =encoding UTF-8
113  
114 =head1 NAME
115  
116 Reply::App - command line app runner for Reply
117  
118 =head1 VERSION
119  
120 version 0.42
121  
122 =head1 SYNOPSIS
123  
124 use Reply::App;
125 exit(Reply::App->new->run(@ARGV));
126  
127 =head1 DESCRIPTION
128  
129 This module encapsulates the various bits of functionality related to running
130 L<Reply> as a command line application.
131  
132 =head1 METHODS
133  
134 =head2 new
135  
136 Returns a new Reply::App instance. Takes no arguments.
137  
138 =head2 run(@argv)
139  
140 Parses the argument list given (typically from @ARGV), along with the user's configuration file, and attempts to start a Reply shell. A default configuration file will be generated for the user if none exists.
141  
142 =head2 generate_default_config($file)
143  
144 Generates default configuration file as per specified destination.
145  
146 =head2 usage($exitcode)
147  
148 Prints usage information to the screen. If C<$exitcode> is 0, it will be
149 printed to C<STDOUT>, otherwise it will be printed to C<STDERR>.
150  
151 =head2 version($exitcode)
152  
153 Prints version information to the screen. If C<$exitcode> is 0, it will be
154 printed to C<STDOUT>, otherwise it will be printed to C<STDERR>.
155  
156 =head1 AUTHOR
157  
158 Jesse Luehrs <doy@tozt.net>
159  
160 =head1 COPYRIGHT AND LICENSE
161  
162 This software is Copyright (c) 2016 by Jesse Luehrs.
163  
164 This is free software, licensed under:
165  
166 The MIT (X11) License
167  
168 =cut
169  
170 __DATA__
« Reply/Plugin/ReadLine.pm ↑ Summary Reply/Plugin/Autocomplete/Functions.pm »