lib/App/LinkSite.pm

filter: Statementstmt 92.3% 97 / 105 Branchbran 62.5% 15 / 24 Conditioncond 71.4% 10 / 14 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 21 / 21 Podpod 100% 3 / 3 totaltotal 85.2% 150 / 176 CC 19 SCAR 15.8
CC
19
Cov
85%
run
11.1
do_this
8.2
__ANON__
6.0
CRAP 4.8
« lib/App/LinkSite/Link.pm ↑ Summary lib/App/LinkSite/Social.pm »
1 =head1 NAME
2  
3 App::LinkSite - Create a website listing all of your links
4  
5 =head1 SYNOPIS
6  
7 (You probably want to just look at the L<linksite> application.)
8  
9 =head1 DESCRIPTION
10  
11 The main driver class for App::LinkSite.
12  
13 =cut
14  
15 15 ▶use Feature::Compat::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 15 class App::LinkSite {
18 15 our $VERSION = '0.2.0';
19 15 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 15 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 15 ▶ use feature qw[say signatures];
Subroutine BEGIN: called CC=1 SCAR=0.0
22 15 ▶ no if $] >= 5.038, 'warnings', qw[experimental::signatures experimental::class];
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 15 ▶ use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 15 ▶ use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 15 ▶ use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 15 ▶ use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 15 ▶ use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 15 ▶ use FindBin '$Bin';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 15 ▶ use File::ShareDir 'dist_dir';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 15 ▶ use App::LinkSite::Site;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 15 ▶ use App::LinkSite::Link;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 15 ▶ use App::LinkSite::Social;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 15 ▶ use App::LinkSite::Section;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 15 field $file :reader :param = 'links.json';
38 # Where to look for the templates.
39 # If we've been installed from CPAN, then File::Share::dist_name
40 # gives us the correct directory. Otherwise, just look in the local
41 # src directory. Note that dist_name() dies if the directory is not
42 # found - hence the use of eval.
43 15 field $src :reader :param = eval { dist_dir("App-LinkSite") } || "$Bin/../src";
44 15 field $out :reader :param = 'docs';
45 15 field $ga4 :reader :param = undef;
46 15 field $site :reader :param = undef;
47  
48 15 field $tt;
49  
50 18 ▶ ADJUST {
Subroutine __ANON__: called CC=6 SCAR=18.0
51 18 my $json = path($file)->slurp_utf8;
52 18 my $data = JSON->new->decode($json);
53  
54 18 ▶ $ga4 = $data->{ga4} // '';
Condition100%2/2
$data->{"ga4"} // ""
l!l
810
MC/DC100%1/1
$data->{"ga4"} // ""
$data->{"ga4"}
Truth table100%2/2
A
A= $data->{"ga4"}
Aresult
11
00
55  
56 18 $tt = Template->new({
57 # Templates in the CPAN distro directory
58 INCLUDE_PATH => $src,
59 ENCODING => 'utf8',
60 # Output in the data directory
61 OUTPUT_PATH => $out,
62 VARIABLES => {
63 ga4 => $ga4,
64 version => $VERSION,
65 }
66 });
67  
68 my $socials = [ map {
69 71 ▶ $_->{handle} //= $data->{handle};
Condition66%2/3
$_->{"handle"} //= $data->{"handle"}
l!l&&r!l&&!r
16550
MC/DC0%0/2
$_->{"handle"} //= $data->{"handle"}
$_->{"handle"} $data->{"handle"}
Truth table66%2/3
A //= B
A= $_->{"handle"}B= $data->{"handle"}
ABresult
1X1
011
000
70 71 App::LinkSite::Social->new(%$_)
71 18 } $data->{social}->@* ];
72  
73 my $make_link = sub {
74 my ($config) = @_;
75 my $utm = exists $config->{utm} ? $config->{utm} : $data->{utm};
76 $utm = { $data->{utm}->%*, %$utm }
77 if ref $data->{utm} eq 'HASH' && ref $utm eq 'HASH';
78 return App::LinkSite::Link->new(%$config, utm => $utm);
79 18 };
80 18 my $links = [ map { $make_link->($_) } $data->{links}->@* ];
81  
82 18 my $sections = [];
83 18 ▶ if ($data->{sections}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"sections"}) 1 17 18
84 $sections = [ map {
85 1 my $section_links = [ map { $make_link->($_) } $_->{links}->@* ];
86 App::LinkSite::Section->new(
87 title => $_->{title},
88 1 links => $section_links,
89 )
90 1 } $data->{sections}->@* ];
91 }
92  
93 $site = App::LinkSite::Site->new(
94 name => $data->{name},
95 handle => $data->{handle},
96 image => $data->{image},
97 desc => $data->{desc},
98 og_image => $data->{og_image},
99 site_url => $data->{site_url},
100 socials => $socials,
101 links => $links,
102 sections => $sections,
103 $data->{text_color} ? (text_color => $data->{text_color}) : (),
104 18 ▶ $data->{background_color} ? (background_color => $data->{background_color} ) : (),
Branch: 2 branches
BranchTrueFalseTotal
$data->{'text_color'} ? : 0 18 18
$data->{'background_color'} ? : 0 18 18
105 );
106 }
107  
108 =head1 METHODS
109  
110 =head2 run
111  
112 The main driver method for the process.
113  
114 =cut
115  
116 13 ▶ method run {
Subroutine run: called CC=6 SCAR=24.0
Pod documented
117 13 debug("src is: $src");
118 13 debug("out is: $out");
119 13 path($out)->mkdir;
120 13 ▶ find( { wanted => sub { $self->do_this }, no_chdir => 1 }, $src);
Subroutine __ANON__: called CC=1 SCAR=0.0
121  
122 13 ▶ if ($site->image or $site->og_image) {
Branch: 1 branch
BranchTrueFalseTotal
if ($site->image or $site->og_image) 0 13 13
Condition33%1/3
$site->image or $site->og_image
l!l&&r!l&&!r
0013
MC/DC0%0/2
$site->image or $site->og_image
$site->image $site->og_image
Truth table33%1/3
A or B
A= $site->imageB= $site->og_image
ABresult
1X1
011
000
123 0 path("$out/img")->mkdir;
124 0 debug("Copy images");
125 0 for my $img ($site->image, $site->og_image) {
126 0 ▶ next unless $img;
Branch: 1 branch
BranchTrueFalseTotal
unless $img 0 0 0
127 0 path("img/$img")->copy("$out/img");
128 }
129 }
130  
131 13 ▶ if (-f './CNAME') {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "./CNAME") 0 13 13
132 0 debug("Copy CNAME");
133 0 path('./CNAME')->copy("$out/CNAME");
134 }
135  
136 13 debug("Copy input JSON file");
137 13 path($file)->copy($out);
138 }
139  
140 =head2 do_this
141  
142 A method is called for each file that is found in the `src` directory.
143  
144 Template files retain their source-relative path with only the final C<.tt>
145 suffix removed. Other files are copied unchanged at their relative paths.
146  
147 =cut
148  
149 64 ▶ method do_this {
Subroutine do_this: called CC=8 SCAR=21.0
Pod documented
150 64 ▶ if ($File::Find::name eq $src or $File::Find::name eq "$src/") {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Find::name eq $src or $File::Find::name eq "$src/") 13 51 64
Condition66%2/3
$File::Find::name eq $src or $File::Find::name eq "$src/"
l!l&&r!l&&!r
13051
MC/DC50%1/2
$File::Find::name eq $src or $File::Find::name eq "$src/"
$File::Find::name eq $src $File::Find::name eq "$src/"
Truth table66%2/3
A or B
A= $File::Find::name eq $srcB= $File::Find::name eq "$src/"
ABresult
1X1
011
000
151 13 debug("Skipping $File::Find::name");
152 13 return;
153 }
154  
155 51 my $path = $File::Find::name =~ s|^$src/||r;
156  
157 51 ▶ if (-f && /\.tt\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $_ and /\.tt\z/) { } 18 33 51
Condition100%3/3
-f $_ and /\.tt\z/
!ll&&!rl&&r
181518
MC/DC100%2/2
-f $_ and /\.tt\z/
-f $_ /\.tt\z/
Truth table100%3/3
A and B
A= -f $_B= /\.tt\z/
ABresult
0X0
100
111
158 18 my $output = $path =~ s/\.tt\z//r;
159 18 debug("Process $path to", $output);
160 18 ▶ $tt->process($path, { site => $self->site }, $output, { binmode => ':encoding(UTF-8)' })
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($path, {"site", $self->site}, $output, {"binmode", ":encoding(UTF-8)"}) or die $tt->error 18 0 18
161 or die $tt->error;
162 } else {
163 33 ▶ if (-d) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $_) { } 18 15 33
elsif (-f $_) { } 15 0 15
164 18 debug("Make directory $path");
165 18 path("$out/$path")->mkdir;
166 } elsif (-f) {
167 15 debug("Copy $path");
168 15 path("$src/$path")->copy("$out/$path");
169 } else {
170 0 debug("Confused by $File::Find::name");
171 }
172 }
173 }
174  
175 =head2 debug
176  
177 Debug output. Set `LINKSITE_DEBUG` to a true value to turn this on.
178  
179 export LINKSITE_DEBUG=1
180  
181 =cut
182  
183 103 ▶ sub debug {
Subroutine debug: called CC=2 SCAR=7.6
Pod documented
184 103 ▶ warn "@_\n" if $ENV{LINKSITE_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"LINKSITE_DEBUG"} 0 103 103
185 }
186 }
187  
188 =head1 AUTHOR
189  
190 Dave Cross <dave@davecross.co.uk>
191  
192 =head1 COPYRIGHT AND LICENCE
193  
194 Copyright (c) 2024, Magnum Solutions Ltd. All Rights Reserved.
195  
196 This library is free software; you can redistribute it and/or modify it
197 under the same terms as Perl itself.
198  
199 =cut
200  
201 15 1;
« lib/App/LinkSite/Link.pm ↑ Summary lib/App/LinkSite/Social.pm »