Search in sources :

Example 41 with GradleVersion

use of com.android.ide.common.repository.GradleVersion in project android by JetBrains.

the class GradleVersionReader method getQuickFixes.

@Override
@NotNull
public List<NotificationHyperlink> getQuickFixes(@NotNull Module module, @Nullable VersionRange expectedVersion, @Nullable PositionInFile location) {
    List<NotificationHyperlink> quickFixes = new ArrayList<>();
    AndroidPluginInfo pluginInfo = AndroidPluginInfo.find(module.getProject());
    if (pluginInfo != null) {
        GradleVersion pluginVersion = GradleVersion.parse(pluginInfo.getPluginGeneration().getLatestKnownVersion());
        GradleVersion gradleVersion = GradleVersion.parse(GRADLE_LATEST_VERSION);
        String text = "Fix Gradle version (as part of the update, the Android plugin will be updated to version " + pluginVersion + ")";
        quickFixes.add(new FixAndroidGradlePluginVersionHyperlink(text, pluginVersion, gradleVersion));
    }
    quickFixes.add(new OpenUrlHyperlink("https://developer.android.com/studio/releases/index.html#Revisions", "Open Documentation"));
    return quickFixes;
}
Also used : FixAndroidGradlePluginVersionHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.FixAndroidGradlePluginVersionHyperlink) AndroidPluginInfo(com.android.tools.idea.gradle.plugin.AndroidPluginInfo) ArrayList(java.util.ArrayList) OpenUrlHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.OpenUrlHyperlink) GradleVersion(com.android.ide.common.repository.GradleVersion) NotificationHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink) NotNull(org.jetbrains.annotations.NotNull)

Example 42 with GradleVersion

use of com.android.ide.common.repository.GradleVersion in project android by JetBrains.

the class JCenterRepository method parse.

SearchResult parse(@NotNull Reader response) {
    /*
      Sample response:
      [
        {
          "name": "com.atlassian.guava:guava",
          "repo": "jcenter",
          "owner": "bintray",
          "desc": null,
          "system_ids": [
            "com.atlassian.guava:guava"
          ],
          "versions": [
            "15.0"
          ],
          "latest_version": "15.0"
        },
        {
          "name": "com.atlassian.bundles:guava",
          "repo": "jcenter",
          "owner": "bintray",
          "desc": null,
          "system_ids": [
            "com.atlassian.bundles:guava"
          ],
          "versions": [
            "8.1",
            "8.0",
            "1.0-actually-8.1"
          ],
          "latest_version": "8.1"
        },
        {
          "name": "io.janusproject.guava:guava",
          "repo": "jcenter",
          "owner": "bintray",
          "desc": null,
          "system_ids": [
            "io.janusproject.guava:guava"
          ],
          "versions": [
            "19.0.0",
            "17.0.2",
            "17.0"
          ],
          "latest_version": "19.0.0"
        },
        {
          "name": "com.google.guava:guava",
          "repo": "jcenter",
          "owner": "bintray",
          "desc": "Guava is a suite of core and expanded libraries that include\n    utility classes, google's collections, io classes, and much\n    much more.\n\n    Guava has two code dependencies - javax.annotation\n    per the JSR-305 spec and javax.inject per the JSR-330 spec.",
          "system_ids": [
            "com.google.guava:guava"
          ],
          "versions": [
            "19.0",
            "19.0-rc3",
            "19.0-rc2",
            "19.0-rc1",
            "18.0",
            "18.0-rc2",
            "18.0-rc1",
            "11.0.2-atlassian-02",
            "17.0",
            "17.0-rc2",
            "17.0-rc1",
            "16.0.1",
            "16.0",
            "16.0-rc1",
            "15.0",
            "15.0-rc1",
            "14.0.1",
            "14.0",
            "14.0-rc3",
            "14.0-rc2",
            "14.0-rc1",
            "13.0.1",
            "13.0",
            "13.0-final",
            "13.0-rc2",
            "13.0-rc1",
            "12.0.1",
            "12.0",
            "12.0-rc2",
            "12.0-rc1",
            "11.0.2-atlassian-01",
            "11.0.2",
            "11.0.1",
            "11.0",
            "11.0-rc1",
            "10.0.1",
            "10.0",
            "10.0-rc3",
            "10.0-rc2",
            "10.0-rc1",
            "r09",
            "r08",
            "r07",
            "r06",
            "r05",
            "r03"
          ],
          "latest_version": "19.0"
        }
      ]
     */
    JsonParser parser = new JsonParser();
    JsonArray array = parser.parse(response).getAsJsonArray();
    int totalFound = array.size();
    List<FoundArtifact> artifacts = Lists.newArrayListWithExpectedSize(totalFound);
    for (int i = 0; i < totalFound; i++) {
        JsonObject root = array.get(i).getAsJsonObject();
        String name = root.getAsJsonPrimitive("name").getAsString();
        List<GradleVersion> availableVersions = Lists.newArrayList();
        JsonArray versions = root.getAsJsonArray("versions");
        versions.forEach(element -> {
            String version = element.getAsString();
            availableVersions.add(GradleVersion.parse(version));
        });
        List<String> coordinate = Splitter.on(GRADLE_PATH_SEPARATOR).splitToList(name);
        assert coordinate.size() == 2;
        artifacts.add(new FoundArtifact(getName(), coordinate.get(0), coordinate.get(1), availableVersions));
    }
    return new SearchResult(getName(), artifacts, totalFound);
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) GradleVersion(com.android.ide.common.repository.GradleVersion) JsonParser(com.google.gson.JsonParser)

