Search in sources :

Example 1 with ComponentVersionReader

use of com.android.tools.idea.gradle.project.sync.compatibility.version.ComponentVersionReader in project android by JetBrains.

the class VersionCompatibilityChecker method collectComponentIncompatibilities.

private void collectComponentIncompatibilities(@NotNull Module module, @NotNull Map<String, VersionIncompatibility> incompatibilitiesByCheck, @NotNull ComponentVersionAndReaderCache cache) {
    for (CompatibilityCheck check : myMetadata.getCompatibilityChecks()) {
        Component component = check.getComponent();
        Pair<ComponentVersionReader, String> readerAndVersion = getComponentVersion(component, module, cache);
        if (readerAndVersion == null) {
            continue;
        }
        String version = readerAndVersion.getSecond();
        if (!component.getVersionRange().contains(version)) {
            continue;
        }
        for (Component requirement : component.getRequirements()) {
            Pair<ComponentVersionReader, String> readerAndRequirementVersion = getComponentVersion(requirement, module, cache);
            if (readerAndRequirementVersion == null) {
                continue;
            }
            String requirementVersion = readerAndRequirementVersion.getSecond();
            if (requirement.getVersionRange().contains(requirementVersion)) {
                continue;
            }
            String id;
            boolean projectLevelCheck = readerAndVersion.getFirst().isProjectLevel();
            String componentName = check.getComponent().getName();
            if (projectLevelCheck) {
                id = componentName;
            } else {
                id = module.getName() + "." + componentName;
            }
            VersionIncompatibility versionIncompatibility = incompatibilitiesByCheck.get(id);
            if (versionIncompatibility == null) {
                ComponentVersionReader reader = readerAndRequirementVersion.getFirst();
                versionIncompatibility = new VersionIncompatibility(module, check, readerAndVersion, requirement, reader);
                incompatibilitiesByCheck.put(id, versionIncompatibility);
            }
            if (readerAndRequirementVersion.getFirst().isProjectLevel()) {
                // If the requirement is at project level, show only one message, instead of one message per module.
                if (!versionIncompatibility.hasMessages()) {
                    String msg = String.format("but project is using version %1$s.", requirementVersion);
                    versionIncompatibility.addMessage(msg);
                }
                continue;
            }
            String msg = String.format("Module '%1$s' is using version %2$s", module.getName(), requirementVersion);
            versionIncompatibility.addMessage(msg);
        }
    }
}
Also used : ComponentVersionReader(com.android.tools.idea.gradle.project.sync.compatibility.version.ComponentVersionReader)

Example 2 with ComponentVersionReader

use of com.android.tools.idea.gradle.project.sync.compatibility.version.ComponentVersionReader in project android by JetBrains.

the class VersionIncompatibility method reportMessages.

void reportMessages(@NotNull Project project) {
    ComponentVersionReader reader = myReaderAndVersion.getFirst();
    String componentName = reader.getComponentName();
    String version = myReaderAndVersion.getSecond();
    String requirementComponentName = myRequirementVersionReader.getComponentName();
    StringBuilder msg = new StringBuilder();
    msg.append(componentName).append(" ").append(version);
    PositionInFile position = reader.getVersionSource(myModule);
    if (!reader.isProjectLevel() && position == null) {
        msg.append(", in module '").append(myModule.getName()).append(",'");
    }
    msg.append(" requires ").append(requirementComponentName).append(" ");
    VersionRange requirementVersionRange = myRequirement.getVersionRange();
    msg.append(requirementVersionRange.getDescription());
    int messageCount = myMessages.size();
    if (messageCount == 1) {
        msg.append(" ").append(myMessages.get(0));
    } else if (messageCount > 1) {
        msg.append("<ul>");
        for (String message : myMessages) {
            msg.append("<li>").append(message).append("</li>");
        }
        msg.append("</ul>");
    }
    MessageType messageType = myCompatibilityCheck.getType();
    SyncMessage message;
    List<String> textLines = new ArrayList<>();
    textLines.add(msg.toString());
    String failureMsg = myRequirement.getFailureMessage();
    if (failureMsg != null) {
        List<String> lines = Splitter.on("\\n").omitEmptyStrings().splitToList(failureMsg);
        textLines.addAll(lines);
    }
    String[] text = toStringArray(textLines);
    if (position != null) {
        message = new SyncMessage(project, DEFAULT_GROUP, messageType, position, text);
    } else {
        message = new SyncMessage(DEFAULT_GROUP, messageType, text);
    }
    message.add(myRequirementVersionReader.getQuickFixes(myModule, requirementVersionRange, position));
    SyncMessages.getInstance(project).report(message);
}
Also used : SyncMessage(com.android.tools.idea.gradle.project.sync.messages.SyncMessage) ArrayList(java.util.ArrayList) PositionInFile(com.android.tools.idea.gradle.util.PositionInFile) VersionRange(com.android.tools.idea.gradle.project.sync.compatibility.version.VersionRange) ComponentVersionReader(com.android.tools.idea.gradle.project.sync.compatibility.version.ComponentVersionReader) MessageType(com.android.tools.idea.gradle.project.sync.messages.MessageType)

