| 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
| |||||||||||||||||||||||||||||
| 15 | 'input=s' => \$input, | ||||||||||||||||||||||||||||
| 16 | 'output=s' => \$output, | ||||||||||||||||||||||||||||
| 17 | 'help' => \$help, | ||||||||||||||||||||||||||||
| 18 | ) or usage(); | ||||||||||||||||||||||||||||
| 19 | 3 | ▶ | usage() if $help; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | $input //= 'script/commands.json'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$input //= "script/commands.json"
MC/DC0%0/1
$input //= "script/commands.json"
$input
Truth table50%1/2
A
A= $input
| |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | $output //= 'lib/Async/Redis/Commands.pm'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$output //= "lib/Async/Redis/Commands.pm"
MC/DC0%0/1
$output //= "lib/Async/Redis/Commands.pm"
$output
Truth table50%1/2
A
A= $output
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 3 | ▶ | die "Input file not found: $input\n" unless -f $input; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Load commands.json | ||||||||||||||||||||||||||||
| 27 | 2 | ▶ | open my $fh, '<', $input or die "Cannot open $input: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$dir and not -d $dir
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
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 2 | ▶ | open my $out, '>', $output or die "Cannot write $output: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||