Skip to content

Commit

Permalink
[LARAI] Stores absolute path of lara imports when path is available
Browse files Browse the repository at this point in the history
  • Loading branch information
joaobispo committed Aug 8, 2024
1 parent 7764917 commit bb0f624
Showing 1 changed file with 68 additions and 76 deletions.
144 changes: 68 additions & 76 deletions LARAI/src/larai/LaraI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,8 @@
*/
package larai;

import java.io.File;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import larac.LaraC;
import larac.utils.output.Output;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.lara.interpreter.Interpreter;
Expand All @@ -52,16 +40,13 @@
import org.lara.interpreter.weaver.interf.events.Stage;
import org.lara.interpreter.weaver.utils.LaraResourceProvider;
import org.lara.language.specification.dsl.LanguageSpecificationV2;
import org.suikasoft.jOptions.JOptionKeys;
import org.suikasoft.jOptions.Interfaces.DataStore;
import org.suikasoft.jOptions.JOptionKeys;
import org.suikasoft.jOptions.app.AppPersistence;
import org.suikasoft.jOptions.storedefinition.StoreDefinition;
import org.suikasoft.jOptions.storedefinition.StoreDefinitionBuilder;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;

import larac.LaraC;
import larac.utils.output.Output;
import pt.up.fe.specs.jsengine.JsEngine;
import pt.up.fe.specs.jsengine.JsEngineType;
import pt.up.fe.specs.jsengine.JsFileType;
Expand All @@ -80,6 +65,14 @@
import pt.up.fe.specs.util.utilities.SpecsThreadLocal;
import tdrc.utils.Pair;

