Revert "ability to compile text and small refactoring"
This reverts commit 4ddf101397.
This commit is contained in:
@@ -17,21 +17,23 @@
|
||||
package org.jetbrains.jet.compiler;
|
||||
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.testFramework.LightVirtualFile;
|
||||
import com.intellij.util.LocalTimeCounter;
|
||||
import com.intellij.psi.JavaPsiFacade;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.util.Processor;
|
||||
import jet.modules.AllModules;
|
||||
import jet.modules.Module;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.codegen.ClassFileFactory;
|
||||
import org.jetbrains.jet.codegen.GeneratedClassLoader;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
|
||||
import org.jetbrains.jet.plugin.JetLanguage;
|
||||
import org.jetbrains.jet.plugin.JetMainDetector;
|
||||
import org.jetbrains.jet.plugin.compiler.PathUtil;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Method;
|
||||
@@ -57,18 +59,10 @@ public class CompileEnvironment {
|
||||
private boolean ignoreErrors = false;
|
||||
|
||||
public CompileEnvironment() {
|
||||
this(FileNameTransformer.IDENTITY, null);
|
||||
this(FileNameTransformer.IDENTITY);
|
||||
}
|
||||
|
||||
public CompileEnvironment(ClassLoader loader) {
|
||||
this(FileNameTransformer.IDENTITY, loader);
|
||||
}
|
||||
|
||||
public CompileEnvironment(FileNameTransformer fileNameTransformer) {
|
||||
this(fileNameTransformer, null);
|
||||
}
|
||||
|
||||
public CompileEnvironment(FileNameTransformer fileNameTransformer, ClassLoader loader) {
|
||||
myRootDisposable = new Disposable() {
|
||||
@Override
|
||||
public void dispose() {
|
||||
@@ -76,10 +70,6 @@ public class CompileEnvironment {
|
||||
};
|
||||
myEnvironment = new JetCoreEnvironment(myRootDisposable);
|
||||
myFileNameTransformer = fileNameTransformer;
|
||||
|
||||
if(loader != null) {
|
||||
myEnvironment.addToClasspathFromClassLoader(loader);
|
||||
}
|
||||
}
|
||||
|
||||
public void setErrorStream(PrintStream errorStream) {
|
||||
@@ -94,6 +84,78 @@ public class CompileEnvironment {
|
||||
Disposer.dispose(myRootDisposable);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getUnpackedRuntimePath() {
|
||||
URL url = CompileEnvironment.class.getClassLoader().getResource("jet/JetObject.class");
|
||||
if (url != null && url.getProtocol().equals("file")) {
|
||||
return new File(url.getPath()).getParentFile().getParentFile();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getRuntimeJarPath() {
|
||||
URL url = CompileEnvironment.class.getClassLoader().getResource("jet/JetObject.class");
|
||||
if (url != null && url.getProtocol().equals("jar")) {
|
||||
String path = url.getPath();
|
||||
return new File(path.substring(path.indexOf(":") + 1, path.indexOf("!/")));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void ensureRuntime() {
|
||||
ensureRuntime(myEnvironment);
|
||||
}
|
||||
|
||||
public static void ensureRuntime(@NotNull JetCoreEnvironment env) {
|
||||
Project project = env.getProject();
|
||||
if (JavaPsiFacade.getInstance(project).findClass("java.lang.Object", GlobalSearchScope.allScope(project)) == null) {
|
||||
// TODO: prepend
|
||||
env.addToClasspath(findRtJar());
|
||||
}
|
||||
|
||||
if (JavaPsiFacade.getInstance(project).findClass("jet.JetObject", GlobalSearchScope.allScope(project)) == null) {
|
||||
// TODO: prepend
|
||||
File kotlin = PathUtil.getDefaultRuntimePath();
|
||||
if (kotlin == null || !kotlin.exists()) {
|
||||
kotlin = getUnpackedRuntimePath();
|
||||
if (kotlin == null) kotlin = getRuntimeJarPath();
|
||||
}
|
||||
if (kotlin == null) {
|
||||
throw new IllegalStateException("kotlin runtime not found");
|
||||
}
|
||||
env.addToClasspath(kotlin);
|
||||
}
|
||||
}
|
||||
|
||||
public static File findRtJar() {
|
||||
String javaHome = System.getProperty("java.home");
|
||||
if ("jre".equals(new File(javaHome).getName())) {
|
||||
javaHome = new File(javaHome).getParent();
|
||||
}
|
||||
|
||||
File rtJar = findRtJar(javaHome);
|
||||
|
||||
if (rtJar == null || !rtJar.exists()) {
|
||||
throw new CompileEnvironmentException("No JDK rt.jar found under" + javaHome);
|
||||
}
|
||||
|
||||
return rtJar;
|
||||
}
|
||||
|
||||
private static File findRtJar(String javaHome) {
|
||||
File rtJar = new File(javaHome, "jre/lib/rt.jar");
|
||||
if (rtJar.exists()) {
|
||||
return rtJar;
|
||||
}
|
||||
|
||||
File classesJar = new File(new File(javaHome).getParentFile().getAbsolutePath(), "Classes/classes.jar");
|
||||
if (classesJar.exists()) {
|
||||
return classesJar;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void compileModuleScript(String moduleFile, @Nullable String jarPath, @Nullable String outputDir, boolean jarRuntime) {
|
||||
final List<Module> modules = loadModuleScript(moduleFile);
|
||||
|
||||
@@ -127,7 +189,7 @@ public class CompileEnvironment {
|
||||
public List<Module> loadModuleScript(String moduleFile) {
|
||||
CompileSession scriptCompileSession = new CompileSession(myEnvironment, myFileNameTransformer);
|
||||
scriptCompileSession.addSources(moduleFile);
|
||||
myEnvironment.ensureRuntime();
|
||||
ensureRuntime();
|
||||
|
||||
if (!scriptCompileSession.analyze(myErrorStream)) {
|
||||
return null;
|
||||
@@ -150,9 +212,8 @@ public class CompileEnvironment {
|
||||
method.setAccessible(true);
|
||||
method.invoke(null);
|
||||
|
||||
ArrayList<Module> modules = AllModules.modules.get();
|
||||
ArrayList<Module> answer = new ArrayList<Module>(modules);
|
||||
modules.clear();
|
||||
ArrayList<Module> answer = new ArrayList<Module>(AllModules.modules);
|
||||
AllModules.modules.clear();
|
||||
return answer;
|
||||
}
|
||||
catch (Exception e) {
|
||||
@@ -186,7 +247,7 @@ public class CompileEnvironment {
|
||||
myEnvironment.addToClasspath(new File(classpathRoot));
|
||||
}
|
||||
|
||||
myEnvironment.ensureRuntime();
|
||||
ensureRuntime();
|
||||
|
||||
if (!moduleCompileSession.analyze(myErrorStream) && !ignoreErrors) {
|
||||
return null;
|
||||
@@ -224,7 +285,7 @@ public class CompileEnvironment {
|
||||
}
|
||||
|
||||
private static void writeRuntimeToJar(final JarOutputStream stream) throws IOException {
|
||||
final File unpackedRuntimePath = JetCoreEnvironment.getUnpackedRuntimePath();
|
||||
final File unpackedRuntimePath = getUnpackedRuntimePath();
|
||||
if (unpackedRuntimePath != null) {
|
||||
FileUtil.processFilesRecursively(unpackedRuntimePath, new Processor<File>() {
|
||||
@Override
|
||||
@@ -247,7 +308,7 @@ public class CompileEnvironment {
|
||||
});
|
||||
}
|
||||
else {
|
||||
File runtimeJarPath = JetCoreEnvironment.getRuntimeJarPath();
|
||||
File runtimeJarPath = getRuntimeJarPath();
|
||||
if (runtimeJarPath != null) {
|
||||
JarInputStream jis = new JarInputStream(new FileInputStream(runtimeJarPath));
|
||||
try {
|
||||
@@ -271,18 +332,6 @@ public class CompileEnvironment {
|
||||
}
|
||||
}
|
||||
|
||||
public ClassLoader compileText(String code) {
|
||||
CompileSession session = new CompileSession(myEnvironment, myFileNameTransformer);
|
||||
session.addSources(new LightVirtualFile("script" + LocalTimeCounter.currentTime() + ".kt", JetLanguage.INSTANCE, code));
|
||||
|
||||
if (!session.analyze(myErrorStream) && !ignoreErrors) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ClassFileFactory factory = session.generate();
|
||||
return new GeneratedClassLoader(factory);
|
||||
}
|
||||
|
||||
public boolean compileBunchOfSources(String sourceFileOrDir, String jar, String outputDir, boolean includeRuntime) {
|
||||
CompileSession session = new CompileSession(myEnvironment, myFileNameTransformer);
|
||||
session.addSources(sourceFileOrDir);
|
||||
@@ -296,7 +345,7 @@ public class CompileEnvironment {
|
||||
}
|
||||
}
|
||||
|
||||
myEnvironment.ensureRuntime();
|
||||
ensureRuntime();
|
||||
|
||||
if (!session.analyze(myErrorStream) && !ignoreErrors) {
|
||||
return false;
|
||||
|
||||
@@ -20,19 +20,12 @@ import com.intellij.core.JavaCoreEnvironment;
|
||||
import com.intellij.lang.java.JavaParserDefinition;
|
||||
import com.intellij.mock.MockApplication;
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.JavaPsiFacade;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.parsing.JetParserDefinition;
|
||||
import org.jetbrains.jet.lang.types.JetStandardLibrary;
|
||||
import org.jetbrains.jet.plugin.JetFileType;
|
||||
import org.jetbrains.jet.plugin.compiler.PathUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -64,74 +57,6 @@ public class JetCoreEnvironment extends JavaCoreEnvironment {
|
||||
JetStandardLibrary.initialize(getProject());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getUnpackedRuntimePath() {
|
||||
URL url = CompileEnvironment.class.getClassLoader().getResource("jet/JetObject.class");
|
||||
if (url != null && url.getProtocol().equals("file")) {
|
||||
return new File(url.getPath()).getParentFile().getParentFile();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getRuntimeJarPath() {
|
||||
URL url = CompileEnvironment.class.getClassLoader().getResource("jet/JetObject.class");
|
||||
if (url != null && url.getProtocol().equals("jar")) {
|
||||
String path = url.getPath();
|
||||
return new File(path.substring(path.indexOf(":") + 1, path.indexOf("!/")));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static File findRtJar() {
|
||||
String javaHome = System.getProperty("java.home");
|
||||
if ("jre".equals(new File(javaHome).getName())) {
|
||||
javaHome = new File(javaHome).getParent();
|
||||
}
|
||||
|
||||
File rtJar = findRtJar(javaHome);
|
||||
|
||||
if (rtJar == null || !rtJar.exists()) {
|
||||
throw new CompileEnvironmentException("No JDK rt.jar found under" + javaHome);
|
||||
}
|
||||
|
||||
return rtJar;
|
||||
}
|
||||
|
||||
private static File findRtJar(String javaHome) {
|
||||
File rtJar = new File(javaHome, "jre/lib/rt.jar");
|
||||
if (rtJar.exists()) {
|
||||
return rtJar;
|
||||
}
|
||||
|
||||
File classesJar = new File(new File(javaHome).getParentFile().getAbsolutePath(), "Classes/classes.jar");
|
||||
if (classesJar.exists()) {
|
||||
return classesJar;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void ensureRuntime() {
|
||||
Project project = getProject();
|
||||
if (JavaPsiFacade.getInstance(project).findClass("java.lang.Object", GlobalSearchScope.allScope(project)) == null) {
|
||||
// TODO: prepend
|
||||
addToClasspath(findRtJar());
|
||||
}
|
||||
|
||||
if (JavaPsiFacade.getInstance(project).findClass("jet.JetObject", GlobalSearchScope.allScope(project)) == null) {
|
||||
// TODO: prepend
|
||||
File kotlin = PathUtil.getDefaultRuntimePath();
|
||||
if (kotlin == null || !kotlin.exists()) {
|
||||
kotlin = getUnpackedRuntimePath();
|
||||
if (kotlin == null) kotlin = getRuntimeJarPath();
|
||||
}
|
||||
if (kotlin == null) {
|
||||
throw new IllegalStateException("kotlin runtime not found");
|
||||
}
|
||||
addToClasspath(kotlin);
|
||||
}
|
||||
}
|
||||
|
||||
public MockApplication getApplication() {
|
||||
return myApplication;
|
||||
}
|
||||
@@ -143,18 +68,4 @@ public class JetCoreEnvironment extends JavaCoreEnvironment {
|
||||
public void setCompilerPlugins(List<CompilerPlugin> compilerPlugins) {
|
||||
this.compilerPlugins = compilerPlugins;
|
||||
}
|
||||
|
||||
public void addToClasspathFromClassLoader(ClassLoader loader) {
|
||||
ClassLoader parent = loader.getParent();
|
||||
if(parent != null)
|
||||
addToClasspathFromClassLoader(parent);
|
||||
|
||||
if(loader instanceof URLClassLoader) {
|
||||
for (URL url : ((URLClassLoader) loader).getURLs()) {
|
||||
File file = new File(url.getPath());
|
||||
if(!file.isFile() || file.getPath().endsWith(".jar"))
|
||||
addToClasspath(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user