| 1 | package Skilletor::Bundles; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Bundle files (spec §15), their expansion, and name patterns | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 34 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 34 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 34 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 34 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 34 | ▶ | use Skilletor::Config; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 34 | ▶ | use Skilletor::Error::Bundle; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 34 | ▶ | use Skilletor::Error::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 34 | ▶ | use Skilletor::Frontmatter qw( parse_yaml_document is_yaml_float ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 34 | ▶ | use Skilletor::JSON qw( js_ordered js_stringify js_type ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 34 | ▶ | use Skilletor::JSString qw( js_lc js_trim ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 34 | ▶ | use Skilletor::Spec qw( resolve_spec ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 34 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 34 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||||||||||||
| 20 | PATTERN_CHAR is_pattern matches_pattern same_identity match_entry | ||||||||||||||||||||||||||||||||||||||||||
| 21 | parse_bundle expand_bundle entry_miss | ||||||||||||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | # The character that makes a name a pattern (spec §3). | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 34 | ▶ | use constant PATTERN_CHAR => '*'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 34 | my $DOT = qr/[^\n\r\x{2028}\x{2029}]/; # JavaScript's `.` | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 34 | my @TYPE_KEYS = ( skills => 'skill', agents => 'agent', rules => 'rule' ); | |||||||||||||||||||||||||||||||||||||||||
| 30 | 34 | my @KEYS = qw( description skills agents rules bundles vars ); | |||||||||||||||||||||||||||||||||||||||||
| 31 | 34 | my %KEY = map { $_ => 1 } @KEYS; | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 44 | ▶ | sub is_pattern { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_pattern: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 44 | my ( $name ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 44 | ▶ | return index( $name, PATTERN_CHAR ) >= 0 ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | # `*` matches any run of characters but a line terminator (JavaScript's `.*`); the rest is literal. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 103 | ▶ | sub matches_pattern { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
matches_pattern: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 103 | my ( $pattern, $name ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 103 | my $re = join "$DOT*", map { quotemeta } split /\Q${\PATTERN_CHAR}\E/, $pattern, -1; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 103 | ▶ | return $name =~ /\A$re\z/ ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # Two source identities (`git`/`url` values) are the same modulo a trailing `/`, `.git` and host case. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 10 | ▶ | sub same_identity { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
same_identity: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 10 | my ( $a, $b ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 10 | ▶ | return _normalize_identity($a) eq _normalize_identity($b) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 20 | ▶ | sub _normalize_identity { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_identity: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 20 | my ( $u ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 20 | ( my $s = js_trim($u) ) =~ s{/+\z}{}; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 20 | $s =~ s/\.git\z//; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 20 | ▶ | if ( $s =~ m{\A([a-z][a-z0-9+.\-]*://)([^/]*)($DOT*)\z}i ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 16 | $s = js_lc($1).js_lc($2).$3; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 20 | return $s; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | # Catalog items of $type a name or pattern names (spec §3). | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 15 | ▶ | sub match_entry { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
match_entry: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 15 | my ( $cat, $type, $entry ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 15 | ▶ | return [ grep { $_->{type} eq $type && matches_pattern( $entry, $_->{name} ) } @{ $cat->{items} } ] if is_pattern($entry); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$_->{'type'} eq $type && matches_pattern($entry, $_->{'name'})
MC/DC100%2/2
$_->{'type'} eq $type && matches_pattern($entry, $_->{'name'})
$_->{'type'} eq $type
matches_pattern($entry, $_->{'name'})
Truth table100%3/3
A && B
A= $_->{'type'} eq $typeB= matches_pattern($entry, $_->{'name'})
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 10 | ▶ | return [ grep { $_->{type} eq $type && $_->{name} eq $entry } @{ $cat->{items} } ]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$_->{'type'} eq $type && $_->{'name'} eq $entry
MC/DC100%2/2
$_->{'type'} eq $type && $_->{'name'} eq $entry
$_->{'type'} eq $type
$_->{'name'} eq $entry
Truth table100%3/3
A && B
A= $_->{'type'} eq $typeB= $_->{'name'} eq $entry
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | # The warning for an entry that matched $count items, if any (a bare `*` stays silent, spec §3). | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 15 | ▶ | sub entry_miss { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
entry_miss: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 15 | my ( $type, $entry, $count ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 15 | ▶ | return undef if $count > 0 || $entry eq PATTERN_CHAR; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$count > 0 or $entry eq "*"
MC/DC50%1/2
$count > 0 or $entry eq "*"
$count > 0
$entry eq "*"
Truth table66%2/3
A or B
A= $count > 0B= $entry eq "*"
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | ▶ | return is_pattern($entry) ? 'pattern '.$type.':'.$entry.' matches nothing' : $type.' '.$entry.' not found in the source'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 216 | ▶ | sub _fail { Skilletor::Error::Bundle->throw( $_[0] ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fail: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | sub _message { my ( $err ) = @_; return blessed $err && $err->can('message') ? $err->message : "$err" =~ s/ at \S+ line \d+\.?\n?\z//r } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_message: called CC=3 SCAR=13.1
Branch: 1 branch
Condition33%1/3
builtin::blessed($err) and $err->can("message")
MC/DC0%0/2
builtin::blessed($err) and $err->can("message")
builtin::blessed($err)
$err->can("message")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->can("message")
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | # A YAML value as plain JSON-like data (floats become numbers). | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 41 | ▶ | sub _plain { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_plain: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 41 | my ( $v ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 41 | ▶ | return $v->value if is_yaml_float($v); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 38 | ▶ | return [ map { _plain($_) } @$v ] if ref $v eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 37 | ▶ | return js_ordered( map { ( $_ => _plain( $v->{$_} ) ) } keys %$v ) if ref $v eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 24 | return $v; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 245 | ▶ | sub _string_list { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_string_list: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 245 | my ( $value, $key ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 245 | ▶ | return [] unless defined $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 74 | ▶ | _fail( $key.' must be a list' ) unless ref $value eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | return [ | ||||||||||||||||||||||||||||||||||||||||||
| 93 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 72 | ▶ | _fail( $key.' entries must be strings (got '.js_stringify( _plain($_) ).')' ) unless _is_string($_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 85 | ▶ | _fail( $key.' has an empty entry' ) if js_trim($_) eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 82 | js_trim($_); | |||||||||||||||||||||||||||||||||||||||||
| 97 | } @$value | ||||||||||||||||||||||||||||||||||||||||||
| 98 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 168 | ▶ | sub _is_string { my ( $v ) = @_; return !ref $v && js_type($v) eq 'string' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_string: called CC=2 SCAR=6.9
Condition100%3/3
!ref($v) && js_type($v) eq 'string'
MC/DC100%2/2
!ref($v) && js_type($v) eq 'string'
!ref($v)
js_type($v) eq 'string'
Truth table100%3/3
not(A) && B
A= !ref($v)B= js_type($v) eq 'string'
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | # `name@<spec>` resolved without a probe (spec §15.6): local paths, generic hosts, an address | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # config load would refuse as a source and a ref or a path pinned by the address are errors. | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 24 | ▶ | sub _foreign_entry { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_foreign_entry: called CC=16 SCAR=27.9
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 24 | my ( $type, $entry, $key ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 24 | my $at = index $entry, '@'; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 24 | my $name = substr $entry, 0, $at; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 24 | my $spec = substr $entry, $at + 1; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 24 | ▶ | _fail( $key.': "'.$entry.'" has an empty name or address' ) if $name eq '' || $spec eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$name eq "" or $spec eq ""
MC/DC100%2/2
$name eq "" or $spec eq ""
$name eq ""
$spec eq ""
Truth table100%3/3
A or B
A= $name eq ""B= $spec eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 22 | my $r; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 22 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 113 | 22 | ▶ | $r = resolve_spec( $spec, sub { Skilletor::Error::Spec->throw('probe') } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 20 | ▶ | if ( $r->{kind} eq 'local' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | _fail( $key.': "'.$entry.'" names a local path; a bundle can only name remote sources' ); | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 19 | ▶ | if ( defined $r->{ref} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 2 | ▶ | my $pins = 'ref '.js_stringify( $r->{ref} ).( defined $r->{path} ? ' and path '.js_stringify( $r->{path} ) : '' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 2 | _fail( $key.': "'.$entry.'" pins '.$pins.', but an entry names its source by address alone: write '.$name.'@'.$r->{value} ); | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 17 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 122 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 22 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 5 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 5 | ▶ | die $err if blessed $err && $err->isa('Skilletor::Error::Bundle'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($err) and $err->isa("Skilletor::Error::Bundle")
MC/DC50%1/2
builtin::blessed($err) and $err->isa("Skilletor::Error::Bundle")
builtin::blessed($err)
$err->isa("Skilletor::Error::Bundle")
Truth table66%2/3
A and B
A= builtin::blessed($err)B= $err->isa("Skilletor::Error::Bundle")
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | ▶ | if ( blessed $err && $err->isa('Skilletor::Error::Spec') && $err->message eq 'probe' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
builtin::blessed($err) and $err->isa("Skilletor::Error::Spec") and $err->message eq "probe"
builtin::blessed($err) and $err->isa("Skilletor::Error::Spec")
MC/DC33%1/3
builtin::blessed($err) and $err->isa("Skilletor::Error::Spec") and $err->message eq "probe"
builtin::blessed($err)
$err->isa("Skilletor::Error::Spec")
$err->message eq "probe"
Truth table50%2/4
A and B and C
A= builtin::blessed($err)B= $err->isa("Skilletor::Error::Spec")C= $err->message eq "probe"
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | _fail( $key.': "'.$entry.'": a generic host must be written as a full https:// URL' ); | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | _fail( $key.': "'.$entry.'": '._message($err) ); | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 17 | ▶ | my $field = $r->{kind} eq 'git' ? { git => $r->{value} } : { url => $r->{value} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 17 | ▶ | unless ( eval { Skilletor::Config->source_fields( $field, 'sources.'.$r->{derivedName} ); 1 } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 1 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | ▶ | die $err unless blessed $err && $err->isa('Skilletor::Error::Config'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($err) and $err->isa("Skilletor::Error::Config")
MC/DC0%0/2
builtin::blessed($err) and $err->isa("Skilletor::Error::Config")
builtin::blessed($err)
$err->isa("Skilletor::Error::Config")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->isa("Skilletor::Error::Config")
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | _fail( $key.': "'.$entry.'" cannot be added as a source: '.$err->message ); | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 138 | type => $type, | ||||||||||||||||||||||||||||||||||||||||||
| 139 | entry => $entry, | ||||||||||||||||||||||||||||||||||||||||||
| 140 | name => $name, | ||||||||||||||||||||||||||||||||||||||||||
| 141 | spec => $spec, | ||||||||||||||||||||||||||||||||||||||||||
| 142 | url => $r->{value}, | ||||||||||||||||||||||||||||||||||||||||||
| 143 | kind => $r->{kind}, | ||||||||||||||||||||||||||||||||||||||||||
| 144 | derivedName => $r->{derivedName}, | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 16 | }; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 257 | ▶ | sub parse_bundle { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_bundle: called CC=18 SCAR=29.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 257 | my ( $text ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 257 | my $data; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 257 | ▶ | unless ( eval { $data = parse_yaml_document($text); 1 } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 150 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 150 | ▶ | _fail( $err->message ) if blessed $err && $err->isa('Skilletor::Error::Frontmatter'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($err) and $err->isa("Skilletor::Error::Frontmatter")
MC/DC0%0/2
builtin::blessed($err) and $err->isa("Skilletor::Error::Frontmatter")
builtin::blessed($err)
$err->isa("Skilletor::Error::Frontmatter")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->isa("Skilletor::Error::Frontmatter")
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | die $err; | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 107 | for my $key ( keys %$data ) { | |||||||||||||||||||||||||||||||||||||||||
| 157 | 199 | ▶ | _fail( 'unknown key "'.$key.'" (allowed: '.join( ', ', @KEYS ).')' ) unless $KEY{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 89 | my $description = $data->{description}; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 89 | ▶ | _fail('description is required (a string)') unless defined $description && _is_string($description) && js_trim($description) ne ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $description and _is_string($description) and js_trim($description) ne ""
defined $description and _is_string($description)
MC/DC100%3/3
defined $description and _is_string($description) and js_trim($description) ne ""
defined $description
_is_string($description)
js_trim($description) ne ""
Truth table100%4/4
A and B and C
A= defined $descriptionB= _is_string($description)C= js_trim($description) ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 74 | my %def = ( description => $description, items => [], foreign => [], bundles => [], vars => js_ordered() ); | |||||||||||||||||||||||||||||||||||||||||
| 162 | 74 | my @pairs = @TYPE_KEYS; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 74 | ▶ | while ( my ( $key, $type ) = splice @pairs, 0, 2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 188 | for my $entry ( @{ _string_list( $data->{$key}, $key ) } ) { | |||||||||||||||||||||||||||||||||||||||||
| 165 | 53 | ▶ | if ( index( $entry, '@' ) >= 0 ) { push @{ $def{foreign} }, _foreign_entry( $type, $entry, $key ) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 29 | else { push @{ $def{items} }, { type => $type, entry => $entry } } | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 57 | for my $name ( @{ _string_list( $data->{bundles}, 'bundles' ) } ) { | |||||||||||||||||||||||||||||||||||||||||
| 170 | 29 | ▶ | _fail( 'bundles: "'.$name.'" must be a bare name of this source' ) if index( $name, '@' ) >= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 27 | ▶ | _fail( 'bundles: "'.$name.'": patterns over bundle names are not supported' ) if is_pattern($name); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 25 | push @{ $def{bundles} }, $name; | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 52 | my $vars = $data->{vars}; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 52 | ▶ | if ( defined $vars ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 15 | ▶ | _fail('vars must be a mapping') unless ref $vars eq 'HASH' && !blessed $vars; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $vars eq "HASH" and not builtin::blessed($vars)
MC/DC50%1/2
ref $vars eq "HASH" and not builtin::blessed($vars)
ref $vars eq "HASH"
not builtin::blessed($vars)
Truth table66%2/3
A and not(B)
A= ref $vars eq "HASH"B= not builtin::blessed($vars)
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 11 | $def{vars} = _plain($vars); | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 48 | return \%def; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | #### Expansion (spec §15.2) ############################################ | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 4 | ▶ | sub _label_path { join '/', @{ $_[0] } } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_label_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 11 | ▶ | sub expand_bundle { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
expand_bundle: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 11 | my ( $cat, $name ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 188 | 11 | my %by_name = map { ( $_->{name} => $_ ) } @{ $cat->{bundles} }; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 11 | my ( %members, @order ); | |||||||||||||||||||||||||||||||||||||||||
| 190 | 11 | my $out = { items => [], foreign => [], warnings => [] }; | |||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | 11 | my $visit; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 21 | ▶ | $visit = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=16 SCAR=27.7
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 21 | my ( $bundle, $path, $outer ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 21 | my $cb = $by_name{$bundle}; | |||||||||||||||||||||||||||||||||||||||||
| 196 | 21 | ▶ | my $where = @$path ? 'bundle '.$bundle.' (included by '.$path->[-1].')' : 'bundle '.$bundle; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 21 | ▶ | _fail( $where.' not found' ) unless $cb; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 19 | ▶ | _fail( $where.': '.$cb->{error} ) if defined $cb->{error}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 15 | my $here = [ @$path, $bundle ]; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 15 | my $def = $cb->{def}; | |||||||||||||||||||||||||||||||||||||||||
| 201 | # Inner vars first, then the outer chain on top: the outer bundle wins. | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 15 | my $chain = { path => $here, vars => js_ordered( map { ( $_ => $def->{vars}{$_} ) } keys %{ $def->{vars} } ), setters => {} }; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 15 | $chain->{setters}{$_} = $bundle for keys %{ $def->{vars} }; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 15 | ▶ | if ($outer) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 8 | $chain->{vars}{$_} = $outer->{vars}{$_} for keys %{ $outer->{vars} }; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 8 | $chain->{setters}{$_} = $outer->{setters}{$_} for keys %{ $outer->{setters} }; | |||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 15 | for my $it ( @{ $def->{items} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 209 | 15 | my $found = match_entry( $cat, $it->{type}, $it->{entry} ); | |||||||||||||||||||||||||||||||||||||||||
| 210 | 15 | my $miss = entry_miss( $it->{type}, $it->{entry}, scalar @$found ); | |||||||||||||||||||||||||||||||||||||||||
| 211 | 15 | ▶ | push @{ $out->{warnings} }, { bundle => $bundle, message => $miss } if defined $miss; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 15 | for my $ci (@$found) { | |||||||||||||||||||||||||||||||||||||||||
| 213 | 14 | my $key = $ci->{type}.'/'.$ci->{name}; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 14 | ▶ | unless ( $members{$key} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 12 | $members{$key} = { type => $ci->{type}, name => $ci->{name}, chains => [] }; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 12 | push @order, $key; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 14 | my $m = $members{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 219 | 14 | ▶ | push @{ $m->{chains} }, $chain unless grep { _label_path( $_->{path} ) eq _label_path($here) } @{ $m->{chains} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 15 | push @{ $out->{foreign} }, { %$_, chain => $chain } for @{ $def->{foreign} }; | |||||||||||||||||||||||||||||||||||||||||
| 223 | 15 | for my $inner ( @{ $def->{bundles} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 224 | 11 | my ($at) = grep { $here->[$_] eq $inner } 0 .. $#$here; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 11 | ▶ | _fail( 'bundle cycle '.join( " \x{2192} ", @{$here}[ $at .. $#$here ], $inner ) ) if defined $at; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 10 | $visit->( $inner, $here, { %$chain, path => $here } ); | |||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 11 | }; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 11 | my $ok = eval { $visit->( $name, [], undef ); 1 }; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 11 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 11 | undef $visit; # the closure refers to itself | |||||||||||||||||||||||||||||||||||||||||
| 232 | 11 | ▶ | die $err unless $ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 4 | $out->{items} = [ map { $members{$_} } @order ]; | |||||||||||||||||||||||||||||||||||||||||
| 234 | 4 | return $out; | |||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 34 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | __END__ | ||||||||||||||||||||||||||||||||||||||||||