lib/App/scriptdist.pm

filter: Statementstmt 89.2% 83 / 93 Branchbran 61.1% 11 / 18 Conditioncond 28.5% 2 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 90.0% 18 / 20 Podpod 100% 6 / 6 totaltotal 81.0% 120 / 148 CC 13 SCAR 10.5
CC
13
Cov
81%
copy
6.2
gitify
4.5
content
3.1
CRAP 2.9
↑ Summary script/scriptdist »
1 #!/usr/bin/perl
2 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package App::scriptdist;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 @EXPORT @EXPORT_OK %EXPORT_TAGS
12 %Content $VERSION
13 $Quiet
14 1 );
15  
16 1 $VERSION = '1.006';
17  
18 1 @EXPORT_OK = qw(
19 prompt find_files copy gitify content
20 script_template
21 %Content
22 );
23 1 %EXPORT_TAGS = (
24 'all' => [ @EXPORT_OK ],
25 );
26  
27 1 $Quiet = 0;
28  
29 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use ExtUtils::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use ExtUtils::Manifest;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use FindBin ();
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 =encoding utf8
40  
41 =head1 NAME
42  
43 App::scriptdist - create a distribution around a perl script
44  
45 =head1 SYNOPSIS
46  
47 use App::scriptdist qw(:all);
48  
49 =head1 DESCRIPTION
50  
51 This module provides the utility functions for the scriptdist program
52 that builds a basic Perl CPAN distribution around a standalone script
53 file that already exists.
54  
55 I do not intend this for new development and don't want to create an
56 authoring tool. You can do that with some other tool (or fork this
57 one and build your own).
58  
59 =head1 FUNCTIONS
60  
61 =over 4
62  
63 =item prompt( QUERY )
64  
65 Provide a prompt, get the response, chomp the neewline, and return
66 the answer.
67  
68 =cut
69  
70 0 ▶sub prompt {
Subroutine prompt: not called CC=1 SCAR=6.9
Pod documented
71 0 my( $query ) = shift;
72  
73 0 print $query;
74  
75 0 chomp( my $reply = <STDIN> );
76  
77 0 return $reply;
78 }
79  
80 =item find_files( DIRECTORY )
81  
82 Find all the files under a directory.
83  
84 =cut
85  
86 1 ▶sub find_files {
Subroutine find_files: called CC=1 SCAR=0.0
Pod documented
87 1 my $directory = shift;
88  
89 1 my @files = ();
90  
91 3 ▶ my $wanted = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
92 3 ▶ return unless -f $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ 1 2 3
93 2 ▶ return if $_ =~ m<(?:CVS|\.svn|\.git)>;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /(?:CVS|\.svn|\.git)/ 0 2 2
94 2 push @files, File::Spec->canonpath( $File::Find::name );
95 1 };
96  
97 1 my %options = (
98 wanted => $wanted,
99 );
100  
101 1 find( \%options, $directory );
102  
103 1 return @files;
104 }
105  
106 =item copy( INPUT_FILE, OUTPUT_FILE, CONFIG_HASH )
107  
108 Copy the file from one place to another.
109  
110 =cut
111  
112 3 ▶sub copy {
Subroutine copy: called CC=6 SCAR=18.3
Pod documented
113 3 my( $input, $output, $hash ) = @_;
114  
115 3 print STDERR "Opening input [$input] for output [$output]\n";
116  
117 3 ▶ open my $in_fh, '<', $input or die "Could not open [$input]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $in_fh, "<", $input or die "Could not open [$input]: $!\n" 3 0 3
118 3 ▶ open my $out_fh, '>', $output or warn "Could not open [$output]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $out_fh, ">", $output or warn "Could not open [$output]: $!\n" 3 0 3
119  
120 3 my $count = 0;
121  
122 3 ▶ while( readline $in_fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $in_fh)) 2 3 5
123 2 ▶ $count += s/%%SCRIPTDIST_(.*?)%%/$hash->{ lc $1 } || ''/gie;
Condition50%1/2
$$hash{lc $1} || ''
l!l
20
MC/DC0%0/1
$$hash{lc $1} || ''
$$hash{lc $1}
Truth table50%1/2
A
A= $$hash{lc $1}
Aresult
11
00
124 2 print {$out_fh} $_
125 }
126  
127 3 ▶ print STDERR "Copied [$input] with $count replacements\n" unless $Quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $App::scriptdist::Quiet 3 0 3
128 }
129  
130 =item gitify()
131  
132 Unless the environment variable C<SCRIPTDIST_SKIP_GIT> is set, init
133 a git repo, add all the files, and make the initial commit.
134  
135 =cut
136  
137 1 ▶sub gitify {
Subroutine gitify: called CC=4 SCAR=15.0
Pod documented
138 1 ▶ return if $ENV{SCRIPTDIST_SKIP_GIT};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SCRIPTDIST_SKIP_GIT"} 0 1 1
139 1 chomp( my $git = `which git` );
140 1 ▶ return unless length $git && -x $git;
Branch: 1 branch
BranchTrueFalseTotal
unless length $git and -x $git 0 1 1
Condition33%1/3
length $git and -x $git
!ll&&!rl&&r
001
MC/DC0%0/2
length $git and -x $git
length $git -x $git
Truth table33%1/3
A and B
A= length $gitB= -x $git
ABresult
0X0
100
111
141  
142 1 system $git, qw'init';
143 1 system $git, qw'add .';
144 1 system $git, qw'commit -a -m ', "Initial commit by $0 $VERSION";
145 }
146  
147 =item script_template( SCRIPT_NAME )
148  
149 Return the script template.
150  
151 =cut
152  
153 0 ▶sub script_template {
Subroutine script_template: not called CC=1 SCAR=6.9
Pod documented
154 0 my $script_name = shift;
155  
156 # Test::Pod thinks this stuff is pod if it's at the beginning
157 # of the line
158 0 my $script = <<"HERE";
159 #!/usr/bin/perl
160  
161 =head1 NAME
162  
163 $script_name - this script does something
164  
165 =head1 SYNOPSIS
166  
167 =head1 DESCRIPTION
168  
169 =head1 AUTHOR
170  
171 =head1 COPYRIGHT
172  
173 =cut
174 HERE
175  
176 0 $script =~ s/^\s+//gm;
177  
178 0 return $script;
179 }
180  
181 =item content( CONFIG_HASH )
182  
183 Return a hash reference of the contents of the files to add. The key
184 is the filename and the value is its contents.
185  
186 =cut
187  
188 1 ▶sub content {
Subroutine content: called CC=3 SCAR=11.4
Pod documented
189 1 my $hash = shift;
190  
191 1 $Content{"Changes"} =<<"CHANGES";
192 1 0.10 - @{ [ scalar localtime ] }
193 + initial distribution created with $hash->{name}
194 CHANGES
195  
196 1 $Content{"Makefile.PL"} =<<"MAKEFILE_PL";
197 use ExtUtils::MakeMaker 6.48;
198  
199 eval "use Test::Manifest 1.21";
200  
201 my \$script_name = "$$hash{script}";
202  
203 WriteMakefile(
204 'NAME' => \$script_name,
205 'VERSION' => '$$hash{version}',
206  
207 'EXE_FILES' => [ \$script_name ],
208  
209 'PREREQ_PM' => {
210 @{ [
211 join ",\n", (
212 map ( {
213 0 ▶ my $v = $_->version // 0;
Condition0%0/2
$_->version // 0
l!l
00
MC/DC0%0/1
$_->version // 0
$_->version
Truth table0%0/2
A
A= $_->version
Aresult
11
00
214 0 "\t\t\t" . $_->module . " => '$v'"
215 1 } @{$hash->{modules}} ), ''
216 ),
217 ] } },
218  
219  
220 MIN_PERL_VERSION => $$hash{minimum_perl_version},
221  
222 clean => { FILES => "*.bak \$script_name-*" },
223 );
224  
225 1;
226 MAKEFILE_PL
227  
228 1 $Content{"MANIFEST.SKIP"} =<<"MANIFEST_SKIP";
229 #!include_default
230  
231 \\.DS_Store
232 \\.releaserc
233 \\.svn
234 \\.git
235 $$hash{script}-.*
236  
237 MANIFEST_SKIP
238  
239 1 $Content{".releaserc"} =<<"RELEASE_RC";
240 1 ▶cpan_user @{[ $ENV{CPAN_USER} ? $ENV{CPAN_USER} : '' ]}
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'CPAN_USER'} ? : 0 1 1
241 RELEASE_RC
242  
243 1 $Content{".gitignore"} =<<"GITIGNORE";
244 .DS_Store
245 .lwpcookies
246 $$hash{script}-*
247 blib
248 Makefile
249 pm_to_blib
250 MYMETA\.
251 GITIGNORE
252  
253 1 $Content{"t/test_manifest"} =<<"TEST_MANIFEST";
254 compile.t
255 pod.t
256 TEST_MANIFEST
257  
258 1 $Content{"t/pod.t"} = <<"POD_T";
259 use Test::More 0.98;
260 eval "use Test::Pod 1.00";
261 plan skip_all => "Test::Pod 1.00 required for testing POD" if \$@;
262 all_pod_files_ok();
263 POD_T
264  
265 1 $Content{"t/compile.t"} = <<"COMPILE_T";
266 use Test::More 0.98;
267  
268 my \$file = "blib/script/$$hash{script}";
269  
270 BAIL_OUT( "Script file is missing!" ) unless -e \$file;
271  
272 my \$output = `$^X -c \$file 2>&1`;
273  
274 BAIL_OUT( "Script file is missing!" ) unless
275 like( \$output, qr/syntax OK\$/, 'script compiles' );
276  
277 done_testing();
278 COMPILE_T
279  
280 1 \%Content;
281 }
282  
283 =back
284  
285 =head1 TO DO
286  
287 =over 4
288  
289 =item * Copy modules into lib directory (to create module dist)
290  
291 =item * Command line switches to turn things on and off
292  
293 =back
294  
295 =head2 Maybe a good idea, maybe not
296  
297 =over 4
298  
299 =item * Add a cover.t and pod coverage test?
300  
301 =item * Interactive mode?
302  
303 =back
304  
305 =head1 SOURCE AVAILABILITY
306  
307 This source is part of a Github project.
308  
309 https://github.com/briandfoy/scriptdist
310  
311 =head1 CREDITS
312  
313 Thanks to Soren Andersen for putting this script through its paces
314 and suggesting many changes to actually make it work.
315  
316 =head1 AUTHOR
317  
318 brian d foy, C<< <briandfoy@pobox.com> >>
319  
320 =head1 COPYRIGHT
321  
322 Copyright © 2004-2025, brian d foy C<< <briandfoy@pobox.com> >>. All rights reserved.
323  
324 This code is available under the Artistic License 2.0.
325  
326 =cut
327  
328 1 1;
↑ Summary script/scriptdist »