]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Set __SCRIPT_PATH__ and __SCRIPT_DIR__ variables in SCL runFromFile
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 30 Aug 2022 10:03:08 +0000 (13:03 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 30 Aug 2022 10:03:08 +0000 (13:03 +0300)
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

bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java

index d0e44a9c7b263a36c0a75b60cc5f678432081165..af4a04ee2e4c0d4cf2c592d9f34815564b2989d9 100644 (file)
@@ -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);