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:
Stepan Koltsov
2012-05-23 02:52:30 +04:00
parent f4051f45ab
commit 6063c01a1c
8 changed files with 38 additions and 12 deletions
@@ -97,7 +97,7 @@ public class BytecodeCompiler {
*/
public void sourcesToDir ( @NotNull String src, @NotNull String output, @Nullable String stdlib, @Nullable String[] classpath ) {
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 */);
if ( ! success ) {
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 ) {
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 ) {
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
if (arguments.getSourceDirs() != null) {
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSourceDirectories(configuration,
arguments.getSourceDirs(), arguments.jar,
arguments.outputDir,
arguments.includeRuntime);
arguments.getSourceDirs(), arguments.jar, arguments.outputDir, arguments.script, arguments.includeRuntime);
}
else {
List<String> sources = Lists.newArrayList();
@@ -131,8 +129,7 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
}
sources.addAll(arguments.freeArgs);
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration,
sources, arguments.jar, arguments.outputDir,
arguments.includeRuntime);
sources, arguments.jar, arguments.outputDir, arguments.script, arguments.includeRuntime);
}
}
return noErrors ? OK : COMPILATION_ERROR;
@@ -66,6 +66,9 @@ public class K2JVMCompilerArguments extends CompilerArguments {
@Argument(value = "module", description = "module to compile")
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")
public boolean tags;
@@ -23,6 +23,7 @@ import com.intellij.psi.PsiFile;
import com.intellij.testFramework.LightVirtualFile;
import com.intellij.util.LocalTimeCounter;
import jet.Function0;
import jet.modules.AllModules;
import jet.modules.Module;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -46,6 +47,8 @@ import org.jetbrains.jet.utils.Progress;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
/**
@@ -117,6 +120,7 @@ public class KotlinToJVMBytecodeCompiler {
K2JVMCompileEnvironmentConfiguration configuration,
String jar,
String outputDir,
boolean script,
boolean includeRuntime
) {
FqName mainClass = null;
@@ -146,6 +150,20 @@ 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("Script");
scriptClass.newInstance();
} 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");
}
@@ -158,24 +176,23 @@ public class KotlinToJVMBytecodeCompiler {
public static boolean compileBunchOfSources(
K2JVMCompileEnvironmentConfiguration configuration,
List<String> sourceFilesOrDirs, String jar, String outputDir, boolean includeRuntime) {
List<String> sourceFilesOrDirs, String jar, String outputDir, boolean script, boolean includeRuntime) {
for (String sourceFileOrDir : sourceFilesOrDirs) {
configuration.getEnvironment().addSources(sourceFileOrDir);
}
return compileBunchOfSources(configuration, jar, outputDir, includeRuntime);
return compileBunchOfSources(configuration, jar, outputDir, script, includeRuntime);
}
public static boolean compileBunchOfSourceDirectories(
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) {
configuration.getEnvironment().addSources(source);
}
return compileBunchOfSources(configuration, jar, outputDir, includeRuntime);
return compileBunchOfSources(configuration, jar, outputDir, script, includeRuntime);
}
@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 -output [String] output directory
OUT -module [String] module to compile
OUT -script [flag]
OUT -tags [flag]
OUT -verbose [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);
}
@Test
public void script() throws Exception {
runCompiler("script", "-script", "hello.ktscript");
}
}