blib/lib/App/Grok/Resource/Spec.pm

filter: Statementstmt 72.9% 62 / 85 Branchbran 35.7% 10 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.6% 11 / 12 Podpod 100% 4 / 4 totaltotal 67.4% 87 / 129 CC 21 SCAR 33.2
CC
21
Cov
64%
spec_update
90.0
spec_fetch
5.0
_build_index
5.0
CRAP 27.8
↑ Summary blib/lib/App/Grok/Resource/Tablet.pm »
1 package App::Grok::Resource::Spec;
2 40 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 40 $App::Grok::Resource::Spec::AUTHORITY = 'cpan:HINRIK';
4 }
5 {
6 40 $App::Grok::Resource::Spec::VERSION = '0.26';
7 }
8  
9 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use App::Grok::Common qw<data_dir download>;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use File::ShareDir qw<dist_dir>;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use File::Spec::Functions qw<catdir catfile splitpath>;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 40 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 40 our @EXPORT_OK = qw(spec_index spec_fetch spec_locate spec_update);
17 40 our %EXPORT_TAGS = ( ALL => [@EXPORT_OK] );
18  
19 40 my %index;
20 40 my $dist_dir = dist_dir('Perl6-Doc');
21 my %docs = map {
22 40 substr($_, 0, 1) => catdir($dist_dir, $_)
23 } qw<Apocalypse Exegesis Magazine Synopsis>;
24  
25 0 ▶sub spec_update {
Subroutine spec_update: not called CC=9 SCAR=45.0
Pod documented
26 0 my $res_dir = catdir(data_dir(), 'resources', 'spec');
27 0 ▶ if (!-d $res_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $res_dir) 0 0 0
28 0 ▶ mkdir $res_dir or die "Can't create $res_dir: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $res_dir or die "Can't create ${res_dir}: $!\n" 0 0 0
29  
30 }
31 0 my $s32_dir = catdir($res_dir, 'S32-setting-library');
32 0 ▶ if (!-d $s32_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $s32_dir) 0 0 0
33 0 ▶ mkdir $s32_dir or die "Can't create $s32_dir: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $s32_dir or die "Can't create ${s32_dir}: $!\n" 0 0 0
34 }
35  
36 0 print "Downloading specs...\n";
37 0 my @specs = map { chomp; $_ } <DATA>;
38  
39 0 my $i = 0;
40 0 for my $spec_url (@specs) {
41 0 $i++;
42 0 my $s32 = $spec_url =~ /S32/;
43 0 my ($filename) = $spec_url =~ m{(?<=/)([^/]+)$};
44 0 ▶ my $title = "($i/".scalar @specs.") ".($s32?'S32-setting-library/': '').$filename;
Branch: 1 branch
BranchTrueFalseTotal
$s32 ? : 0 0 0
45 0 my $content = download($title, $spec_url);
46 0 ▶ my $file = catfile(($s32 ? $s32_dir : $res_dir), $filename);
Branch: 1 branch
BranchTrueFalseTotal
$s32 ? : 0 0 0
47 0 ▶ open my $fh, '>:encoding(utf8)', $file or die "Can't open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(utf8)", $file or die "Can't open ${file}: $!\n" 0 0 0
48 0 print $fh $content;
49 0 close $fh;
50 }
51  
52 0 return;
53 }
54  
55 8 ▶sub spec_fetch {
Subroutine spec_fetch: called CC=5 SCAR=16.2
Pod documented
56 8 my ($topic) = @_;
57 8 ▶ _build_index() if !%index;
Branch: 1 branch
BranchTrueFalseTotal
unless %index 8 0 8
58
59 8 for my $doc (keys %index) {
60 298 ▶ if ($doc =~ /^\Q$topic/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc =~ /^\Q$topic\E/i) 6 292 298
61 6 ▶ open my $handle, '<', $index{$doc} or die "Can't open $index{$doc}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<", $index{$doc} or die "Can't open $index{$doc}: $!" 6 0 6
62 6 my $pod = do { local $/ = undef; scalar <$handle> };
63 6 close $handle;
64 6 return $pod;
65 }
66 }
67 2 return;
68 }
69  
70 2 ▶sub spec_index {
Subroutine spec_index: called CC=2 SCAR=7.2
Pod documented
71 2 ▶ _build_index() if !%index;
Branch: 1 branch
BranchTrueFalseTotal
unless %index 2 0 2
72 2 return keys %index;
73 }
74  
75 2 ▶sub spec_locate {
Subroutine spec_locate: called CC=4 SCAR=14.3
Pod documented
76 2 my ($topic) = @_;
77 2 ▶ _build_index() if !%index;
Branch: 1 branch
BranchTrueFalseTotal
unless %index 2 0 2
78
79 2 for my $doc (keys %index) {
80 78 ▶ return $index{$doc} if $doc =~ /^$topic/i;
Branch: 1 branch
BranchTrueFalseTotal
if $doc =~ /^$topic/i 2 76 78
81 }
82  
83 0 return;
84 }
85  
86 12 ▶sub _build_index {
Subroutine _build_index: called CC=5 SCAR=16.1
87 12 ▶ while (my ($type, $dir) = each %docs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %docs) 48 12 60
88 48 for my $file (glob "$dir/*.pod") {
89 504 my $name = (splitpath($file))[2];
90 504 $name =~ s/\.pod$//;
91 504 $index{$name} = $file;
92 }
93 }
94  
95 # man pages (perlintro, etc)
96 12 my $pages_dir = catdir($dist_dir, 'man_pages');
97 12 for my $file (glob "$pages_dir/*.pod") {
98 48 my $name = (splitpath($file))[2];
99 48 $name =~ s/\.pod$//;
100 48 $index{$name} = $file;
101 }
102  
103 # synopsis 32
104 12 my $S32_dir = catdir($docs{S}, 'S32-setting-library');
105 12 for my $file (glob "$S32_dir/*.pod") {
106 120 my $name = (splitpath($file))[2];
107 120 $name =~ s/\.pod$//;
108 120 $name = "S32-$name";
109 120 $index{$name} = $file;
110 }
111  
112 12 return;
113 }
114  
115 40 1;
116  
117 =encoding utf8
118  
119 =head1 NAME
120  
121 App::Grok::Resource::Spec - Perl 6 specification resource for grok
122  
123 =head1 SYNOPSIS
124  
125 use strict;
126 use warnings;
127 use App::Grok::Resource::Spec qw<:ALL>;
128  
129 # list of all Synopsis, Exegeses, etc
130 my @index = spec_index();
131  
132 # get the contents of Synopsis 02
133 my $pod = spec_fetch('s02');
134  
135 # filename containing S02
136 my $file = spec_locate('s02');
137  
138 =head1 DESCRIPTION
139  
140 This module the locates Apocalypses, Exegeses, Synopsis and magazine articles
141 distributed with L<Perl6::Doc>.
142  
143 It also includes user documentation like F<perlintro> and F<perlsyn>.
144  
145 =head1 FUNCTIONS
146  
147 =head2 C<spec_update>
148  
149 Takes no arguments. Downloads the latest specifications (Synopses) into
150 grok's data dir.
151  
152 =head2 C<spec_index>
153  
154 Doesn't take any arguments. Returns a list of all documents known to this
155 resource.
156  
157 =head2 C<spec_fetch>
158  
159 Takes the name of a document as an argument. It is case-insensitive and you
160 only need to specify the first three characters (though more are allowed),
161 e.g. C<spec_fetch('s02')>. Returns the Pod text of the document.
162  
163 =head2 C<spec_locate>
164  
165 Takes the same argument as L<C<spec_fetch>|/spec_fetch>. Returns the filename
166 corresponding to the given document.
167  
168 =cut
169 __DATA__
↑ Summary blib/lib/App/Grok/Resource/Tablet.pm »