From 8f64aec2cd31e8b94b0ab5dfe7cc0a4791a114e1 Mon Sep 17 00:00:00 2001 From: "Vladislav.Soroka" Date: Wed, 18 Sep 2013 12:17:17 +0400 Subject: [PATCH] IDEA-113581 Gradle: New Project Wizard: it could be possible to enable "Use default gradle wrapper" option --- .../wizard/AbstractExternalModuleBuilder.java | 34 +++++++------- ...bstractExternalProjectSettingsControl.java | 10 ++-- .../util/ExternalSystemUtil.java | 17 ++++++- ...ile.gradle.ft => Gradle_default.gradle.ft} | 0 .../Gradle_with_custom_wrapper.gradle.ft | 17 +++++++ .../plugins/gradle/GradleManager.java | 46 +++++++++++++++++-- ...butionWithSourcesNotificationProvider.java | 40 ++++++++++++++-- .../project/GradleExecutionHelper.java | 26 +++++++---- .../project/GradleProjectResolver.java | 4 +- .../project/wizard/GradleModuleBuilder.java | 20 ++++++-- .../GradleProjectSettingsControl.java | 8 ++-- .../settings/ImportFromGradleControl.java | 2 +- .../service/task/GradleTaskManager.java | 4 +- .../gradle/settings/DistributionType.java | 10 +++- .../plugins/gradle/util/GradleUtil.java | 4 +- 15 files changed, 190 insertions(+), 52 deletions(-) rename plugins/gradle/resources/fileTemplates/internal/{Gradle File.gradle.ft => Gradle_default.gradle.ft} (100%) create mode 100644 plugins/gradle/resources/fileTemplates/internal/Gradle_with_custom_wrapper.gradle.ft diff --git a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/wizard/AbstractExternalModuleBuilder.java b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/wizard/AbstractExternalModuleBuilder.java index 7c6ee0ada601..ed32a93ffbfe 100644 --- a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/wizard/AbstractExternalModuleBuilder.java +++ b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/wizard/AbstractExternalModuleBuilder.java @@ -18,6 +18,7 @@ package com.intellij.openapi.externalSystem.service.project.wizard; import com.intellij.ide.fileTemplates.FileTemplate; import com.intellij.ide.fileTemplates.FileTemplateManager; import com.intellij.ide.util.projectWizard.ModuleBuilder; +import com.intellij.ide.util.projectWizard.ModuleBuilderListener; import com.intellij.ide.util.projectWizard.ModuleWizardStep; import com.intellij.ide.util.projectWizard.WizardContext; import com.intellij.openapi.diagnostic.Logger; @@ -56,14 +57,11 @@ public abstract class AbstractExternalModuleBuilder myExternalProjectSettingsControl; - @Nullable private final String myTemplateConfigName; protected AbstractExternalModuleBuilder(@NotNull ProjectSystemId externalSystemId, - @Nullable AbstractExternalProjectSettingsControl externalProjectSettingsControl, - @Nullable String templateConfigName) + @Nullable AbstractExternalProjectSettingsControl externalProjectSettingsControl) { myExternalSystemId = externalSystemId; - myTemplateConfigName = templateConfigName; myExternalProjectSettingsControl = externalProjectSettingsControl; Icon icon = ExternalSystemUiUtil.getUiAware(externalSystemId).getProjectIcon(); myIcon = icon == null ? super.getNodeIcon() : icon; @@ -95,7 +93,7 @@ public abstract class AbstractExternalModuleBuilder + * 'Refresh' here means 'obtain the most up-to-date version and apply it to the ide'. + * + * @param project target ide project + * @param externalSystemId target external system which projects should be refreshed + * @param force flag which defines if external project refresh should be performed if it's config is up-to-date + */ + public static void refreshProjects(@NotNull final Project project, @NotNull final ProjectSystemId externalSystemId, boolean force, boolean modal) { ExternalSystemManager manager = ExternalSystemApiUtil.getManager(externalSystemId); if (manager == null) { return; @@ -175,7 +188,7 @@ public class ExternalSystemUtil { } final ProjectDataManager projectDataManager = ServiceManager.getService(ProjectDataManager.class); - final int[] counter = new int[1]; + final int[] counter = new int[1]; ExternalProjectRefreshCallback callback = new ExternalProjectRefreshCallback() { @@ -244,7 +257,7 @@ public class ExternalSystemUtil { if (!toRefresh.isEmpty()) { counter[0] = toRefresh.size(); for (String path : toRefresh) { - refreshProject(project, externalSystemId, path, callback, true, false); + refreshProject(project, externalSystemId, path, callback, true, modal); } } } diff --git a/plugins/gradle/resources/fileTemplates/internal/Gradle File.gradle.ft b/plugins/gradle/resources/fileTemplates/internal/Gradle_default.gradle.ft similarity index 100% rename from plugins/gradle/resources/fileTemplates/internal/Gradle File.gradle.ft rename to plugins/gradle/resources/fileTemplates/internal/Gradle_default.gradle.ft diff --git a/plugins/gradle/resources/fileTemplates/internal/Gradle_with_custom_wrapper.gradle.ft b/plugins/gradle/resources/fileTemplates/internal/Gradle_with_custom_wrapper.gradle.ft new file mode 100644 index 000000000000..60464171c93d --- /dev/null +++ b/plugins/gradle/resources/fileTemplates/internal/Gradle_with_custom_wrapper.gradle.ft @@ -0,0 +1,17 @@ +apply plugin: 'java' + +sourceCompatibility = 1.5 +version = '1.0' + +task wrapper(type: Wrapper) { + gradleVersion = '1.8-rc-1' + distributionUrl = 'http://services.gradle.org/distributions/gradle-1.8-rc-1-all.zip' +} + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.11' +} \ No newline at end of file diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/GradleManager.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/GradleManager.java index c5b3738af7e8..1acfbf15fd69 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/GradleManager.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/GradleManager.java @@ -19,18 +19,23 @@ import com.intellij.execution.ExecutionException; import com.intellij.execution.configurations.SimpleJavaParameters; import com.intellij.externalSystem.JavaProjectData; import com.intellij.openapi.application.PathManager; +import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.externalSystem.ExternalSystemAutoImportAware; import com.intellij.openapi.externalSystem.ExternalSystemConfigurableAware; import com.intellij.openapi.externalSystem.ExternalSystemManager; import com.intellij.openapi.externalSystem.ExternalSystemUiAware; +import com.intellij.openapi.externalSystem.model.DataNode; import com.intellij.openapi.externalSystem.model.ProjectSystemId; import com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings; import com.intellij.openapi.externalSystem.model.execution.ExternalTaskExecutionInfo; import com.intellij.openapi.externalSystem.model.execution.ExternalTaskPojo; import com.intellij.openapi.externalSystem.model.project.ExternalProjectPojo; +import com.intellij.openapi.externalSystem.model.project.ProjectData; +import com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback; import com.intellij.openapi.externalSystem.service.project.ExternalSystemProjectResolver; import com.intellij.openapi.externalSystem.service.project.autoimport.CachingExternalSystemAutoImportAware; +import com.intellij.openapi.externalSystem.service.project.manage.ProjectDataManager; import com.intellij.openapi.externalSystem.service.ui.DefaultExternalSystemUiAware; import com.intellij.openapi.externalSystem.task.ExternalSystemTaskManager; import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil; @@ -42,6 +47,7 @@ import com.intellij.openapi.module.ModuleType; import com.intellij.openapi.module.StdModuleTypes; import com.intellij.openapi.options.Configurable; import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.ex.ProjectRootManagerEx; import com.intellij.openapi.startup.StartupActivity; import com.intellij.openapi.util.NotNullLazyValue; import com.intellij.openapi.util.Pair; @@ -84,13 +90,12 @@ import java.util.Map; * @since 4/10/13 1:19 PM */ public class GradleManager -implements ExternalSystemConfigurableAware, ExternalSystemUiAware, ExternalSystemAutoImportAware, StartupActivity, ExternalSystemManager< + implements ExternalSystemConfigurableAware, ExternalSystemUiAware, ExternalSystemAutoImportAware, StartupActivity, ExternalSystemManager< GradleProjectSettings, GradleSettingsListener, GradleSettings, GradleLocalSettings, - GradleExecutionSettings> -{ + GradleExecutionSettings> { private static final Logger LOG = Logger.getInstance("#" + GradleManager.class.getName()); @@ -173,7 +178,8 @@ implements ExternalSystemConfigurableAware, ExternalSystemUiAware, ExternalSyste GradleUtil.isGradleDefaultWrapperFilesExist(pair.second) ? DistributionType.DEFAULT_WRAPPED : DistributionType.LOCAL; } else { - distributionType = projectLevelSettings.getDistributionType() == null ? DistributionType.LOCAL : projectLevelSettings.getDistributionType(); + distributionType = + projectLevelSettings.getDistributionType() == null ? DistributionType.LOCAL : projectLevelSettings.getDistributionType(); } GradleExecutionSettings result = new GradleExecutionSettings(localGradlePath, @@ -304,6 +310,38 @@ implements ExternalSystemConfigurableAware, ExternalSystemUiAware, ExternalSyste public void onServiceDirectoryPathChange(@Nullable String oldPath, @Nullable String newPath) { ExternalSystemUtil.refreshProjects(project, GradleConstants.SYSTEM_ID, true); } + + @Override + public void onProjectsLinked(@NotNull Collection settings) { + final ProjectDataManager projectDataManager = ServiceManager.getService(ProjectDataManager.class); + for (GradleProjectSettings gradleProjectSettings : settings) { + ExternalSystemUtil.refreshProject( + project, GradleConstants.SYSTEM_ID, gradleProjectSettings.getExternalProjectPath(), + new ExternalProjectRefreshCallback() { + @Override + public void onSuccess(@Nullable final DataNode externalProject) { + if (externalProject == null) { + return; + } + ExternalSystemApiUtil.executeProjectChangeAction(true, new Runnable() { + @Override + public void run() { + ProjectRootManagerEx.getInstanceEx(project).mergeRootsChangesDuring(new Runnable() { + @Override + public void run() { + projectDataManager.importData(externalProject.getKey(), Collections.singleton(externalProject), project, true); + } + }); + } + }); + } + + @Override + public void onFailure(@NotNull String errorMessage, @Nullable String errorDetails) { + } + }, true, true); + } + } }); // We used to assume that gradle scripts are always named 'build.gradle' and kept path to that build.gradle file at ide settings. diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/codeInsight/UseDistributionWithSourcesNotificationProvider.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/codeInsight/UseDistributionWithSourcesNotificationProvider.java index b71bec2aa27d..754a52664d57 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/codeInsight/UseDistributionWithSourcesNotificationProvider.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/codeInsight/UseDistributionWithSourcesNotificationProvider.java @@ -16,8 +16,15 @@ package org.jetbrains.plugins.gradle.codeInsight; import com.intellij.ProjectTopics; +import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.externalSystem.model.DataNode; +import com.intellij.openapi.externalSystem.model.project.ProjectData; +import com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback; +import com.intellij.openapi.externalSystem.service.project.manage.ProjectDataManager; +import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil; import com.intellij.openapi.externalSystem.util.ExternalSystemConstants; +import com.intellij.openapi.externalSystem.util.ExternalSystemUtil; import com.intellij.openapi.fileEditor.FileEditor; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleUtilCore; @@ -26,6 +33,7 @@ import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ModuleRootAdapter; import com.intellij.openapi.roots.ModuleRootEvent; +import com.intellij.openapi.roots.ex.ProjectRootManagerEx; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.LocalFileSystem; @@ -107,8 +115,34 @@ public class UseDistributionWithSourcesNotificationProvider extends EditorNotifi panel.createActionLabel(GradleBundle.message("gradle.notifications.apply.suggestion"), new Runnable() { @Override public void run() { - updateWrapperConfiguration(rootProjectPath); + updateDefaultWrapperConfiguration(rootProjectPath); EditorNotifications.getInstance(module.getProject()).updateAllNotifications(); + final ProjectDataManager projectDataManager = ServiceManager.getService(ProjectDataManager.class); + ExternalSystemUtil.refreshProject( + module.getProject(), GradleConstants.SYSTEM_ID, settings.getExternalProjectPath(), + new ExternalProjectRefreshCallback() { + @Override + public void onSuccess(@Nullable final DataNode externalProject) { + if (externalProject == null) { + return; + } + ExternalSystemApiUtil.executeProjectChangeAction(true, new Runnable() { + @Override + public void run() { + ProjectRootManagerEx.getInstanceEx(module.getProject()).mergeRootsChangesDuring(new Runnable() { + @Override + public void run() { + projectDataManager.importData(externalProject.getKey(), Collections.singleton(externalProject), module.getProject(), true); + } + }); + } + }); + } + + @Override + public void onFailure(@NotNull String errorMessage, @Nullable String errorDetails) { + } + }, true, true); } }); return panel; @@ -122,9 +156,9 @@ public class UseDistributionWithSourcesNotificationProvider extends EditorNotifi return null; } - private static void updateWrapperConfiguration(@NotNull String linkedProjectPath) { + private static void updateDefaultWrapperConfiguration(@NotNull String linkedProjectPath) { try { - final VirtualFile wrapperPropertiesFile = GradleUtil.findWrapperPropertiesFile(linkedProjectPath); + final VirtualFile wrapperPropertiesFile = GradleUtil.findDefaultWrapperPropertiesFile(linkedProjectPath); if (wrapperPropertiesFile == null) return; final WrapperConfiguration wrapperConfiguration = GradleUtil.getWrapperConfiguration(linkedProjectPath); if (wrapperConfiguration == null) return; diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/GradleExecutionHelper.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/GradleExecutionHelper.java index c23359673be5..a0b52be4e4c3 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/GradleExecutionHelper.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/GradleExecutionHelper.java @@ -37,6 +37,7 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.gradle.settings.DistributionType; import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings; import org.jetbrains.plugins.gradle.util.GradleConstants; +import org.jetbrains.plugins.gradle.util.GradleUtil; import java.io.File; import java.io.IOException; @@ -181,6 +182,13 @@ public class GradleExecutionHelper { @NotNull String projectPath, @NotNull GradleExecutionSettings settings, @NotNull ExternalSystemTaskNotificationListener listener) { + + if (!settings.getDistributionType().isWrapped()) return; + + if (settings.getDistributionType() == DistributionType.DEFAULT_WRAPPED && + GradleUtil.findDefaultWrapperPropertiesFile(projectPath) != null) { + return; + } ProjectConnection connection = getConnection(projectPath, settings); try { BuildLauncher launcher = getBuildLauncher(id, connection, settings, listener); @@ -253,15 +261,17 @@ public class GradleExecutionHelper { } break; case WRAPPED: - if(settings.getWrapperPropertyFile() != null) { + if (settings.getWrapperPropertyFile() != null) { File propertiesFile = new File(settings.getWrapperPropertyFile()); - Distribution distribution = - new DistributionFactoryExt(StartParameter.DEFAULT_GRADLE_USER_HOME).getWrappedDistribution(propertiesFile); - try { - setField(connector, "distribution", distribution); - } - catch (Exception e) { - throw new ExternalSystemException(e); + if (propertiesFile.exists()) { + Distribution distribution = + new DistributionFactoryExt(StartParameter.DEFAULT_GRADLE_USER_HOME).getWrappedDistribution(propertiesFile); + try { + setField(connector, "distribution", distribution); + } + catch (Exception e) { + throw new ExternalSystemException(e); + } } } break; diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/GradleProjectResolver.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/GradleProjectResolver.java index 57f00e566af8..c0873f7b4ce4 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/GradleProjectResolver.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/GradleProjectResolver.java @@ -62,9 +62,7 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver> extensionClasses = settings.getResolverExtensions(); if (myCachedExtensions == null || !myCachedExtensions.first.equals(extensionClasses)) { List extensions = ContainerUtilRt.newArrayList(); diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/wizard/GradleModuleBuilder.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/wizard/GradleModuleBuilder.java index 787a81d8b374..c339bd56a3bb 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/wizard/GradleModuleBuilder.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/wizard/GradleModuleBuilder.java @@ -27,6 +27,7 @@ import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.gradle.service.settings.GradleProjectSettingsControl; +import org.jetbrains.plugins.gradle.settings.DistributionType; import org.jetbrains.plugins.gradle.settings.GradleProjectSettings; import org.jetbrains.plugins.gradle.util.GradleConstants; @@ -39,7 +40,7 @@ import java.io.File; public class GradleModuleBuilder extends AbstractExternalModuleBuilder { public GradleModuleBuilder() { - super(GradleConstants.SYSTEM_ID, new GradleProjectSettingsControl(new GradleProjectSettings()), "Gradle File.gradle"); + super(GradleConstants.SYSTEM_ID, new GradleProjectSettingsControl(new GradleProjectSettings())); } @Nullable @@ -50,16 +51,29 @@ public class GradleModuleBuilder extends AbstractExternalModuleBuilder f = new Function() { @Override diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/settings/DistributionType.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/settings/DistributionType.java index e04dc14ce944..1c49cc6ed28c 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/settings/DistributionType.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/settings/DistributionType.java @@ -45,5 +45,13 @@ public enum DistributionType { /** * Locally installed gradle to be used */ - LOCAL + LOCAL; + + /** + * Check for wrapped mode + * @return true in case of DEFAULT_WRAPPED or WRAPPED mode + */ + public boolean isWrapped() { + return this == DEFAULT_WRAPPED || this == WRAPPED; + } } diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/util/GradleUtil.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/util/GradleUtil.java index 0f95add8162f..afb13bd6af33 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/util/GradleUtil.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/util/GradleUtil.java @@ -70,7 +70,7 @@ public class GradleUtil { */ @Nullable public static WrapperConfiguration getWrapperConfiguration(@Nullable String gradleProjectPath) { - final VirtualFile wrapperPropertiesFile = findWrapperPropertiesFile(gradleProjectPath); + final VirtualFile wrapperPropertiesFile = findDefaultWrapperPropertiesFile(gradleProjectPath); if (wrapperPropertiesFile == null) return null; final WrapperConfiguration wrapperConfiguration = new WrapperConfiguration(); @@ -190,7 +190,7 @@ public class GradleUtil { } @Nullable - public static VirtualFile findWrapperPropertiesFile(@Nullable String gradleProjectPath) { + public static VirtualFile findDefaultWrapperPropertiesFile(@Nullable String gradleProjectPath) { if (gradleProjectPath == null) { return null; }