invoke translating compilers per-module chunk
[fedora-idea.git] / plugins / groovy / src / org / jetbrains / plugins / groovy / compiler / generator / GroovycStubGenerator.java
blobb44e9462b2d82edecfb097b2319a0372c4f74228
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.jetbrains.plugins.groovy.compiler.generator;
19 import com.intellij.compiler.impl.TranslatingCompilerFilesMonitor;
20 import com.intellij.openapi.application.Result;
21 import com.intellij.openapi.command.WriteCommandAction;
22 import com.intellij.openapi.compiler.CompileContext;
23 import com.intellij.openapi.compiler.CompileScope;
24 import com.intellij.openapi.compiler.CompilerPaths;
25 import com.intellij.openapi.compiler.ex.CompileContextEx;
26 import com.intellij.openapi.compiler.options.ExcludedEntriesConfiguration;
27 import com.intellij.openapi.diagnostic.Logger;
28 import com.intellij.openapi.fileTypes.StdFileTypes;
29 import com.intellij.openapi.module.Module;
30 import com.intellij.openapi.project.Project;
31 import com.intellij.openapi.util.io.FileUtil;
32 import com.intellij.openapi.vfs.LocalFileSystem;
33 import com.intellij.openapi.vfs.VfsUtil;
34 import com.intellij.openapi.vfs.VirtualFile;
35 import com.intellij.util.Chunk;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.plugins.groovy.GroovyFileType;
38 import org.jetbrains.plugins.groovy.compiler.GroovyCompilerBase;
39 import org.jetbrains.plugins.groovy.compiler.GroovyCompilerConfiguration;
41 import java.io.File;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.List;
46 /**
47 * @author peter
49 public class GroovycStubGenerator extends GroovyCompilerBase {
50 private static final Logger LOG = Logger.getInstance("#org.jetbrains.plugins.groovy.compiler.generator.GroovycStubGenerator");
52 public GroovycStubGenerator(Project project) {
53 super(project);
56 @Override
57 public void compile(CompileContext compileContext, Chunk<Module> moduleChunk, VirtualFile[] virtualFiles, OutputSink sink) {
58 final CompileScope scope = compileContext.getCompileScope();
59 if (scope.getFiles(StdFileTypes.JAVA, true).length == 0) {
60 return;
63 boolean hasGroovy = false;
64 final ExcludedEntriesConfiguration excluded = GroovyCompilerConfiguration.getExcludeConfiguration(myProject);
65 List<VirtualFile> total = new ArrayList<VirtualFile>();
66 for (final VirtualFile virtualFile : virtualFiles) {
67 if (!excluded.isExcluded(virtualFile)) {
68 total.add(virtualFile);
69 if (virtualFile.getFileType() == GroovyFileType.GROOVY_FILE_TYPE) {
70 hasGroovy = true;
75 if (!hasGroovy) {
76 return;
79 super.compile(compileContext, moduleChunk, total.toArray(new VirtualFile[total.size()]), sink);
82 @Override
83 protected void compileFiles(CompileContext compileContext, Module module,
84 final List<VirtualFile> toCompile, OutputSink sink, boolean tests) {
85 boolean hasGroovy = false;
86 boolean hasJava = false;
87 for (final VirtualFile file : toCompile) {
88 if (file.getFileType() == StdFileTypes.JAVA) {
89 hasJava = true;
91 if (file.getFileType() == GroovyFileType.GROOVY_FILE_TYPE) {
92 hasGroovy = true;
96 if (!hasGroovy) {
97 return;
100 final String rootPath = CompilerPaths.getGeneratedDataDirectory(myProject) + "/groovyStubs/";
101 final File outDir = new File(rootPath + myProject.getLocationHash() + "/" + module.getName() + "/" + (tests ? "tests" : "production") + "/");
102 outDir.mkdirs();
104 if (!hasJava) {
105 //always pass groovyc stub generator at least 1 java file, or it won't generate stubs
106 //todo not needed anymore with groovy 1.7?
107 toCompile.add(createMockJavaFile(rootPath));
110 final VirtualFile tempOutput = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(outDir);
111 assert tempOutput != null;
112 cleanDirectory(tempOutput);
114 ((CompileContextEx)compileContext).assignModule(tempOutput, module, tests);
116 runGroovycCompiler(compileContext, module, toCompile, true, tempOutput, sink, tests);
119 private VirtualFile createMockJavaFile(final String rootPath) {
120 return new WriteCommandAction<VirtualFile>(myProject) {
121 protected void run(Result<VirtualFile> result) throws Throwable {
122 final VirtualFile root = LocalFileSystem.getInstance().refreshAndFindFileByPath(FileUtil.toSystemIndependentName(rootPath));
123 final String sampleClassName = "$$$VeryEmptyJavaClass$$$";
124 assert root != null;
125 final VirtualFile sampleClass = root.findOrCreateChildData(this, sampleClassName + ".java");
126 VfsUtil.saveText(sampleClass, "public class " + sampleClassName + " {}");
127 result.setResult(sampleClass);
129 }.execute().getResultObject();
132 private void cleanDirectory(final VirtualFile dir) {
133 new WriteCommandAction(myProject) {
134 protected void run(Result result) throws Throwable {
135 deleteChildrenRecursively(dir);
138 private void deleteChildrenRecursively(final VirtualFile dir) throws IOException {
139 for (final VirtualFile child : dir.getChildren()) {
140 if (child.isDirectory()) {
141 deleteChildrenRecursively(child);
143 TranslatingCompilerFilesMonitor.removeSourceInfo(child);
144 child.delete(this);
147 }.execute();
150 @Override
151 public boolean isCompilableFile(VirtualFile file, CompileContext context) {
152 return super.isCompilableFile(file, context) || StdFileTypes.JAVA == file.getFileType();
155 @NotNull
156 public String getDescription() {
157 return "Groovy to java source code generator";
160 public boolean validateConfiguration(CompileScope scope) {
161 return true;