Search in sources :

Example 1 with GradleDslFile

use of com.android.tools.idea.gradle.dsl.parser.GradleDslFile in project android by JetBrains.

the class GradleDslExpression method resolveReferenceInParentModules.

@Nullable
private static GradleDslElement resolveReferenceInParentModules(@NotNull GradleDslFile dslFile, @NotNull String referenceText) {
    GradleDslFile parentDslFile = dslFile.getParentModuleDslFile();
    while (parentDslFile != null) {
        ExtDslElement extDslElement = parentDslFile.getPropertyElement(EXT_BLOCK_NAME, ExtDslElement.class);
        if (extDslElement != null) {
            GradleDslElement extPropertyElement = extDslElement.getPropertyElement(referenceText);
            if (extPropertyElement != null) {
                return extPropertyElement;
            }
        }
        if (parentDslFile.getParentModuleDslFile() == null) {
            // resolveReferenceInSameModule method.
            return null;
        }
        GradleDslElement propertyElement = resolveReferenceInPropertiesFile(parentDslFile, referenceText);
        if (propertyElement != null) {
            return propertyElement;
        }
        parentDslFile = parentDslFile.getParentModuleDslFile();
    }
    return null;
}
Also used : ExtDslElement(com.android.tools.idea.gradle.dsl.parser.ext.ExtDslElement) GradleDslFile(com.android.tools.idea.gradle.dsl.parser.GradleDslFile) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with GradleDslFile

use of com.android.tools.idea.gradle.dsl.parser.GradleDslFile in project android by JetBrains.

the class GradleDslExpression method resolveReference.

/**
   * Returns the resolved value of the given {@code referenceText} of type {@code clazz} when the {@code referenceText} is referring to
   * an element with the value of that type, or {@code null} otherwise.
   */
@Nullable
protected <T> T resolveReference(@NotNull String referenceText, @NotNull Class<T> clazz) {
    GradleDslElement searchStartElement = this;
    String searchReferenceText = referenceText;
    List<String> referenceTextSegments = Splitter.on('.').trimResults().omitEmptyStrings().splitToList(referenceText);
    int index = 0;
    int segmentCount = referenceTextSegments.size();
    for (; index < segmentCount; index++) {
        // Resolve the project reference elements like parent, rootProject etc.
        GradleDslFile dslFile = resolveProjectReference(searchStartElement, referenceTextSegments.get(index));
        if (dslFile == null) {
            break;
        }
        // Now lets search in that project build.gradle file.
        searchStartElement = dslFile;
    }
    /* For a project with the below hierarchy ...

    | <GRADLE_USER_HOME>/gradle.properties
    | RootProject
    | - - build.gradle
    | - - gradle.properties
    | - - FirstLevelChildProject
    | - - - - build.gradle
    | - - - - gradle.properties
    | - - - - SecondLevelChildProject
    | - - - - - - build.gradle
    | - - - - - - gralde.properties
    | - - - - - - ThirdLevelChildProject
    | - - - - - - - - build.gradle
    | - - - - - - - - gradle.properties

    the resolution path for a property defined in ThirdLevelChildProject's build.gradle file will be ...

      1. ThirdLevelChildProject/build.gradle
      2. <GRADLE_USER_HOME>/gradle.properties
      3. ThirdLevelChildProject/gradle.properties
      4. RootProject/gradle.properties
      5. SecondLevelChildProject/build.gradle
      6. SecondLevelChildProject/gradle.properties
      7. FirstLevelChildProject/build.gradle
      8. FirstLevelChildProject/gradle.properties
      9. RootProject/build.gradle
    */
    GradleDslElement resolvedElement;
    if (index >= segmentCount) {
        // the reference text is fully resolved by now. ex: if the while text itself is "rootProject" etc.
        resolvedElement = searchStartElement;
    } else {
        // Search in the file the searchStartElement belongs to.
        searchReferenceText = Joiner.on('.').join(referenceTextSegments.subList(index, segmentCount));
        resolvedElement = resolveReferenceInSameModule(searchStartElement, searchReferenceText);
    }
    GradleDslFile dslFile = searchStartElement.getDslFile();
    if (resolvedElement == null) {
        // Now look in the parent projects ext blocks.
        resolvedElement = resolveReferenceInParentModules(dslFile, searchReferenceText);
    }
    if (resolvedElement != null) {
        T result = null;
        if (clazz.isInstance(resolvedElement)) {
            result = clazz.cast(resolvedElement);
        } else if (resolvedElement instanceof GradleDslExpression) {
            result = ((GradleDslExpression) resolvedElement).getValue(clazz);
        }
        if (result != null) {
            setResolvedVariables(ImmutableList.of(new GradleResolvedVariable(referenceText, result, resolvedElement)));
            return result;
        }
    }
    if (clazz.isAssignableFrom(String.class)) {
        if ("rootDir".equals(searchReferenceText)) {
            // resolve the rootDir reference to project root directory.
            return clazz.cast(Projects.getBaseDirPath(dslFile.getProject()).getPath());
        }
        if ("projectDir".equals(searchReferenceText)) {
            // resolve the projectDir reference to module directory.
            return clazz.cast(dslFile.getDirectoryPath().getPath());
        }
        return clazz.cast(referenceText);
    }
    return null;
}
Also used : GradleDslFile(com.android.tools.idea.gradle.dsl.parser.GradleDslFile) GradleResolvedVariable(com.android.tools.idea.gradle.dsl.parser.GradleResolvedVariable) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GradleDslFile

