Stubb.pm

filter: Statementstmt 70.5% 108 / 153 Branchbran 36.3% 16 / 44 Conditioncond 27.7% 10 / 36 MC/DCmcdc 4.7% 1 / 21 Subroutinesub 85.7% 24 / 28 Podpod 50.0% 2 / 4 totaltotal 56.2% 161 / 286 CC 43 SCAR 37.4
CC
43
Cov
58%
list
182.0
init
27.0
_get_template
9.4
CRAP 42.3
↑ Summary Stubb/Render.pm »
1 package File::Stubb;
2 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 our $VERSION = '0.04';
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use File::Stubb::Home;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Stubb::Render;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 use constant {
14 2 MODE_DEFAULT => 0,
15 MODE_BATCH => 1,
16 MODE_LIST => 2,
17 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 $0 =~ s!^.*[/\\]!!;
20  
21 2 my $PRGNAM = 'stubb';
22 2 my $PRGVER = $VERSION;
23  
24 2 my $HELP = <<"HERE";
25 $PRGNAM - $PRGVER
26 Usage:
27 $0 [options] file template
28 $0 [options] file.template
29 $0 [options] -t template file ...
30 $0 [options] -l template
31  
32 Options:
33 -d|--template-dir=<dir> Specify template directory
34 -t|--template=<template> Specify template for batch file creation
35 -s|--substitute=<params> Provide stubb substitution parameters
36 -a|--hidden Toggle whether to render hidden files or not
37 -A|--no-hidden
38 -c|--copy-perms Toggle whether to copy template permissions or not
39 -C|--no-copy-perms
40 -w|--follow-symlinks Toggle whether to follow symlinks or not
41 -W|--no-follow-symlinks
42 -R|--restricted Do not render Perl or shell targets
43 -U|--no-defaults Disable the use of default substitution parameters
44 -I|--no-config Ignore template's configuration file
45 -l|--list List substitution targets in template
46 -q|--quiet Disbale informative output
47  
48 -h|--help Display this help message
49 -v|--version Display version/copyright info
50  
51 Consults the $PRGNAM(1) manual for more documentation.
52 HERE
53  
54 2 my $VER_MSG = <<"HERE";
55 $PRGNAM - $PRGVER
56  
57 Copyright (C) 2025 Samuel Young
58  
59 This program is free software: you can redistribute it and/or modify
60 it under the terms of the GNU General Public License as published by
61 the Free Software Foundation, either version 3 of the License, or
62 (at your option) any later version.
63 HERE
64  
65 sub _subst_params {
66  
67 2 ▶ my ($str) = @_;
Subroutine _subst_params: called CC=3 SCAR=11.0
68  
69 2 my $params = {};
70  
71 # Temporarily hide escaped commas.
72 2 $str =~ s/\\,/\x{fffd}/g;
73  
74 2 my @kvs = grep { /=>/ } split /,/, $str;
75  
76 2 for my $kv (@kvs) {
77  
78 6 my ($k, $v) = split /=>/, $kv, 2;
79 6 $k =~ s/^\s+|\s+$//g;
80 6 $v =~ s/^\s+|\s+$//g;
81  
82 6 ▶ unless ($k =~ /^[a-zA-Z0-9_]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($k =~ /^[a-zA-Z0-9_]+$/u) 0 6 6
83 0 die "'$k' is not a valid substitution target\n";
84 }
85  
86 6 $v =~ s/\x{fffd}/,/g;
87  
88 6 $params->{ $k } = $v;
89  
90 }
91  
92 2 return $params;
93  
94 }
95  
96 sub _get_template {
97  
98 11 ▶ my ($self, $str, %param) = @_;
Subroutine _get_template: called CC=6 SCAR=22.4
99 11 ▶ my $no_path = $param{ no_path } // 0;
Condition100%2/2
$param{"no_path"} // 0
l!l
110
MC/DC100%1/1
$param{"no_path"} // 0
$param{"no_path"}
Truth table100%2/2
A
A= $param{"no_path"}
Aresult
11
00
100  
101 my ($p) =
102 23 grep { -e }
103 23 map { File::Spec->catfile($_, "$str.stubb") }
104 11 @{ $self->{ TempDir } };
105  
106  
107 11 ▶ if ($str !~ /[\/\\]/ and defined $p) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $str =~ m([/\\])u and defined $p) { } 11 0 11
elsif (not $no_path and -e $str) { } 0 0 0
Condition33%1/3
not $str =~ m([/\\])u and defined $p
!ll&&!rl&&r
0011
MC/DC0%0/2
not $str =~ m([/\\])u and defined $p
not $str =~ m([/\\])u defined $p
Truth table33%1/3
not(A) and B
A= not $str =~ m([/\\])uB= defined $p
ABresult
0X0
100
111
108 11 return $p;
109 ▶ } elsif (!$no_path and -e $str) {
Condition0%0/3
not $no_path and -e $str
!ll&&!rl&&r
000
MC/DC0%0/2
not $no_path and -e $str
not $no_path -e $str
Truth table0%0/3
not(A) and B
A= not $no_pathB= -e $str
ABresult
0X0
100
111
110 0 return $str;
111 } else {
112 0 return undef;
113 }
114  
115 }
116  
117 sub render {
118  
119 9 ▶ my ($self) = @_;
Subroutine render: called CC=5 SCAR=16.4
Pod undocumented
120  
121 my $render = File::Stubb::Render->new(
122 template => $self->{ Template },
123 subst => $self->{ Subst },
124 ignore_config => $self->{ IgnoreConf },
125 render_hidden => $self->{ Hidden },
126 follow_symlinks => $self->{ FollowLinks },
127 copy_perms => $self->{ CopyPerms },
128 defaults => $self->{ Defaults },
129 restricted => $self->{ Restricted },
130 9 );
131  
132 9 my @created;
133  
134 9 for my $f (@{ $self->{ Files } }) {
135 9 push @created, $render->render($f);
136 }
137  
138 9 ▶ if ($self->{ Verbose } and $self->{ Files }[0] ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Verbose"} and $self->{"Files"}[0] ne "-") 9 0 9
Condition33%1/3
$self->{"Verbose"} and $self->{"Files"}[0] ne "-"
!ll&&!rl&&r
009
MC/DC0%0/2
$self->{"Verbose"} and $self->{"Files"}[0] ne "-"
$self->{"Verbose"} $self->{"Files"}[0] ne "-"
Truth table33%1/3
A and B
A= $self->{"Verbose"}B= $self->{"Files"}[0] ne "-"
ABresult
0X0
100
111
139 9 say "Created:";
140 9 for my $f (@created) {
141 17 say " $f";
142 }
143 }
144  
145 9 return 1;
146  
147 }
148  
149 sub list {
150  
151 0 ▶ my ($self) = @_;
Subroutine list: not called CC=13 SCAR=52.0
Pod undocumented
152  
153 my $render = File::Stubb::Render->new(
154 template => $self->{ Template },
155 ignore_config => $self->{ IgnoreConf },
156 hidden => $self->{ Hidden },
157 follow_symlinks => $self->{ FollowLinks },
158 restricted => $self->{ Restricted },
159 0 );
160  
161 0 my $targets = $render->targets;
162  
163 0 my $have_basic = !! @{ $targets->{ basic } };
164 0 my $have_perl = !! @{ $targets->{ perl } };
165 0 my $have_shell = !! @{ $targets->{ shell } };
166  
167 0 ▶ unless ($have_basic or $have_perl or $have_shell) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($have_basic or $have_perl or $have_shell) 0 0 0
Condition0%0/6
$have_basic or $have_perl or $have_shell
l!l&&r!l&&!r
000
$have_basic or $have_perl
l!l&&r!l&&!r
000
MC/DC0%0/3
$have_basic or $have_perl or $have_shell
$have_basic $have_perl $have_shell
Truth table0%0/4
A or B or C
A= $have_basicB= $have_perlC= $have_shell
ABCresult
1XX1
01X1
0011
0000
168 0 say "No targets in $self->{ Template }";
169 0 return 1;
170 }
171  
172 0 ▶ if ($have_basic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_basic) 0 0 0
173 0 say "Substitution targets:";
174 0 for my $t (@{ $targets->{ basic } }) {
175 0 say " $t";
176 }
177 0 ▶ print "\n" if $have_perl or $have_shell;
Branch: 1 branch
BranchTrueFalseTotal
if $have_perl or $have_shell 0 0 0
Condition0%0/3
$have_perl or $have_shell
l!l&&r!l&&!r
000
MC/DC0%0/2
$have_perl or $have_shell
$have_perl $have_shell
Truth table0%0/3
A or B
A= $have_perlB= $have_shell
ABresult
1X1
011
000
178 }
179  
180 0 ▶ if ($have_perl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_perl) 0 0 0
181 0 say "Perl targets:";
182 0 for my $t (@{ $targets->{ perl } }) {
183 0 say " $t";
184 }
185 0 ▶ print "\n" if $have_shell;
Branch: 1 branch
BranchTrueFalseTotal
if $have_shell 0 0 0
186 }
187  
188 0 ▶ if ($have_shell) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_shell) 0 0 0
189 0 say "Shell targets:";
190 0 for my $t (@{ $targets->{ shell } }) {
191 0 say " $t";
192 }
193 }
194  
195 0 return 1;
196  
197 }
198  
199 sub init {
200  
201 11 ▶ my ($class) = @_;
Subroutine init: called CC=17 SCAR=32.9
Pod documented
202  
203 11 my $self = {
204 Mode => MODE_DEFAULT,
205 Files => [],
206 Template => undef,
207 Subst => undef,
208 TempDir => [],
209 Verbose => 1,
210 # Render options
211 Hidden => undef,
212 FollowLinks => undef,
213 CopyPerms => undef,
214 Defaults => undef,
215 IgnoreConf => undef,
216 Restricted => undef,
217 };
218  
219 11 bless $self, $class;
220  
221 11 my $subst = [];
222 11 my $temp;
223  
224 11 Getopt::Long::config('bundling');
225 GetOptions(
226 'template-dir|d=s' => $self->{ TempDir },
227 'template|t=s' => sub {
228 1 ▶ $self->{ Mode } = MODE_BATCH;
Subroutine __ANON__: called CC=1 SCAR=0.0
229 1 $temp = $_[1];
230 },
231 'substitute|s=s' => $subst,
232 0 ▶ 'list|l' => sub { $self->{ Mode } = MODE_LIST },
Subroutine __ANON__: not called CC=1 SCAR=6.9
233 1 ▶ 'quiet|q' => sub { $self->{ Verbose } = 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
234 # Render options
235 1 ▶ 'hidden|a' => sub { $self->{ Hidden } = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
236 2 ▶ 'no-hidden|A' => sub { $self->{ Hidden } = 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
237 1 ▶ 'follow-symlinks|w' => sub { $self->{ FollowLinks } = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
238 1 ▶ 'no-follow-symlinks|W' => sub { $self->{ FollowLinks } = 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
239 1 ▶ 'copy-perms|c' => sub { $self->{ CopyPerms } = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
240 1 ▶ 'no-copy-perms|C' => sub { $self->{ CopyPerms } = 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
241 1 ▶ 'restricted|R' => sub { $self->{ Restricted } = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
242 2 ▶ 'no-defaults|U' => sub { $self->{ Defaults } = 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
243 2 ▶ 'no-config|I' => sub { $self->{ IgnoreConf } = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
244 # Message options
245 0 ▶ 'help|h' => sub { print $HELP; exit 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
246 0 ▶ 'version|v' => sub { print $VER_MSG; exit 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
247 11 ▶ ) or die "Invalid command-line arguments\n";
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("template-dir|d=s", $self->{"TempDir"}, "template|t=s", sub { $self->{"Mode"} = 1; $temp = $_[1]; } , "substitute|s=s", $subst, "list|l", sub { $self->{"Mode"} = 2; } , "quiet|q", sub { $self->{"Verbose"} = 0; } , "hidden|a", sub { $self->{"Hidden"} = 1; } , "no-hidden|A", sub { $self->{"Hidden"} = 0; } , "follow-symlinks|w", sub { $self->{"FollowLinks"} = 1; } , "no-follow-symlinks|W", sub { $self->{"FollowLinks"} = 0; } , "copy-perms|c", sub { $self->{"CopyPerms"} = 1; } , "no-copy-perms|C", sub { $self->{"CopyPerms"} = 0; } , "restricted|R", sub { $self->{"Restricted"} = 1; } , "no-defaults|U", sub { $self->{"Defaults"} = 0; } , "no-config|I", sub { $self->{"IgnoreConf"} = 1; } , "help|h", sub { print $HELP; exit 0; } , "version|v", sub { print $VER_MSG; exit 0; } ) or die "Invalid command-line arguments\n" 0 11 11
248  
249 11 ▶ if (exists $ENV{ STUBB_TEMPLATES }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"STUBB_TEMPLATES"}) 11 0 11
250 11 push @{ $self->{ TempDir } }, split /:/, $ENV{ STUBB_TEMPLATES };
251 }
252  
253 11 push @{ $self->{ TempDir } }, File::Spec->catfile(home, '.stubb');
254  
255 11 ▶ if ($self->{ Mode } == MODE_DEFAULT) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'Mode'} == 0) { } 10 1 11
elsif ($self->{'Mode'} == 1) { } 1 0 1
elsif ($self->{'Mode'} == 2) { } 0 0 0
256 10 ▶ $self->{ Files }[0] = shift @ARGV // die $HELP;
Condition50%1/2
shift @ARGV // die($HELP)
l!l
100
MC/DC0%0/1
shift @ARGV // die($HELP)
shift @ARGV
Truth table50%1/2
A
A= shift @ARGV
Aresult
11
00
257 10 $temp = shift @ARGV;
258 10 ▶ if (defined $temp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $temp) { } 9 1 10
259 9 ▶ $self->{ Template } = $self->_get_template($temp)
Condition50%1/2
$self->_get_template($temp) // die("'${temp}' is not a valid template\n")
l!l
90
MC/DC0%0/1
$self->_get_template($temp) // die("'${temp}' is not a valid template\n")
$self->_get_template($temp)
Truth table50%1/2
A
A= $self->_get_template($temp)
Aresult
11
00
260 // die "'$temp' is not a valid template\n";
261 } else {
262 1 $temp = (fileparse($self->{ Files }[0], qr/\.[^.]*/))[2];
263 1 $temp =~ s/^\.//;
264 1 ▶ if ($temp eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($temp eq "") 0 1 1
265 0 die "Please specify a stub template either by an additional argument or a file suffix\n";
266 }
267 1 ▶ $self->{ Template } = $self->_get_template($temp, no_path => 1)
Condition50%1/2
$self->_get_template($temp, "no_path", 1) // die("'${temp}' is not a valid template\n")
l!l
10
MC/DC0%0/1
$self->_get_template($temp, "no_path", 1) // die("'${temp}' is not a valid template\n")
$self->_get_template($temp, "no_path", 1)
Truth table50%1/2
A
A= $self->_get_template($temp, "no_path", 1)
Aresult
11
00
268 // die "'$temp' is not a valid template\n";
269 }
270 } elsif ($self->{ Mode } == MODE_BATCH) {
271 1 ▶ die $HELP unless @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 1 1
272 1 $self->{ Template } = $self->_get_template($temp);
273 1 $self->{ Files } = [ @ARGV ];
274 1 ▶ if (@{ $self->{ Files } } > 1 and grep { $_ eq '-' } @{ $self->{ Files } }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"Files"};} > 1 and grep {$_ eq "-";} @{$$self{"Files"};}) 0 1 1
Condition33%1/3
@{$self->{"Files"};} > 1 and grep {$_ eq "-";} @{$$self{"Files"};}
!ll&&!rl&&r
100
MC/DC0%0/2
@{$self->{"Files"};} > 1 and grep {$_ eq "-";} @{$$self{"Files"};}
@{$self->{"Files"};} > 1 grep {$_ eq "-";} @{$$self{"Files"};}
Truth table33%1/3
A and B
A= @{$self->{"Files"};} > 1B= grep {$_ eq "-";} @{$$self{"Files"};}
ABresult
0X0
100
111
275 0 die "Cannot write stub to stdout in batch stub creation\n";
276 }
277 } elsif ($self->{ Mode } == MODE_LIST) {
278 0 ▶ $temp = shift @ARGV // die $HELP;
Condition0%0/2
shift @ARGV // die($HELP)
l!l
00
MC/DC0%0/1
shift @ARGV // die($HELP)
shift @ARGV
Truth table0%0/2
A
A= shift @ARGV
Aresult
11
00
279 0 ▶ $self->{ Template } = $self->_get_template($temp)
Condition0%0/2
$self->_get_template($temp) // die("'${temp}' is not a valid template\n")
l!l
00
MC/DC0%0/1
$self->_get_template($temp) // die("'${temp}' is not a valid template\n")
$self->_get_template($temp)
Truth table0%0/2
A
A= $self->_get_template($temp)
Aresult
11
00
280 // die "'$temp' is not a valid template\n";
281 }
282  
283 11 ▶ if (@$subst) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$subst) 2 9 11
284 $self->{ Subst } = _subst_params(
285 # Don't lead trailing backslashes escape commas in next param
286 2 join ',', map { $_ =~ s/\\$/\\ /r } @$subst
287 );
288 }
289  
290 11 return $self;
291  
292 }
293  
294 sub run {
295  
296 9 ▶ my ($self) = @_;
Subroutine run: called CC=4 SCAR=16.4
Pod documented
297  
298 9 ▶ if ($self->{ Mode } == MODE_DEFAULT or $self->{ Mode } == MODE_BATCH) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'Mode'} == 0 or $self->{'Mode'} == 1) { } 9 0 9
elsif ($self->{'Mode'} == 2) { } 0 0 0
Condition66%2/3
$self->{"Mode"} == 0 or $self->{"Mode"} == 1
l!l&&r!l&&!r
810
MC/DC0%0/2
$self->{"Mode"} == 0 or $self->{"Mode"} == 1
$self->{"Mode"} == 0 $self->{"Mode"} == 1
Truth table66%2/3
A or B
A= $self->{"Mode"} == 0B= $self->{"Mode"} == 1
ABresult
1X1
011
000
299 9 $self->render;
300 } elsif ($self->{ Mode } == MODE_LIST) {
301 0 $self->list;
302 }
303  
304 9 return 1;
305  
306 }
307  
308 2 1;
309  
310 =head1 NAME
311  
312 File::Stubb - Stub file creator
313  
314 =head1 USAGE
315  
316 use File::Stubb;
317  
318 my $stubb = File::Stubb->init;
319 $stubb->run;
320  
321 =head1 DESCRIPTION
322  
323 B<File::Stubb> is the module that provides the command-line interface for
324 L<stubb>. This is a private module, for user documentation you should consult
325 the L<stubb> manual.
326  
327 =head1 METHODS
328  
329 =over 4
330  
331 =item $stubb = File::Stubb->init;
332  
333 Reads C<@ARGV> and returns a blessed B<File::Stubb> object. View the L<stubb>
334 manual for a list of valid arguments.
335  
336 =item $stubb->run;
337  
338 Runs L<stubb> based on the arguments parsed during C<init()>.
339  
340 =back
341  
342 =head1 AUTHOR
343  
344 Written by Samuel Young, E<lt>samyoung12788@gmail.comE<gt>.
345  
346 This project's source can be found on its
347 L<Codeberg page|https://codeberg.org/1-1sam/stubb.git>. Comments and pull
348 requests are welcome!
349  
350 =head1 COPYRIGHT
351  
352 Copyright (C) 2025 Samuel Young
353  
354 This program is free software: you can redistribute it and/or modify
355 it under the terms of the GNU General Public License as published by
356 the Free Software Foundation, either version 3 of the License, or
357 (at your option) any later version.
358  
359 =head1 SEE ALSO
360  
361 L<stubb>
362  
363 =cut
364  
365 # vim: expandtab shiftwidth=4
↑ Summary Stubb/Render.pm »