pull script up in cli
This commit is contained in:
@@ -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.<String>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 ));
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+1
-7
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+33
-17
@@ -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
|
||||
|
||||
@@ -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.<String>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");
|
||||
|
||||
@@ -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.<String>emptyList()), false);
|
||||
.analyzeAndGenerate(new K2JVMCompileEnvironmentConfiguration(myEnvironment, MessageCollector.PLAIN_TEXT_TO_SYSTEM_ERR, false), false);
|
||||
|
||||
if (generationState == null) {
|
||||
throw new RuntimeException("There were compilation errors");
|
||||
|
||||
Reference in New Issue
Block a user