Example 3 with ComponentVersionReader

use of com.android.tools.idea.gradle.project.sync.compatibility.version.ComponentVersionReader in project android by JetBrains.

the class VersionIncompatibilityTest method testReportMessagesWithError.

public void testReportMessagesWithError() throws Exception {
    loadSimpleApplication();
    mySyncMessagesStub.clearReportedMessages();
    Module appModule = myModules.getAppModule();
    Component base = new Component("grade", "2.14.1", null);
    Pair<ComponentVersionReader, String> baseReaderAndVersion = Pair.create(GRADLE, "2.14.1");
    String failureMessage = "Wrong Android Gradle plugin version";
    Component requirement = new Component("android-gradle-plugin", "2.1.3", failureMessage);
    base.addRequirement(requirement);
    CompatibilityCheck check = new CompatibilityCheck(base, ERROR);
    VersionIncompatibility incompatibility = new VersionIncompatibility(appModule, check, baseReaderAndVersion, requirement, ANDROID_GRADLE_PLUGIN);
    incompatibility.reportMessages(getProject());
    SyncMessage message = mySyncMessagesStub.getFirstReportedMessage();
    assertNotNull(message);
    assertThat(message.getText()).hasLength(2);
    // @formatter:off
    assertAbout(syncMessage()).that(message).hasType(ERROR).hasMessageLine("Gradle 2.14.1 requires Android Gradle plugin 2.1.3 (or newer)", 0).hasMessageLine(failureMessage, 1);
// @formatter:on
}
Also used : SyncMessage(com.android.tools.idea.gradle.project.sync.messages.SyncMessage) Module(com.intellij.openapi.module.Module) ComponentVersionReader(com.android.tools.idea.gradle.project.sync.compatibility.version.ComponentVersionReader)

Example 4 with ComponentVersionReader

use of com.android.tools.idea.gradle.project.sync.compatibility.version.ComponentVersionReader in project android by JetBrains.

the class VersionCompatibilityChecker method getComponentVersion.

@Nullable
private Pair<ComponentVersionReader, String> getComponentVersion(@NotNull Component component, @NotNull Module module, @NotNull ComponentVersionAndReaderCache cache) {
    String componentName = component.getName();
    // First check if the value is already cached for project
    Pair<ComponentVersionReader, String> readerAndVersion = cache.projectComponents.get(componentName);
    if (readerAndVersion == null) {
        // Value has not been cached for project, check for cached value for module
        Map<String, Pair<ComponentVersionReader, String>> componentVersionsByModule = cache.moduleComponents.get(componentName);
        if (componentVersionsByModule != null) {
            readerAndVersion = componentVersionsByModule.get(module.getName());
        }
    }
    if (readerAndVersion != null) {
        return readerAndVersion;
    }
    // There is no cached value for this component's version. Go ahead and read it from project.
    ComponentVersionReader reader = myMetadata.findComponentVersionReader(componentName);
    if (reader == null) {
        getLogger().info(String.format("Failed to find version reader for component '%1$s'", componentName));
        return null;
    }
    if (!reader.appliesTo(module)) {
        // Silently quit (e.g. getting Android model version from a Java library module)
        return null;
    }
    String version = reader.getComponentVersion(module);
    if (version != null) {
        // Cache the value for potential future use
        readerAndVersion = Pair.create(reader, version);
        if (reader.isProjectLevel()) {
            cache.projectComponents.put(componentName, readerAndVersion);
        } else {
            Map<String, Pair<ComponentVersionReader, String>> componentVersionsByModule = cache.moduleComponents.get(componentName);
            if (componentVersionsByModule == null) {
                componentVersionsByModule = Maps.newHashMap();
                cache.moduleComponents.put(componentName, componentVersionsByModule);
            }
            componentVersionsByModule.put(module.getName(), readerAndVersion);
        }
        return readerAndVersion;
    }
    Project project = module.getProject();
    String msg = String.format("Failed to read version for component '%1$s'", componentName);
    if (reader.isProjectLevel()) {
        msg += String.format(" for project '%1$s'", project.getName());
    } else {
        msg += String.format(" for module '%1$s', in project '%2$s'", module.getName(), project.getName());
    }
    getLogger().info(msg);
    return null;
}
Also used : Project(com.intellij.openapi.project.Project) ComponentVersionReader(com.android.tools.idea.gradle.project.sync.compatibility.version.ComponentVersionReader) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with ComponentVersionReader

