Scripting: Add a CLI handler for scripts

This allows to execute directly a script at started

[Added] Support executing a script at startup

Change-Id: Ib8a955a99a310242dab7f700d9bd623fb0bd56ff
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/153008
Tested-by: CI Bot
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
diff --git a/analyses/org.eclipse.tracecompass.incubator.scripting.core/META-INF/MANIFEST.MF b/analyses/org.eclipse.tracecompass.incubator.scripting.core/META-INF/MANIFEST.MF
index 942ea64..7c8449d 100644
--- a/analyses/org.eclipse.tracecompass.incubator.scripting.core/META-INF/MANIFEST.MF
+++ b/analyses/org.eclipse.tracecompass.incubator.scripting.core/META-INF/MANIFEST.MF
@@ -19,9 +19,11 @@
  org.eclipse.tracecompass.statesystem.core,
  org.eclipse.tracecompass.analysis.os.linux.core,
  org.eclipse.tracecompass.tmf.analysis.xml.core,
- org.eclipse.tracecompass.incubator.filters.core;resolution:=optional
+ org.eclipse.tracecompass.incubator.filters.core;resolution:=optional,
+ org.eclipse.tracecompass.tmf.cli.core
 Export-Package: org.eclipse.tracecompass.incubator.internal.scripting.core;x-friends:="org.eclipse.tracecompass.incubator.scripting.core.tests,org.eclipse.tracecompass.incubator.scripting.ui.tests",
  org.eclipse.tracecompass.incubator.internal.scripting.core.analysis;x-friends:="org.eclipse.tracecompass.incubator.scripting.core.tests,org.eclipse.tracecompass.incubator.scripting.ui",
+ org.eclipse.tracecompass.incubator.internal.scripting.core.cli;x-internal:=true,
  org.eclipse.tracecompass.incubator.internal.scripting.core.data.provider;x-friends:="org.eclipse.tracecompass.incubator.scripting.core.tests,org.eclipse.tracecompass.incubator.scripting.ui",
  org.eclipse.tracecompass.incubator.internal.scripting.core.trace;x-friends:="org.eclipse.tracecompass.incubator.scripting.core.tests,org.eclipse.tracecompass.incubator.scripting.ui",
  org.eclipse.tracecompass.incubator.scripting.core,
diff --git a/analyses/org.eclipse.tracecompass.incubator.scripting.core/plugin.xml b/analyses/org.eclipse.tracecompass.incubator.scripting.core/plugin.xml
index fdb2925..dcd4d43 100644
--- a/analyses/org.eclipse.tracecompass.incubator.scripting.core/plugin.xml
+++ b/analyses/org.eclipse.tracecompass.incubator.scripting.core/plugin.xml
@@ -61,4 +61,11 @@
             id="org.eclipse.tracecompass.incubator.scripting.dataprovider">
       </dataProviderFactory>
    </extension>
+   <extension
+         point="org.eclipse.tracecompass.tmf.cli.parser.extension">
+      <parser
+            class="org.eclipse.tracecompass.incubator.internal.scripting.core.cli.ScriptCliParser"
+            priority="8">
+      </parser>
+   </extension>
 </plugin>
