Introducing KotlinPaths to impose some discipline on compiler/library location

This commit is contained in:
Andrey Breslav
2012-11-21 18:16:09 +04:00
parent 4ee76a6649
commit 4ed07cd9ae
20 changed files with 244 additions and 164 deletions
@@ -19,7 +19,7 @@ package org.jetbrains.jet.compiler.runner;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.cli.common.messages.MessageCollector;
import org.jetbrains.jet.utils.PathUtil;
import org.jetbrains.jet.utils.KotlinPaths;
import java.io.File;
@@ -31,39 +31,26 @@ import static org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity.ERRO
*/
public final class CompilerEnvironment {
@NotNull
public static CompilerEnvironment getEnvironmentFor(boolean tests, @Nullable File mainOutput, @Nullable File outputDirectoryForTests) {
final File outputDir = tests ? outputDirectoryForTests : mainOutput;
return getEnvironmentFor(outputDir);
public static CompilerEnvironment getEnvironmentFor(@NotNull KotlinPaths kotlinPaths, @Nullable File outputDir) {
return new CompilerEnvironment(kotlinPaths, outputDir);
}
public static CompilerEnvironment getEnvironmentFor(@Nullable File outputDir) {
File kotlinHome = PathUtil.getDefaultCompilerPath();
return getEnvironmentFor(kotlinHome, outputDir);
}
public static CompilerEnvironment getEnvironmentFor(@Nullable File kotlinHome, @Nullable File outputDir) {
return new CompilerEnvironment(kotlinHome, outputDir);
}
@Nullable
private final File kotlinHome;
private final KotlinPaths kotlinPaths;
@Nullable
private final File output;
public CompilerEnvironment(@Nullable File home, @Nullable File output) {
this.kotlinHome = home;
private CompilerEnvironment(@NotNull KotlinPaths kotlinPaths, @Nullable File output) {
this.kotlinPaths = kotlinPaths;
this.output = output;
}
public boolean success() {
return kotlinHome != null && output != null;
return kotlinPaths.getHomePath().exists() && output != null;
}
@NotNull
public File getKotlinHome() {
assert kotlinHome != null;
return kotlinHome;
public KotlinPaths getKotlinPaths() {
return kotlinPaths;
}
@NotNull
@@ -76,8 +63,8 @@ public final class CompilerEnvironment {
if (output == null) {
messageCollector.report(ERROR, "[Internal Error] No output directory", NO_LOCATION);
}
if (kotlinHome == null) {
messageCollector.report(ERROR, "Cannot find kotlinc home. Make sure plugin is properly installed", NO_LOCATION);
if (!kotlinPaths.getHomePath().exists()) {
messageCollector.report(ERROR, "Cannot find kotlinc home: " + kotlinPaths.getHomePath() + ". Make sure plugin is properly installed", NO_LOCATION);
}
}
@@ -19,6 +19,7 @@ package org.jetbrains.jet.compiler.runner;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.common.messages.*;
import org.jetbrains.jet.utils.KotlinPaths;
import java.io.*;
import java.lang.ref.SoftReference;
@@ -36,8 +37,8 @@ public class CompilerRunnerUtil {
private static SoftReference<URLClassLoader> ourClassLoaderRef = new SoftReference<URLClassLoader>(null);
public static List<File> kompilerClasspath(File kotlinHome, MessageCollector messageCollector) {
File libs = new File(kotlinHome, "lib");
public static List<File> kompilerClasspath(KotlinPaths paths, MessageCollector messageCollector) {
File libs = paths.getLibPath();
if (!libs.exists() || libs.isFile()) {
messageCollector.report(ERROR, "Broken compiler at '" + libs.getAbsolutePath() + "'. Make sure plugin is properly installed", NO_LOCATION);
@@ -49,17 +50,17 @@ public class CompilerRunnerUtil {
return answer;
}
public static URLClassLoader getOrCreateClassLoader(File kotlinHome, MessageCollector messageCollector) {
public static URLClassLoader getOrCreateClassLoader(KotlinPaths paths, MessageCollector messageCollector) {
URLClassLoader answer = ourClassLoaderRef.get();
if (answer == null) {
answer = createClassloader(kotlinHome, messageCollector);
answer = createClassloader(paths, messageCollector);
ourClassLoaderRef = new SoftReference<URLClassLoader>(answer);
}
return answer;
}
private static URLClassLoader createClassloader(File kotlinHome, MessageCollector messageCollector) {
List<File> jars = kompilerClasspath(kotlinHome, messageCollector);
private static URLClassLoader createClassloader(KotlinPaths paths, MessageCollector messageCollector) {
List<File> jars = kompilerClasspath(paths, messageCollector);
URL[] urls = new URL[jars.size()];
for (int i = 0; i < urls.length; i++) {
try {
@@ -90,7 +91,7 @@ public class CompilerRunnerUtil {
public static Object invokeExecMethod(CompilerEnvironment environment,
PrintStream out,
MessageCollector messageCollector, String[] arguments, String name) throws Exception {
URLClassLoader loader = getOrCreateClassLoader(environment.getKotlinHome(), messageCollector);
URLClassLoader loader = getOrCreateClassLoader(environment.getKotlinPaths(), messageCollector);
Class<?> kompiler = Class.forName(name, true, loader);
Method exec = kompiler.getMethod("exec", PrintStream.class, String[].class);
return exec.invoke(kompiler.newInstance(), out, arguments);
@@ -69,7 +69,7 @@ public class KotlinCompilerRunner {
String compilerClassName = "org.jetbrains.jet.cli.jvm.K2JVMCompiler";
String[] arguments = commandLineArguments(environment.getOutput(), scriptFile);
messageCollector.report(CompilerMessageSeverity.INFO,
"Using kotlinHome=" + environment.getKotlinHome(),
"Using kotlinHome=" + environment.getKotlinPaths(),
CompilerMessageLocation.NO_LOCATION);
messageCollector.report(CompilerMessageSeverity.INFO,
"Invoking in-process compiler " + compilerClassName + " with arguments " + Arrays.asList(arguments),
@@ -107,7 +107,7 @@ public class KotlinCompilerRunner {
params.getProgramParametersList().add(arg);
}
for (File jar : CompilerRunnerUtil.kompilerClasspath(environment.getKotlinHome(), messageCollector)) {
for (File jar : CompilerRunnerUtil.kompilerClasspath(environment.getKotlinPaths(), messageCollector)) {
params.getClassPath().add(jar);
}