use of com.android.tools.idea.gradle.dsl.parser.GradleResolvedVariable in project android by JetBrains.
the class GradleValue method getResolvedVariables.
@NotNull
public Map<String, GradleNotNullValue<Object>> getResolvedVariables() {
if (myDslElement == null) {
return ImmutableMap.of();
}
ImmutableMap.Builder<String, GradleNotNullValue<Object>> builder = ImmutableMap.builder();
for (GradleResolvedVariable variable : myDslElement.getResolvedVariables()) {
String variableName = variable.getVariableName();
Object resolvedValue = variable.getValue();
GradleDslElement element = variable.getElement();
builder.put(variableName, new GradleNotNullValue<>(element, resolvedValue));
}
return builder.build();
}
use of com.android.tools.idea.gradle.dsl.parser.GradleResolvedVariable 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.GradleResolvedVariable in project android by JetBrains.
the class GradleDslLiteral method getValue.
@Override
@Nullable
public Object getValue() {
if (myUnsavedValue != null) {
return myUnsavedValue;
}
if (myExpression == null) {
return null;
}
Object value = ((GrLiteral) myExpression).getValue();
if (value != null) {
return value;
}
if (myExpression instanceof GrString) {
// String literal with variables. ex: compileSdkVersion = "$ANDROID-${VERSION}"
String literalText = myExpression.getText();
if (isQuotedString(literalText)) {
literalText = unquoteString(literalText);
}
List<GradleResolvedVariable> resolvedVariables = Lists.newArrayList();
GrStringInjection[] injections = ((GrString) myExpression).getInjections();
for (GrStringInjection injection : injections) {
String variableName = null;
GrClosableBlock closableBlock = injection.getClosableBlock();
if (closableBlock != null) {
String blockText = closableBlock.getText();
variableName = blockText.substring(1, blockText.length() - 1);
} else {
GrExpression expression = injection.getExpression();
if (expression != null) {
variableName = expression.getText();
}
}
if (!isEmpty(variableName)) {
GradleDslExpression resolvedExpression = resolveReference(variableName, GradleDslExpression.class);
if (resolvedExpression != null) {
Object resolvedValue = resolvedExpression.getValue();
if (resolvedValue != null) {
resolvedVariables.add(new GradleResolvedVariable(variableName, resolvedValue, resolvedExpression));
literalText = literalText.replace(injection.getText(), resolvedValue.toString());
}
}
}
}
setResolvedVariables(resolvedVariables);
return literalText;
}
return null;
}
Aggregations