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
.fileTypes
.StdFileTypes
;
28 import com
.intellij
.openapi
.module
.Module
;
29 import com
.intellij
.openapi
.module
.ModuleUtil
;
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
;
42 import java
.io
.IOException
;
43 import java
.util
.ArrayList
;
44 import java
.util
.List
;
49 public class GroovycStubGenerator
extends GroovyCompilerBase
{
51 public GroovycStubGenerator(Project project
) {
56 public void compile(CompileContext compileContext
, Chunk
<Module
> moduleChunk
, VirtualFile
[] virtualFiles
, OutputSink sink
) {
57 if (!GroovyCompilerConfiguration
.getInstance(myProject
).isUseGroovycStubs()) {
61 final CompileScope scope
= compileContext
.getCompileScope();
62 final VirtualFile
[] javaFiles
= scope
.getFiles(StdFileTypes
.JAVA
, true);
63 if (javaFiles
.length
== 0) {
67 boolean hasJava
= false;
68 for (VirtualFile javaFile
: javaFiles
) {
69 final Module module
= ModuleUtil
.findModuleForFile(javaFile
, myProject
);
70 if (module
!= null && moduleChunk
.containsNode(module
)) {
80 boolean hasGroovy
= false;
81 final ExcludedEntriesConfiguration excluded
= GroovyCompilerConfiguration
.getExcludeConfiguration(myProject
);
82 List
<VirtualFile
> total
= new ArrayList
<VirtualFile
>();
83 for (final VirtualFile virtualFile
: virtualFiles
) {
84 if (!excluded
.isExcluded(virtualFile
)) {
85 total
.add(virtualFile
);
86 if (virtualFile
.getFileType() == GroovyFileType
.GROOVY_FILE_TYPE
) {
96 super.compile(compileContext
, moduleChunk
, VfsUtil
.toVirtualFileArray(total
), sink
);
100 protected void compileFiles(CompileContext compileContext
, Module module
,
101 final List
<VirtualFile
> toCompile
, OutputSink sink
, boolean tests
) {
102 boolean hasGroovy
= false;
103 boolean hasJava
= false;
104 for (final VirtualFile file
: toCompile
) {
105 if (file
.getFileType() == StdFileTypes
.JAVA
) {
108 if (file
.getFileType() == GroovyFileType
.GROOVY_FILE_TYPE
) {
117 final String rootPath
= CompilerPaths
.getGeneratedDataDirectory(myProject
) + "/groovyStubs/";
118 final File outDir
= new File(rootPath
+ myProject
.getLocationHash() + "/" + module
.getName() + "/" + (tests ?
"tests" : "production") + "/");
122 //always pass groovyc stub generator at least 1 java file, or it won't generate stubs
123 toCompile
.add(createMockJavaFile(rootPath
));
126 final VirtualFile tempOutput
= LocalFileSystem
.getInstance().refreshAndFindFileByIoFile(outDir
);
127 assert tempOutput
!= null;
128 cleanDirectory(tempOutput
);
130 ((CompileContextEx
)compileContext
).assignModule(tempOutput
, module
, tests
);
132 runGroovycCompiler(compileContext
, module
, toCompile
, true, tempOutput
, sink
, tests
);
135 private VirtualFile
createMockJavaFile(final String rootPath
) {
136 return new WriteCommandAction
<VirtualFile
>(myProject
) {
137 protected void run(Result
<VirtualFile
> result
) throws Throwable
{
138 final VirtualFile root
= LocalFileSystem
.getInstance().refreshAndFindFileByPath(FileUtil
.toSystemIndependentName(rootPath
));
139 final String sampleClassName
= "$$$VeryEmptyJavaClass$$$";
141 final VirtualFile sampleClass
= root
.findOrCreateChildData(this, sampleClassName
+ ".java");
142 VfsUtil
.saveText(sampleClass
, "public class " + sampleClassName
+ " {}");
143 result
.setResult(sampleClass
);
145 }.execute().getResultObject();
148 private void cleanDirectory(final VirtualFile dir
) {
149 new WriteCommandAction(myProject
) {
150 protected void run(Result result
) throws Throwable
{
151 deleteChildrenRecursively(dir
);
154 private void deleteChildrenRecursively(final VirtualFile dir
) throws IOException
{
155 for (final VirtualFile child
: dir
.getChildren()) {
156 if (child
.isDirectory()) {
157 deleteChildrenRecursively(child
);
159 TranslatingCompilerFilesMonitor
.removeSourceInfo(child
);
163 catch (IOException ignored
) {
164 //may be a leaked handle from some non-completely terminated compiler process, or compiler caches, or something else
165 //not a big deal, we'll delete it next time
173 public boolean isCompilableFile(VirtualFile file
, CompileContext context
) {
174 return super.isCompilableFile(file
, context
) || StdFileTypes
.JAVA
== file
.getFileType();
178 public String
getDescription() {
179 return "Groovy to java source code generator";
182 public boolean validateConfiguration(CompileScope scope
) {