From 8dd1465371866999407b7a452cb379cbe991a0b7 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Sun, 10 Jun 2012 03:44:29 +0400 Subject: [PATCH] pull script up in cli --- .../jet/buildtools/core/BytecodeCompiler.java | 7 ++- .../jetbrains/jet/cli/jvm/K2JVMCompiler.java | 20 ++++---- .../jvm/compiler/CompileEnvironmentUtil.java | 3 +- .../K2JVMCompileEnvironmentConfiguration.java | 8 +-- .../compiler/KotlinToJVMBytecodeCompiler.java | 50 ++++++++++++------- .../jet/codegen/CompileTextTest.java | 3 +- .../jetbrains/jet/codegen/TestlibTest.java | 3 +- 7 files changed, 49 insertions(+), 45 deletions(-) diff --git a/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java b/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java index cbfcf4b1e2a..0dd913806c1 100644 --- a/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java +++ b/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java @@ -27,7 +27,6 @@ import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode; import java.io.File; import java.util.ArrayList; -import java.util.Collections; import java.util.List; @@ -54,7 +53,7 @@ public class BytecodeCompiler { CompilerDependencies dependencies = CompilerDependencies.compilerDependenciesForProduction(CompilerSpecialMode.REGULAR); JetCoreEnvironment environment = new JetCoreEnvironment(CompileEnvironmentUtil.createMockDisposable(), dependencies); K2JVMCompileEnvironmentConfiguration - env = new K2JVMCompileEnvironmentConfiguration(environment, MessageCollector.PLAIN_TEXT_TO_SYSTEM_ERR, false, Collections.emptyList()); + env = new K2JVMCompileEnvironmentConfiguration(environment, MessageCollector.PLAIN_TEXT_TO_SYSTEM_ERR, false); if (( stdlib != null ) && ( stdlib.trim().length() > 0 )) { File file = new File(stdlib); @@ -100,7 +99,7 @@ public class BytecodeCompiler { K2JVMCompileEnvironmentConfiguration configuration = env(stdlib, classpath); configuration.getEnvironment().addSources(src); - boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration, null, new File(output), false, true); + boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration, null, new File(output), true); if ( ! success ) { throw new CompileEnvironmentException( errorMessage( src, false )); } @@ -125,7 +124,7 @@ public class BytecodeCompiler { K2JVMCompileEnvironmentConfiguration configuration = env(stdlib, classpath); configuration.getEnvironment().addSources(src); - boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration, new File(jar), null, false, includeRuntime); + boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration, new File(jar), null, includeRuntime); if ( ! success ) { throw new CompileEnvironmentException( errorMessage( src, false )); } diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java index ba9f498afb0..f1e265e69b3 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java @@ -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 scriptArgs = arguments.script ? arguments.freeArgs.subList(1, arguments.freeArgs.size()) : Collections.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 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 CLICompileremptyList()), false); + .analyzeAndGenerate(new K2JVMCompileEnvironmentConfiguration(scriptEnvironment, messageCollector, false), false); if (generationState == null) { return null; } diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/K2JVMCompileEnvironmentConfiguration.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/K2JVMCompileEnvironmentConfiguration.java index 22a592528db..f524d42b807 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/K2JVMCompileEnvironmentConfiguration.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/K2JVMCompileEnvironmentConfiguration.java @@ -29,7 +29,6 @@ import java.util.List; public class K2JVMCompileEnvironmentConfiguration extends CompileEnvironmentConfiguration { private final JetCoreEnvironment environment; private final boolean script; - private final List 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 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 getScriptArgs() { - return scriptArgs; - } } diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java index f32606c4e4b..75a9ba85430 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java @@ -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 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 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 diff --git a/compiler/tests/org/jetbrains/jet/codegen/CompileTextTest.java b/compiler/tests/org/jetbrains/jet/codegen/CompileTextTest.java index 0467fcaed64..ccc012acd0f 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/CompileTextTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/CompileTextTest.java @@ -22,14 +22,13 @@ import org.jetbrains.jet.cli.common.messages.MessageCollector; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.Collections; public class CompileTextTest extends CodegenTestCase { public void testMe() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { createEnvironmentWithMockJdkAndIdeaAnnotations(); String text = "import org.jetbrains.jet.codegen.CompileTextTest; fun x() = CompileTextTest()"; K2JVMCompileEnvironmentConfiguration configuration = new K2JVMCompileEnvironmentConfiguration( - myEnvironment, MessageCollector.PLAIN_TEXT_TO_SYSTEM_ERR, false, Collections.emptyList()); + myEnvironment, MessageCollector.PLAIN_TEXT_TO_SYSTEM_ERR, false); configuration.getEnvironment().addToClasspathFromClassLoader(getClass().getClassLoader()); ClassLoader classLoader = KotlinToJVMBytecodeCompiler.compileText(configuration, text); Class namespace = classLoader.loadClass("namespace"); diff --git a/compiler/tests/org/jetbrains/jet/codegen/TestlibTest.java b/compiler/tests/org/jetbrains/jet/codegen/TestlibTest.java index 67125f2079d..96982c8049d 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/TestlibTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/TestlibTest.java @@ -40,7 +40,6 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; import java.net.URL; import java.net.URLClassLoader; -import java.util.Collections; import java.util.Set; /** @@ -85,7 +84,7 @@ public class TestlibTest extends CodegenTestCase { myEnvironment.addSources(localFileSystem.findFileByPath(JetParsingTest.getTestDataDir() + "/../../libraries/kunit/src")); GenerationState generationState = KotlinToJVMBytecodeCompiler - .analyzeAndGenerate(new K2JVMCompileEnvironmentConfiguration(myEnvironment, MessageCollector.PLAIN_TEXT_TO_SYSTEM_ERR, false, Collections.emptyList()), false); + .analyzeAndGenerate(new K2JVMCompileEnvironmentConfiguration(myEnvironment, MessageCollector.PLAIN_TEXT_TO_SYSTEM_ERR, false), false); if (generationState == null) { throw new RuntimeException("There were compilation errors");