Search in sources :

Example 1 with GradleWrapper

use of com.android.tools.idea.gradle.util.GradleWrapper in project android by JetBrains.

the class AndroidGradleTestCase method createGradleWrapper.

protected static void createGradleWrapper(File projectRoot) throws IOException {
    GradleWrapper wrapper = GradleWrapper.create(projectRoot);
    File path = getWorkspaceFile("tools/external/gradle/gradle-" + GRADLE_LATEST_VERSION + "-bin.zip");
    assertAbout(file()).that(path).named("Gradle distribution path").isFile();
    wrapper.updateDistributionUrl(path);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File) TestUtils.getWorkspaceFile(com.android.testutils.TestUtils.getWorkspaceFile) GradleWrapper(com.android.tools.idea.gradle.util.GradleWrapper)

Example 2 with GradleWrapper

use of com.android.tools.idea.gradle.util.GradleWrapper in project android by JetBrains.

the class GradleDslMethodNotFoundErrorHandler method getGradleSettingsHyperlink.

@NotNull
private static NotificationHyperlink getGradleSettingsHyperlink(@NotNull Project project) {
    if (isUsingWrapper(project)) {
        GradleWrapper gradleWrapper = GradleWrapper.find(project);
        if (gradleWrapper != null) {
            VirtualFile propertiesFile = gradleWrapper.getPropertiesFile();
            if (propertiesFile != null) {
                return new NotificationHyperlink("open.wrapper.file", "Open Gradle wrapper file") {

                    @Override
                    protected void execute(@NotNull Project project) {
                        OpenFileDescriptor descriptor = new OpenFileDescriptor(project, propertiesFile);
                        FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
                    }
                };
            }
        }
    }
    return new OpenGradleSettingsHyperlink();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) OpenGradleSettingsHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.OpenGradleSettingsHyperlink) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) NotNull(org.jetbrains.annotations.NotNull) GradleWrapper(com.android.tools.idea.gradle.util.GradleWrapper) NotificationHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GradleWrapper

use of com.android.tools.idea.gradle.util.GradleWrapper in project android by JetBrains.

the class UnsupportedGradleVersionErrorHandler method getQuickFixHyperlinks.

@NotNull
public static List<NotificationHyperlink> getQuickFixHyperlinks(@NotNull Project project, @Nullable String gradleVersion) {
    List<NotificationHyperlink> hyperlinks = new ArrayList<>();
    GradleWrapper gradleWrapper = GradleWrapper.find(project);
    if (gradleWrapper != null) {
        // It is very likely that we need to fix the model version as well. Do everything in one shot.
        NotificationHyperlink hyperlink = createIfProjectUsesGradleWrapper(project, gradleVersion);
        if (hyperlink != null) {
            hyperlinks.add(hyperlink);
        }
    } else {
        GradleProjectSettings gradleProjectSettings = GradleProjectSettingsFinder.getInstance().findGradleProjectSettings(project);
        if (gradleProjectSettings != null && gradleProjectSettings.getDistributionType() == DistributionType.LOCAL) {
            hyperlinks.add(new CreateGradleWrapperHyperlink());
        }
    }
    hyperlinks.add(new OpenGradleSettingsHyperlink());
    return hyperlinks;
}
Also used : OpenGradleSettingsHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.OpenGradleSettingsHyperlink) GradleProjectSettings(org.jetbrains.plugins.gradle.settings.GradleProjectSettings) ArrayList(java.util.ArrayList) GradleWrapper(com.android.tools.idea.gradle.util.GradleWrapper) FixGradleVersionInWrapperHyperlink.createIfProjectUsesGradleWrapper(com.android.tools.idea.gradle.project.sync.hyperlink.FixGradleVersionInWrapperHyperlink.createIfProjectUsesGradleWrapper) NotificationHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink) CreateGradleWrapperHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.CreateGradleWrapperHyperlink) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GradleWrapper

use of com.android.tools.idea.gradle.util.GradleWrapper in project android by JetBrains.

the class GradleDistributionCleanUpTask method cleanUp.

@Override
void cleanUp(@NotNull Project project) {
    GradleProjectSettings gradleSettings = GradleProjectSettingsFinder.getInstance().findGradleProjectSettings(project);
    GradleWrapper gradleWrapper = GradleWrapper.find(project);
    DistributionType distributionType = gradleSettings != null ? gradleSettings.getDistributionType() : null;
    boolean usingWrapper = (distributionType == null || distributionType == DEFAULT_WRAPPED) && gradleWrapper != null;
    if (usingWrapper && gradleSettings != null) {
        // Do this just to ensure that the right distribution type is set. If this is not set, build.gradle editor will not have code
        // completion (see BuildClasspathModuleGradleDataService, line 119).
        gradleSettings.setDistributionType(DEFAULT_WRAPPED);
    } else if (gradleWrapper == null && gradleSettings != null) {
        createWrapperIfNecessary(project, gradleSettings, distributionType);
    }
}
Also used : GradleProjectSettings(org.jetbrains.plugins.gradle.settings.GradleProjectSettings) GradleWrapper(com.android.tools.idea.gradle.util.GradleWrapper) DistributionType(org.jetbrains.plugins.gradle.settings.DistributionType)

