From 678ce07d39ab9ace9df39bad4fbe0690a4e1b950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcial=20Gai=C3=9Fert?= Date: Tue, 3 Sep 2024 12:38:41 +0200 Subject: [PATCH 1/3] Also refer to main files using a relative path To do this, allow $SCRIPT_DIR in the arguments to createScript --- effekt/jvm/src/main/scala/effekt/Runner.scala | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/effekt/jvm/src/main/scala/effekt/Runner.scala b/effekt/jvm/src/main/scala/effekt/Runner.scala index b62b69ea8..f67dfd2eb 100644 --- a/effekt/jvm/src/main/scala/effekt/Runner.scala +++ b/effekt/jvm/src/main/scala/effekt/Runner.scala @@ -42,15 +42,44 @@ trait Runner[Executable] { /** * Creates a OS-specific script file that will execute the command when executed, * forwarding command line arguments. + * `$SCRIPT_DIR` refers to the directory the script is in. + * * @return the actual name of the generated script (might be `!= name`) */ def createScript(name: String, command: String*): String = os match { case OS.POSIX => - IO.createFile(name, s"#!/bin/sh\n${command.mkString(" ")} \"$$@\"", true) + val computeScriptDir = + """# Determine the directory of the script + |if [ -n "$BASH_SOURCE" ]; then + | # Bash + | SCRIPT_DIR=$(dirname "$BASH_SOURCE") + |elif [ -n "$ZSH_VERSION" ]; then + | # Zsh + | SCRIPT_DIR=$(dirname "$0") + |elif [ -n "$KSH_VERSION" ]; then + | # Ksh + | SCRIPT_DIR=$(dirname "${.sh.file}") + |else + | # POSIX shells (sh, dash, etc.) + | SCRIPT_DIR=$(dirname "$0") + |fi + |""".stripMargin + IO.createFile(name, s"#!/bin/sh\n${computeScriptDir}\n${command.mkString(" ")} \"$$@\"", true) name case OS.Windows => + val computeScriptDir = + """setlocal enabledelayedexpansion + | + |:: Get the directory of the batch file + |set "SCRIPT_DIR=%~dp0" + | + |:: Remove trailing backslash + |set "SCRIPT_DIR=%SCRIPT_DIR:~0,-1%" + |""".stripMargin val batName = name + ".bat" - IO.createFile(batName, "@echo off\r\n" + command.mkString(" ") + " %*") + // replace UNIX-style variables in command: $SCRIPT_DIR --> %SCRIPT_DIR% + val cmd = command.mkString(" ").replaceAll("\\$([A-Za-z_][A-Za-z0-9_]*)", "%$1%") + IO.createFile(batName, s"@echo off\r\n${computeScriptDir}\r\n${cmd} %*") batName } @@ -160,9 +189,10 @@ object JSNodeRunner extends Runner[String] { case OS.Windows => val jsMainFilePath = jsFilePath.stripSuffix(s".$extension") + "__main.js" + val jsMainFileName = jsFileName.stripSuffix(s".$extension") + "__main.js" val exePath = jsFilePath.stripSuffix(s".$extension") IO.createFile(jsMainFilePath, jsScript) - createScript(exePath, "node", jsMainFilePath) + createScript(exePath, "node", "$SCRIPT_DIR/" + jsMainFileName) } } object JSWebRunner extends Runner[String] { @@ -222,7 +252,8 @@ trait ChezRunner extends Runner[String] { val out = C.config.outputPath().getAbsolutePath val schemeFilePath = (out / path).canonicalPath.escape val exeScriptPath = schemeFilePath.stripSuffix(s".$extension") - createScript(exeScriptPath, "scheme", "--script", schemeFilePath) + val schemeFileName = ("./" + (path.unixPath.split('/').last)).escape + createScript(exeScriptPath, "scheme", "--script", "$SCRIPT_DIR/" + schemeFileName) } object ChezMonadicRunner extends ChezRunner { From 7842bd1238b39ff5427f866f7b51ca22d29ec3aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Bene=C5=A1?= Date: Tue, 3 Sep 2024 13:25:20 +0200 Subject: [PATCH 2/3] Try skipping the shell detection --- effekt/jvm/src/main/scala/effekt/Runner.scala | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/effekt/jvm/src/main/scala/effekt/Runner.scala b/effekt/jvm/src/main/scala/effekt/Runner.scala index f67dfd2eb..70b5efbd5 100644 --- a/effekt/jvm/src/main/scala/effekt/Runner.scala +++ b/effekt/jvm/src/main/scala/effekt/Runner.scala @@ -50,19 +50,7 @@ trait Runner[Executable] { case OS.POSIX => val computeScriptDir = """# Determine the directory of the script - |if [ -n "$BASH_SOURCE" ]; then - | # Bash - | SCRIPT_DIR=$(dirname "$BASH_SOURCE") - |elif [ -n "$ZSH_VERSION" ]; then - | # Zsh - | SCRIPT_DIR=$(dirname "$0") - |elif [ -n "$KSH_VERSION" ]; then - | # Ksh - | SCRIPT_DIR=$(dirname "${.sh.file}") - |else - | # POSIX shells (sh, dash, etc.) - | SCRIPT_DIR=$(dirname "$0") - |fi + |SCRIPT_DIR=$(dirname "$0") |""".stripMargin IO.createFile(name, s"#!/bin/sh\n${computeScriptDir}\n${command.mkString(" ")} \"$$@\"", true) name From 8aeecb60e7fe87199d6b4b4ee849f35f10859832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcial=20Gai=C3=9Fert?= Date: Tue, 3 Sep 2024 14:10:31 +0200 Subject: [PATCH 3/3] Use realpath of script path --- effekt/jvm/src/main/scala/effekt/Runner.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/effekt/jvm/src/main/scala/effekt/Runner.scala b/effekt/jvm/src/main/scala/effekt/Runner.scala index 70b5efbd5..2f6e0a1a5 100644 --- a/effekt/jvm/src/main/scala/effekt/Runner.scala +++ b/effekt/jvm/src/main/scala/effekt/Runner.scala @@ -50,7 +50,7 @@ trait Runner[Executable] { case OS.POSIX => val computeScriptDir = """# Determine the directory of the script - |SCRIPT_DIR=$(dirname "$0") + |SCRIPT_DIR=$(dirname "$(realpath "$0")") |""".stripMargin IO.createFile(name, s"#!/bin/sh\n${computeScriptDir}\n${command.mkString(" ")} \"$$@\"", true) name