From 9c7a45d78da0d2b99f3a365c39dfc3f2e7221e24 Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Mon, 12 Nov 2018 13:38:41 +0200 Subject: [PATCH] Added -f SCL/Module/function argument for BaselineCreatorApplication This allows the baseline creator to run a custom SCL initialization function that is included in the currently running platform after the platform has been started up and before the platform shut down. gitlab #193 Change-Id: I68565cf638365aaf816845282e3c7a24eb45dd31 --- .../simantics/BaselineCreatorApplication.java | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/bundles/org.simantics/src/org/simantics/BaselineCreatorApplication.java b/bundles/org.simantics/src/org/simantics/BaselineCreatorApplication.java index a3b409027..f186467e4 100644 --- a/bundles/org.simantics/src/org/simantics/BaselineCreatorApplication.java +++ b/bundles/org.simantics/src/org/simantics/BaselineCreatorApplication.java @@ -19,11 +19,14 @@ import org.eclipse.equinox.app.IApplicationContext; import org.eclipse.osgi.service.datalocation.Location; import org.simantics.application.arguments.Arguments; import org.simantics.application.arguments.IArgumentFactory; -import org.simantics.application.arguments.IArgumentFactory.StringArgumentFactory; import org.simantics.application.arguments.IArgumentFactory.NoValueArgumentFactory; +import org.simantics.application.arguments.IArgumentFactory.StringArgumentFactory; import org.simantics.application.arguments.IArguments; import org.simantics.application.arguments.SimanticsArguments; import org.simantics.internal.Activator; +import org.simantics.scl.runtime.SCLContext; +import org.simantics.scl.runtime.reporting.SCLReportingHandler; +import org.simantics.scl.runtime.tuple.Tuple0; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,6 +41,24 @@ public class BaselineCreatorApplication implements IApplication { private static final IArgumentFactory OUTPUT = new StringArgumentFactory("-o"); private static final IArgumentFactory VERBOSE = new NoValueArgumentFactory("-v"); + /** + * The -f argument specifies an SCL function that shall be executed after the + * platform has been started and before the platform is shutdown. + * + *

+ * The function must be: + *

    + *
  1. a parameterless function
  2. + *
  3. a function with <Proc> effect only
  4. . It must internally invoke + * syncWrite etc. to have other effects. + *
+ * + *

+ * This means the function must have the following signature: + * func :: () + */ + private static final IArgumentFactory FUNCTION = new StringArgumentFactory("-f"); + IArgumentFactory[] accepted = { SimanticsArguments.RECOVERY_POLICY_FIX_ERRORS, SimanticsArguments.ONTOLOGY_RECOVERY_POLICY_REINSTALL, @@ -45,6 +66,7 @@ public class BaselineCreatorApplication implements IApplication { SimanticsArguments.DATABASE_ID, OUTPUT, VERBOSE, + FUNCTION, }; private static String currentLocalDateTimeStamp() { @@ -80,17 +102,35 @@ public class BaselineCreatorApplication implements IApplication { IArguments parsedArgs = Arguments.parse(args, accepted); Path output = constructOutputPath(workspace, parsedArgs); + LOGGER.info("Selected output file: {} ", output); // Create database and indexes IProgressMonitor progress = parsedArgs.contains(VERBOSE) ? new TimingProgressMonitor() : new NullProgressMonitor(); Simantics.startUpHeadless(parsedArgs, progress); + + if (parsedArgs.contains(FUNCTION)) { + String func = parsedArgs.get(FUNCTION); + String[] moduleAndFunc = splitFunction(func); + if (moduleAndFunc != null) { + try { + LOGGER.info("Invoking SCL function {}/{}", moduleAndFunc[0], moduleAndFunc[1]); + SCLContext.getCurrent().put(SCLReportingHandler.REPORTING_HANDLER, SCLReportingHandler.DEFAULT); + Simantics.applySCL(moduleAndFunc[0], moduleAndFunc[1], Tuple0.INSTANCE); + } catch (Throwable t) { + LOGGER.error("Invocation failed", t); + } + } else { + LOGGER.error("SCL function '{}' not invocable for baselining the database", func); + } + } + Simantics.shutdown(progress); // Create the baseline package file Path actualOutput = DatabaseBaselines.packageBaseline(workspace, output); - System.out.println("OK " + actualOutput.toAbsolutePath()); + System.out.println("OK " + actualOutput.normalize().toAbsolutePath()); return IApplication.EXIT_OK; } catch (Exception e) { @@ -101,6 +141,13 @@ public class BaselineCreatorApplication implements IApplication { } } + private String[] splitFunction(String func) { + int l = func.lastIndexOf('/'); + if (l < 0) + return null; + return new String[] { func.substring(0, l), func.substring(l+1) }; + } + @Override public void stop() { } -- 2.43.2