diff --git a/analyses/org.eclipse.tracecompass.incubator.scripting.core/src/org/eclipse/tracecompass/incubator/internal/scripting/core/cli/Messages.java b/analyses/org.eclipse.tracecompass.incubator.scripting.core/src/org/eclipse/tracecompass/incubator/internal/scripting/core/cli/Messages.java
new file mode 100644
index 0000000..77059f9
--- /dev/null
+++ b/analyses/org.eclipse.tracecompass.incubator.scripting.core/src/org/eclipse/tracecompass/incubator/internal/scripting/core/cli/Messages.java
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * Copyright (c) 2019 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.incubator.internal.scripting.core.cli;
+
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.osgi.util.NLS;
+
+/**
+ * Messages for the command line parser
+ *
+ * @author Geneviève Bastien
+ */
+public class Messages extends NLS {
+    private static final String BUNDLE_NAME = Messages.class.getPackage().getName() + ".messages"; //$NON-NLS-1$
+
+    /** Run script command description */
+    public static @Nullable String CliParser_RunScriptDescription;
+    /** Message when script does not end properly */
+    public static @Nullable String CliParser_ScriptExecutionError;
+
+    static {
+        // initialize resource bundle
+        NLS.initializeMessages(BUNDLE_NAME, Messages.class);
+    }
+
+    private Messages() {
+    }
+}
diff --git a/analyses/org.eclipse.tracecompass.incubator.scripting.core/src/org/eclipse/tracecompass/incubator/internal/scripting/core/cli/ScriptCliParser.java b/analyses/org.eclipse.tracecompass.incubator.scripting.core/src/org/eclipse/tracecompass/incubator/internal/scripting/core/cli/ScriptCliParser.java
new file mode 100644
index 0000000..d83cefb
--- /dev/null
+++ b/analyses/org.eclipse.tracecompass.incubator.scripting.core/src/org/eclipse/tracecompass/incubator/internal/scripting/core/cli/ScriptCliParser.java
@@ -0,0 +1,68 @@
+/*******************************************************************************
+ * Copyright (c) 2019 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.incubator.internal.scripting.core.cli;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.tracecompass.incubator.internal.scripting.core.Activator;
+import org.eclipse.tracecompass.incubator.internal.scripting.core.ScriptExecutionHelper;
+import org.eclipse.tracecompass.internal.provisional.tmf.cli.core.parser.CliCommandLine;
+import org.eclipse.tracecompass.internal.provisional.tmf.cli.core.parser.CliOption;
+import org.eclipse.tracecompass.internal.provisional.tmf.cli.core.parser.ICliParser;
+
+/**
+ * Command line parser for scripts
+ *
+ * @author Geneviève Bastien
+ */
+@SuppressWarnings("restriction")
+public class ScriptCliParser implements ICliParser {
+
+    private static final String OPTION_COMMAND_LINE_RUN_SCRIPT_SHORT = "s"; //$NON-NLS-1$
+    private static final String OPTION_COMMAND_LINE_RUN_SCRIPT_LONG = "script"; //$NON-NLS-1$
+    private static final String OPTION_COMMAND_LINE_RUN_SCRIPT_DESCRIPTION = Objects.requireNonNull(Messages.CliParser_RunScriptDescription);
+    private final ArrayList<CliOption> fOptions;
+
+    /**
+     * Constructor
+     */
+    public ScriptCliParser() {
+        fOptions = new ArrayList<>();
+        fOptions.add(CliOption.createOptionWithArgs(OPTION_COMMAND_LINE_RUN_SCRIPT_SHORT, OPTION_COMMAND_LINE_RUN_SCRIPT_LONG, OPTION_COMMAND_LINE_RUN_SCRIPT_DESCRIPTION, true, true, "script")); //$NON-NLS-1$
+    }
+
+    @Override
+    public List<CliOption> getCmdLineOptions() {
+        return fOptions;
+    }
+
+    @Override
+    public @NonNull IStatus workspaceLoading(@NonNull CliCommandLine commandLine, @NonNull IProgressMonitor monitor) {
+        if (commandLine.hasOption(OPTION_COMMAND_LINE_RUN_SCRIPT_SHORT)) {
+            // The script option should be handled once the workspace is ready
+            String[] scripts = commandLine.getOptionValues(OPTION_COMMAND_LINE_RUN_SCRIPT_SHORT);
+            for (String script : scripts) {
+                Object scriptRet = ScriptExecutionHelper.executeScript(String.valueOf(script));
+                if (scriptRet == null) {
+                    // Return after script execution failure
+                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CliParser_ScriptExecutionError);
+                }
+            }
+        }
+        return Status.OK_STATUS;
+    }
+
+}
diff --git a/analyses/org.eclipse.tracecompass.incubator.scripting.core/src/org/eclipse/tracecompass/incubator/internal/scripting/core/cli/messages.properties b/analyses/org.eclipse.tracecompass.incubator.scripting.core/src/org/eclipse/tracecompass/incubator/internal/scripting/core/cli/messages.properties
new file mode 100644
index 0000000..46a714e
--- /dev/null
+++ b/analyses/org.eclipse.tracecompass.incubator.scripting.core/src/org/eclipse/tracecompass/incubator/internal/scripting/core/cli/messages.properties
@@ -0,0 +1,11 @@
+###############################################################################
+# Copyright (c) 2019 École Polytechnique de Montréal
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+###############################################################################
+
+CliParser_RunScriptDescription=Run a script once the workspace is ready
+CliParser_ScriptExecutionError=Script execution did not complete properly
diff --git a/analyses/org.eclipse.tracecompass.incubator.scripting.core/src/org/eclipse/tracecompass/incubator/internal/scripting/core/cli/package-info.java b/analyses/org.eclipse.tracecompass.incubator.scripting.core/src/org/eclipse/tracecompass/incubator/internal/scripting/core/cli/package-info.java
new file mode 100644
index 0000000..2f06d19
--- /dev/null
+++ b/analyses/org.eclipse.tracecompass.incubator.scripting.core/src/org/eclipse/tracecompass/incubator/internal/scripting/core/cli/package-info.java
@@ -0,0 +1,11 @@
+/*******************************************************************************
+ * Copyright (c) 2019 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+@org.eclipse.jdt.annotation.NonNullByDefault
+package org.eclipse.tracecompass.incubator.internal.scripting.core.cli;
diff --git a/analyses/org.eclipse.tracecompass.incubator.scripting.ui/META-INF/MANIFEST.MF b/analyses/org.eclipse.tracecompass.incubator.scripting.ui/META-INF/MANIFEST.MF
index d8aa677..ba28a68 100644
--- a/analyses/org.eclipse.tracecompass.incubator.scripting.ui/META-INF/MANIFEST.MF
+++ b/analyses/org.eclipse.tracecompass.incubator.scripting.ui/META-INF/MANIFEST.MF
@@ -22,7 +22,8 @@
  org.eclipse.debug.ui,
  org.eclipse.ui.views,
  org.eclipse.ease.ui,
- org.eclipse.core.expressions
+ org.eclipse.core.expressions,
+ org.eclipse.tracecompass.rcp.ui;resolution:=optional
 Export-Package: org.eclipse.tracecompass.incubator.internal.scripting.ui;x-friends:="org.eclipse.tracecompass.incubator.scripting.ui.tests",
  org.eclipse.tracecompass.incubator.internal.scripting.ui.project.handlers;x-internal:=true,
  org.eclipse.tracecompass.incubator.internal.scripting.ui.views.timegraph;x-internal:=true,
diff --git a/rcp/org.eclipse.tracecompass.incubator.rcp.product/legacy/tracing.incubator.product b/rcp/org.eclipse.tracecompass.incubator.rcp.product/legacy/tracing.incubator.product
index a3fba4a..d33a990 100644
--- a/rcp/org.eclipse.tracecompass.incubator.rcp.product/legacy/tracing.incubator.product
+++ b/rcp/org.eclipse.tracecompass.incubator.rcp.product/legacy/tracing.incubator.product
@@ -1,16 +1,16 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <?pde version="3.5"?>
 
-<product name="Trace Compass (Incubator)" uid="org.eclipse.tracecompass.incubator.rcp" id="org.eclipse.tracecompass.rcp.branding.product" application="org.eclipse.tracecompass.rcp.ui.application" version="0.0.1.qualifier" useFeatures="true" includeLaunchers="true">
+<product name="Trace Compass (Incubator)" uid="org.eclipse.tracecompass.incubator.rcp" id="org.eclipse.tracecompass.incubator.rcp.branding.product" application="org.eclipse.tracecompass.rcp.ui.application" version="0.0.1.qualifier" useFeatures="true" includeLaunchers="true">
 
    <aboutInfo>
       <image path="/org.eclipse.tracecompass.incubator.rcp.branding/icons/tc_about.png"/>
       <text>
-         Trace Compass Incubator
+         Trace Compass (Incubator)
 
 Version: 0.0.1
 
-Copyright (c) 2019 École Polytechnique de Montréal
+Copyright (c) 2019 École Polytechnique de Montréal and others
 
 All rights reserved. This program and the accompanying materials are
 made available under the terms of the Eclipse Public License v1.0 which