Support Fiji update processing

This commit is contained in:
Jarek Sacha 2023-05-22 18:05:20 -04:00
parent 30d41e3a40
commit 876e62bcd4
No known key found for this signature in database
GPG key ID: F29625CE62288163
5 changed files with 76 additions and 11 deletions

View file

@ -17,7 +17,7 @@ class Launcher(logger: Logger):
private val jarsDirName = "jars"
def run(config: Config): Unit =
createCommandLine(config) match
prepareLaunch(config) match
case Right(commandLine) =>
if config.dryRun then
logger.debug("dry-run")
@ -27,9 +27,10 @@ class Launcher(logger: Logger):
case Left(errorMessage) =>
logger.error(errorMessage)
private def createCommandLine(config: Config): Either[String, Seq[String]] =
private def prepareLaunch(config: Config): Either[String, Seq[String]] =
for
ijDir <- locateIJDir(config)
_ <- Updater.update(Path(ijDir), config.dryRun, logger)
launcherJar <- findImageJLauncherJar(ijDir)
javaExe <- locateJavaExecutable(config, ijDir)
systemType <- determineSystemType()

View file

@ -58,10 +58,6 @@ object Main:
.action((path, c) => c.copy(javaHome = Option(path)))
.text("specify JAVA_HOME explicitly"),
//
opt[Unit]("print-java-home")
.action((_, c) => c.copy(dryRun = true))
.text("print ImageJ's idea of JAVA_HOME"),
//
opt[File]("ij-dir")
.valueName("<path>")
.action((path, c) => c.copy(ijDir = Option(path)))

View file

@ -0,0 +1,60 @@
package ij_plugins.imagej_launcher
import os.Path
import scala.util.control.NonFatal
object Updater:
/**
* Perform update using files in `update` subdirectory, if it exists.
* @param ijDir imagej
* @param dryRun locate files to process but do not make any changes, but count the files that would be processed
* @param logger configured logger
* @return Number of files processed or an error message.
*/
def update(ijDir: Path, dryRun: Boolean, logger: Logger): Either[String, Long] =
try
val updateDir = ijDir / "update"
// Count used only for debug info
var count = 0
if os.exists(updateDir) then
logger.info("Performing update..")
os.walk(updateDir)
.filter(os.isFile)
.foreach { src =>
val dst = ijDir / src.relativeTo(updateDir)
if os.size(src) == 0 then
logger.debug(s"remove: $dst")
if !dryRun then os.remove(dst)
logger.debug(s"remove: $src")
if !dryRun then os.remove(src)
else
logger.debug(s"move : $src -> $dst")
if !dryRun then os.move(src, dst, replaceExisting = true, createFolders = true)
count += 1
}
logger.debug(s"Delete update directory: $updateDir")
if !dryRun then deleteEmptyDirs(updateDir, logger)
Right(count)
else
logger.info("No update found")
Right(count)
catch
case NonFatal(ex) =>
ex.printStackTrace()
Left(s"Failed to perform update: ${ex.getMessage} - ${ex.getClass.getSimpleName}")
private def deleteEmptyDirs(dir: Path, logger: Logger): Unit =
logger.debug(s"Cleaning directory: $dir")
os.list(dir)
.filter(os.isDir)
.foreach(p => deleteEmptyDirs(p, logger))
if os.list(dir).isEmpty then
logger.debug(s"Removing empty dir: $dir")
os.remove(dir)
else
logger.debug(s"Cannot delete directory, not empty: $dir")
end Updater

View file

@ -0,0 +1,9 @@
package ij_plugins.imagej_launcher
import os.Path
@main
def updaterDemo(ijDir: String): Unit =
Updater.update(Path(ijDir), dryRun = false, new Logger(Logger.Level.Debug)) match
case Right(count) => println(s"Processed $count files")
case Left(error) => println(s"Failed with error: $error")