Example 5 with GradleWrapper

use of com.android.tools.idea.gradle.util.GradleWrapper in project android by JetBrains.

the class AndroidPluginVersionUpdater method updatePluginVersion.

/**
   * Updates the plugin version and, optionally, the Gradle version used by the project.
   *
   * @param pluginVersion the plugin version to update to.
   * @param gradleVersion the version of Gradle to update to (optional.)
   * @return the result of the update operation.
   */
@NotNull
public UpdateResult updatePluginVersion(@NotNull GradleVersion pluginVersion, @Nullable GradleVersion gradleVersion) {
    List<GradleBuildModel> modelsToUpdate = Lists.newArrayList();
    BuildFileProcessor.getInstance().processRecursively(myProject, buildModel -> {
        DependenciesModel dependencies = buildModel.buildscript().dependencies();
        for (ArtifactDependencyModel dependency : dependencies.artifacts(CLASSPATH)) {
            String artifactId = dependency.name().value();
            String groupId = dependency.group().value();
            if (AndroidPluginGeneration.find(artifactId, groupId) != null) {
                String versionValue = dependency.version().value();
                if (isEmpty(versionValue) || pluginVersion.compareTo(versionValue) != 0) {
                    dependency.setVersion(pluginVersion.toString());
                    modelsToUpdate.add(buildModel);
                }
                break;
            }
        }
        return true;
    });
    UpdateResult result = new UpdateResult();
    boolean updateModels = !modelsToUpdate.isEmpty();
    if (updateModels) {
        try {
            runWriteCommandAction(myProject, new ThrowableComputable<Void, RuntimeException>() {

                @Override
                public Void compute() {
                    for (GradleBuildModel buildModel : modelsToUpdate) {
                        buildModel.applyChanges();
                    }
                    result.pluginVersionUpdated();
                    return null;
                }
            });
        } catch (Throwable e) {
            result.setPluginVersionUpdateError(e);
        }
    }
    if (gradleVersion != null) {
        String basePath = myProject.getBasePath();
        if (basePath != null) {
            try {
                File wrapperPropertiesFilePath = getDefaultPropertiesFilePath(new File(basePath));
                GradleWrapper gradleWrapper = GradleWrapper.get(wrapperPropertiesFilePath);
                String current = gradleWrapper.getGradleVersion();
                GradleVersion parsedCurrent = null;
                if (current != null) {
                    parsedCurrent = GradleVersion.tryParse(current);
                }
                if (parsedCurrent != null && !isSupportedGradleVersion(parsedCurrent)) {
                    gradleWrapper.updateDistributionUrl(gradleVersion.toString());
                    result.gradleVersionUpdated();
                }
            } catch (Throwable e) {
                result.setGradleVersionUpdateError(e);
            }
        }
    }
    return result;
}
Also used : ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel) GradleVersion(com.android.ide.common.repository.GradleVersion) GradleUtil.isSupportedGradleVersion(com.android.tools.idea.gradle.util.GradleUtil.isSupportedGradleVersion) File(java.io.File) GradleWrapper(com.android.tools.idea.gradle.util.GradleWrapper) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GradleWrapper (com.android.tools.idea.gradle.util.GradleWrapper)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 NotNull (org.jetbrains.annotations.NotNull)3 GradleProjectSettings (org.jetbrains.plugins.gradle.settings.GradleProjectSettings)3 GradleVersion (com.android.ide.common.repository.GradleVersion)2 BuildFileKey (com.android.tools.idea.gradle.parser.BuildFileKey)2 NotificationHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink)2 OpenGradleSettingsHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.OpenGradleSettingsHyperlink)2 ConfigurationException (com.intellij.openapi.options.ConfigurationException)2 File (java.io.File)2 DistributionType (org.jetbrains.plugins.gradle.settings.DistributionType)2 Revision (com.android.repository.Revision)1 BuildToolInfo (com.android.sdklib.BuildToolInfo)1 AndroidSdkHandler (com.android.sdklib.repository.AndroidSdkHandler)1 TestUtils.getWorkspaceFile (com.android.testutils.TestUtils.getWorkspaceFile)1 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)1 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)1 DependenciesModel (com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)1 AndroidPluginVersionUpdater (com.android.tools.idea.gradle.plugin.AndroidPluginVersionUpdater)1 UpdateResult (com.android.tools.idea.gradle.plugin.AndroidPluginVersionUpdater.UpdateResult)1