Support classpath argument when running scripts using the -script flag

Signed-off-by: Isak Karlsson <isak-kar@dsv.su.se>
This commit is contained in:
Isak Karlsson
2015-03-11 15:00:25 +01:00
committed by Alexander Udalov
parent f632a90741
commit efae5c05a1
3 changed files with 15 additions and 8 deletions
@@ -176,7 +176,7 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
List<String> scriptArgs = arguments.freeArgs.subList(1, arguments.freeArgs.size());
JetCoreEnvironment environment =
JetCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
KotlinToJVMBytecodeCompiler.compileAndExecuteScript(paths, environment, scriptArgs);
KotlinToJVMBytecodeCompiler.compileAndExecuteScript(configuration, paths, environment, scriptArgs);
}
else {
JetCoreEnvironment environment =
@@ -222,11 +222,12 @@ public class KotlinToJVMBytecodeCompiler {
}
public static void compileAndExecuteScript(
@NotNull CompilerConfiguration configuration,
@NotNull KotlinPaths paths,
@NotNull JetCoreEnvironment environment,
@NotNull List<String> scriptArgs
) {
Class<?> scriptClass = compileScript(paths, environment);
Class<?> scriptClass = compileScript(configuration, paths, environment);
if (scriptClass == null) return;
try {
@@ -241,7 +242,11 @@ public class KotlinToJVMBytecodeCompiler {
}
@Nullable
public static Class<?> compileScript(@NotNull KotlinPaths paths, @NotNull JetCoreEnvironment environment) {
public static Class<?> compileScript(
@NotNull CompilerConfiguration configuration,
@NotNull KotlinPaths paths,
@NotNull JetCoreEnvironment environment
) {
List<AnalyzerScriptParameter> scriptParameters = environment.getConfiguration().getList(JVMConfigurationKeys.SCRIPT_PARAMETERS);
if (!scriptParameters.isEmpty()) {
JetScriptDefinitionProvider.getInstance(environment.getProject()).addScriptDefinition(
@@ -255,11 +260,13 @@ public class KotlinToJVMBytecodeCompiler {
GeneratedClassLoader classLoader;
try {
List<URL> classPaths = Lists.newArrayList(paths.getRuntimePath().toURI().toURL());
for (File file : configuration.get(JVMConfigurationKeys.CLASSPATH_KEY, Collections.<File>emptyList())) {
classPaths.add(file.toURI().toURL());
}
classLoader = new GeneratedClassLoader(state.getFactory(),
new URLClassLoader(new URL[] {
// TODO: add all classpath
paths.getRuntimePath().toURI().toURL()
}, AllModules.class.getClassLoader())
new URLClassLoader(classPaths.toArray(new URL[classPaths.size()]),
AllModules.class.getClassLoader())
);
FqName nameForScript = ScriptNameUtil.classNameForScript(environment.getSourceFiles().get(0).getScript());
@@ -91,7 +91,7 @@ public class ScriptTest {
try {
JetScriptDefinitionProvider.getInstance(environment.getProject()).markFileAsScript(environment.getSourceFiles().get(0));
return KotlinToJVMBytecodeCompiler.compileScript(paths, environment);
return KotlinToJVMBytecodeCompiler.compileScript(configuration, paths, environment);
}
catch (CompilationException e) {
messageCollector.report(CompilerMessageSeverity.EXCEPTION, OutputMessageUtil.renderException(e),