| 1 | package Luv::CLI::Command::Add; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: add a library dependency from a git repo or the registry | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use v5.38; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Object::Pad; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use App::Cmd::Setup -command; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use File::HomeDir; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Luv::CLI::Manifest; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Luv::CLI::Git; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Luv::CLI::Registry; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 0 | ▶ | sub abstract { | ||||||||||||||||||||||||||
|
Subroutine
abstract: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 0 | "Adds a library dependency from a git repo."; | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub opt_spec { | ||||||||||||||||||||||||||
|
Subroutine
opt_spec: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 0 | return ( [ 'ref=s' => 'git ref (branch/tag/commit) to check out' ], ); | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub execute ( $self, $opt, $args ) { | ||||||||||||||||||||||||||
|
Subroutine
execute: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | die "Usage: luv add <repo-url|library-name> [...]\n" unless @$args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | my $manifest_path = "luv.json"; | |||||||||||||||||||||||||||
| 27 | 0 | ▶ | die "No luv.json found — run 'luv init' first\n" unless -e $manifest_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | my $manifest = Luv::CLI::Manifest->new( path => $manifest_path ); | |||||||||||||||||||||||||||
| 30 | 0 | $manifest->load; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | for my $target (@$args) { | |||||||||||||||||||||||||||
| 33 | 0 | my ( $name, $url ); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ( $target =~ m{^https?://} || $target =~ m{^git\@} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$target =~ m[^https?://]u or $target =~ /^git\@/u
MC/DC0%0/2
$target =~ m[^https?://]u or $target =~ /^git\@/u
$target =~ m[^https?://]u
$target =~ /^git\@/u
Truth table0%0/3
A or B
A= $target =~ m[^https?://]uB= $target =~ /^git\@/u
| |||||||||||||||||||||||||||||
| 36 | 0 | $url = $target; | |||||||||||||||||||||||||||
| 37 | 0 | ($name) = $url =~ m{([^/]+?)(?:\.git)?/?$}; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | else { | ||||||||||||||||||||||||||||
| 40 | 0 | my $cache_path | |||||||||||||||||||||||||||
| 41 | = File::HomeDir->my_home . '/.cache/luv/registry.json'; | ||||||||||||||||||||||||||||
| 42 | 0 | my $registry | |||||||||||||||||||||||||||
| 43 | = Luv::CLI::Registry->new( cache_path => $cache_path ); | ||||||||||||||||||||||||||||
| 44 | 0 | $registry->load; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | my $entry = $registry->find($target); | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | unless ($entry) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | warn | |||||||||||||||||||||||||||
| 49 | "Library '$target' not found in registry — try 'luv search $target' or pass a git URL\n"; | ||||||||||||||||||||||||||||
| 50 | 0 | next; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | $name = $target; | |||||||||||||||||||||||||||
| 54 | 0 | $url = $entry->{url}; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ( $manifest->has_dependency($name) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | warn "Dependency '$name' already exists — skipping\n"; | |||||||||||||||||||||||||||
| 59 | 0 | next; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | my $dest = $manifest->library_dir . "/$name"; | |||||||||||||||||||||||||||
| 63 | 0 | my $git = Luv::CLI::Git->new; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | eval { | |||||||||||||||||||||||||||
| 66 | 0 | $git->clone( $url, $dest, $opt->{ref} ); | |||||||||||||||||||||||||||
| 67 | $manifest->add_dependency( | ||||||||||||||||||||||||||||
| 68 | $name, | ||||||||||||||||||||||||||||
| 69 | url => $url, | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | ref => $opt->{ref} // 'main', | ||||||||||||||||||||||||||
|
Condition0%0/2
$opt->{"ref"} // "main"
MC/DC0%0/1
$opt->{"ref"} // "main"
$opt->{"ref"}
Truth table0%0/2
A
A= $opt->{"ref"}
| |||||||||||||||||||||||||||||
| 71 | path => $dest | ||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | 0 | print "Added '$name' from $url\n"; | |||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | warn "Failed to add '$target': $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | $manifest->save; | |||||||||||||||||||||||||||
| 79 | 0 | return; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | 1; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | __END__ | ||||||||||||||||||||||||||||