Search in sources :

Example 1 with GradleEditorEntityValueManager

use of com.android.tools.idea.gradle.editor.value.GradleEditorEntityValueManager 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)

Aggregations

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