script/generate-commands

filter: Statementstmt 94.4% 68 / 72 Branchbran 72.7% 16 / 22 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 92.3% 12 / 13 Podpod n/a totaltotal 83.9% 99 / 118 CC 7 SCAR 8.8
CC
7
Cov
86%
generate_method
5.0
generate_module
3.0
usage
2.0
CRAP 2.4
« blib/lib/Async/Redis/Transaction.pm ↑ Summary blib/lib/Async/Redis/Error.pm »
1 #!/usr/bin/env perl
2 # script/generate-commands
3 #
4 # Generates Async::Redis::Commands from redis-doc commands.json
5  
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 my ($input, $output, $help);
14 3 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("input=s", \$input, "output=s", \$output, "help", \$help) or usage() 3 0 3
15 'input=s' => \$input,
16 'output=s' => \$output,
17 'help' => \$help,
18 ) or usage();
19 3 ▶usage() if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 3 3
20  
21 3 ▶$input //= 'script/commands.json';
Condition50%1/2
$input //= "script/commands.json"
l!l
30
MC/DC0%0/1
$input //= "script/commands.json"
$input
Truth table50%1/2
A
A= $input
Aresult
11
00
22 3 ▶$output //= 'lib/Async/Redis/Commands.pm';
Condition50%1/2
$output //= "lib/Async/Redis/Commands.pm"
l!l
30
MC/DC0%0/1
$output //= "lib/Async/Redis/Commands.pm"
$output
Truth table50%1/2
A
A= $output
Aresult
11
00
23  
24 3 ▶die "Input file not found: $input\n" unless -f $input;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $input 1 2 3
25  
26 # Load commands.json
27 2 ▶open my $fh, '<', $input or die "Cannot open $input: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $input or die "Cannot open ${input}: $!" 2 0 2
28 2 my $json = do { local $/; <$fh> };
29 2 close $fh;
30  
31 2 my $commands = decode_json($json);
32  
33 # Generate module
34 2 my $code = generate_module($commands);
35  
36 # Write output
37 2 my $dir = dirname($output);
38 2 ▶system("mkdir", "-p", $dir) if $dir && !-d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir and not -d $dir 0 2 2
Condition33%1/3
$dir and not -d $dir
!ll&&!rl&&r
020
MC/DC0%0/2
$dir and not -d $dir
$dir not -d $dir
Truth table33%1/3
A and not(B)
A= $dirB= not -d $dir
ABresult
0X0
100
111
39  
40 2 ▶open my $out, '>', $output or die "Cannot write $output: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", $output or die "Cannot write ${output}: $!" 2 0 2
41 2 print $out $code;
42 2 close $out;
43  
44 2 say "Generated $output with " . scalar(keys %$commands) . " commands";
45 2 exit 0;
46  
47 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
48 0 say "Usage: $0 [--input FILE] [--output FILE]";
49 0 say " --input Path to commands.json (default: script/commands.json)";
50 0 say " --output Output module path (default: lib/Async/Redis/Commands.pm)";
51 0 exit 1;
52 }
53  
54 2 ▶sub generate_module {
Subroutine generate_module: called CC=3 SCAR=11.0
55 2 my ($commands) = @_;
56  
57 2 my @methods;
58 my %seen;
59  
60 2 for my $cmd (sort keys %$commands) {
61 740 my $info = $commands->{$cmd};
62 740 my $method = cmd_to_method($cmd);
63  
64 740 ▶ next if $seen{$method}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$method}++ 0 740 740
65  
66 740 my $parts = [split /\s+/, $cmd];
67 740 my $is_subcommand = @$parts > 1;
68  
69 740 push @methods, generate_method($method, $parts, $info);
70 }
71  
72 2 return <<"END_MODULE";
73 # lib/Async/Redis/Commands.pm
74 # AUTO-GENERATED - DO NOT EDIT
75 # Generated from redis-doc commands.json
76 #
77 # This module provides async method wrappers for all Redis commands.
78 # Consume this role in Async::Redis.
79  
80 package Async::Redis::Commands;
81  
82 use strict;
83 use warnings;
84 use 5.018;
85  
86 use Future::AsyncAwait;
87  
88 2 @{[ join("\n\n", @methods) ]}
89  
90 1;
91  
92 __END__
« blib/lib/Async/Redis/Transaction.pm ↑ Summary blib/lib/Async/Redis/Error.pm »