| 1 | package Skilletor::Backend; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Choose a source's backend (author mode) and build it – one value for trust and fetch | ||||||||||||||||||||||||||||
| 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 Cwd (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 34 | ▶ | use Skilletor::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 34 | ▶ | use Skilletor::FSUtil qw( decode_utf8_lossy path_resolve ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 34 | ▶ | use Skilletor::JSON (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 34 | ▶ | use Skilletor::Source::Git; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 34 | ▶ | use Skilletor::Source::Local; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 34 | ▶ | use Skilletor::Source::Url; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 34 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 450 | ▶ | sub local_class { 'Skilletor::Source::Local' } | ||||||||||||||||||||||||||
|
Subroutine
local_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 140 | ▶ | sub git_class { 'Skilletor::Source::Git' } | ||||||||||||||||||||||||||
|
Subroutine
git_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | sub url_class { 'Skilletor::Source::Url' } | ||||||||||||||||||||||||||
|
Subroutine
url_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # A `local` field as an absolute path: the real path when it is an existing directory. | ||||||||||||||||||||||||||||
| 22 | 238 | ▶ | sub local_dir { | ||||||||||||||||||||||||||
|
Subroutine
local_dir: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 238 | my ( $self, $path, $home ) = @_; | |||||||||||||||||||||||||||
| 24 | 238 | my $abs = path_resolve( $self->local_class->expand_home( $path, $home ) ); | |||||||||||||||||||||||||||
| 25 | 238 | ▶ | if ( -d $abs ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 236 | my $real = eval { Cwd::abs_path($abs) }; | |||||||||||||||||||||||||||
| 27 | # abs_path answers in bytes; an address is a character string, as the reference's is. | ||||||||||||||||||||||||||||
| 28 | 236 | ▶ | return { path => decode_utf8_lossy($real), exists => 1 } if defined $real; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | 2 | return { path => $abs, exists => 0 }; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Author mode: `local` if that directory exists, else `git`, else `url`; a `local` alone whose | ||||||||||||||||||||||||||||
| 34 | # directory is missing stays `local`. The origin is the chosen field's (`project` without | ||||||||||||||||||||||||||||
| 35 | # one); the source's `path` goes with whichever is chosen. | ||||||||||||||||||||||||||||
| 36 | 429 | ▶ | sub resolve { | ||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 429 | my ( $self, $src, $home ) = @_; | |||||||||||||||||||||||||||
| 38 | 429 | my $b = $self->_chosen( $src, $home ); | |||||||||||||||||||||||||||
| 39 | 428 | ▶ | if ( exists $src->{path} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $b->{path} = ref $src->{path} ? [ @{ $src->{path} } ] : [ $src->{path} ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 428 | return $b; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 429 | ▶ | sub _chosen { | ||||||||||||||||||||||||||
|
Subroutine
_chosen: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 46 | 429 | my ( $self, $src, $home ) = @_; | |||||||||||||||||||||||||||
| 47 | 429 | ▶ | my $origin = sub { $src->{origins}{ $_[0] } // 'project' }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$src->{"origins"}{$_[0]} // "project"
MC/DC100%1/1
$src->{"origins"}{$_[0]} // "project"
$src->{"origins"}{$_[0]}
Truth table100%2/2
A
A= $src->{"origins"}{$_[0]}
| |||||||||||||||||||||||||||||
| 48 | 429 | ▶ | my $local = exists $src->{local} ? $self->local_dir( $src->{local}, $home ) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 429 | ▶ | return { kind => 'local', address => $local->{path}, origin => $origin->('local') } if $local && $local->{exists}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$local and $local->{"exists"}
MC/DC100%2/2
$local and $local->{"exists"}
$local
$local->{"exists"}
Truth table100%3/3
A and B
A= $localB= $local->{"exists"}
| |||||||||||||||||||||||||||||
| 50 | 193 | ▶ | if ( exists $src->{git} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 188 | my %b = ( kind => 'git', address => $src->{git}, origin => $origin->('git') ); | |||||||||||||||||||||||||||
| 52 | 188 | ▶ | $b{ref} = $src->{ref} if exists $src->{ref}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 188 | return \%b; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 5 | ▶ | return { kind => 'url', address => $src->{url}, origin => $origin->('url') } if exists $src->{url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 2 | ▶ | return { kind => 'local', address => $local->{path}, origin => $origin->('local') } if $local; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 1 | Skilletor::Error->throw( 'source '.$src->{name}.' has no backend' ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # The backend object for a resolved backend – nothing else is consulted. | ||||||||||||||||||||||||||||
| 61 | 354 | ▶ | sub make { | ||||||||||||||||||||||||||
|
Subroutine
make: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 354 | my ( $self, $backend, $cache_root, $timeout_ms ) = @_; | |||||||||||||||||||||||||||
| 63 | 354 | ▶ | my %timeout = defined $timeout_ms ? ( timeout_ms => $timeout_ms ) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 354 | ▶ | return $self->local_class->new( local_path => $backend->{address} ) if $backend->{kind} eq 'local'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 142 | ▶ | if ( $backend->{kind} eq 'git' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | return $self->git_class->new( | ||||||||||||||||||||||||||||
| 67 | url => $backend->{address}, | ||||||||||||||||||||||||||||
| 68 | 140 | ▶ | ( exists $backend->{ref} ? ( ref => $backend->{ref} ) : () ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | cache_root => $cache_root, | ||||||||||||||||||||||||||||
| 70 | %timeout, | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 2 | ▶ | return $self->url_class->new( url => $backend->{address}, cache_root => $cache_root, %timeout ) if $backend->{kind} eq 'url'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | Skilletor::Error->throw( 'unknown backend kind '.$backend->{kind} ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # The key under which a run resolves a backend once for every source name that uses it. | ||||||||||||||||||||||||||||
| 78 | 338 | ▶ | sub identity { | ||||||||||||||||||||||||||
|
Subroutine
identity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 338 | my ( $self, $backend ) = @_; | |||||||||||||||||||||||||||
| 80 | 338 | return Skilletor::JSON::js_stringify( [ $backend->{kind}, $backend->{address}, $backend->{ref} ] ); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # A trust request for a source, naming the backend that would be used. | ||||||||||||||||||||||||||||
| 84 | 1 | ▶ | sub trust_request { | ||||||||||||||||||||||||||
|
Subroutine
trust_request: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 1 | my ( $self, $name, $backend ) = @_; | |||||||||||||||||||||||||||
| 86 | 1 | return { name => $name, kind => $backend->{kind}, url => $backend->{address} }; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 34 | 1; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||||||||||