Stubb.pm

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