NPE, if there were errors reported
This commit is contained in:
@@ -144,15 +144,17 @@ public class CompileEnvironment {
|
|||||||
final String directory = new File(moduleFile).getParent();
|
final String directory = new File(moduleFile).getParent();
|
||||||
for (Module moduleBuilder : modules) {
|
for (Module moduleBuilder : modules) {
|
||||||
ClassFileFactory moduleFactory = compileModule(moduleBuilder, directory);
|
ClassFileFactory moduleFactory = compileModule(moduleBuilder, directory);
|
||||||
if (outputDir != null) {
|
if (moduleFactory != null) {
|
||||||
writeToOutputDirectory(moduleFactory, outputDir);
|
if (outputDir != null) {
|
||||||
}
|
writeToOutputDirectory(moduleFactory, outputDir);
|
||||||
else {
|
}
|
||||||
String path = jarPath != null ? jarPath : new File(directory, moduleBuilder.getModuleName() + ".jar").getPath();
|
else {
|
||||||
try {
|
String path = jarPath != null ? jarPath : new File(directory, moduleBuilder.getModuleName() + ".jar").getPath();
|
||||||
writeToJar(moduleFactory, new FileOutputStream(path), null, jarRuntime);
|
try {
|
||||||
} catch (FileNotFoundException e) {
|
writeToJar(moduleFactory, new FileOutputStream(path), null, jarRuntime);
|
||||||
throw new CompileEnvironmentException("Invalid jar path " + path, e);
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new CompileEnvironmentException("Invalid jar path " + path, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,31 +1,38 @@
|
|||||||
package org.jetbrains.jet.plugin.compiler;
|
package org.jetbrains.jet.plugin.compiler;
|
||||||
|
|
||||||
import com.google.common.base.Predicates;
|
import com.intellij.compiler.impl.javaCompiler.ModuleChunk;
|
||||||
import com.google.common.collect.Lists;
|
import com.intellij.execution.configurations.GeneralCommandLine;
|
||||||
import com.intellij.openapi.application.ApplicationManager;
|
import com.intellij.execution.configurations.SimpleJavaParameters;
|
||||||
|
import com.intellij.execution.process.OSProcessHandler;
|
||||||
|
import com.intellij.execution.process.ProcessAdapter;
|
||||||
|
import com.intellij.execution.process.ProcessEvent;
|
||||||
import com.intellij.openapi.compiler.CompileContext;
|
import com.intellij.openapi.compiler.CompileContext;
|
||||||
import com.intellij.openapi.compiler.CompileScope;
|
import com.intellij.openapi.compiler.CompileScope;
|
||||||
import com.intellij.openapi.compiler.CompilerMessageCategory;
|
import com.intellij.openapi.compiler.CompilerMessageCategory;
|
||||||
import com.intellij.openapi.compiler.TranslatingCompiler;
|
import com.intellij.openapi.compiler.TranslatingCompiler;
|
||||||
|
import com.intellij.openapi.compiler.ex.CompileContextEx;
|
||||||
import com.intellij.openapi.editor.Document;
|
import com.intellij.openapi.editor.Document;
|
||||||
import com.intellij.openapi.module.Module;
|
import com.intellij.openapi.module.Module;
|
||||||
|
import com.intellij.openapi.projectRoots.JavaSdkType;
|
||||||
|
import com.intellij.openapi.projectRoots.JdkUtil;
|
||||||
|
import com.intellij.openapi.projectRoots.Sdk;
|
||||||
|
import com.intellij.openapi.projectRoots.SimpleJavaSdkType;
|
||||||
|
import com.intellij.openapi.util.Key;
|
||||||
import com.intellij.openapi.util.TextRange;
|
import com.intellij.openapi.util.TextRange;
|
||||||
import com.intellij.openapi.util.io.FileUtil;
|
import com.intellij.openapi.util.io.FileUtil;
|
||||||
import com.intellij.openapi.vfs.VirtualFile;
|
import com.intellij.openapi.vfs.VirtualFile;
|
||||||
import com.intellij.psi.PsiFile;
|
import com.intellij.psi.PsiFile;
|
||||||
import com.intellij.psi.PsiManager;
|
|
||||||
import com.intellij.util.Chunk;
|
import com.intellij.util.Chunk;
|
||||||
|
import com.intellij.util.SystemProperties;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.codegen.*;
|
import org.jetbrains.jet.codegen.CompilationException;
|
||||||
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
|
|
||||||
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
||||||
import org.jetbrains.jet.lang.psi.JetFile;
|
|
||||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
|
||||||
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
|
|
||||||
import org.jetbrains.jet.plugin.JetFileType;
|
import org.jetbrains.jet.plugin.JetFileType;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static com.intellij.openapi.compiler.CompilerMessageCategory.ERROR;
|
import static com.intellij.openapi.compiler.CompilerMessageCategory.ERROR;
|
||||||
@@ -61,96 +68,105 @@ public class JetCompiler implements TranslatingCompiler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ApplicationManager.getApplication().runReadAction(new Runnable() {
|
File kotlinHome = PathUtil.getDefaultCompilerPath();
|
||||||
@Override
|
if (kotlinHome == null) {
|
||||||
public void run() {
|
compileContext.addMessage(ERROR, "Cannot file kotlinc home. Make sure plugin is properly installed", "", -1, -1);
|
||||||
VirtualFile[] allFiles = compileContext.getCompileScope().getFiles(null, true);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
StringBuilder script = new StringBuilder();
|
||||||
|
|
||||||
GenerationState generationState = new GenerationState(compileContext.getProject(), ClassBuilderFactory.BINARIES);
|
script.append("import kotlin.modules.*\n");
|
||||||
List<JetFile> files = Lists.newArrayList();
|
script.append("fun project() {\n");
|
||||||
for (VirtualFile virtualFile : allFiles) {
|
script.append("module(\"" + moduleChunk.getNodes().iterator().next().getName() + "\") {\n");
|
||||||
PsiFile psiFile = PsiManager.getInstance(compileContext.getProject()).findFile(virtualFile);
|
|
||||||
if (psiFile instanceof JetFile) {
|
|
||||||
files.add(((JetFile) psiFile));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BindingContext bindingContext =
|
for (VirtualFile sourceFile : virtualFiles) {
|
||||||
AnalyzerFacade.analyzeFilesWithJavaIntegration(compileContext.getProject(), files, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY);
|
script.append("sources += \"" + path(sourceFile) + "\"\n");
|
||||||
|
}
|
||||||
|
|
||||||
boolean errors = false;
|
ModuleChunk chunk = new ModuleChunk((CompileContextEx) compileContext, moduleChunk, Collections.<Module, List<VirtualFile>>emptyMap());
|
||||||
for (Diagnostic diagnostic : bindingContext.getDiagnostics()) {
|
|
||||||
switch (diagnostic.getSeverity()) {
|
// TODO: have a bootclasspath in script API
|
||||||
case ERROR:
|
for (VirtualFile root : chunk.getCompilationBootClasspathFiles()) {
|
||||||
errors = true;
|
script.append("classpath += \"" + path(root) + "\"\n");
|
||||||
report(diagnostic, CompilerMessageCategory.ERROR, compileContext);
|
}
|
||||||
break;
|
|
||||||
case INFO:
|
for (VirtualFile root : chunk.getCompilationClasspathFiles()) {
|
||||||
report(diagnostic, CompilerMessageCategory.INFORMATION, compileContext);
|
script.append("classpath += \"" + path(root) + "\"\n");
|
||||||
break;
|
}
|
||||||
case WARNING:
|
|
||||||
report(diagnostic, CompilerMessageCategory.WARNING, compileContext);
|
script.append("}\n");
|
||||||
break;
|
script.append("}\n");
|
||||||
}
|
|
||||||
}
|
File scriptFile = new File(path(outputDir), "script.kts");
|
||||||
|
try {
|
||||||
if (!errors) {
|
FileUtil.writeToFile(scriptFile, script.toString());
|
||||||
generationState.compileCorrectFiles(bindingContext, files, new CompilationErrorHandler() {
|
} catch (IOException e) {
|
||||||
@Override
|
compileContext.addMessage(ERROR, "[Internal Error] Cannot write script to " + scriptFile.getAbsolutePath(), "", -1, -1);
|
||||||
public void reportException(Throwable exception, String fileUrl) {
|
return;
|
||||||
if (exception instanceof CompilationException) {
|
}
|
||||||
report((CompilationException) exception, compileContext);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
compileContext.addMessage(CompilerMessageCategory.ERROR, exception.getClass().getCanonicalName() + ": " + exception.getMessage(), fileUrl, 0, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
///////////
|
|
||||||
// GenerationState generationState2 = new GenerationState(compileContext.getProject(), ClassBuilderFactory.TEXT);
|
|
||||||
// generationState2.compileCorrectFiles(bindingContext, namespaces);
|
|
||||||
// StringBuilder answer = new StringBuilder();
|
|
||||||
//
|
|
||||||
// final ClassFileFactory factory2 = generationState2.getFactory();
|
|
||||||
// List<String> files2 = factory2.files();
|
|
||||||
// for (String file : files2) {
|
|
||||||
// answer.append("@").append(file).append('\n');
|
|
||||||
// answer.append(factory2.asText(file));
|
|
||||||
// }
|
|
||||||
// System.out.println(answer.toString());
|
|
||||||
///////////
|
|
||||||
|
|
||||||
|
|
||||||
final ClassFileFactory factory = generationState.getFactory();
|
final SimpleJavaParameters params = new SimpleJavaParameters();
|
||||||
List<String> filesNames = factory.files();
|
params.setJdk(new SimpleJavaSdkType().createJdk("tmp", SystemProperties.getJavaHome()));
|
||||||
for (String file : filesNames) {
|
params.setMainClass("org.jetbrains.jet.cli.KotlinCompiler");
|
||||||
File target = new File(outputDir.getPath(), file);
|
params.getProgramParametersList().add("-module", scriptFile.getAbsolutePath());
|
||||||
try {
|
params.getProgramParametersList().add("-output", path(outputDir));
|
||||||
FileUtil.writeToFile(target, factory.asBytes(file));
|
|
||||||
} catch (IOException e) {
|
File libs = new File(kotlinHome, "lib");
|
||||||
compileContext.addMessage(ERROR, e.getMessage(), null, 0, 0);
|
|
||||||
}
|
if (!libs.exists() || libs.isFile()) {
|
||||||
}
|
compileContext.addMessage(ERROR, "Broken compiler at '" + libs.getAbsolutePath() + "'. Make sure plugin is properly installed", "", -1, -1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
File[] jars = libs.listFiles();
|
||||||
|
if (jars != null) {
|
||||||
|
for (File jar : jars) {
|
||||||
|
if (jar.isFile() && jar.getName().endsWith(".jar")) {
|
||||||
|
params.getClassPath().add(jar);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
// Map<Module, ModuleCompileState> moduleMap = new HashMap<Module, ModuleCompileState>();
|
params.getVMParametersList().addParametersString("-Djava.awt.headless=true -Xmx512m");
|
||||||
//
|
|
||||||
// for (VirtualFile virtualFile : virtualFiles) {
|
|
||||||
// Module module = compileContext.getModuleByFile(virtualFile);
|
Sdk sdk = params.getJdk();
|
||||||
// ModuleCompileState state = moduleMap.get(module);
|
|
||||||
// if (state == null) {
|
|
||||||
// state = new ModuleCompileState(compileContext, module, outputSink);
|
|
||||||
// moduleMap.put(module, state);
|
|
||||||
// }
|
|
||||||
// state.compile(virtualFile);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// for (ModuleCompileState state : moduleMap.values()) {
|
|
||||||
// state.done();
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
final GeneralCommandLine commandLine = JdkUtil.setupJVMCommandLine(
|
||||||
|
((JavaSdkType) sdk.getSdkType()).getVMExecutablePath(sdk), params, false);
|
||||||
|
try {
|
||||||
|
final OSProcessHandler processHandler = new OSProcessHandler(commandLine.createProcess(), commandLine.getCommandLineString()) {
|
||||||
|
@Override
|
||||||
|
public Charset getCharset() {
|
||||||
|
return commandLine.getCharset();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
processHandler.addProcessListener(new ProcessAdapter() {
|
||||||
|
@Override
|
||||||
|
public void onTextAvailable(ProcessEvent event, Key outputType) {
|
||||||
|
System.out.println(event.getText());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
processHandler.startNotify();
|
||||||
|
processHandler.waitFor();
|
||||||
|
} catch (Exception e) {
|
||||||
|
compileContext.addMessage(ERROR, "[Internal Error] " + e.getLocalizedMessage(), "", -1, -1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String path(VirtualFile root) {
|
||||||
|
String path = root.getPath();
|
||||||
|
if (path.endsWith("!/")) {
|
||||||
|
return path.substring(0, path.length() - 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void report(Diagnostic diagnostic, CompilerMessageCategory severity, CompileContext compileContext) {
|
private void report(Diagnostic diagnostic, CompilerMessageCategory severity, CompileContext compileContext) {
|
||||||
|
|||||||
Reference in New Issue
Block a user