IDEA-133078 Gradle: build script classpath resolve sometimes require toplevel(for...
[idea/community.git] / plugins / gradle / tooling-extension-impl / src / org / jetbrains / plugins / gradle / tooling / builder / ModelBuildScriptClasspathBuilderImpl.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.jetbrains.plugins.gradle.tooling.builder;
17
18 import org.gradle.api.Project;
19 import org.gradle.api.artifacts.Configuration;
20 import org.gradle.api.artifacts.ConfigurationContainer;
21 import org.gradle.plugins.ide.idea.IdeaPlugin;
22 import org.gradle.plugins.ide.idea.model.Dependency;
23 import org.gradle.plugins.ide.idea.model.IdeaModule;
24 import org.gradle.plugins.ide.idea.model.ModuleLibrary;
25 import org.gradle.plugins.ide.idea.model.Path;
26 import org.gradle.util.GradleVersion;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29 import org.jetbrains.plugins.gradle.model.BuildScriptClasspathModel;
30 import org.jetbrains.plugins.gradle.model.ClasspathEntryModel;
31 import org.jetbrains.plugins.gradle.tooling.ErrorMessageBuilder;
32 import org.jetbrains.plugins.gradle.tooling.ModelBuilderService;
33 import org.jetbrains.plugins.gradle.tooling.internal.BuildScriptClasspathModelImpl;
34 import org.jetbrains.plugins.gradle.tooling.internal.ClasspathEntryModelImpl;
35
36 import java.io.File;
37 import java.util.*;
38 import java.util.concurrent.ConcurrentHashMap;
39
40 /**
41  * @author Vladislav.Soroka
42  * @since 12/20/13
43  */
44 public class ModelBuildScriptClasspathBuilderImpl implements ModelBuilderService {
45
46   private static final String COMPILE_SCOPE = "COMPILE";
47   private static final String PLUS_CONFIGURATION = "plus";
48   private static final String MINUS_CONFIGURATION = "minus";
49   private static final String CLASSPATH_CONFIGURATION_NAME = "classpath";
50   private final Map<String, BuildScriptClasspathModelImpl> cache = new ConcurrentHashMap<String, BuildScriptClasspathModelImpl>();
51
52   @Override
53   public boolean canBuild(String modelName) {
54     return BuildScriptClasspathModel.class.getName().equals(modelName);
55   }
56
57   @Nullable
58   @Override
59   public Object buildAll(final String modelName, final Project project) {
60     BuildScriptClasspathModelImpl buildScriptClasspath = cache.get(project.getPath());
61     if (buildScriptClasspath != null) return buildScriptClasspath;
62
63     buildScriptClasspath = new BuildScriptClasspathModelImpl();
64     final File gradleHomeDir = project.getGradle().getGradleHomeDir();
65     buildScriptClasspath.setGradleHomeDir(gradleHomeDir);
66     buildScriptClasspath.setGradleVersion(GradleVersion.current().getVersion());
67
68     final IdeaPlugin ideaPlugin = project.getPlugins().getPlugin(IdeaPlugin.class);
69     if (ideaPlugin != null) {
70       Project parent = project.getParent();
71       if (parent != null) {
72         BuildScriptClasspathModelImpl parentBuildScriptClasspath = (BuildScriptClasspathModelImpl)buildAll(modelName, parent);
73         if (parentBuildScriptClasspath != null) {
74           for (ClasspathEntryModel classpathEntryModel : parentBuildScriptClasspath.getClasspath()) {
75             buildScriptClasspath.add(classpathEntryModel);
76           }
77         }
78       }
79       Configuration classpathConfiguration = project.getBuildscript().getConfigurations().findByName(CLASSPATH_CONFIGURATION_NAME);
80       if (classpathConfiguration == null) return null;
81
82       final Configuration configuration;
83       final IdeaModule ideaModule = ideaPlugin.getModel().getModule();
84       final ConfigurationContainer configurations = ideaModule.getProject().getConfigurations();
85
86       if (classpathConfiguration.getState() == Configuration.State.UNRESOLVED) {
87         configuration = classpathConfiguration;
88         configurations.add(configuration);
89       }
90       else {
91         String confName = project.getPath() + ":" + classpathConfiguration.getName();
92         if(configurations.findByName(confName) != null) {
93           confName += (":" + UUID.randomUUID().toString());
94         }
95
96         configuration = configurations.maybeCreate(confName);
97         configuration.getDependencies().addAll(classpathConfiguration.getAllDependencies());
98         configuration.getArtifacts().addAll(classpathConfiguration.getAllArtifacts());
99         project.getRepositories().addAll(project.getBuildscript().getRepositories());
100       }
101
102       Collection<Configuration> plusConfigurations = Collections.singletonList(configuration);
103
104       final Map<String, Map<String, Collection<Configuration>>> scopes =
105         new HashMap<String, Map<String, Collection<Configuration>>>(ideaModule.getScopes());
106
107       Map<String, Map<String, Collection<Configuration>>> buildScriptScope = new HashMap<String, Map<String, Collection<Configuration>>>();
108       Map<String, Collection<Configuration>> plusConfiguration = new HashMap<String, Collection<Configuration>>();
109       plusConfiguration.put(PLUS_CONFIGURATION, plusConfigurations);
110       if (scopes.get(COMPILE_SCOPE) != null) {
111         plusConfiguration.put(MINUS_CONFIGURATION, scopes.get(COMPILE_SCOPE).get(PLUS_CONFIGURATION));
112       }
113       buildScriptScope.put(COMPILE_SCOPE, plusConfiguration);
114       ideaModule.setScopes(buildScriptScope);
115       final Set<Dependency> buildScriptDependencies = ideaModule.resolveDependencies();
116       for (Dependency dependency : buildScriptDependencies) {
117         if (dependency instanceof ModuleLibrary) {
118           ModuleLibrary moduleLibrary = (ModuleLibrary)dependency;
119           if (COMPILE_SCOPE.equals(moduleLibrary.getScope())) {
120             buildScriptClasspath.add(new ClasspathEntryModelImpl(
121               convert(moduleLibrary.getClasses()), convert(moduleLibrary.getSources()), convert(moduleLibrary.getJavadoc())));
122           }
123         }
124       }
125
126       ideaModule.setScopes(scopes);
127       configurations.remove(configuration);
128     }
129
130     cache.put(project.getPath(), buildScriptClasspath);
131     return buildScriptClasspath;
132   }
133
134   @NotNull
135   @Override
136   public ErrorMessageBuilder getErrorMessageBuilder(@NotNull Project project, @NotNull Exception e) {
137     return ErrorMessageBuilder.create(
138       project, e, "Project build classpath resolve errors"
139     ).withDescription("Unable to resolve additional buildscript classpath dependencies");
140   }
141
142   private static Set<String> convert(Set<Path> paths) {
143     Set<String> result = new HashSet<String>(paths.size());
144     for (Path path : paths) {
145       result.add(path.getRelPath());
146     }
147     return result;
148   }
149 }