From bce48e8e4a5a69f6bda7458ad02775cd76124644 Mon Sep 17 00:00:00 2001 From: Karol Stasiak Date: Fri, 31 Jul 2020 13:26:44 +0200 Subject: [PATCH] Support BAT files for the `-r` options --- docs/api/command-line.md | 5 ++++- src/main/scala/millfork/Main.scala | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/api/command-line.md b/docs/api/command-line.md index e7e69149..1fde1c8f 100644 --- a/docs/api/command-line.md +++ b/docs/api/command-line.md @@ -66,7 +66,10 @@ Unlike `-I`, this does not replace the default include directory and allows usin * `-t ` – Target platform. It is loaded from an `.ini` file found in any of the include directories. See also [this document](target-platforms.md). -* `-r ` – Run given program after successful compilation. Useful for automatically launching emulators without any external scripting. +* `-r ` – Run given program after successful compilation. +Useful for automatically launching emulators without any external scripting. +The program is run with the working directory set to its own directory, +and it's passed the full path to the output file as its argument. * `-R ` – Adds a parameter to the command line of the program run with `-r`. All `-R` options are added in order, before the output file name. diff --git a/src/main/scala/millfork/Main.scala b/src/main/scala/millfork/Main.scala index c5ec7ac4..3adccfc1 100644 --- a/src/main/scala/millfork/Main.scala +++ b/src/main/scala/millfork/Main.scala @@ -167,8 +167,16 @@ object Main { } errorReporting.debug(s"Total time: ${Math.round((System.nanoTime() - startTime)/1e6)} ms") c.runFileName.foreach{ program => + if (!new File(program).exists()) { + errorReporting.error(s"Program $program does not exist") + } val outputAbsolutePath = Paths.get(defaultPrgOutput).toAbsolutePath.toString - val cmdline = program +: c.runParams :+ outputAbsolutePath + val isBatch = File.separatorChar == '\\' && program.toLowerCase(Locale.ROOT).endsWith(".bat") + val cmdline = if (isBatch) { + List("cmd", "/c", program) ++ c.runParams :+ outputAbsolutePath + } else { + program +: c.runParams :+ outputAbsolutePath + } errorReporting.debug(s"Running: ${cmdline.mkString(" ")}") new ProcessBuilder(cmdline.toArray: _*).directory(new File(program).getParentFile).start() }