| 1 | package OpenAPI::Generator::From::Definitions; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use File::Find; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use OpenAPI::Generator::Util qw(merge_definitions); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | BEGIN { | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | if (eval { require YAML::XS }) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=15.2
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 13 | 0 | YAML::XS->import('Load'); | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | 2 | elsif (eval { require YAML }) { | |||||||||||||||||||||||||||
| 16 | 0 | YAML->import('Load'); | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | else { | ||||||||||||||||||||||||||||
| 19 | 2 | require CPAN::Meta::YAML; | |||||||||||||||||||||||||||
| 20 | 2 | CPAN::Meta::YAML->import('Load'); | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | if (eval { require JSON::XS }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 0 | JSON::XS->import('decode_json') | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | else { | ||||||||||||||||||||||||||||
| 27 | 2 | require JSON::PP; | |||||||||||||||||||||||||||
| 28 | 2 | JSON::PP->import('decode_json'); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub new { | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | bless {}, shift | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub generate { | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | my($self, $conf) = @_; | ||||||||||||||||||||||||||
|
Subroutine
generate: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | my $src = $conf->{src}; | |||||||||||||||||||||||||||
| 43 | 1 | $self->_check_src($src); | |||||||||||||||||||||||||||
| 44 | 1 | my @files = $self->_src_as_files($src); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | if (!@files) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | croak "no files found in $src"; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | my @defs = @{ $conf->{definitions} }; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | for my $file (@files) { | |||||||||||||||||||||||||||
| 53 | 3 | ▶ | croak "$file is not readable" unless -r $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 3 | local $/ = ''; | |||||||||||||||||||||||||||
| 56 | 3 | ▶ | open my $fh, '<', $file or croak "can't open file $file"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 3 | my $content = <$fh>; | |||||||||||||||||||||||||||
| 58 | 3 | close $fh; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 3 | ▶ | if ($file =~ /\.json$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 1 | push @defs, decode_json($content); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||
| 65 | 2 | push @defs, Load($content); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 1 | merge_definitions(@defs) | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub _check_src { | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | ▶ | croak "$_[1] is not file or directory" unless(-f $_[1] or -d $_[1]); | ||||||||||||||||||||||||||
|
Subroutine
_check_src: called CC=4 SCAR=16.9
Branch: 1 branch
Condition33%1/3
-f $_[1] or -d $_[1]
MC/DC0%0/2
-f $_[1] or -d $_[1]
-f $_[1]
-d $_[1]
Truth table33%1/3
A or B
A= -f $_[1]B= -d $_[1]
| |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | croak "$_[1] is not readable" unless(-r $_[1]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub _src_as_files { | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | ▶ | return $_[1] if -f $_[1]; | ||||||||||||||||||||||||||
|
Subroutine
_src_as_files: called CC=2 SCAR=7.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | my @files; | |||||||||||||||||||||||||||
| 83 | 1 | ▶ | find sub { push @files, $File::Find::name if /\.(yml|yaml|json)$/ }, $_[1]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | @files | ||||||||||||||||||||||||||||
| 85 | 1 | } | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | 1 | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||||||||||