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