use of com.android.tools.idea.gradle.dsl.parser.GradleDslFile in project android by JetBrains.

the class GradleBuildModel method populateWithParentModuleSubProjectsProperties.

private static void populateWithParentModuleSubProjectsProperties(@NotNull GradleBuildDslFile buildDslFile) {
    GradleSettingsModel gradleSettingsModel = GradleSettingsModel.get(buildDslFile.getProject());
    if (gradleSettingsModel == null) {
        return;
    }
    String modulePath = gradleSettingsModel.moduleWithDirectory(buildDslFile.getDirectoryPath());
    if (modulePath == null) {
        return;
    }
    GradleBuildModel parentModuleModel = gradleSettingsModel.getParentModuleModel(modulePath);
    if (parentModuleModel == null) {
        return;
    }
    GradleDslFile parentModuleDslFile = parentModuleModel.myGradleDslFile;
    buildDslFile.setParentModuleDslFile(parentModuleDslFile);
    SubProjectsDslElement subProjectsDslElement = parentModuleDslFile.getPropertyElement(SUBPROJECTS_BLOCK_NAME, SubProjectsDslElement.class);
    if (subProjectsDslElement == null) {
        return;
    }
    buildDslFile.setParsedElement(SUBPROJECTS_BLOCK_NAME, subProjectsDslElement);
    for (Map.Entry<String, GradleDslElement> entry : subProjectsDslElement.getPropertyElements().entrySet()) {
        buildDslFile.setParsedElement(entry.getKey(), entry.getValue());
    }
}
Also used : SubProjectsDslElement(com.android.tools.idea.gradle.dsl.parser.build.SubProjectsDslElement) GradleDslFile(com.android.tools.idea.gradle.dsl.parser.GradleDslFile) Map(java.util.Map) GradleDslExpressionMap(com.android.tools.idea.gradle.dsl.parser.elements.GradleDslExpressionMap) GradleDslElement(com.android.tools.idea.gradle.dsl.parser.elements.GradleDslElement)

Example 4 with GradleDslFile

use of com.android.tools.idea.gradle.dsl.parser.GradleDslFile in project android by JetBrains.

the class GradleBuildModel method populateSiblingDslFileWithGradlePropertiesFile.

