From 35d6bef0e0c63a1cbf5953fe6a1a192f57e5bdc1 Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Tue, 30 Aug 2022 13:03:08 +0300 Subject: [PATCH] Set __SCRIPT_PATH__ and __SCRIPT_DIR__ variables in SCL runFromFile Scripts executed can now read `__SCRIPT_PATH__ :: Maybe Path` and `__SCRIPT_DIR__ :: Maybe Path` to perform file system resolution logic based on the ran script's file system location. gitlab #863 --- .../scl/compiler/commands/CommandSession.java | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java index d0e44a9c7..af4a04ee2 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java @@ -3,11 +3,15 @@ package org.simantics.scl.compiler.commands; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.IOError; import java.io.IOException; import java.io.PrintStream; import java.io.Reader; import java.io.StringReader; import java.io.UnsupportedEncodingException; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -57,6 +61,8 @@ import org.simantics.scl.runtime.reporting.DelegatingSCLReportingHandler; import org.simantics.scl.runtime.reporting.SCLReporting; import org.simantics.scl.runtime.reporting.SCLReportingHandler; import org.simantics.scl.runtime.tuple.Tuple0; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import gnu.trove.map.hash.THashMap; import gnu.trove.procedure.TObjectProcedure; @@ -65,6 +71,8 @@ import gnu.trove.set.hash.THashSet; public class CommandSession { + private static final Logger LOGGER = LoggerFactory.getLogger(CommandSession.class); + ModuleRepository moduleRepository; SCLReportingHandler defaultHandler; @@ -647,13 +655,37 @@ public class CommandSession { updateRuntimeEnvironment(true); } + public static final Type MAYBE_PATH = Types.apply(Types.MAYBE, Types.con("Files", "Path")); + + private static final String VAR_SCRIPT_DIR = "__SCRIPT_DIR__"; + private static final String VAR_SCRIPT_PATH = "__SCRIPT_PATH__"; + public void runFromFile(String fileName, SCLReportingHandler handler) { - try { - Reader reader = new LaxUTF8Reader(fileName); + try (Reader reader = new LaxUTF8Reader(fileName)) { + Type prevPathType = getVariableType(VAR_SCRIPT_PATH); + Object prevPathValue = getVariableValue(VAR_SCRIPT_PATH); + Type prevDirType = getVariableType(VAR_SCRIPT_DIR); + Object prevDirValue = getVariableValue(VAR_SCRIPT_DIR); try { + Path absPath = Paths.get(fileName).toAbsolutePath(); + setVariable(VAR_SCRIPT_PATH, MAYBE_PATH, absPath); + setVariable(VAR_SCRIPT_DIR, MAYBE_PATH, absPath != null ? absPath.getParent() : null); + execute(reader, handler); + } catch (InvalidPathException | IOError e) { + LOGGER.error("Failed to resolve absolute script path from provided fileName \"{}\"", fileName, e); + // Just evaluate the script without script paths + removeVariable(VAR_SCRIPT_PATH); + removeVariable(VAR_SCRIPT_DIR); execute(reader, handler); } finally { - reader.close(); + if (prevPathValue != null && prevPathType != null) + setVariable(VAR_SCRIPT_PATH, prevPathType, prevPathValue); + else + removeVariable(VAR_SCRIPT_PATH); + if (prevDirValue != null && prevDirType != null) + setVariable(VAR_SCRIPT_DIR, prevDirType, prevDirValue); + else + removeVariable(VAR_SCRIPT_DIR); } } catch(IOException e) { formatException(handler, e); -- 2.47.1