use of com.android.tools.idea.gradle.project.sync.compatibility.version.ComponentVersionReader in project android by JetBrains.

the class CompatibilityChecksMetadataTest method loadMetadata.

@Test
public void loadMetadata() throws Exception {
    @Language("XML") String metadataText = "<compatibility version='1'>\n" + "  <check failureType='error'>\n" + "    <component name='gradle' version='[2.4, +)'>\n" + "      <requires name='android-gradle-plugin' version='[1.5.0, +]'>\n" + "        <failureMsg>\n" + "           <![CDATA[\n" + "Please use Android Gradle plugin 1.5.0 or newer.\n" + "]]>\n" + "        </failureMsg>\n" + "      </requires>\n" + "    </component>\n" + "  </check>\n" + "</compatibility>";
    Document document = loadDocument(metadataText);
    CompatibilityChecksMetadata metadata = CompatibilityChecksMetadata.load(document.getRootElement());
    List<CompatibilityCheck> compatibilityChecks = metadata.getCompatibilityChecks();
    assertThat(compatibilityChecks).hasSize(1);
    CompatibilityCheck compatibilityCheck = compatibilityChecks.get(0);
    assertSame(ERROR, compatibilityCheck.getType());
    Component component = compatibilityCheck.getComponent();
    assertEquals("gradle", component.getName());
    // @formatter:off
    assertAbout(versionRange()).that(component.getVersionRange()).hasMinVersion("2.4").hasMaxVersion(null).isMinVersionInclusive(true).isMaxVersionInclusive(false);
    // @formatter:on
    List<Component> requirements = component.getRequirements();
    assertThat(requirements).hasSize(1);
    Component requirement = requirements.get(0);
    assertEquals("android-gradle-plugin", requirement.getName());
    // @formatter:off
    assertAbout(versionRange()).that(requirement.getVersionRange()).hasMinVersion("1.5.0").hasMaxVersion(null).isMinVersionInclusive(true).isMaxVersionInclusive(true);
    // @formatter:on
    assertEquals("Please use Android Gradle plugin 1.5.0 or newer.", requirement.getFailureMessage());
    Map<String, ComponentVersionReader> readers = metadata.getReadersByComponentName();
    assertSame(GRADLE, readers.get("gradle"));
    assertSame(ANDROID_GRADLE_PLUGIN, readers.get("android-gradle-plugin"));
}
Also used : Language(org.intellij.lang.annotations.Language) Document(org.jdom.Document) JDOMUtil.loadDocument(com.intellij.openapi.util.JDOMUtil.loadDocument) ComponentVersionReader(com.android.tools.idea.gradle.project.sync.compatibility.version.ComponentVersionReader) Test(org.junit.Test)

Aggregations

ComponentVersionReader (com.android.tools.idea.gradle.project.sync.compatibility.version.ComponentVersionReader)6 SyncMessage (com.android.tools.idea.gradle.project.sync.messages.SyncMessage)3 Module (com.intellij.openapi.module.Module)2 VersionRange (com.android.tools.idea.gradle.project.sync.compatibility.version.VersionRange)1 MessageType (com.android.tools.idea.gradle.project.sync.messages.MessageType)1 PositionInFile (com.android.tools.idea.gradle.util.PositionInFile)1 Project (com.intellij.openapi.project.Project)1 JDOMUtil.loadDocument (com.intellij.openapi.util.JDOMUtil.loadDocument)1 Pair (com.intellij.openapi.util.Pair)1 ArrayList (java.util.ArrayList)1 Language (org.intellij.lang.annotations.Language)1 Document (org.jdom.Document)1 Nullable (org.jetbrains.annotations.Nullable)1 Test (org.junit.Test)1