# Cool JavaBuild by Todd C. Gleason # Revision history: # v1.0.0 -- 03-19-2000 -- Initial version # Automatically repeats until no more files to build. # Displays count of built files # Ignores .java files that don't build corresponding .class files # Still, it takes a LONG time to run when there are a lot of .java files. # print "Cool JavaBuild v1.0.0\n"; (3 <= @ARGV) or usage(), exit; # imports use File::Find; use File::Copy; use File::Path; ($dirSrc, $dirDest, $classpath) = @ARGV; print "Building $dirSrc to $dirDest\n"; # some stats $nFilesBuilt = 0; $nFilesBuiltTotal = 0; $nFilesEmpty = 0; #nPasses = 0; # let's just run through multi-pass style do { $nFilesBuilt = 0; $nFilesNotBuilt = 0; $nFilesEmpty = 0; find(\&cool_findmatch, $dirSrc); $nFilesBuiltTotal += $nFilesBuilt; $nPasses++; print "----\nFiles Built this pass: $nFilesBuilt\n"; print "Total files built: $nFilesBuiltTotal\n"; print "Empty files: $nFilesEmpty\n"; print "Files Remaining: $nFilesNotBuilt\n"; print "Number of passes: $nPasses\n"; } while ($nFilesBuilt); exit; sub cool_findmatch { # current dir: $File::Find::dir # current file spec: $File::Find::name # current filename: $_ $currentfile = FixPathSeparators($File::Find::name); # $_; #print "Currentfile is $currentfile\n"; if (-f $currentfile) { # compile or copy file if (fileextension($currentfile) eq "java") { $bCopy = 0; # got class file? $classfile = pathnoext($currentfile) . ".class"; if (-f $classfile) { # set to copy (even if redundant, it won't take long) #$bCopy = 1; } else { # compile that sucker #print "Would try to compile $currentfile\n"; $ret = system(("javac", "-classpath", "$classpath", "$currentfile")); print "Command returned $ret\n"; if ($ret == 0) { $bCopy = ((-f $classfile) ? 1 : 0); if ($bCopy) { $nFilesBuilt++; # increment if built } else { $nFilesEmpty++; } } else { $nFilesNotBuilt++; # increment if built } } if ($bCopy) { #copy($currentfile, SourcePathToDestPath($dirSrc, $dirDest, $currentfile)); print "Copying $classfile...\n"; copy($classfile, SourcePathToDestPath($dirSrc, $dirDest, $classfile)); } } } else { EnsurePathExists(SourcePathToDestPath($dirSrc, $dirDest, $currentfile)); } } sub FixPathSeparators { $_[0] =~ s/\//\\/g ; # replace / with \ return $_[0]; } sub EnsurePathExists { $destPath = $_[0]; print "Ensuring path exists: $destPath\n"; -d $destPath or mkpath($destPath); #mkdir $destPath; } sub SourcePathToDestPath { ($pathRootSrc, $pathRootDest, $pathSrc) = @_; if (!(substr($pathRootDest, length($pathRootDest) - 1, 1) eq "\\")) { $pathRootDest = $pathRootDest . "\\"; } $pathlen = length $pathSrc; $pathlenRoot = length $pathRootSrc; $relativepath = substr $pathSrc, $pathlenRoot + 1, $pathlen - $pathlenRoot; $pathDest = $pathRootDest . $relativepath; #print "Destination path is $pathDest\n"; if ($pathDest =~ /.*\/.*/) { print "Oops!\n"; print "$pathRootSrc, $pathRootDest,\n" . "$pathSrc, $relativepath,\n" . "$pathDest\n"; exit; } return $pathDest; } sub pathnoext { $_[0] =~ /(.*)\.[A-Za-z]+/; #print "pathnoext is $1\n"; return $1; } sub fileextension { $_[0] =~ /.*\.([A-Za-z]+)/; #print "Extension is $1\n"; return $1; } sub usage { print "Args: SrcDir DestDir Classpath\n"; print "(Classpath is probably the parent dir of DestDir)\n"; }