| 1 | # PODNAME: Luv::CLI::Registry | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: local cache of the awesome-love2d library list | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use v5.38; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Object::Pad; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use File::Path qw(make_path); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | class Luv::CLI::Registry; | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 2 | field $cache_path : param; | |||||||||||||||||||||||||||||||
| 13 | 2 | field %entries; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | method cache_path () { return $cache_path; } | ||||||||||||||||||||||||||||||
|
Subroutine
cache_path: not called CC=1 SCAR=4.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | method load () { | ||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | return unless -e $cache_path; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | open my $fh, '<', $cache_path or die "Cannot read $cache_path: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 20 | 1 | local $/; | |||||||||||||||||||||||||||||||
| 21 | 1 | my $data = JSON::PP->new->utf8->decode(<$fh>); | |||||||||||||||||||||||||||||||
| 22 | 1 | close $fh; | |||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | %entries = %{ $data->{entries} // {} }; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$data->{"entries"} // {}
MC/DC0%0/1
$data->{"entries"} // {}
$data->{"entries"}
Truth table50%1/2
A
A= $data->{"entries"}
| |||||||||||||||||||||||||||||||||
| 24 | 1 | return; | |||||||||||||||||||||||||||||||
| 25 | 2 | } | |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | method save () { | ||||||||||||||||||||||||||||||
|
Subroutine
save: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 28 | 1 | File::Path::make_path( $self->cache_dir ); | |||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | open my $fh, '>', $cache_path or die "Cannot write $cache_path: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 30 | 1 | print {$fh} JSON::PP->new->utf8->canonical->pretty->encode( | |||||||||||||||||||||||||||||||
| 31 | { updated_at => time, | ||||||||||||||||||||||||||||||||
| 32 | entries => \%entries, | ||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||
| 35 | 1 | close $fh; | |||||||||||||||||||||||||||||||
| 36 | 1 | return; | |||||||||||||||||||||||||||||||
| 37 | 2 | } | |||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | method cache_dir () { | ||||||||||||||||||||||||||||||
|
Subroutine
cache_dir: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 40 | 1 | my ($dir) = $cache_path =~ m{^(.*)/[^/]+$}; | |||||||||||||||||||||||||||||||
| 41 | 1 | return $dir; | |||||||||||||||||||||||||||||||
| 42 | 2 | } | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | method is_stale ( $max_age_seconds = 86400 * 7 ) { | ||||||||||||||||||||||||||||||
|
Subroutine
is_stale: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | return 1 unless -e $cache_path; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 0 | return ( time - ( stat($cache_path) )[9] ) > $max_age_seconds; | |||||||||||||||||||||||||||||||
| 47 | 2 | } | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 12 | ▶ | method add_entry ( $name, %info ) { | ||||||||||||||||||||||||||||||
|
Subroutine
add_entry: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 50 | $entries{ lc $name } = { | ||||||||||||||||||||||||||||||||
| 51 | name => $name, | ||||||||||||||||||||||||||||||||
| 52 | url => $info{url}, | ||||||||||||||||||||||||||||||||
| 53 | category => $info{category}, | ||||||||||||||||||||||||||||||||
| 54 | description => $info{description}, | ||||||||||||||||||||||||||||||||
| 55 | 12 | }; | |||||||||||||||||||||||||||||||
| 56 | 12 | return; | |||||||||||||||||||||||||||||||
| 57 | 2 | } | |||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | method search ($term) { | ||||||||||||||||||||||||||||||
|
Subroutine
search: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 60 | 2 | my @matches; | |||||||||||||||||||||||||||||||
| 61 | 2 | for my $name ( keys %entries ) { | |||||||||||||||||||||||||||||||
| 62 | 2 | push @matches, { name => $name, %{ $entries{$name} } } | |||||||||||||||||||||||||||||||
| 63 | if $name =~ /\Q$term\E/i | ||||||||||||||||||||||||||||||||
| 64 | 6 | ▶ | || ( $entries{$name}{description} // '' ) =~ /\Q$term\E/i; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
$name =~ /\Q$term\E/iu or ($entries{$name}{"description"} // "") =~ /\Q$term\E/iu
$entries{$name}{"description"} // ""
MC/DC0%0/2
$name =~ /\Q$term\E/iu or ($entries{$name}{"description"} // "") =~ /\Q$term\E/iu
$name =~ /\Q$term\E/iu
$entries{$name}{"description"}
Truth table66%2/3
A or B
A= $name =~ /\Q$term\E/iuB= $entries{$name}{"description"}
| |||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | 2 | return @matches; | |||||||||||||||||||||||||||||||
| 67 | 2 | } | |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 3 | ▶ | method find ($name) { | ||||||||||||||||||||||||||||||
|
Subroutine
find: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 70 | 3 | my ($match) = grep { lc($_) eq lc($name) } keys %entries; | |||||||||||||||||||||||||||||||
| 71 | 3 | ▶ | return $match ? $entries{$match} : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 2 | } | |||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 4 | ▶ | method parse_readme ($markdown) { | ||||||||||||||||||||||||||||||
|
Subroutine
parse_readme: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 75 | 4 | my $category = 'Uncategorized'; | |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 4 | for my $line ( split /\n/, $markdown ) { | |||||||||||||||||||||||||||||||
| 78 | 32 | ▶ | if ( $line =~ /^##\s+(.+)/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | 8 | $category = $1; | |||||||||||||||||||||||||||||||
| 80 | 8 | next; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 24 | ▶ | if ( $line =~ /^\s*[-*]\s*\[([^\]]+)\]\(([^)]+)\)\s*-\s*(.+)/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 12 | my ( $name, $url, $description ) = ( $1, $2, $3 ); | |||||||||||||||||||||||||||||||
| 85 | 12 | $self->add_entry( | |||||||||||||||||||||||||||||||
| 86 | $name, | ||||||||||||||||||||||||||||||||
| 87 | url => $url, | ||||||||||||||||||||||||||||||||
| 88 | category => $category, | ||||||||||||||||||||||||||||||||
| 89 | description => $description | ||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | 4 | return; | |||||||||||||||||||||||||||||||
| 95 | 2 | } | |||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | method refresh () { | ||||||||||||||||||||||||||||||
|
Subroutine
refresh: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 98 | 0 | my $readme_url | |||||||||||||||||||||||||||||||
| 99 | = 'https://raw.githubusercontent.com/love2d-community/awesome-love2d/master/README.md'; | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | 0 | require IPC::Run; | |||||||||||||||||||||||||||||||
| 102 | 0 | my ( $out, $err ); | |||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | IPC::Run::run( [ 'curl', '-sL', $readme_url ], \undef, \$out, \$err ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 104 | or die "Failed to fetch awesome-love2d README:\n$err"; | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 0 | %entries = (); | |||||||||||||||||||||||||||||||
| 107 | 0 | $self->parse_readme($out); | |||||||||||||||||||||||||||||||
| 108 | 0 | $self->save; | |||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 0 | return scalar keys %entries; | |||||||||||||||||||||||||||||||
| 111 | 2 | } | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | method all_entries () { | ||||||||||||||||||||||||||||||
|
Subroutine
all_entries: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 114 | 1 | return values %entries; | |||||||||||||||||||||||||||||||
| 115 | 2 | } | |||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 2 | 1; | |||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||||||