Example 43 with GradleVersion

use of com.android.ide.common.repository.GradleVersion in project android by JetBrains.

the class MavenCentralRepository method parse.

@VisibleForTesting
@NotNull
SearchResult parse(@NotNull Reader response) throws JDOMException, IOException {
    /*
    Sample response:

    <response>
    <result name="response" numFound="409" start="41">
        <doc>
            <str name="a">guice-bean</str>
            <arr name="ec">
                <str>.pom</str>
            </arr>
            <str name="g">org.sonatype.spice.inject</str>
            <str name="id">org.sonatype.spice.inject:guice-bean</str>
            <str name="latestVersion">1.3.4</str>
            <str name="p">pom</str>
            <str name="repositoryId">central</str>
            <arr name="text">
                <str>org.sonatype.spice.inject</str>
                <str>guice-bean</str>
                <str>.pom</str>
            </arr>
            <long name="timestamp">1283070402000</long>
            <int name="versionCount">10</int>
        </doc>
    </result>
    </response>
    */
    List<FoundArtifact> artifacts = Lists.newArrayList();
    int totalFound = 0;
    Element root = JdomKt.loadElement(response);
    Element result = root.getChild("result");
    if (result != null) {
        String found = result.getAttributeValue("numFound");
        if (isNotEmpty(found)) {
            try {
                totalFound = Integer.parseInt(found);
            } catch (NumberFormatException ignored) {
            }
        }
        for (Element doc : result.getChildren("doc")) {
            String id = null;
            GradleVersion latestVersion = null;
            for (Element str : doc.getChildren("str")) {
                String name = str.getAttributeValue("name");
                if ("id".equals(name)) {
                    id = str.getTextTrim();
                } else if ("latestVersion".equals(name)) {
                    String value = str.getTextTrim();
                    if (isNotEmpty(value)) {
                        latestVersion = GradleVersion.parse(value);
                    }
                }
                if (isNotEmpty(id) && latestVersion != null) {
                    List<String> coordinate = Splitter.on(GRADLE_PATH_SEPARATOR).splitToList(id);
                    assert coordinate.size() == 2;
                    artifacts.add(new FoundArtifact(getName(), coordinate.get(0), coordinate.get(1), latestVersion));
                    break;
                }
            }
        }
    }
    return new SearchResult(getName(), artifacts, totalFound);
}
Also used : Element(org.jdom.Element) GradleVersion(com.android.ide.common.repository.GradleVersion) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NotNull(org.jetbrains.annotations.NotNull)

Example 44 with GradleVersion

use of com.android.ide.common.repository.GradleVersion in project android by JetBrains.

the class GradleLocalCache method findLatestVersionInGradleCache.

@Nullable
private static GradleVersion findLatestVersionInGradleCache(@NotNull File gradleServicePath, @NotNull String groupId, @NotNull String artifactId, @Nullable String versionPrefix) {
    File gradleCacheFolder = new File(gradleServicePath, "caches");
    if (!gradleCacheFolder.isDirectory()) {
        return null;
    }
    List<GradleVersion> versions = Lists.newArrayList();
    for (File moduleFolder : notNullize(gradleCacheFolder.listFiles())) {
        if (!isDirectoryWithNamePrefix(moduleFolder, "modules-")) {
            continue;
        }
        for (File metadataFolder : notNullize(moduleFolder.listFiles())) {
            if (!isDirectoryWithNamePrefix(metadataFolder, "metadata-")) {
                continue;
            }
            File versionFolder = new File(metadataFolder, join("descriptors", groupId, artifactId));
            if (!versionFolder.isDirectory()) {
                continue;
            }
            for (File versionFile : notNullize(versionFolder.listFiles())) {
                String version = versionFile.getName();
                if ((versionPrefix == null || version.startsWith(versionPrefix)) && !version.isEmpty() && Character.isDigit(version.charAt(0))) {
                    GradleVersion parsedVersion = GradleVersion.tryParse(version);
                    if (parsedVersion != null) {
                        versions.add(parsedVersion);
                    }
                }
            }
        }
    }
    int versionCount = versions.size();
    if (versionCount == 1) {
        return versions.get(0);
    } else if (versionCount > 1) {
        sort(versions);
        return versions.get(versionCount - 1);
    }
    return null;
}
Also used : GradleVersion(com.android.ide.common.repository.GradleVersion) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 45 with GradleVersion

use of com.android.ide.common.repository.GradleVersion 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

GradleVersion (com.android.ide.common.repository.GradleVersion)77 File (java.io.File)25 Test (org.junit.Test)16 Module (com.intellij.openapi.module.Module)12 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)11 Nullable (org.jetbrains.annotations.Nullable)10 Project (com.intellij.openapi.project.Project)6 NotNull (org.jetbrains.annotations.NotNull)6 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)5 IOException (java.io.IOException)5 AndroidPluginInfo (com.android.tools.idea.gradle.plugin.AndroidPluginInfo)4 FixAndroidGradlePluginVersionHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.FixAndroidGradlePluginVersionHyperlink)3 NotificationHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink)3 SyncMessage (com.android.tools.idea.gradle.project.sync.messages.SyncMessage)3 ZipFile (java.util.zip.ZipFile)3 VisibleForTesting (com.android.annotations.VisibleForTesting)2 Variant (com.android.builder.model.Variant)2 IDevice (com.android.ddmlib.IDevice)2 Revision (com.android.repository.Revision)2 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)2