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
.config
;
19 import com
.intellij
.openapi
.application
.ApplicationManager
;
20 import com
.intellij
.openapi
.module
.Module
;
21 import com
.intellij
.openapi
.module
.ModuleUtil
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.roots
.LibraryOrderEntry
;
24 import com
.intellij
.openapi
.roots
.ModifiableRootModel
;
25 import com
.intellij
.openapi
.roots
.ModuleRootManager
;
26 import com
.intellij
.openapi
.roots
.OrderRootType
;
27 import com
.intellij
.openapi
.roots
.libraries
.Library
;
28 import com
.intellij
.openapi
.ui
.Messages
;
29 import com
.intellij
.openapi
.util
.Ref
;
30 import com
.intellij
.openapi
.vfs
.VirtualFile
;
31 import com
.intellij
.psi
.PsiElement
;
32 import com
.intellij
.util
.Function
;
33 import com
.intellij
.util
.containers
.ContainerUtil
;
34 import org
.jetbrains
.annotations
.NonNls
;
35 import org
.jetbrains
.annotations
.NotNull
;
36 import org
.jetbrains
.annotations
.Nullable
;
37 import org
.jetbrains
.plugins
.groovy
.GroovyBundle
;
38 import org
.jetbrains
.plugins
.groovy
.GroovyIcons
;
39 import org
.jetbrains
.plugins
.groovy
.util
.GroovyUtils
;
40 import org
.jetbrains
.plugins
.groovy
.util
.LibrariesUtil
;
42 import java
.util
.Collection
;
47 public abstract class GroovyConfigUtils
extends AbstractConfigUtils
{
48 @NonNls public static final String GROOVY_ALL_JAR_PATTERN
= "groovy-all-(.*)\\.jar";
50 private static GroovyConfigUtils myGroovyConfigUtils
;
51 @NonNls public static final String GROOVY_JAR_PATTERN
= "groovy-(\\d.*)\\.jar";
52 public static final String NO_VERSION
= "<no version>";
54 private GroovyConfigUtils() {
57 public static GroovyConfigUtils
getInstance() {
58 if (myGroovyConfigUtils
== null) {
59 myGroovyConfigUtils
= new GroovyConfigUtils() {
61 STARTER_SCRIPT_FILE_NAME
= "groovy";
64 return myGroovyConfigUtils
;
68 public String
getSDKVersion(@NotNull final String path
) {
69 String groovyJarVersion
= getSDKJarVersion(path
+ "/lib", GROOVY_JAR_PATTERN
, MANIFEST_PATH
);
70 if (groovyJarVersion
!= null) {
71 return groovyJarVersion
;
74 groovyJarVersion
= getSDKJarVersion(path
+ "/lib", GROOVY_ALL_JAR_PATTERN
, MANIFEST_PATH
);
75 if (groovyJarVersion
!= null) {
76 return groovyJarVersion
;
79 return UNDEFINED_VERSION
;
82 public boolean isSDKLibrary(Library library
) {
83 if (library
== null) return false;
84 return isGroovyLibrary(library
.getFiles(OrderRootType
.CLASSES
));
87 public static boolean isGroovyLibrary(VirtualFile
[] classFiles
) {
88 for (VirtualFile file
: classFiles
) {
89 if (isAnyGroovyJar(file
.getName())) {
96 public String
getSDKVersion(@NotNull Library library
) {
97 return getSDKVersion(LibrariesUtil
.getGroovyLibraryHome(library
));
100 public static boolean isAnyGroovyJar(@NonNls final String name
) {
101 return name
.matches(GROOVY_ALL_JAR_PATTERN
) || name
.matches(GROOVY_JAR_PATTERN
);
105 public String
getSDKVersion(@NotNull Module module
) {
106 final String path
= LibrariesUtil
.getGroovyHomePath(module
);
107 if (path
== null) return null;
108 return getSDKVersion(path
);
111 public boolean isAtLeastGroovy1_7(Module module
) {
112 if (module
== null) return false;
113 final String version
= getSDKVersion(module
);
114 if (version
== null) return false;
115 return version
.compareTo("1.7") >= 0;
118 public boolean isAtLeastGroovy1_7(PsiElement psiElement
) {
119 return isAtLeastGroovy1_7(ModuleUtil
.findModuleForPsiElement(psiElement
));
123 public String
getSDKVersion(PsiElement psiElement
) {
124 final Module module
= ModuleUtil
.findModuleForPsiElement(psiElement
);
125 if (module
== null) {
128 final String s
= getSDKVersion(module
);
129 return s
!= null ? s
: NO_VERSION
;
134 public String
getSDKInstallPath(Module module
) {
135 if (module
== null) return "";
136 Library
[] libraries
= getSDKLibrariesByModule(module
);
137 if (libraries
.length
== 0) return "";
138 Library library
= libraries
[0];
139 return LibrariesUtil
.getGroovyLibraryHome(library
);
143 public boolean isSDKHome(VirtualFile file
) {
144 if (file
!= null && file
.isDirectory()) {
145 final String path
= file
.getPath();
146 if (GroovyUtils
.getFilesInDirectoryByPattern(path
+ "/lib", GROOVY_JAR_PATTERN
).length
> 0) {
149 if (GroovyUtils
.getFilesInDirectoryByPattern(path
+ "/embeddable", GROOVY_ALL_JAR_PATTERN
).length
> 0) {
156 public boolean tryToSetUpGroovyFacetOntheFly(final Module module
) {
157 final Project project
= module
.getProject();
158 final Library
[] libraries
= getAllSDKLibraries(project
);
159 if (libraries
.length
> 0) {
160 final Library library
= libraries
[0];
161 int result
= Messages
162 .showOkCancelDialog(GroovyBundle
.message("groovy.like.library.found.text", module
.getName(), library
.getName(), getSDKLibVersion(library
)),
163 GroovyBundle
.message("groovy.like.library.found"), GroovyIcons
.GROOVY_ICON_32x32
);
164 final Ref
<Boolean
> ref
= new Ref
<Boolean
>();
167 ApplicationManager
.getApplication().runWriteAction(new Runnable() {
169 final ModifiableRootModel model
= ModuleRootManager
.getInstance(module
).getModifiableModel();
170 LibraryOrderEntry entry
= model
.addLibraryEntry(libraries
[0]);
171 LibrariesUtil
.placeEntryToCorrectPlace(model
, entry
);
177 return ref
.get().booleanValue();
183 public String
getSDKLibVersion(Library library
) {
184 return getSDKVersion(LibrariesUtil
.getGroovyLibraryHome(library
));
187 public Collection
<String
> getSDKVersions(final Project project
) {
188 return ContainerUtil
.map2List(getAllSDKLibraries(project
), new Function
<Library
, String
>() {
189 public String
fun(Library library
) {
190 return getSDKLibVersion(library
);