command line script launcher
% ./bin/kotlin -script hello.ktscript
hello world
% cat hello.ktscript
fun hello(what: String) = println("hello $what")
hello("world")
Note it currently takes 8 seconds to evaluate hello world script.
This commit is contained in:
@@ -97,7 +97,7 @@ public class BytecodeCompiler {
|
|||||||
*/
|
*/
|
||||||
public void sourcesToDir ( @NotNull String src, @NotNull String output, @Nullable String stdlib, @Nullable String[] classpath ) {
|
public void sourcesToDir ( @NotNull String src, @NotNull String output, @Nullable String stdlib, @Nullable String[] classpath ) {
|
||||||
try {
|
try {
|
||||||
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(env(stdlib, classpath), Collections.singletonList(src), null, output, true
|
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(env(stdlib, classpath), Collections.singletonList(src), null, output, false, true
|
||||||
/* Last arg is ignored anyway */);
|
/* Last arg is ignored anyway */);
|
||||||
if ( ! success ) {
|
if ( ! success ) {
|
||||||
throw new CompileEnvironmentException( errorMessage( src, false ));
|
throw new CompileEnvironmentException( errorMessage( src, false ));
|
||||||
@@ -120,7 +120,7 @@ public class BytecodeCompiler {
|
|||||||
*/
|
*/
|
||||||
public void sourcesToJar ( @NotNull String src, @NotNull String jar, boolean includeRuntime, @Nullable String stdlib, @Nullable String[] classpath ) {
|
public void sourcesToJar ( @NotNull String src, @NotNull String jar, boolean includeRuntime, @Nullable String stdlib, @Nullable String[] classpath ) {
|
||||||
try {
|
try {
|
||||||
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(env(stdlib, classpath), Collections.singletonList(src), jar, null, includeRuntime);
|
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(env(stdlib, classpath), Collections.singletonList(src), jar, null, false, includeRuntime);
|
||||||
if ( ! success ) {
|
if ( ! success ) {
|
||||||
throw new CompileEnvironmentException( errorMessage( src, false ));
|
throw new CompileEnvironmentException( errorMessage( src, false ));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,9 +120,7 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
|
|||||||
// TODO ideally we'd unify to just having a single field that supports multiple files/dirs
|
// TODO ideally we'd unify to just having a single field that supports multiple files/dirs
|
||||||
if (arguments.getSourceDirs() != null) {
|
if (arguments.getSourceDirs() != null) {
|
||||||
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSourceDirectories(configuration,
|
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSourceDirectories(configuration,
|
||||||
arguments.getSourceDirs(), arguments.jar,
|
arguments.getSourceDirs(), arguments.jar, arguments.outputDir, arguments.script, arguments.includeRuntime);
|
||||||
arguments.outputDir,
|
|
||||||
arguments.includeRuntime);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
List<String> sources = Lists.newArrayList();
|
List<String> sources = Lists.newArrayList();
|
||||||
@@ -131,8 +129,7 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
|
|||||||
}
|
}
|
||||||
sources.addAll(arguments.freeArgs);
|
sources.addAll(arguments.freeArgs);
|
||||||
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration,
|
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration,
|
||||||
sources, arguments.jar, arguments.outputDir,
|
sources, arguments.jar, arguments.outputDir, arguments.script, arguments.includeRuntime);
|
||||||
arguments.includeRuntime);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return noErrors ? OK : COMPILATION_ERROR;
|
return noErrors ? OK : COMPILATION_ERROR;
|
||||||
|
|||||||
@@ -66,6 +66,9 @@ public class K2JVMCompilerArguments extends CompilerArguments {
|
|||||||
@Argument(value = "module", description = "module to compile")
|
@Argument(value = "module", description = "module to compile")
|
||||||
public String module;
|
public String module;
|
||||||
|
|
||||||
|
@Argument(value = "script", description = "evaluate script")
|
||||||
|
public boolean script;
|
||||||
|
|
||||||
@Argument(value = "tags", description = "Demarcate each compilation message (error, warning, etc) with an open and close tag")
|
@Argument(value = "tags", description = "Demarcate each compilation message (error, warning, etc) with an open and close tag")
|
||||||
public boolean tags;
|
public boolean tags;
|
||||||
|
|
||||||
|
|||||||
+22
-5
@@ -23,6 +23,7 @@ import com.intellij.psi.PsiFile;
|
|||||||
import com.intellij.testFramework.LightVirtualFile;
|
import com.intellij.testFramework.LightVirtualFile;
|
||||||
import com.intellij.util.LocalTimeCounter;
|
import com.intellij.util.LocalTimeCounter;
|
||||||
import jet.Function0;
|
import jet.Function0;
|
||||||
|
import jet.modules.AllModules;
|
||||||
import jet.modules.Module;
|
import jet.modules.Module;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@@ -46,6 +47,8 @@ import org.jetbrains.jet.utils.Progress;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLClassLoader;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -117,6 +120,7 @@ public class KotlinToJVMBytecodeCompiler {
|
|||||||
K2JVMCompileEnvironmentConfiguration configuration,
|
K2JVMCompileEnvironmentConfiguration configuration,
|
||||||
String jar,
|
String jar,
|
||||||
String outputDir,
|
String outputDir,
|
||||||
|
boolean script,
|
||||||
boolean includeRuntime
|
boolean includeRuntime
|
||||||
) {
|
) {
|
||||||
FqName mainClass = null;
|
FqName mainClass = null;
|
||||||
@@ -146,6 +150,20 @@ public class KotlinToJVMBytecodeCompiler {
|
|||||||
else if (outputDir != null) {
|
else if (outputDir != null) {
|
||||||
CompileEnvironmentUtil.writeToOutputDirectory(factory, outputDir);
|
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("Script");
|
||||||
|
scriptClass.newInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Failed to evaluate script: " + e, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
throw new CompileEnvironmentException("Output directory or jar file is not specified - no files will be saved to the disk");
|
throw new CompileEnvironmentException("Output directory or jar file is not specified - no files will be saved to the disk");
|
||||||
}
|
}
|
||||||
@@ -158,24 +176,23 @@ public class KotlinToJVMBytecodeCompiler {
|
|||||||
|
|
||||||
public static boolean compileBunchOfSources(
|
public static boolean compileBunchOfSources(
|
||||||
K2JVMCompileEnvironmentConfiguration configuration,
|
K2JVMCompileEnvironmentConfiguration configuration,
|
||||||
|
List<String> sourceFilesOrDirs, String jar, String outputDir, boolean script, boolean includeRuntime) {
|
||||||
List<String> sourceFilesOrDirs, String jar, String outputDir, boolean includeRuntime) {
|
|
||||||
for (String sourceFileOrDir : sourceFilesOrDirs) {
|
for (String sourceFileOrDir : sourceFilesOrDirs) {
|
||||||
configuration.getEnvironment().addSources(sourceFileOrDir);
|
configuration.getEnvironment().addSources(sourceFileOrDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
return compileBunchOfSources(configuration, jar, outputDir, includeRuntime);
|
return compileBunchOfSources(configuration, jar, outputDir, script, includeRuntime);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean compileBunchOfSourceDirectories(
|
public static boolean compileBunchOfSourceDirectories(
|
||||||
K2JVMCompileEnvironmentConfiguration configuration,
|
K2JVMCompileEnvironmentConfiguration configuration,
|
||||||
|
|
||||||
List<String> sources, String jar, String outputDir, boolean includeRuntime) {
|
List<String> sources, String jar, String outputDir, boolean script, boolean includeRuntime) {
|
||||||
for (String source : sources) {
|
for (String source : sources) {
|
||||||
configuration.getEnvironment().addSources(source);
|
configuration.getEnvironment().addSources(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
return compileBunchOfSources(configuration, jar, outputDir, includeRuntime);
|
return compileBunchOfSources(configuration, jar, outputDir, script, includeRuntime);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ OUT -altHeaders [String] Path to the alternative library headers paths
|
|||||||
OUT -mode [String] Special compiler modes: stubs or altHeaders
|
OUT -mode [String] Special compiler modes: stubs or altHeaders
|
||||||
OUT -output [String] output directory
|
OUT -output [String] output directory
|
||||||
OUT -module [String] module to compile
|
OUT -module [String] module to compile
|
||||||
|
OUT -script [flag]
|
||||||
OUT -tags [flag]
|
OUT -tags [flag]
|
||||||
OUT -verbose [flag]
|
OUT -verbose [flag]
|
||||||
OUT -version [flag]
|
OUT -version [flag]
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
println("hello world")
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
OUT hello world
|
||||||
|
Return code: 0
|
||||||
@@ -50,4 +50,9 @@ public class CompilerSmokeTest extends KotlinIntegrationTestBase {
|
|||||||
|
|
||||||
runCompiler("hello.compile", "-src", "hello.kt", "-jar", jar);
|
runCompiler("hello.compile", "-src", "hello.kt", "-jar", jar);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void script() throws Exception {
|
||||||
|
runCompiler("script", "-script", "hello.ktscript");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user