/*
 * Copyright 2018 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.core.console;

import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.stream.Collectors;

import com.b2international.commons.extension.Component;
import com.b2international.scripting.api.ScriptEngine;
import com.b2international.snowowl.core.ApplicationContext;
import com.b2international.snowowl.core.setup.Plugins;

import picocli.CommandLine;
import picocli.CommandLine.HelpCommand;
import picocli.CommandLine.Parameters;

/**
 * @since 7.0
 */
@Component
@picocli.CommandLine.Command(
	name = "scripts",
	header = "Scripting shell commands",
	description = "Provides shell commands to work with Groovy scripts",
	subcommands = {
		HelpCommand.class,
		ScriptsCommand.RunCommand.class
	}
)
public class ScriptsCommand extends Command {

	@Override
	public void run(CommandLineStream out) {
		CommandLine.usage(this, (PrintStream) out);
	}
	
	@picocli.CommandLine.Command(
		name = "run",
		header = "Scripting shell commands",
		description = ""
	)	
	private static final class RunCommand extends Command {

		@Parameters(paramLabel = "PATH", description = "Absolute path to the script file to run.")
		String path;
		
		@Override
		public void run(CommandLineStream out) {
			try {
				final String script = Files.lines(Paths.get(path)).collect(Collectors.joining(System.getProperty("line.separator")));
				final ClassLoader classLoader = ApplicationContext.getServiceForClass(Plugins.class).getCompositeClassLoader();
				ScriptEngine.run("groovy", classLoader, script, Collections.emptyMap());
			} catch (Exception e) {
				e.printStackTrace();
				out.println(e.getMessage());
			}
		}
		
	}

}

@@ -13,16 +13,21 @@
 ght 2018
+-2020
  B2i Hea
@@ -672,16 +672,74 @@
 nsole;%0A%0A
+import static com.google.common.collect.Maps.newHashMap;%0A%0A
 import j
@@ -831,27 +831,19 @@
 va.util.
-Collections
+Map
 ;%0Aimport
@@ -1088,20 +1088,191 @@
 ore.
-setup.Plugin
+ServiceProvider;%0Aimport com.b2international.snowowl.core.setup.Environment;%0Aimport com.b2international.snowowl.core.setup.Plugins;%0Aimport com.b2international.snowowl.eventbus.IEventBu
 s;%0A%0A
@@ -2139,16 +2139,21 @@
 %09%09try %7B%0A
+%09%09%09%09%0A
 %09%09%09%09fina
@@ -2378,24 +2378,350 @@
 ssLoader();%0A
+%09%09%09%09%0A%09%09%09%09// Include the CLI-authorized IEventBus when running scripts%0A%09%09%09%09final ServiceProvider context = ApplicationContext.getServiceForClass(Environment.class)%0A%09%09%09%09%09%09.inject()%0A%09%09%09%09%09%09.bind(IEventBus.class, getBus())%0A%09%09%09%09%09%09.build();%0A%09%09%09%09%0A%09%09%09%09final Map%3CString, Object%3E binding = newHashMap();%0A%09%09%09%09binding.put(%22ctx%22, context);%0A
 %09%09%09%09ScriptEn
@@ -2764,32 +2764,22 @@
 pt, 
-Collections.emptyMap());
+binding);%0A%09%09%09%09
 %0A%09%09%09
@@ -2874,12 +2874,8 @@
 %09%7D%0A%09
-%09%0A%09%7D%0A
+%7D
 %0A%7D%0A

Make CLI-authorized IEventBus accessible to console scripts