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;
}
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;
}
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());
}
}
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);
}
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);
}
Aggregations