script parameters

still a lot of things to do
This commit is contained in:
Stepan Koltsov
2012-05-28 20:30:23 +04:00
parent 6244404344
commit 30e44fdc5f
49 changed files with 833 additions and 108 deletions
@@ -36,6 +36,7 @@ import org.jetbrains.jet.utils.PathUtil;
import java.io.File;
import java.io.PrintStream;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.jet.cli.common.ExitCode.*;
@@ -84,8 +85,8 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
CompilerDependencies dependencies = new CompilerDependencies(mode, CompilerDependencies.findRtJar(), jdkHeadersJar, runtimeJar);
JetCoreEnvironment environment = JetCoreEnvironment.getCoreEnvironmentForJVM(rootDisposable, dependencies);
K2JVMCompileEnvironmentConfiguration configuration =
new K2JVMCompileEnvironmentConfiguration(environment, messageCollector);
List<String> scriptArgs = arguments.script ? arguments.freeArgs.subList(1, arguments.freeArgs.size()) : Collections.<String>emptyList();
K2JVMCompileEnvironmentConfiguration configuration = new K2JVMCompileEnvironmentConfiguration(environment, messageCollector, arguments.script, scriptArgs);
messageCollector.report(CompilerMessageSeverity.LOGGING, "Configuring the compilation environment",
CompilerMessageLocation.NO_LOCATION);
@@ -115,7 +116,12 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
if (arguments.src != null) {
sources.add(arguments.src);
}
sources.addAll(arguments.freeArgs);
if (arguments.script) {
sources.add(arguments.freeArgs.get(0));
}
else {
sources.addAll(arguments.freeArgs);
}
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration,
sources, arguments.jar, arguments.outputDir, arguments.script, arguments.includeRuntime);
}
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.cli.jvm.compiler;
import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.ref.JetTypeName;
import java.util.Collections;
import java.util.List;
/**
* @author Stepan Koltsov
*/
public class CommandLineScriptUtils {
private static final Name ARGS_NAME = Name.identifier("args");
private static final JetTypeName ARGS_TYPE = JetTypeName.parse("jet.Array<jet.String>");
public static List<AnalyzerScriptParameter> scriptParameters() {
AnalyzerScriptParameter argsParameter = new AnalyzerScriptParameter(ARGS_NAME, ARGS_TYPE);
return Collections.singletonList(argsParameter);
}
}
@@ -45,6 +45,7 @@ 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.*;
@@ -143,7 +144,7 @@ public class CompileEnvironmentUtil {
scriptEnvironment.addSources(moduleScriptFile);
GenerationState generationState = KotlinToJVMBytecodeCompiler
.analyzeAndGenerate(new K2JVMCompileEnvironmentConfiguration(scriptEnvironment, messageCollector), false);
.analyzeAndGenerate(new K2JVMCompileEnvironmentConfiguration(scriptEnvironment, messageCollector, false, Collections.<String>emptyList()), false);
if (generationState == null) {
return null;
}
@@ -21,11 +21,15 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.common.CompileEnvironmentConfiguration;
import org.jetbrains.jet.cli.common.messages.MessageCollector;
import java.util.List;
/**
* @author abreslav
*/
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.
@@ -33,12 +37,22 @@ public class K2JVMCompileEnvironmentConfiguration extends CompileEnvironmentConf
* @see Disposer
*/
public K2JVMCompileEnvironmentConfiguration(@NotNull JetCoreEnvironment environment,
@NotNull MessageCollector messageCollector) {
@NotNull MessageCollector messageCollector, boolean script, List<String> scriptArgs) {
super(messageCollector);
this.environment = environment;
this.script = script;
this.scriptArgs = scriptArgs;
}
public JetCoreEnvironment getEnvironment() {
return environment;
}
public boolean isScript() {
return script;
}
public List<String> getScriptArgs() {
return scriptArgs;
}
}
@@ -37,6 +37,7 @@ import org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetPsiUtil;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -49,6 +50,7 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
import java.util.List;
/**
@@ -159,7 +161,7 @@ public class KotlinToJVMBytecodeCompiler {
},
AllModules.class.getClassLoader()));
Class<?> scriptClass = classLoader.loadClass("Script");
scriptClass.newInstance();
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);
}
@@ -219,7 +221,7 @@ public class KotlinToJVMBytecodeCompiler {
K2JVMCompileEnvironmentConfiguration configuration,
boolean stubs
) {
AnalyzeExhaust exhaust = analyze(configuration, stubs);
AnalyzeExhaust exhaust = analyze(configuration, configuration.isScript(), stubs);
if (exhaust == null) {
return null;
@@ -233,18 +235,23 @@ public class KotlinToJVMBytecodeCompiler {
@Nullable
private static AnalyzeExhaust analyze(
final K2JVMCompileEnvironmentConfiguration configuration,
boolean stubs) {
boolean script, boolean stubs) {
final JetCoreEnvironment environment = configuration.getEnvironment();
AnalyzerWithCompilerReport analyzerWithCompilerReport = new AnalyzerWithCompilerReport(configuration.getMessageCollector());
final Predicate<PsiFile> filesToAnalyzeCompletely =
stubs ? Predicates.<PsiFile>alwaysFalse() : Predicates.<PsiFile>alwaysTrue();
final List<AnalyzerScriptParameter> scriptParameters =
script ? CommandLineScriptUtils.scriptParameters() : Collections.<AnalyzerScriptParameter>emptyList();
analyzerWithCompilerReport.analyzeAndReport(
new Function0<AnalyzeExhaust>() {
@NotNull
@Override
public AnalyzeExhaust invoke() {
return AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(
environment.getProject(), environment.getSourceFiles(), filesToAnalyzeCompletely,
environment.getProject(),
environment.getSourceFiles(),
scriptParameters,
filesToAnalyzeCompletely,
configuration.getEnvironment().getCompilerDependencies());
}
}, environment.getSourceFiles()