gradle: fix gradle home detection for customizable gradle wrapper mode (IDEA-125057)

This commit is contained in:
Vladislav.Soroka
2016-06-15 17:07:37 +03:00
parent 38e1056980
commit 4f7154795c
7 changed files with 73 additions and 21 deletions
@@ -264,7 +264,7 @@ public abstract class AbstractExternalSystemLocalSettings {
}
}
private static <K, V> void setIfNotNull(@NotNull AtomicReference<Map<K, V>> ref, @Nullable Map<K, V> candidate) {
protected static <K, V> void setIfNotNull(@NotNull AtomicReference<Map<K, V>> ref, @Nullable Map<K, V> candidate) {
if (candidate == null) {
return;
}
@@ -20,7 +20,9 @@ import com.intellij.openapi.externalSystem.model.ProjectKeys;
import com.intellij.openapi.externalSystem.model.ProjectSystemId;
import com.intellij.openapi.externalSystem.model.project.AbstractExternalEntityData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
@@ -35,6 +37,9 @@ public class BuildScriptClasspathData extends AbstractExternalEntityData {
public static final Key<BuildScriptClasspathData> KEY =
Key.create(BuildScriptClasspathData.class, ProjectKeys.LIBRARY_DEPENDENCY.getProcessingWeight() + 1);
@Nullable
private File gradleHomeDir;
@NotNull
private final List<ClasspathEntry> myClasspathEntries;
@@ -44,6 +49,15 @@ public class BuildScriptClasspathData extends AbstractExternalEntityData {
myClasspathEntries = classpathEntries;
}
@Nullable
public File getGradleHomeDir() {
return gradleHomeDir;
}
public void setGradleHomeDir(@Nullable File gradleHomeDir) {
this.gradleHomeDir = gradleHomeDir;
}
@NotNull
public List<ClasspathEntry> getClasspathEntries() {
return myClasspathEntries;
@@ -41,6 +41,7 @@ import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.gradle.settings.DistributionType;
import org.jetbrains.plugins.gradle.settings.GradleLocalSettings;
import org.jetbrains.plugins.gradle.settings.GradleProjectSettings;
import org.jetbrains.plugins.gradle.settings.GradleSettings;
import org.jetbrains.plugins.gradle.util.GradleEnvironment;
@@ -186,14 +187,18 @@ public class GradleInstallationManager {
if (settings == null || settings.getDistributionType() == null) {
return null;
}
return getGradleHome(settings.getDistributionType(), linkedProjectPath, settings.getGradleHome());
String gradleHome = settings.getDistributionType() == DistributionType.WRAPPED
? GradleLocalSettings.getInstance(project).getGradleHome(linkedProjectPath)
: settings.getGradleHome();
return getGradleHome(settings.getDistributionType(), linkedProjectPath, gradleHome);
}
@Nullable
public File getGradleHome(@NotNull DistributionType distributionType, @NotNull String linkedProjectPath, @Nullable String gradleHome) {
private File getGradleHome(@NotNull DistributionType distributionType, @NotNull String linkedProjectPath, @Nullable String gradleHome) {
File candidate = null;
switch (distributionType) {
case LOCAL:
case WRAPPED:
if (gradleHome != null) {
candidate = new File(gradleHome);
}
@@ -202,9 +207,6 @@ public class GradleInstallationManager {
WrapperConfiguration wrapperConfiguration = GradleUtil.getWrapperConfiguration(linkedProjectPath);
candidate = getWrappedGradleHome(linkedProjectPath, wrapperConfiguration);
break;
case WRAPPED:
// not supported yet
break;
case BUNDLED:
WrapperConfiguration bundledWrapperSettings = new WrapperConfiguration();
DistributionLocator distributionLocator = new DistributionLocator();
@@ -250,8 +250,10 @@ public class GradleExecutionHelper {
return;
}
final long ttlInMs = settings.getRemoteProcessIdleTtlInMs();
ProjectConnection connection = getConnection(projectPath, settings);
try {
settings.setRemoteProcessIdleTtlInMs(100);
try {
final File wrapperPropertyFileLocation = FileUtil.createTempFile("wrap", "loc");
wrapperPropertyFileLocation.deleteOnExit();
@@ -282,6 +284,7 @@ public class GradleExecutionHelper {
LOG.warn("Can't update wrapper", e);
}
finally {
settings.setRemoteProcessIdleTtlInMs(ttlInMs);
try {
connection.close();
}
@@ -258,6 +258,7 @@ public class BaseGradleProjectResolverExtension implements GradleProjectResolver
classpathEntries = ContainerUtil.emptyList();
}
BuildScriptClasspathData buildScriptClasspathData = new BuildScriptClasspathData(GradleConstants.SYSTEM_ID, classpathEntries);
buildScriptClasspathData.setGradleHomeDir(buildScriptClasspathModel != null ? buildScriptClasspathModel.getGradleHomeDir() : null);
ideModule.createChild(BuildScriptClasspathData.KEY, buildScriptClasspathData);
}
@@ -36,12 +36,14 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.gradle.model.data.BuildScriptClasspathData;
import org.jetbrains.plugins.gradle.service.GradleBuildClasspathManager;
import org.jetbrains.plugins.gradle.service.GradleInstallationManager;
import org.jetbrains.plugins.gradle.settings.GradleLocalSettings;
import org.jetbrains.plugins.gradle.settings.GradleProjectSettings;
import org.jetbrains.plugins.gradle.settings.GradleSettings;
import org.jetbrains.plugins.gradle.util.GradleConstants;
@@ -80,16 +82,19 @@ public class BuildClasspathModuleGradleDataService extends AbstractProjectDataSe
AbstractExternalSystemLocalSettings localSettings = manager.getLocalSettingsProvider().fun(project);
final String linkedExternalProjectPath = projectData.getLinkedExternalProjectPath();
final File gradleHomeDir = toImport.iterator().next().getData().getGradleHomeDir();
final GradleLocalSettings gradleLocalSettings = GradleLocalSettings.getInstance(project);
if (gradleHomeDir != null) {
gradleLocalSettings.setGradleHome(linkedExternalProjectPath, gradleHomeDir.getPath());
}
final GradleProjectSettings settings = GradleSettings.getInstance(project).getLinkedProjectSettings(linkedExternalProjectPath);
final NotNullLazyValue<Set<String>> externalProjectGradleSdkLibs = new NotNullLazyValue<Set<String>>() {
@NotNull
@Override
protected Set<String> compute() {
GradleProjectSettings settings = GradleSettings.getInstance(project).getLinkedProjectSettings(linkedExternalProjectPath);
if (settings == null || settings.getDistributionType() == null) return Collections.emptySet();
final Set<String> gradleSdkLibraries = ContainerUtil.newLinkedHashSet();
File gradleHome =
gradleInstallationManager.getGradleHome(settings.getDistributionType(), linkedExternalProjectPath, settings.getGradleHome());
File gradleHome = gradleInstallationManager.getGradleHome(project, linkedExternalProjectPath);
if (gradleHome != null && gradleHome.isDirectory()) {
final Collection<File> libraries = gradleInstallationManager.getClassRoots(project, linkedExternalProjectPath);
if (libraries != null) {
@@ -114,7 +119,7 @@ public class BuildClasspathModuleGradleDataService extends AbstractProjectDataSe
for (Module module : modelsProvider.getModules(projectData)) {
final String projectPath = ExternalSystemApiUtil.getExternalProjectPath(module);
if(projectPath != null && StringUtil.startsWith(projectPath, linkedExternalProjectPath + "/buildSrc")) {
final List<String> sourceRoots = ContainerUtil.map(modelsProvider.getSourceRoots(module, false), file -> file.getPath());
final List<String> sourceRoots = ContainerUtil.map(modelsProvider.getSourceRoots(module, false), VirtualFile::getPath);
result.addAll(sourceRoots);
}
}
@@ -130,7 +135,6 @@ public class BuildClasspathModuleGradleDataService extends AbstractProjectDataSe
if (moduleDataNode == null) continue;
String externalModulePath = moduleDataNode.getData().getLinkedExternalProjectPath();
GradleProjectSettings settings = GradleSettings.getInstance(project).getLinkedProjectSettings(linkedExternalProjectPath);
if (settings == null || settings.getDistributionType() == null) {
LOG.warn("Gradle SDK distribution type was not configured for the project at " + linkedExternalProjectPath);
}
@@ -150,9 +154,7 @@ public class BuildClasspathModuleGradleDataService extends AbstractProjectDataSe
ExternalProjectBuildClasspathPojo projectBuildClasspathPojo = localProjectBuildClasspath.get(linkedExternalProjectPath);
if (projectBuildClasspathPojo == null) {
projectBuildClasspathPojo = new ExternalProjectBuildClasspathPojo(
moduleDataNode.getData().getExternalName(),
ContainerUtil.<String>newArrayList(),
ContainerUtil.<String, ExternalModuleBuildClasspathPojo>newHashMap());
moduleDataNode.getData().getExternalName(), ContainerUtil.newArrayList(), ContainerUtil.newHashMap());
localProjectBuildClasspath.put(linkedExternalProjectPath, projectBuildClasspathPojo);
}
@@ -18,18 +18,25 @@ package org.jetbrains.plugins.gradle.settings;
import com.intellij.openapi.components.*;
import com.intellij.openapi.externalSystem.settings.AbstractExternalSystemLocalSettings;
import com.intellij.openapi.project.Project;
import com.intellij.util.containers.ContainerUtilRt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.gradle.util.GradleConstants;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author Denis Zhdanov
* @since 5/3/12 6:16 PM
*/
@State(name = "GradleLocalSettings", storages = {@Storage(StoragePathMacros.WORKSPACE_FILE)} )
public class GradleLocalSettings extends AbstractExternalSystemLocalSettings
implements PersistentStateComponent<AbstractExternalSystemLocalSettings.State>
implements PersistentStateComponent<GradleLocalSettings.MyState>
{
private final AtomicReference<Map<String/* external project path */, String>> myGradleHomes =
new AtomicReference<Map<String, String>>(ContainerUtilRt.newHashMap());
public GradleLocalSettings(@NotNull Project project) {
super(GradleConstants.SYSTEM_ID, project);
@@ -41,15 +48,38 @@ public class GradleLocalSettings extends AbstractExternalSystemLocalSettings
}
@Nullable
public String getGradleHome(String linkedProjectPath) {
return myGradleHomes.get().get(linkedProjectPath);
}
public void setGradleHome(@NotNull String linkedProjectPath, @NotNull String gradleHome) {
myGradleHomes.get().put(linkedProjectPath, gradleHome);
}
@Override
public State getState() {
State state = new State();
public void forgetExternalProjects(@NotNull Set<String> linkedProjectPathsToForget) {
super.forgetExternalProjects(linkedProjectPathsToForget);
for (String path : linkedProjectPathsToForget) {
myGradleHomes.get().remove(path);
}
}
@Nullable
@Override
public MyState getState() {
MyState state = new MyState();
fillState(state);
state.myGradleHomes = myGradleHomes.get();
return state;
}
@Override
public void loadState(@NotNull State state) {
super.loadState(state);
public void loadState(@NotNull MyState state) {
super.loadState(state);
setIfNotNull(myGradleHomes, state.myGradleHomes);
}
public static class MyState extends AbstractExternalSystemLocalSettings.State {
public Map<String/* project path */, String> myGradleHomes = ContainerUtilRt.newHashMap();
}
}