summaryrefslogtreecommitdiff
path: root/android/src/com/android/tools/idea/gradle/service/notification/FixGradleModelVersionHyperlink.java
blob: 26adf4b03b9aa7b40f80d778716ba5a41772ac8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tools.idea.gradle.service.notification;

import com.android.tools.idea.gradle.parser.GradleBuildFile;
import com.android.tools.idea.gradle.project.AndroidGradleNotification;
import com.android.tools.idea.gradle.project.GradleProjectImporter;
import com.android.tools.idea.gradle.util.GradleUtil;
import com.intellij.ide.BrowserUtil;
import com.intellij.notification.NotificationListener;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import java.io.File;

import static com.android.SdkConstants.GRADLE_LATEST_VERSION;
import static com.android.SdkConstants.GRADLE_PLUGIN_RECOMMENDED_VERSION;
import static com.android.tools.idea.gradle.parser.BuildFileKey.PLUGIN_VERSION;
import static com.android.tools.idea.gradle.service.notification.FixGradleVersionInWrapperHyperlink.updateGradleVersion;

class FixGradleModelVersionHyperlink extends NotificationHyperlink {
  private final boolean myOpenMigrationGuide;

  FixGradleModelVersionHyperlink() {
    this("Open migration guide, fix plug-in version and sync project", true);
  }

  FixGradleModelVersionHyperlink(@NotNull String text, boolean openMigrationGuide) {
    super("fixGradleElements", text);
    myOpenMigrationGuide = openMigrationGuide;
  }

  @Override
  protected void execute(@NotNull Project project) {
    if (myOpenMigrationGuide) {
      BrowserUtil.browse("http://tools.android.com/tech-docs/new-build-system/migrating_to_09");
    }

    ModuleManager moduleManager = ModuleManager.getInstance(project);
    boolean atLeastOnUpdated = false;
    for (Module module : moduleManager.getModules()) {
      VirtualFile file = GradleUtil.getGradleBuildFile(module);
      if (file != null) {
        final GradleBuildFile buildFile = new GradleBuildFile(file, project);
        Object pluginVersion = buildFile.getValue(PLUGIN_VERSION);
        if (pluginVersion != null) {
          WriteCommandAction.runWriteCommandAction(project, new Runnable() {
            @Override
            public void run() {
              buildFile.setValue(PLUGIN_VERSION, GRADLE_PLUGIN_RECOMMENDED_VERSION);
            }
          });
          atLeastOnUpdated = true;
        }
      }
    }
    if (!atLeastOnUpdated) {
      NotificationListener notificationListener =
        new CustomNotificationListener(project, new SearchInBuildFilesHyperlink("com.android.tools.build:gradle"));
      AndroidGradleNotification notification = AndroidGradleNotification.getInstance(project);
      String msg = "Unable to find any references to the Android Gradle plug-in in build.gradle files.\n\n" +
                   "Please click the link to perform a textual search and then update the build files manually.";
      notification.showBalloon(ERROR_MSG_TITLE, msg, NotificationType.ERROR, notificationListener);
      return;
    }
    File wrapperPropertiesFile = GradleUtil.findWrapperPropertiesFile(project);
    if (wrapperPropertiesFile != null) {
      updateGradleVersion(project, wrapperPropertiesFile, GRADLE_LATEST_VERSION);
    }
    GradleProjectImporter.getInstance().requestProjectSync(project, null);
  }
}