pull script up in cli

This commit is contained in:
Stepan Koltsov
2012-06-10 03:44:29 +04:00
parent 7851101c0e
commit 8dd1465371
7 changed files with 49 additions and 45 deletions
@@ -18,7 +18,6 @@ package org.jetbrains.jet.cli.jvm;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.intellij.openapi.Disposable;
import jet.modules.Module;
import org.jetbrains.annotations.NotNull;
@@ -98,8 +97,7 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
}
JetCoreEnvironment environment = JetCoreEnvironment.getCoreEnvironmentForJVM(rootDisposable, dependencies);
List<String> scriptArgs = arguments.script ? arguments.freeArgs.subList(1, arguments.freeArgs.size()) : Collections.<String>emptyList();
K2JVMCompileEnvironmentConfiguration configuration = new K2JVMCompileEnvironmentConfiguration(environment, messageCollector, arguments.script, scriptArgs);
K2JVMCompileEnvironmentConfiguration configuration = new K2JVMCompileEnvironmentConfiguration(environment, messageCollector, arguments.script);
messageCollector.report(CompilerMessageSeverity.LOGGING, "Configuring the compilation environment",
CompilerMessageLocation.NO_LOCATION);
@@ -121,6 +119,11 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
directory, jar, outputDir,
arguments.includeRuntime);
}
else if (arguments.script) {
configuration.getEnvironment().addSources(arguments.freeArgs.get(0));
List<String> scriptArgs = arguments.freeArgs.subList(1, arguments.freeArgs.size());
noErrors = KotlinToJVMBytecodeCompiler.compileAndExecuteScript(configuration, scriptArgs);
}
else {
// TODO ideally we'd unify to just having a single field that supports multiple files/dirs
if (arguments.getSourceDirs() != null) {
@@ -131,17 +134,12 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
if (arguments.src != null) {
configuration.getEnvironment().addSources(arguments.src);
}
if (arguments.script) {
configuration.getEnvironment().addSources(arguments.freeArgs.get(0));
}
else {
for (String freeArg : arguments.freeArgs) {
configuration.getEnvironment().addSources(freeArg);
}
for (String freeArg : arguments.freeArgs) {
configuration.getEnvironment().addSources(freeArg);
}
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(
configuration, jar, outputDir, arguments.script, arguments.includeRuntime);
configuration, jar, outputDir, arguments.includeRuntime);
}
}
return noErrors ? OK : COMPILATION_ERROR;
@@ -45,7 +45,6 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.jar.*;
@@ -144,7 +143,7 @@ public class CompileEnvironmentUtil {
scriptEnvironment.addSources(moduleScriptFile);
GenerationState generationState = KotlinToJVMBytecodeCompiler
.analyzeAndGenerate(new K2JVMCompileEnvironmentConfiguration(scriptEnvironment, messageCollector, false, Collections.<String>emptyList()), false);
.analyzeAndGenerate(new K2JVMCompileEnvironmentConfiguration(scriptEnvironment, messageCollector, false), false);
if (generationState == null) {
return null;
}
@@ -29,7 +29,6 @@ import java.util.List;
public class K2JVMCompileEnvironmentConfiguration extends CompileEnvironmentConfiguration {
private final JetCoreEnvironment environment;
private final boolean script;
private final List<String> scriptArgs;
/**
* NOTE: It's very important to call dispose for every object of this class or there will be memory leaks.
@@ -37,11 +36,10 @@ public class K2JVMCompileEnvironmentConfiguration extends CompileEnvironmentConf
* @see Disposer
*/
public K2JVMCompileEnvironmentConfiguration(@NotNull JetCoreEnvironment environment,
@NotNull MessageCollector messageCollector, boolean script, List<String> scriptArgs) {
@NotNull MessageCollector messageCollector, boolean script) {
super(messageCollector);
this.environment = environment;
this.script = script;
this.scriptArgs = scriptArgs;
}
public JetCoreEnvironment getEnvironment() {
@@ -51,8 +49,4 @@ public class K2JVMCompileEnvironmentConfiguration extends CompileEnvironmentConf
public boolean isScript() {
return script;
}
public List<String> getScriptArgs() {
return scriptArgs;
}
}
@@ -151,9 +151,9 @@ public class KotlinToJVMBytecodeCompiler {
K2JVMCompileEnvironmentConfiguration configuration,
@Nullable File jar,
@Nullable File outputDir,
boolean script,
boolean includeRuntime
) {
FqName mainClass = findMainClass(configuration.getEnvironment().getSourceFiles());
GenerationState generationState = analyzeAndGenerate(configuration);
@@ -185,20 +185,6 @@ public class KotlinToJVMBytecodeCompiler {
else if (outputDir != null) {
CompileEnvironmentUtil.writeToOutputDirectory(factory, outputDir);
}
else if (script) {
try {
GeneratedClassLoader classLoader = new GeneratedClassLoader(factory, new URLClassLoader(new URL[]{
// TODO: add all classpath
configuration.getEnvironment().getCompilerDependencies().getRuntimeJar().toURI().toURL()
},
AllModules.class.getClassLoader()));
Class<?> scriptClass = classLoader.loadClass(ScriptCodegen.SCRIPT_DEFAULT_CLASS_NAME.getFqName().getFqName());
scriptClass.getConstructor(String[].class).newInstance(new Object[]{ configuration.getScriptArgs().toArray(new String[0]) });
} catch (Exception e) {
throw new RuntimeException("Failed to evaluate script: " + e, e);
}
}
else {
throw new CompileEnvironmentException("Output directory or jar file is not specified - no files will be saved to the disk");
}
@@ -209,6 +195,36 @@ public class KotlinToJVMBytecodeCompiler {
}
}
public static boolean compileAndExecuteScript(
@NotNull K2JVMCompileEnvironmentConfiguration configuration,
@NotNull List<String> scriptArgs) {
GenerationState generationState = analyzeAndGenerate(configuration);
if (generationState == null) {
return false;
}
try {
ClassFileFactory factory = generationState.getFactory();
try {
GeneratedClassLoader classLoader = new GeneratedClassLoader(factory, new URLClassLoader(new URL[]{
// TODO: add all classpath
configuration.getEnvironment().getCompilerDependencies().getRuntimeJar().toURI().toURL()
},
AllModules.class.getClassLoader()));
Class<?> scriptClass = classLoader.loadClass(ScriptCodegen.SCRIPT_DEFAULT_CLASS_NAME.getFqName().getFqName());
scriptClass.getConstructor(String[].class).newInstance(new Object[]{scriptArgs.toArray(new String[0])});
}
catch (Exception e) {
throw new RuntimeException("Failed to evaluate script: " + e, e);
}
return true;
}
finally {
generationState.destroy();
}
}
public static boolean compileBunchOfSources(
K2JVMCompileEnvironmentConfiguration configuration,
List<String> sourceFilesOrDirs, File jar, File outputDir, boolean script, boolean includeRuntime) {
@@ -216,7 +232,7 @@ public class KotlinToJVMBytecodeCompiler {
configuration.getEnvironment().addSources(sourceFileOrDir);
}
return compileBunchOfSources(configuration, jar, outputDir, script, includeRuntime);
return compileBunchOfSources(configuration, jar, outputDir, includeRuntime);
}
public static boolean compileBunchOfSourceDirectories(
@@ -227,7 +243,7 @@ public class KotlinToJVMBytecodeCompiler {
configuration.getEnvironment().addSources(source);
}
return compileBunchOfSources(configuration, jar, outputDir, script, includeRuntime);
return compileBunchOfSources(configuration, jar, outputDir, includeRuntime);
}
@Nullable