lib/MakeWithPerl.pm

filter: Statementstmt 18.9% 33 / 174 Branchbran 3.2% 3 / 92 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 6 totaltotal 14.1% 42 / 296 CC 56 SCAR 72.4
CC
56
Cov
13%
makeWithPerl
1695.8
test
17.1
removeClasses
6.0
CRAP 1391.8
↑ Summary
1 #!/usr/bin/perl -I/home/phil/perl/cpan/DataTableText/lib -I/home/phil/perl/cpan/JavaDoc/lib -I/home/phil/perl/cpan/DitaPCD/lib/ -I/home/phil/perl/cpan/DataEditXml/lib/ -I/home/phil/perl/cpan/GitHubCrud/lib/ -I/home/phil/perl/cpan/DataDFA/lib/ -I/home/phil/perl/cpan/DataNFA/lib/ -I//home/phil/perl/cpan/PreprocessOps/lib/
2 #-------------------------------------------------------------------------------
3 # Make with Perl
4 # Philip R Brenan at gmail dot com, Appa Apps Ltd, 2017
5 #-------------------------------------------------------------------------------
6 package MakeWithPerl;
7 our $VERSION = "20210601";
8 use warnings FATAL => qw(all);
9 use strict;
10 use Carp qw(confess);
11 use Data::Dump qw(dump);
12 use Data::Table::Text qw(:all);
13 use Getopt::Long;
14 use utf8;
15  
16 1 ▶sub mwpl {qq(makeWithPerlLocally.pl)} # Make with Perl locally
Subroutine mwpl: called CC=1 SCAR=0.0
Pod undocumented
17  
18 my $javaHome; # Location of java files
19 my $cIncludes; # C includes folder
20 my $compile; # Compile
21 my $coverage; # Get coverage of code
22 my $doc; # Documentation
23 my $gccVersion; # Alternate version of gcc is set. Example: --gccVersion gcc-10
24 my $htmlToPdf; # Convert html to pdf
25 my $run; # Run
26 my $search; # Search for a local file to make the specified file
27 my $upload; # Upload files
28 my $valgrind; # Check C memory usage
29 my $xmlCatalog; # Verify xml
30  
31 sub makeWithPerl { # Make a file
32 1 ▶GetOptions(
Subroutine makeWithPerl: called CC=50 SCAR=74.4
Pod undocumented
33 'cIncludes=s' =>\$cIncludes,
34 'compile' =>\$compile,
35 'coverage' =>\$coverage,
36 'doc' =>\$doc,
37 'gccVersion=s'=>\$gccVersion,
38 'htmlToPdf' =>\$htmlToPdf,
39 'javaHome=s' =>\$javaHome,
40 'run' =>\$run,
41 'search!' =>\$search,
42 'valgrind' =>\$valgrind,
43 'upload' =>\$upload,
44 'xmlCatalog=s'=>\$xmlCatalog,
45 );
46  
47 1 ▶my $file = shift @ARGV // $0; # File to process
Condition0%0/3
shift @ARGV // $0
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @ARGV // $0
shift @ARGV $0
Truth table0%0/3
A // B
A= shift @ARGVB= $0
ABresult
1X1
011
000
48  
49 1 ▶unless($file) # Confirm we have a file
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 0 0
50 1 {confess "Use %f to specify the file to process";
51 }
52  
53 1 ▶if (! -e $file) # No such file
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 0 0
54 1 {confess "No such file:\n$file"
55 }
56  
57 1 ▶if ($search) # Upload files to GitHub or run some other action defined in the containing folder hierarchy unless search is forbidden
Branch: 1 branch
BranchTrueFalseTotal
if ($search) 0 0 0
58 1 {my @d = split m{/}, $file; # Split file name
59 1 pop @d;
60 1 ▶ while(@d) # Look for a folder that contains a push command
Branch: 1 branch
BranchTrueFalseTotal
if (@d) 0 0 0
61 1 {for my $n(qw(pushToGitHub upload package))
62 1 {my $u = "/".fpe(@d, $n, q(pl));
63 1 ▶ if (-e $u)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $u) 0 0 0
64 1 {say STDERR $u;
65 1 qx(perl $u);
66 1 exit;
67 }
68 }
69 1 pop @d;
70 }
71 1 confess "Unable to find pushToGitHub in folders down to $file";
72 }
73  
74 1 ▶if ($doc) # Documentation
Branch: 1 branch
BranchTrueFalseTotal
if ($doc) 0 0 0
75 1 ▶ {if ($file =~ m((pl|pm)\Z)s) # Document perl
Branch: 2 branches
BranchTrueFalseTotal
if ($file =~ /(pl|pm)\Z/s) { } 0 0 0
elsif ($file =~ /(java)\Z/s) { } 0 0 0
76 1 {say STDERR "Document perl $file";
77 1 updatePerlModuleDocumentation($file);
78 }
79 elsif ($file =~ m((java)\Z)s) # Document java
80 1 {say STDERR "Document java $file";
81  
82 1 my %files;
83 1 for(findFiles($javaHome))
84 1 ▶ {next if m/Test\.java\Z/ or m(/java/z/); # Exclude test files and /java/ sub folders
Branch: 1 branch
BranchTrueFalseTotal
if /Test\.java\Z/ or m[/java/z/] 0 0 0
Condition0%0/3
/Test\.java\Z/ or m[/java/z/]
l!l&&r!l&&!r
000
MC/DC0%0/2
/Test\.java\Z/ or m[/java/z/]
/Test\.java\Z/ m[/java/z/]
Truth table0%0/3
A or B
A= /Test\.java\Z/B= m[/java/z/]
ABresult
1X1
011
000
85 1 ▶ $files{$_}++ if /\.java\Z/
Branch: 1 branch
BranchTrueFalseTotal
if /\.java\Z/ 0 0 0
86 }
87 1 confess;
88 #my $j = Java::Doc::new;
89 #$j->source = [sort keys %files];
90 #$j->target = my $f = filePathExt($javaHome, qw(documentation html));
91 #$j->indent = 20;
92 #$j->colors = [map {"#$_"} qw(ccFFFF FFccFF FFFFcc CCccFF FFCCcc ccFFCC)];
93 #$j->html;
94 #qx(opera $f);
95 }
96 else
97 1 {confess "Unable to document file $file";
98 }
99 exit
100 1 }
101  
102 1 ▶if (-e mwpl and $run) # Make with Perl locally
Branch: 1 branch
BranchTrueFalseTotal
if (-e mwpl() and $run) 0 0 0
Condition0%0/3
-e mwpl() and $run
!ll&&!rl&&r
000
MC/DC0%0/2
-e mwpl() and $run
-e mwpl() $run
Truth table0%0/3
A and B
A= -e mwpl()B= $run
ABresult
0X0
100
111
103 1 {my $p = join ' ', @ARGV;
104 0 my $c = mwpl;
105 0 print STDERR qx(perl -CSDA $c $p);
106 0 exit;
107 }
108  
109 0 ▶if ($file =~ m(\.p[lm]\Z)) # Perl
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.p[lm]\Z/) 0 0 0
110 0 ▶ {if ($compile) # Syntax check perl
Branch: 3 branches
BranchTrueFalseTotal
if ($compile) { } 0 0 0
elsif ($run) { } 0 0 0
elsif ($doc) { } 0 0 0
111 0 {print STDERR qx(perl -CSDA -cw "$file");
112 }
113 elsif ($run) # Run perl
114 0 ▶ {if ($file =~ m(.cgi\Z)s) # Run from web server
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /.cgi\Z/s) { } 0 0 0
115 0 {&cgiPerl($file);
116 }
117 else # Run from command line
118 0 {say STDERR qq(perl -CSDA -w "$file");
119 0 print STDERR qx(perl -CSDA -w "$file");
120 }
121 }
122 elsif ($doc) # Document perl
123 0 {say STDERR "Document perl $file";
124 0 updatePerlModuleDocumentation($file);
125 }
126 0 exit;
127 }
128  
129 0 ▶if ($file =~ m(\.(txt|htm)\Z)) # Html
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.(txt|htm)\Z/) 0 0 0
130 0 {my $s = expandWellKnownUrlsInHtmlFormat
131 expandWellKnownWordsAsUrlsInHtmlFormat
132 readFile $file;
133 0 my $o = setFileExtension $file, q(html); # Output file
134 0 my $f = owf $o, $s;
135  
136 0 ▶ if ($htmlToPdf) # Convert html to pdf if requested
Branch: 1 branch
BranchTrueFalseTotal
if ($htmlToPdf) { } 0 0 0
137 0 {my $p = setFileExtension($file, q(pdf));
138 0 say STDERR qx(wkhtmltopdf $f $p);
139 }
140 else # Show html in opera
141 0 {my $c = qq(timeout 3m opera $o);
142 0 say STDERR qq($c);
143 0 say STDERR qx($c);
144 }
145 0 exit;
146 }
147  
148 0 ▶if ($file =~ m(\.(dita|ditamap|xml)\Z)) # Process xml
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.(dita|ditamap|xml)\Z/) 0 0 0
149 0 {my $source = readFile($file);
150 0 my $C = $xmlCatalog;
151 0 my $c = qq(xmllint --noent --noout "$file" && echo "Parses OK!" && export XML_CATALOG_FILES=$C && xmllint --noent --noout --valid - < "$file" && echo Valid);
152 0 say STDERR $c;
153 0 say STDERR qx($c);
154 0 exit;
155 }
156  
157 0 ▶if ($file =~ m(\.asm\Z)) # Process assembler
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.asm\Z/) 0 0 0
158 0 {my $o = setFileExtension $file, q(o);
159 0 my $e = setFileExtension $file;
160 0 my $l = setFileExtension $file, q(txt);
161 0 my $c = qq(nasm -f elf64 -g -l $l -o $o $file);
162 0 ▶ if ($compile)
Branch: 1 branch
BranchTrueFalseTotal
if ($compile) { } 0 0 0
163 0 {say STDERR $c;
164 0 say STDERR qx($c; cat $l);
165 }
166 else
167 0 {$c = "$c; ld -o $e $o; $e";
168 0 say STDERR $c;
169 0 say STDERR qx($c);
170 }
171 0 exit;
172 }
173  
174 0 ▶if ($file =~ m(\.cp*\Z)) # GCC
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.cp*\Z/) 0 0 0
175 0 {my $cp = join ' ', map {split /\s+/} grep {!/\A#/} split /\n/, <<END; # Compiler options
176 -finput-charset=UTF-8 -fmax-errors=7 -rdynamic
177 -Wall -Wextra -Wno-unused-function
178 END
179  
180 0 ▶ my $gcc = $gccVersion // 'gcc'; # Gcc version 10
Condition0%0/2
$gccVersion // "gcc"
l!l
00
MC/DC0%0/1
$gccVersion // "gcc"
$gccVersion
Truth table0%0/2
A
A= $gccVersion
Aresult
11
00
181 0 ▶ if ($compile)
Branch: 1 branch
BranchTrueFalseTotal
if ($compile) { } 0 0 0
182 0 {my $cmd = qq($gcc $cp -c "$file" -o /dev/null); # Syntax check
183 0 say STDERR $cmd;
184 0 print STDERR $_ for qx($cmd);
185 }
186 else
187 0 {my $e = $file =~ s(\.cp?p?\Z) ()gsr; # Execute
188 0 my $o = fpe($e, q(o)); # Object file
189 0 unlink $e, $o;
190  
191 0 my $c = qq($gcc $cp -o "$e" "$file" && $e); # Compile and run
192 0 lll qq($c);
193 0 lll qx($c);
194 0 unlink $o;
195  
196 0 ▶ if ($valgrind) # Valgrind requested
Branch: 1 branch
BranchTrueFalseTotal
if ($valgrind) 0 0 0
197 0 {my $c = qq(valgrind --leak-check=full --leak-resolution=high --show-leak-kinds=definite --track-origins=yes $e 2>&1);
198 0 lll qq($c);
199 0 my $result = qx($c);
200 0 lll $result;
201 0 ▶ exit(1) unless $result =~ m(ERROR SUMMARY: 0 errors from 0 contexts);
Branch: 1 branch
BranchTrueFalseTotal
unless $result =~ /ERROR SUMMARY: 0 errors from 0 contexts/ 0 0 0
202 0 lll "SUCCESS: no memory leaks"
203 }
204 }
205 0 exit;
206 }
207  
208 0 ▶if ($file =~ m(\.js\Z)) # Javascript
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.js\Z/) 0 0 0
209 0 ▶ {if ($compile)
Branch: 1 branch
BranchTrueFalseTotal
if ($compile) { } 0 0 0
210 0 {say STDERR "Compile javascript $file";
211 0 print STDERR qx(nodejs -c "$file"); # Syntax check javascript
212 }
213 else
214 0 {my $c = qq(nodejs --max_old_space_size=4096 "$file"); # Run javascript
215 0 say STDERR $c;
216 0 print STDERR qx($c);
217 0 say STDERR q();
218 }
219 0 exit;
220 }
221  
222 0 ▶if ($file =~ m(\.sh\Z)) # Bash script
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.sh\Z/) 0 0 0
223 0 ▶ {if ($compile)
Branch: 1 branch
BranchTrueFalseTotal
if ($compile) { } 0 0 0
224 0 {say STDERR "Test bash $file";
225 0 print STDERR qx(bash -x "$file"); # Debug bash
226 }
227 else
228 0 {print STDERR qx(bash "$file"); # Bash
229 }
230 0 exit;
231 }
232  
233 0 ▶if ($file =~ m(\.adblog\Z)) # Android log
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.adblog\Z/) 0 0 0
234 0 {my $adb = q(/home/phil/android/sdk/platform-tools/adb);
235 0 my $c = qq($adb -e logcat "*:W" -d > $file && $adb -e logcat -c);
236 0 say STDERR "Android log\n$c";
237 0 print STDERR qx($c);
238 0 exit;
239 }
240  
241 0 ▶if ($file =~ m(\.java\Z)) # Java
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.java\Z/) 0 0 0
242 0 {my $name = fn $file; # Parse file name
243 0 ▶ !$javaHome and confess "Specify --javaHome keyword to specify the folder where class files are to go.";
Branch: 1 branch
BranchTrueFalseTotal
$javaHome or confess("Specify --javaHome keyword to specify the folder where class files are to go.") 0 0 0
244 0 my $package = &getPackageNameFromFile($file); # Get package name
245 0 my $cp = fpd($javaHome, qw(Classes)); # Folder containing java classes
246 0 ▶ if ($compile) # Compile
Branch: 1 branch
BranchTrueFalseTotal
if ($compile) { } 0 0 0
247 0 {my $c = "javac -g -d $cp -cp $cp -Xlint -Xdiags:verbose $file -Xmaxerrs 99";# Syntax check Java
248 0 say STDERR $c;
249 0 print STDERR qx($c);
250 }
251 else # Compile and run
252 0 ▶ {my $class = $package ? "$package.$name" : $name; # Class location
Branch: 1 branch
BranchTrueFalseTotal
$package ? : 0 0 0
253 0 my $p = join ' ', @ARGV; # Collect the remaining parameters and pass them to the java application
254 0 my $c = "javac -g -d $cp -cp $cp $file && java -ea -cp $cp $class $p"; # Run java
255 0 say STDERR $c;
256 0 print STDERR qx($c);
257 }
258 0 &removeClasses;
259 0 exit;
260 }
261  
262 0 ▶if ($file =~ m(\.py\Z)) # Python
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.py\Z/) 0 0 0
263 0 ▶ {if ($compile) # Syntax check
Branch: 3 branches
BranchTrueFalseTotal
if ($compile) { } 0 0 0
elsif ($run) { } 0 0 0
elsif ($doc) { } 0 0 0
264 0 {print STDERR qx(python3 -m py_compile "$file");
265 }
266 elsif ($run) # Run
267 0 {print STDERR qx(python3 "$file");
268 }
269 elsif ($doc) # Document
270 0 {say STDERR "Document perl $file";
271 0 updatePerlModuleDocumentation($file);
272 }
273 0 exit;
274 }
275  
276 0 ▶if ($file =~ m(\.(vala)\Z)) # Vala
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.(vala)\Z/) 0 0 0
277 0 {my $lib = "--pkg gtk+-3.0"; # Libraries
278 0 ▶ if ($compile) # Syntax check
Branch: 3 branches
BranchTrueFalseTotal
if ($compile) { } 0 0 0
elsif ($run) { } 0 0 0
elsif ($doc) { } 0 0 0
279 0 {print STDERR qx(valac -c "$file" $lib);
280 }
281 elsif ($run) # Run
282 0 {print STDERR qx(vala "$file" $lib);
283 }
284 elsif ($doc) # Document
285 0 {say STDERR "Document perl $file";
286 0 updatePerlModuleDocumentation($file);
287 }
288 0 exit;
289 }
290  
291 sub removeClasses
292 0 ▶ {unlink for fileList("*.class")
Subroutine removeClasses: called CC=2 SCAR=17.9
Pod undocumented
293 }
294  
295 sub getPackageNameFromFile($) # Get package name from java file
296 0 ▶ {my ($file) = @_; # File to read
Subroutine getPackageNameFromFile: called CC=1 SCAR=6.9
Pod undocumented
297 0 my $s = readFile($file);
298 0 my ($p) = $s =~ m/package\s+(\S+)\s*;/;
299 0 $p
300 }
301  
302 sub cgiPerl($) # Run perl on web server
303 0 ▶ {my ($file) = @_; # File to read
Subroutine cgiPerl: called CC=2 SCAR=17.9
Pod undocumented
304  
305 0 my $r = qx(perl -CSDA -cw "$file" 2>&1);
306 0 ▶ if ($r !~ m(syntax OK))
Branch: 1 branch
BranchTrueFalseTotal
if (not $r =~ /syntax OK/) { } 0 0 0
307 0 {say STDERR $r;
308 }
309 else
310 0 {my $base = fne $file;
311 0 my $target = fpf(q(/usr/lib/cgi-bin), $base);
312 0 lll qx(echo 121212 | sudo -S cp $file $target);
313 0 lll qx(echo 121212 | sudo chmod ugo+rx $target);
314 0 lll qx(opera http://localhost/cgi-bin/$base &);
315 }
316 }
317 }
318  
319 #d
320 #-------------------------------------------------------------------------------
321 # Export - eeee
322 #-------------------------------------------------------------------------------
323  
324 use Exporter qw(import);
325  
326 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
327  
328 @ISA = qw(Exporter);
329 @EXPORT = qw();
330 @EXPORT_OK = qw(
331 );
332 %EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]);
333  
334 # podDocumentation
335 =pod
336  
337 =encoding utf-8
338  
339 =head1 Name
340  
341 MakeWithPerl - Make with Perl
342  
343 =head1 Synopsis
344  
345 Integrated development environment for Geany or similar editor for compiling
346 running and documenting programs written in a number of languages.
347  
348 =head2 Installation:
349  
350 sudo cpan install MakeWithPerl
351  
352 =head2 Operation
353  
354 Configure Geany as described at
355 L<README.md|https://github.com/philiprbrenan/MakeWithPerl>.
356  
357 =head1 Description
358  
359 Make with Perl
360  
361  
362 Version "20210533".
363  
364  
365 The following sections describe the methods in each functional area of this
366 module. For an alphabetic listing of all methods by name see L<Index|/Index>.
367  
368  
369  
370  
371 =head1 Index
372  
373  
374 =head1 Installation
375  
376 This module is written in 100% Pure Perl and, thus, it is easy to read,
377 comprehend, use, modify and install via B<cpan>:
378  
379 sudo cpan install MakeWithPerl
380  
381 =head1 Author
382  
383 L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com>
384  
385 L<http://www.appaapps.com|http://www.appaapps.com>
386  
387 =head1 Copyright
388  
389 Copyright (c) 2016-2021 Philip R Brenan.
390  
391 This module is free software. It may be used, redistributed and/or modified
392 under the same terms as Perl itself.
393  
394 =cut
395  
396  
397  
398 # Tests and documentation
399  
400 sub test
401 0 ▶ {my $p = __PACKAGE__;
Subroutine test: called CC=5 SCAR=28.4
Pod undocumented
402 0 binmode($_, ":utf8") for *STDOUT, *STDERR;
403 0 ▶ return if eval "eof(${p}::DATA)";
Branch: 1 branch
BranchTrueFalseTotal
if eval "eof(${p}::DATA)" 0 1 1
404 0 my $s = eval "join('', <${p}::DATA>)";
405 0 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
406 0 eval $s;
407 0 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
408 0 1
409 }
410  
411 test unless caller;
412  
413 1;
414 # podDocumentation
415 __DATA__
↑ Summary