| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||
| 4 | # (C) Brad Clawsie, 2021 -- brad.clawsie@gmail.com | ||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||
| 6 | package Lang::Go::Mod; | ||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Path::Tiny qw(path); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | # ABSTRACT: parse and model go.mod files | ||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | 2 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||||
| 17 | 2 | our $AUTHORITY = 'cpan:bclawsie'; | |||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||
| 19 | 2 | our @EXPORT_OK = qw(read_go_mod parse_go_mod); | |||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||
| 21 | sub read_go_mod { | ||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | my $use_msg = 'use: read_go_mod(go_mod_path)'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
read_go_mod: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | my $go_mod_path = shift || croak $use_msg; | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
shift() || croak($use_msg)
MC/DC0%0/2
shift() || croak($use_msg)
shift()
croak($use_msg)
Truth table33%1/3
A || B
A= shift()B= croak($use_msg)
| |||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | my $go_mod_content = path($go_mod_path)->slurp_utf8 || croak "$ERRNO"; | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
path($go_mod_path)->slurp_utf8 || croak("$Lang::Go::Mod::ERRNO")
MC/DC0%0/2
path($go_mod_path)->slurp_utf8 || croak("$Lang::Go::Mod::ERRNO")
path($go_mod_path)->slurp_utf8
croak("$Lang::Go::Mod::ERRNO")
Truth table33%1/3
A || B
A= path($go_mod_path)->slurp_utf8B= croak("$Lang::Go::Mod::ERRNO")
| |||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | 1 | return parse_go_mod($go_mod_content); | |||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||
| 30 | sub parse_go_mod { | ||||||||||||||||||||||||||||||||||||||
| 31 | 13 | ▶ | my $go_mod_content = shift || croak 'use: parse_go_mod(go_mod_content)'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_go_mod: called CC=29 SCAR=33.8
Pod
documented
Condition33%1/3
shift() || croak('use: parse_go_mod(go_mod_content)')
MC/DC0%0/2
shift() || croak('use: parse_go_mod(go_mod_content)')
shift()
croak('use: parse_go_mod(go_mod_content)')
Truth table33%1/3
A || B
A= shift()B= croak('use: parse_go_mod(go_mod_content)')
| |||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||
| 33 | 13 | my $m = {}; | |||||||||||||||||||||||||||||||||||||
| 34 | 13 | $m->{exclude} = {}; | |||||||||||||||||||||||||||||||||||||
| 35 | 13 | $m->{replace} = {}; | |||||||||||||||||||||||||||||||||||||
| 36 | 13 | $m->{'require'} = {}; | |||||||||||||||||||||||||||||||||||||
| 37 | 13 | my ( $excludes, $replaces, $requires ) = ( 0, 0, 0 ); | |||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||
| 39 | 13 | LINE: for my $line ( split /\n/msx, $go_mod_content ) { | |||||||||||||||||||||||||||||||||||||
| 40 | 67 | ▶ | next LINE if ( $line =~ /^\s*$/msx ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 41 | 53 | ▶ | if ($excludes) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | if ( $line =~ /^\s*[)]\s*$/msx ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 43 | 1 | $excludes = 0; | |||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||
| 45 | elsif ( $line =~ /\s*(\S+)\s+(\S+)/msx ) { | ||||||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | $m->{exclude}->{$1} = [] unless ( defined $m->{exclude}->{$1} ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 47 | 1 | push @{ $m->{exclude}->{$1} }, $2; | |||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||||||||||||||||||||
| 50 | 1 | croak "malformed exclude line $line"; | |||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||
| 52 | 2 | next LINE; | |||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||
| 54 | 50 | ▶ | if ($replaces) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 55 | 4 | ▶ | if ( $line =~ /^\s*[)]\s*$/msx ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 56 | 1 | $replaces = 0; | |||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||
| 58 | elsif ( $line =~ /^\s*(\S+)\s+=>\s+(\S+)\s*$/msx ) { | ||||||||||||||||||||||||||||||||||||||
| 59 | croak "duplicate replace for $1" | ||||||||||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | if ( defined $m->{replace}->{$1} ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 61 | 2 | $m->{replace}->{$1} = $2; | |||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||
| 63 | else { | ||||||||||||||||||||||||||||||||||||||
| 64 | 1 | croak "malformed replace line $line"; | |||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||
| 66 | 3 | next LINE; | |||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||
| 68 | 46 | ▶ | if ($requires) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 69 | 5 | ▶ | if ( $line =~ /^\s*[)]\s*$/msx ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 70 | 1 | $requires = 0; | |||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||
| 72 | elsif ( $line =~ /^\s*(\S+)\s+(\S+).*$/msx ) { | ||||||||||||||||||||||||||||||||||||||
| 73 | croak "duplicate require for $1" | ||||||||||||||||||||||||||||||||||||||
| 74 | 3 | ▶ | if ( defined $m->{'require'}->{$1} ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 75 | 3 | $m->{'require'}->{$1} = $2; | |||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||||||||||||
| 78 | 1 | croak "malformed require line $line"; | |||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||
| 80 | 4 | next LINE; | |||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||
| 83 | 41 | ▶ | if ( $line =~ /^module\s+(\S+)$/msx ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||
| 84 | 12 | $m->{module} = $1; | |||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||
| 86 | elsif ( $line =~ /^go\s+(\S+)$/msx ) { | ||||||||||||||||||||||||||||||||||||||
| 87 | 12 | $m->{go} = $1; | |||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||
| 89 | elsif ( $line =~ /^exclude\s+[(]\s*$/msx ) { | ||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||
| 91 | # beginning of exclude block | ||||||||||||||||||||||||||||||||||||||
| 92 | 2 | $excludes = 1; | |||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||
| 94 | elsif ( $line =~ /^replace\s+[(]\s*$/msx ) { | ||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||
| 96 | # beginning of replace block | ||||||||||||||||||||||||||||||||||||||
| 97 | 2 | $replaces = 1; | |||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||
| 99 | elsif ( $line =~ /^require\s+[(]\s*$/msx ) { | ||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | # beginning of require block | ||||||||||||||||||||||||||||||||||||||
| 102 | 2 | $requires = 1; | |||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||
| 104 | elsif ( $line =~ /^exclude\s+(\S+)\s+(\S+)\s*$/msx ) { | ||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||
| 106 | # single exclude | ||||||||||||||||||||||||||||||||||||||
| 107 | 3 | ▶ | $m->{$1} = [] unless ( defined $m->{exclude}->{$1} ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 108 | 3 | push @{ $m->{exclude}->{$1} }, $2; | |||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||
| 110 | elsif ( $line =~ /^replace\s+(\S+)\s+=>\s+(\S+)\s*$/msx ) { | ||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||
| 112 | # single replace | ||||||||||||||||||||||||||||||||||||||
| 113 | croak "duplicate replace for $1" | ||||||||||||||||||||||||||||||||||||||
| 114 | 1 | ▶ | if ( defined $m->{replace}->{$1} ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 115 | 1 | $m->{replace}->{$1} = $2; | |||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||
| 117 | elsif ( $line =~ /^require\s+(\S+)+\s+(\S+).*$/msx ) { | ||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||
| 119 | # single require | ||||||||||||||||||||||||||||||||||||||
| 120 | croak "duplicate require for $1" | ||||||||||||||||||||||||||||||||||||||
| 121 | 1 | ▶ | if ( defined $m->{'require'}->{$1} ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 122 | 1 | $m->{'require'}->{$1} = $2; | |||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||||||||||||||||||||
| 125 | 6 | croak "unknown line content: $line"; | |||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||
| 129 | 4 | ▶ | croak 'missing module line' unless ( defined $m->{module} ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 130 | 3 | ▶ | croak 'missing go line' unless ( defined $m->{go} ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||
| 132 | 2 | return $m; | |||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||
| 135 | 2 | 1; | |||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||||||||||||