Search in sources :

Example 1 with GradleEditorEntityMetaData

use of com.android.tools.idea.gradle.editor.metadata.GradleEditorEntityMetaData 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 GradleEditorEntityMetaData

use of com.android.tools.idea.gradle.editor.metadata.GradleEditorEntityMetaData in project android by JetBrains.

the class GradleEditorDependencyParser method parseDimension.

@Nullable
private static DependencyDimension parseDimension(@NotNull TextRange dimensionRange, @NotNull Assignment assignment, @NotNull String dependencyDeclarationString, @NotNull GradleEditorModelParseContext context) {
    if (assignment.value == null) {
        return null;
    }
    TextRange dimensionRangeInFile = dimensionRange.shiftRight(assignment.value.location.range.getStartOffset());
    List<Variable> dependencies = Lists.newArrayList();
    for (Map.Entry<Variable, Location> entry : assignment.dependencies.entries()) {
        if (dimensionRangeInFile.contains(entry.getValue().range)) {
            dependencies.add(entry.getKey());
        }
    }
    String dimensionValue = dependencyDeclarationString.substring(dimensionRange.getStartOffset(), dimensionRange.getEndOffset());
    List<GradleEditorSourceBinding> sourceBindings = Lists.newArrayList();
    if (dependencies.isEmpty() || dependencies.size() > 1 || // an actual value definition here.
    !GradleEditorModelUtil.isVariable(dimensionValue, dependencies.get(0).name)) {
        TextRange rangeToUse = assignment.value.location.range.cutOut(dimensionRange);
        if (dimensionValue.startsWith("${") && dimensionValue.endsWith("}") && !"${}".equals(dimensionValue)) {
            // There is a possible case that dependency coordinate is defined like "my-group:my-artifact:${var1 + var2}". We want
            // to point to the 'var1 + var2' instead of '${var1 + var2}' then.
            rangeToUse = TextRange.create(rangeToUse.getStartOffset() + "${".length(), rangeToUse.getEndOffset() - "}".length());
            dimensionValue = "";
        }
        Location dimensionLocation = new Location(assignment.value.location.file, rangeToUse);
        GradleEditorSourceBinding sourceBinding = buildSourceBinding(dimensionLocation, context.getProject());
        if (sourceBinding == null) {
            return null;
        }
        sourceBindings.add(sourceBinding);
    } else {
        dimensionValue = "";
    }
    GradleEditorSourceBinding dimensionLocation = buildSourceBinding(new Location(assignment.lValueLocation.file, dimensionRangeInFile), context.getProject());
    if (dimensionLocation == null) {
        return null;
    }
    Set<GradleEditorEntityMetaData> metaData = assignment.lValueLocation.file.equals(context.getTargetFile()) ? Collections.<GradleEditorEntityMetaData>emptySet() : Collections.singleton(StdGradleEditorEntityMetaData.INJECTED);
    if (dependencies.isEmpty()) {
        return new DependencyDimension(dimensionValue, sourceBindings, dimensionLocation, metaData);
    }
    GradleEditorModelUtil.EntityInfo entityInfo = GradleEditorModelUtil.collectInfo(dependencies, context, null);
    String valueToUse = sourceBindings.isEmpty() ? entityInfo.value : "";
    sourceBindings.addAll(entityInfo.sourceBindings);
    return new DependencyDimension(valueToUse, sourceBindings, dimensionLocation, metaData);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) StdGradleEditorEntityMetaData(com.android.tools.idea.gradle.editor.metadata.StdGradleEditorEntityMetaData) GradleEditorEntityMetaData(com.android.tools.idea.gradle.editor.metadata.GradleEditorEntityMetaData) GradleEditorSourceBinding(com.android.tools.idea.gradle.editor.entity.GradleEditorSourceBinding) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GradleEditorEntityMetaData

use of com.android.tools.idea.gradle.editor.metadata.GradleEditorEntityMetaData 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)3 StdGradleEditorEntityMetaData (com.android.tools.idea.gradle.editor.metadata.StdGradleEditorEntityMetaData)3 Nullable (org.jetbrains.annotations.Nullable)3 GradleEditorSourceBinding (com.android.tools.idea.gradle.editor.entity.GradleEditorSourceBinding)2 LibraryVersionsManager (com.android.tools.idea.gradle.editor.value.LibraryVersionsManager)2 ExternalDependencyGradleEditorEntity (com.android.tools.idea.gradle.editor.entity.ExternalDependencyGradleEditorEntity)1 GradleEditorEntityValueManager (com.android.tools.idea.gradle.editor.value.GradleEditorEntityValueManager)1 TextRange (com.intellij.openapi.util.TextRange)1 Matcher (java.util.regex.Matcher)1