private static void populateSiblingDslFileWithGradlePropertiesFile(@NotNull GradleBuildDslFile buildDslFile) {
    File propertiesFilePath = new File(buildDslFile.getDirectoryPath(), FN_GRADLE_PROPERTIES);
    VirtualFile propertiesFile = findFileByIoFile(propertiesFilePath, true);
    if (propertiesFile == null) {
        return;
    }
    GradlePropertiesModel propertiesModel = parsePropertiesFile(propertiesFile, buildDslFile.getProject(), buildDslFile.getName());
    if (propertiesModel == null) {
        return;
    }
    GradleDslFile propertiesDslFile = propertiesModel.myGradleDslFile;
    buildDslFile.setSiblingDslFile(propertiesDslFile);
    propertiesDslFile.setSiblingDslFile(buildDslFile);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleDslFile(com.android.tools.idea.gradle.dsl.parser.GradleDslFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) GradleDslFile(com.android.tools.idea.gradle.dsl.parser.GradleDslFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) File(java.io.File) GradleUtil.getGradleBuildFile(com.android.tools.idea.gradle.util.GradleUtil.getGradleBuildFile) GradlePropertiesModel.parsePropertiesFile(com.android.tools.idea.gradle.dsl.model.GradlePropertiesModel.parsePropertiesFile)

Example 5 with GradleDslFile

use of com.android.tools.idea.gradle.dsl.parser.GradleDslFile in project android by JetBrains.

the class GradleDslExpression method resolveReferenceInSameModule.

@Nullable
private static GradleDslElement resolveReferenceInSameModule(GradleDslElement startElement, @NotNull String referenceText) {
    // Try to resolve in the build.gradle file the startElement is belongs to.
    GradleDslElement element = startElement;
    while (element != null) {
        if (element instanceof GradlePropertiesDslElement) {
            GradleDslElement propertyElement = ((GradlePropertiesDslElement) element).getPropertyElement(referenceText);
            if (propertyElement != null) {
                return propertyElement;
            }
            if (element instanceof GradleDslFile) {
                ExtDslElement extDslElement = ((GradleDslFile) element).getPropertyElement(EXT_BLOCK_NAME, ExtDslElement.class);
                if (extDslElement != null) {
                    GradleDslElement extPropertyElement = extDslElement.getPropertyElement(referenceText);
                    if (extPropertyElement != null) {
                        return extPropertyElement;
                    }
                }
                break;
            }
        }
        element = element.getParent();
    }
    // TODO: Add support to look at <GRADLE_USER_HOME>/gradle.properties before looking at this module's gradle.properties file.
    // Try to resolve in the gradle.properties file of the startElement's module.
    GradleDslFile dslFile = startElement.getDslFile();
    GradleDslElement propertyElement = resolveReferenceInPropertiesFile(dslFile, referenceText);
    if (propertyElement != null) {
        return propertyElement;
    }
    if (dslFile.getParentModuleDslFile() == null) {
        // This is the root project build.gradle file and there is no further path to look up.
        return null;
    }
    // Try to resolve in the root project gradle.properties file.
    GradleDslFile rootProjectDslFile = dslFile;
    while (true) {
        GradleDslFile parentModuleDslFile = rootProjectDslFile.getParentModuleDslFile();
        if (parentModuleDslFile == null) {
            break;
        }
        rootProjectDslFile = parentModuleDslFile;
    }
    return resolveReferenceInPropertiesFile(rootProjectDslFile, referenceText);
}
Also used : ExtDslElement(com.android.tools.idea.gradle.dsl.parser.ext.ExtDslElement) GradleDslFile(com.android.tools.idea.gradle.dsl.parser.GradleDslFile) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GradleDslFile (com.android.tools.idea.gradle.dsl.parser.GradleDslFile)6 Nullable (org.jetbrains.annotations.Nullable)4 ExtDslElement (com.android.tools.idea.gradle.dsl.parser.ext.ExtDslElement)2 File (java.io.File)2 GradlePropertiesModel.parsePropertiesFile (com.android.tools.idea.gradle.dsl.model.GradlePropertiesModel.parsePropertiesFile)1 GradleSettingsModel (com.android.tools.idea.gradle.dsl.model.GradleSettingsModel)1 GradleResolvedVariable (com.android.tools.idea.gradle.dsl.parser.GradleResolvedVariable)1 SubProjectsDslElement (com.android.tools.idea.gradle.dsl.parser.build.SubProjectsDslElement)1 GradleDslElement (com.android.tools.idea.gradle.dsl.parser.elements.GradleDslElement)1 GradleDslExpressionMap (com.android.tools.idea.gradle.dsl.parser.elements.GradleDslExpressionMap)1 GradleUtil.getGradleBuildFile (com.android.tools.idea.gradle.util.GradleUtil.getGradleBuildFile)1 VfsUtil.findFileByIoFile (com.intellij.openapi.vfs.VfsUtil.findFileByIoFile)1 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 Map (java.util.Map)1 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1