import java.io.File;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
* An interpreter for the LARA language, which converts the Aspect-IR into a javascript representation and runs that
* script. This is used in REFLECT as an outer-loop for the project-flow and also for design-space exploration.
Expand Down Expand Up @@ -204,6 +197,7 @@ public JsEngine getScriptEngine() {
// "Could not instantiate weaver engine with class '" + weaverEngine.getClass() + "'", e);
// }
// }

/**
* Executes larai with a Weaving engine implementing {@link WeaverEngine}.
* <p>
Expand All @@ -226,7 +220,6 @@ public static boolean exec(DataStore dataStore, WeaverEngine weaverEngine) {
}



private static boolean execPrivate(DataStore dataStore, WeaverEngine weaverEngine) {

prepareDataStore(dataStore, weaverEngine);
Expand Down Expand Up @@ -318,6 +311,7 @@ private static void prepareDataStore(DataStore dataStore, WeaverEngine weaverEng
// public static boolean exec(String[] args, Class<? extends WeaverEngine> weaverEngine) {
// return exec(args, weaverEngine.newInstance());
// }

/**
* Executes larai with a Weaving engine implementing {@link WeaverEngine}. The varargs are converted into a
* DataStore
Expand Down Expand Up @@ -356,17 +350,19 @@ public static Optional<DataStore> convertArgsToDataStore(String[] args, WeaverEn
case CONFIG -> Optional.of(OptionsConverter.configFile2DataStore(weaverEngine, cmd));
// isRunningGui = false;
// get the configuration file and execute GUI
case CONFIG_GUI -> Optional.empty();
case CONFIG_GUI -> Optional.empty();
// guiFile = OptionsParser.getConfigFile(cmd);
// isRunningGui = true;
// convert options to data store and run
case OPTIONS -> Optional.of(OptionsConverter.commandLine2DataStore(args[0], cmd, weaverEngine.getOptions()));
case OPTIONS ->
Optional.of(OptionsConverter.commandLine2DataStore(args[0], cmd, weaverEngine.getOptions()));
// isRunningGui = false;
// convert configuration file to data store, override with extra options and run
case CONFIG_OPTIONS -> Optional.of(OptionsConverter.configExtraOptions2DataStore(args[0], cmd, weaverEngine));
case CONFIG_OPTIONS ->
Optional.of(OptionsConverter.configExtraOptions2DataStore(args[0], cmd, weaverEngine));
// isRunningGui = false;
// launch GUI
case GUI->Optional.empty();
case GUI -> Optional.empty();
// guiFile = null;
// isRunningGui = true;
};
Expand Down Expand Up @@ -414,43 +410,43 @@ public static LaraiResult execPrivate(String[] args, WeaverEngine weaverEngine)
SpecsLogs.debug("Launching weaver in mode " + mode);

switch (mode) {
// case UNIT_TEST:
// return weaverEngine.executeUnitTestMode(Arrays.asList(args));
case CONFIG: // convert configuration file to data store and run
// System.out.println("CONFIG ARGS:" + Arrays.toString(args));
dataStore = OptionsConverter.configFile2DataStore(weaverEngine, cmd);
success = execPrivate(dataStore, weaverEngine);
isRunningGui = false;
break;
// return execPrivate(dataStore, weaverEngine);
case CONFIG_GUI: // get the configuration file and execute GUI
File guiFile = OptionsParser.getConfigFile(cmd);
LaraLauncher.launchGUI(weaverEngine, Optional.of(guiFile));
success = true;
isRunningGui = true;
break;
case OPTIONS: // convert options to data store and run
// SpecsLogs.debug("Received args: " + Arrays.toString(args));

dataStore = OptionsConverter.commandLine2DataStore(args[0], cmd, weaverEngine.getOptions());

// return execPrivate(dataStore, weaverEngine);
success = execPrivate(dataStore, weaverEngine);
isRunningGui = false;
break;
case CONFIG_OPTIONS: // convert configuration file to data store, override with extra options and run
dataStore = OptionsConverter.configExtraOptions2DataStore(args[0], cmd, weaverEngine);
// case UNIT_TEST:
// return weaverEngine.executeUnitTestMode(Arrays.asList(args));
case CONFIG: // convert configuration file to data store and run
// System.out.println("CONFIG ARGS:" + Arrays.toString(args));
dataStore = OptionsConverter.configFile2DataStore(weaverEngine, cmd);
success = execPrivate(dataStore, weaverEngine);
isRunningGui = false;
break;
// return execPrivate(dataStore, weaverEngine);
success = execPrivate(dataStore, weaverEngine);
isRunningGui = false;
break;
case GUI:
LaraLauncher.launchGUI(weaverEngine, Optional.empty());
success = true;
isRunningGui = true;
break;
default:
throw new NotImplementedException(mode);
case CONFIG_GUI: // get the configuration file and execute GUI
File guiFile = OptionsParser.getConfigFile(cmd);
LaraLauncher.launchGUI(weaverEngine, Optional.of(guiFile));
success = true;
isRunningGui = true;
break;
case OPTIONS: // convert options to data store and run
// SpecsLogs.debug("Received args: " + Arrays.toString(args));

dataStore = OptionsConverter.commandLine2DataStore(args[0], cmd, weaverEngine.getOptions());

// return execPrivate(dataStore, weaverEngine);
success = execPrivate(dataStore, weaverEngine);
isRunningGui = false;
break;
case CONFIG_OPTIONS: // convert configuration file to data store, override with extra options and run
dataStore = OptionsConverter.configExtraOptions2DataStore(args[0], cmd, weaverEngine);
// return execPrivate(dataStore, weaverEngine);
success = execPrivate(dataStore, weaverEngine);
isRunningGui = false;
break;
case GUI:
LaraLauncher.launchGUI(weaverEngine, Optional.empty());
success = true;
isRunningGui = true;
break;
default:
throw new NotImplementedException(mode);
}

return LaraiResult.newInstance(success, isRunningGui);
Expand Down Expand Up @@ -574,8 +570,8 @@ private void startAspectIR() throws DOMException, Exception {
* @throws Exception
*/
public Pair<Document, LaraC> compileWithLARAC(File fileName, LanguageSpecificationV2 langSpec,
LaraIDataStore options,
Output out) throws Exception {
LaraIDataStore options,
Output out) throws Exception {

// Process Lara Bundles in include folders
// includesFolder = processLaraBundles(includesFolder);
Expand Down Expand Up @@ -708,7 +704,6 @@ private void interpret(WeaverEngine weaverEngine) throws Exception {
// final ImporterTopLevel scope = new ImporterTopLevel(cx);



// final FileList folderApplication = options.getWorkingDir();

// if (!folderApplication.exists()) {
Expand Down Expand Up @@ -779,7 +774,7 @@ private void interpret(WeaverEngine weaverEngine) throws Exception {
main = asps.main;
}

weaver.eventTrigger().triggerWeaver(Stage.END, getWeaverArgs(), main,
weaver.eventTrigger().triggerWeaver(Stage.END, getWeaverArgs(), main,
options.getLaraFile().getPath());
finish(engine);
} catch (Exception e) {
Expand Down Expand Up @@ -857,16 +852,14 @@ public DataStore getWeaverArgs() {
}

/**
* @param js
* the js to set
* @param js the js to set
*/
public void setJs(StringBuilder js) {
this.js = js;
}

/**
* @param js
* the js to append
* @param js the js to append
*/
public void appendJs(StringBuilder js) {
this.js.append(js);
Expand Down Expand Up @@ -904,8 +897,7 @@ public MasterWeaver getWeaver() {
}

/**
* @param weaver
* the weaver to set
* @param weaver the weaver to set
*/
public void setWeaver(MasterWeaver weaver) {
this.weaver = weaver;
Expand All @@ -919,8 +911,7 @@ public LaraIDataStore getOptions() {
}

/**
* @param laraIDataStore
* the options to set
* @param laraIDataStore the options to set
*/
public void setOptions(LaraIDataStore laraIDataStore) {
options = laraIDataStore;
Expand Down Expand Up @@ -954,8 +945,8 @@ public static boolean exec(String args[]) {
}

/**
* @deprecated Check if this method can be replaced with getWeaverEngine()
* @return
* @deprecated Check if this method can be replaced with getWeaverEngine()
*/
@Deprecated
public WeaverEngine getEngine() {
Expand Down Expand Up @@ -1009,10 +1000,10 @@ public static AspectClassProcessor buildAspectProcessor(WeaverEngine weaver, JsE

/**
* Loads a LARA import, using the same format as the imports in LARA files (e.g. weaver.Query).
*
*
* <p>
* Does not verify if import has already been imported.
*
*
* @param importName
*/
public static void loadLaraImport(String importName) {
Expand All @@ -1032,9 +1023,10 @@ public static void loadLaraImport(String importName) {
SpecsLogs.debug(
() -> "Loading LARA Import '" + laraImport.getFilename() + "' as " + laraImport.getFileType());

var source = laraImport.getJsFile().map(file -> SpecsIo.normalizePath(file.getAbsolutePath())).orElse(laraImport.getFilename());

weaverEngine.getScriptEngine().eval(laraImport.getCode(), laraImport.getFileType(),
laraImport.getFilename() + " (LARA import '" + importName + "' as "
+ laraImport.getFileType().toString() + ")");
source);
}

}
Expand Down

0 comments on commit bb0f624

Please sign in to comment.