Skip to content

Commit

Permalink
Also refer to main files and chez using a relative path (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
marzipankaiser authored Sep 3, 2024
2 parents 06dc86e + 8aeecb6 commit 458fc9c
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions effekt/jvm/src/main/scala/effekt/Runner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,32 @@ 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
|SCRIPT_DIR=$(dirname "$(realpath "$0")")
|""".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
}

Expand Down Expand Up @@ -160,9 +177,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] {
Expand Down Expand Up @@ -222,7 +240,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 {
Expand Down

0 comments on commit 458fc9c

Please sign in to comment.