Search in sources :

Example 1 with LibraryVersionsManager

use of com.android.tools.idea.gradle.editor.value.LibraryVersionsManager in project android by JetBrains.

the class GradleEditorDependencyParser method parse.

/**
   * Tries to build a {@link GradleEditorEntity} assuming that given assignment hold dependency definition
   * (like <code>'compile "my-group:my-value:my-version"'</code>).
   *
   * @param assignment  assignment that might hold external dependency definition
   * @param context     current context
   * @return            an entity for the dependency extracted from the given assignment (if any);
   *                    <code>null</code> otherwise
   */
@Nullable
public GradleEditorEntity parse(@NotNull Assignment assignment, @NotNull GradleEditorModelParseContext context) {
    if (assignment.value == null) {
        return null;
    }
    String dependencyText = assignment.value.value;
    if (dependencyText.isEmpty() && assignment.rValueString != null) {
        dependencyText = assignment.rValueString;
    }
    Matcher matcher = EXTERNAL_DEPENDENCY_PATTERN.matcher(dependencyText);
    if (!matcher.matches()) {
        return null;
    }
    GradleEditorSourceBinding scopeSourceBinding = buildSourceBinding(assignment.lValueLocation, context.getProject());
    if (scopeSourceBinding == null) {
        return null;
    }
    DependencyDimension group = parseDimension(TextRange.create(matcher.start(1), matcher.end(1)), assignment, dependencyText, context);
    if (group == null) {
        return null;
    }
    DependencyDimension artifact = parseDimension(TextRange.create(matcher.start(2), matcher.end(2)), assignment, dependencyText, context);
    if (artifact == null) {
        return null;
    }
    DependencyDimension version = parseDimension(TextRange.create(matcher.start(3), matcher.end(3)), assignment, dependencyText, context);
    if (version == null) {
        return null;
    }
    GradleEditorSourceBinding entityLocation = buildSourceBinding(assignment, context.getProject());
    if (entityLocation == null) {
        return null;
    }
    Set<GradleEditorEntityMetaData> metaData = Sets.newHashSet();
    metaData.add(StdGradleEditorEntityMetaData.REMOVABLE);
    if (context.getTargetFile().equals(assignment.lValueLocation.file)) {
        if (!assignment.codeStructure.isEmpty()) {
            String s = assignment.codeStructure.get(0);
            if (GradleEditorDsl.ALL_PROJECTS_SECTION.equals(s) || GradleEditorDsl.SUB_PROJECT_SECTION.equals(s)) {
                metaData.add(StdGradleEditorEntityMetaData.OUTGOING);
            }
        }
    } else {
        metaData.add(StdGradleEditorEntityMetaData.INJECTED);
    }
    final GradleEditorEntityValueManager versionValueManager;
    if (group.value.isEmpty() || artifact.value.isEmpty()) {
        versionValueManager = GradleEditorEntityValueManager.NO_OP;
    } else {
        versionValueManager = new LibraryVersionsManager(group.value, artifact.value);
    }
    return new ExternalDependencyGradleEditorEntity(assignment.lValue.name, Arrays.asList(scopeSourceBinding), group.value, group.sourceBindings, artifact.value, artifact.sourceBindings, version.value, version.sourceBindings, entityLocation, version.declarationValueLocation, versionValueManager, metaData);
}
Also used : LibraryVersionsManager(com.android.tools.idea.gradle.editor.value.LibraryVersionsManager) Matcher(java.util.regex.Matcher) GradleEditorSourceBinding(com.android.tools.idea.gradle.editor.entity.GradleEditorSourceBinding) GradleEditorEntityValueManager(com.android.tools.idea.gradle.editor.value.GradleEditorEntityValueManager) StdGradleEditorEntityMetaData(com.android.tools.idea.gradle.editor.metadata.StdGradleEditorEntityMetaData) GradleEditorEntityMetaData(com.android.tools.idea.gradle.editor.metadata.GradleEditorEntityMetaData) ExternalDependencyGradleEditorEntity(com.android.tools.idea.gradle.editor.entity.ExternalDependencyGradleEditorEntity) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with LibraryVersionsManager

use of com.android.tools.idea.gradle.editor.value.LibraryVersionsManager in project android by JetBrains.

the class GradleEditorModelParserV1 method buildGradlePluginVersion.

@Nullable
public static VersionGradleEditorEntity buildGradlePluginVersion(@NotNull final GradleEditorModelParseContext context) {
    final Variable variable = new Variable(CLASSPATH_CONFIGURATION, PROJECT_QUALIFIER);
    GradleEditorDependencyParser dependencyParser = new GradleEditorDependencyParser();
    for (Assignment assignment : context.getAssignments(variable)) {
        GradleEditorEntity entity = dependencyParser.parse(assignment, context);
        if (!(entity instanceof ExternalDependencyGradleEditorEntity)) {
            continue;
        }
        ExternalDependencyGradleEditorEntity e = (ExternalDependencyGradleEditorEntity) entity;
        int i = SdkConstants.GRADLE_PLUGIN_NAME.indexOf(':');
        String groupId = SdkConstants.GRADLE_PLUGIN_NAME.substring(0, i);
        String artifactId = SdkConstants.GRADLE_PLUGIN_NAME.substring(i + 1);
        if (artifactId.endsWith(":")) {
            artifactId = artifactId.substring(0, artifactId.length() - 1);
        }
        if (!groupId.equals(e.getGroupId()) || !artifactId.equals(e.getArtifactId())) {
            continue;
        }
        final Set<GradleEditorEntityMetaData> metaData = Sets.newHashSet(e.getMetaData());
        if (!e.getMetaData().contains(StdGradleEditorEntityMetaData.INJECTED)) {
            // We consider that gradle plugin entity is injected if it's declared in a parent file.
            metaData.add(StdGradleEditorEntityMetaData.OUTGOING);
        }
        metaData.remove(StdGradleEditorEntityMetaData.REMOVABLE);
        return new VersionGradleEditorEntity(AndroidBundle.message("android.gradle.editor.version.gradle.plugin"), e.getVersionSourceBindings(), e.getEntityLocation(), metaData, e.getDeclarationValueLocation(), e.getVersion(), new LibraryVersionsManager(groupId, artifactId), "https://developer.android.com/studio/build/index.html#build-files");
    }
    return null;
}
Also used : LibraryVersionsManager(com.android.tools.idea.gradle.editor.value.LibraryVersionsManager) StdGradleEditorEntityMetaData(com.android.tools.idea.gradle.editor.metadata.StdGradleEditorEntityMetaData) GradleEditorEntityMetaData(com.android.tools.idea.gradle.editor.metadata.GradleEditorEntityMetaData) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GradleEditorEntityMetaData (com.android.tools.idea.gradle.editor.metadata.GradleEditorEntityMetaData)2 StdGradleEditorEntityMetaData (com.android.tools.idea.gradle.editor.metadata.StdGradleEditorEntityMetaData)2 LibraryVersionsManager (com.android.tools.idea.gradle.editor.value.LibraryVersionsManager)2 Nullable (org.jetbrains.annotations.Nullable)2 ExternalDependencyGradleEditorEntity (com.android.tools.idea.gradle.editor.entity.ExternalDependencyGradleEditorEntity)1 GradleEditorSourceBinding (com.android.tools.idea.gradle.editor.entity.GradleEditorSourceBinding)1 GradleEditorEntityValueManager (com.android.tools.idea.gradle.editor.value.GradleEditorEntityValueManager)1 Matcher (java.util.regex.Matcher)1