Search in sources :

Example 1 with GradleNotNullValue

use of com.android.tools.idea.gradle.dsl.model.values.GradleNotNullValue in project android by JetBrains.

the class ArtifactDependencyTest method testParseMapNotationWithVariables.

public void testParseMapNotationWithVariables() throws IOException {
    String text = "ext {\n" + "    guavaVersion = '18.0'\n" + "}\n" + "dependencies {\n" + "    compile group: 'com.google.guava', name: 'guava', version: \"$guavaVersion\"\n" + "}";
    writeToBuildFile(text);
    GradleBuildModel buildModel = getGradleBuildModel();
    DependenciesModel dependenciesModel = buildModel.dependencies();
    List<ArtifactDependencyModel> dependencies = dependenciesModel.artifacts();
    assertThat(dependencies).hasSize(1);
    ExpectedArtifactDependency expected = new ExpectedArtifactDependency(COMPILE, "guava", "com.google.guava", "18.0");
    ArtifactDependencyModel guavaDependencyModel = dependencies.get(0);
    expected.assertMatches(guavaDependencyModel);
    GradleNotNullValue<String> guavaDependency = guavaDependencyModel.compactNotation();
    verifyGradleValue(guavaDependency, "dependencies.compile", "group: 'com.google.guava', name: 'guava', version: \"$guavaVersion\"");
    assertEquals(expected.compactNotation(), guavaDependency.value());
    Map<String, GradleNotNullValue<Object>> guavaResolvedVariables = guavaDependency.getResolvedVariables();
    assertEquals(1, guavaResolvedVariables.size());
    GradleNotNullValue<Object> guavaVersionVariable = guavaResolvedVariables.get("guavaVersion");
    assertNotNull(guavaVersionVariable);
    verifyGradleValue(guavaVersionVariable, "ext.guavaVersion", "'18.0'");
    assertEquals("18.0", guavaVersionVariable.value());
    assertEquals(0, guavaVersionVariable.getResolvedVariables().size());
    // Now test that resolved variables are not reported for group and name properties.
    GradleNullableValue<String> group = guavaDependencyModel.group();
    assertEquals("com.google.guava", group.value());
    assertEquals(0, group.getResolvedVariables().size());
    GradleNotNullValue<String> name = guavaDependencyModel.name();
    assertEquals("guava", name.value());
    assertEquals(0, name.getResolvedVariables().size());
    // and thee guavaVersion variable is reported for version property.
    GradleNullableValue<String> version = guavaDependencyModel.version();
    verifyGradleValue(version, "dependencies.compile.version", "\"$guavaVersion\"");
    assertEquals("18.0", version.value());
    guavaResolvedVariables = version.getResolvedVariables();
    assertEquals(1, guavaResolvedVariables.size());
    guavaVersionVariable = guavaResolvedVariables.get("guavaVersion");
    assertNotNull(guavaVersionVariable);
    verifyGradleValue(guavaVersionVariable, "ext.guavaVersion", "'18.0'");
    assertEquals("18.0", guavaVersionVariable.value());
    assertEquals(0, guavaVersionVariable.getResolvedVariables().size());
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) GradleNotNullValue(com.android.tools.idea.gradle.dsl.model.values.GradleNotNullValue)

Example 2 with GradleNotNullValue

use of com.android.tools.idea.gradle.dsl.model.values.GradleNotNullValue in project android by JetBrains.

the class ArtifactDependencyTest method testParseCompactNotationClosureWithVariables.

public void testParseCompactNotationClosureWithVariables() throws IOException {
    String text = "ext {\n" + "    appcompat = 'com.android.support:appcompat-v7:22.1.1'\n" + "    guavaVersion = '18.0'\n" + "}\n" + "dependencies {\n" + "    compile(appcompat, \"com.google.guava:guava:$guavaVersion\") {\n" + "    }\n" + "}";
    writeToBuildFile(text);
    GradleBuildModel buildModel = getGradleBuildModel();
    DependenciesModel dependenciesModel = buildModel.dependencies();
    List<ArtifactDependencyModel> dependencies = dependenciesModel.artifacts();
    assertThat(dependencies).hasSize(2);
    ExpectedArtifactDependency expected = new ExpectedArtifactDependency(COMPILE, "appcompat-v7", "com.android.support", "22.1.1");
    ArtifactDependencyModel appcompatDependencyModel = dependencies.get(0);
    expected.assertMatches(appcompatDependencyModel);
    GradleNotNullValue<String> appcompatDependency = appcompatDependencyModel.compactNotation();
    verifyGradleValue(appcompatDependency, "dependencies.compile.compile", "appcompat");
    assertEquals(expected.compactNotation(), appcompatDependency.value());
    Map<String, GradleNotNullValue<Object>> appcompatResolvedVariables = appcompatDependency.getResolvedVariables();
    assertEquals(1, appcompatResolvedVariables.size());
    GradleNotNullValue<Object> appcompatVariable = appcompatResolvedVariables.get("appcompat");
    assertNotNull(appcompatVariable);
    verifyGradleValue(appcompatVariable, "ext.appcompat", "'com.android.support:appcompat-v7:22.1.1'");
    assertEquals("com.android.support:appcompat-v7:22.1.1", appcompatVariable.value());
    assertEquals(0, appcompatVariable.getResolvedVariables().size());
    expected = new ExpectedArtifactDependency(COMPILE, "guava", "com.google.guava", "18.0");
    ArtifactDependencyModel guavaDependencyModel = dependencies.get(1);
    expected.assertMatches(guavaDependencyModel);
    GradleNotNullValue<String> guavaDependency = guavaDependencyModel.compactNotation();
    verifyGradleValue(guavaDependency, "dependencies.compile.compile", "\"com.google.guava:guava:$guavaVersion\"");
    assertEquals(expected.compactNotation(), guavaDependency.value());
    Map<String, GradleNotNullValue<Object>> guavaResolvedVariables = guavaDependency.getResolvedVariables();
    assertEquals(1, guavaResolvedVariables.size());
    GradleNotNullValue<Object> guavaVersionVariable = guavaResolvedVariables.get("guavaVersion");
    assertNotNull(guavaVersionVariable);
    verifyGradleValue(guavaVersionVariable, "ext.guavaVersion", "'18.0'");
    assertEquals("18.0", guavaVersionVariable.value());
    assertEquals(0, guavaVersionVariable.getResolvedVariables().size());
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) GradleNotNullValue(com.android.tools.idea.gradle.dsl.model.values.GradleNotNullValue)

Example 3 with GradleNotNullValue

use of com.android.tools.idea.gradle.dsl.model.values.GradleNotNullValue in project android by JetBrains.

the class ExtModelTest method testResolveMultiModuleExtPropertyFromPropertiesFileWithHistory.

public void testResolveMultiModuleExtPropertyFromPropertiesFileWithHistory() throws IOException {
    String settingsText = "include ':" + SUB_MODULE_NAME + "'";
    String mainModulePropertiesText = "first=value_from_gradle_properties";
    String mainModuleBuildText = "ext.second = first";
    String subModuleBuildText = "ext.third = second";
    writeToSettingsFile(settingsText);
    writeToPropertiesFile(mainModulePropertiesText);
    writeToBuildFile(mainModuleBuildText);
    writeToSubModuleBuildFile(subModuleBuildText);
    GradleBuildModel buildModel = getSubModuleGradleBuildModel();
    ExtModel extModel = buildModel.ext();
    GradleNullableValue<String> third = extModel.getLiteralProperty("third", String.class);
    verifyGradleValue(third, "ext.third", "second", toSystemIndependentPath(mySubModuleBuildFile.getPath()));
    assertEquals("value_from_gradle_properties", third.value());
    Map<String, GradleNotNullValue<Object>> thirdResolvedVariables = third.getResolvedVariables();
    assertEquals(1, thirdResolvedVariables.size());
    GradleNotNullValue<Object> second = thirdResolvedVariables.get("second");
    assertNotNull(second);
    verifyGradleValue(second, "ext.second", "first", toSystemIndependentPath(myBuildFile.getPath()));
    assertEquals("value_from_gradle_properties", second.value());
    Map<String, GradleNotNullValue<Object>> secondResolvedVariables = second.getResolvedVariables();
    assertEquals(1, secondResolvedVariables.size());
    GradleNotNullValue<Object> first = secondResolvedVariables.get("first");
    assertNotNull(first);
    assertEquals(toSystemIndependentPath(myPropertiesFile.getPath()), toSystemIndependentPath(first.getFile().getPath()));
    assertEquals("first", first.getPropertyName());
    // There are no psi elements in the properties file.
    assertNull(first.getPsiElement());
    assertNull(first.getDslText());
    assertEquals("value_from_gradle_properties", first.value());
    assertEquals(0, first.getResolvedVariables().size());
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) GradleNotNullValue(com.android.tools.idea.gradle.dsl.model.values.GradleNotNullValue)

Example 4 with GradleNotNullValue

use of com.android.tools.idea.gradle.dsl.model.values.GradleNotNullValue in project android by JetBrains.

the class BuildTypeModel method buildConfigFields.

@Nullable
public List<GradleNotNullValue<BuildConfigField>> buildConfigFields() {
    List<Pair<GradleDslExpressionList, TypeNameValueElement>> typeNameValueElements = getTypeNameValueElements(BUILD_CONFIG_FIELD);
    if (typeNameValueElements == null) {
        return null;
    }
    List<GradleNotNullValue<BuildConfigField>> buildConfigFields = Lists.newArrayListWithCapacity(typeNameValueElements.size());
    for (Pair<GradleDslExpressionList, TypeNameValueElement> pair : typeNameValueElements) {
        GradleDslExpressionList listElement = pair.getFirst();
        TypeNameValueElement typeNameValueElement = pair.getSecond();
        buildConfigFields.add(new GradleNotNullValue<>(listElement, new BuildConfigField(typeNameValueElement.type(), typeNameValueElement.name(), typeNameValueElement.value())));
    }
    return buildConfigFields;
}
Also used : GradleDslExpressionList(com.android.tools.idea.gradle.dsl.parser.elements.GradleDslExpressionList) GradleNotNullValue(com.android.tools.idea.gradle.dsl.model.values.GradleNotNullValue) Pair(com.android.utils.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with GradleNotNullValue

use of com.android.tools.idea.gradle.dsl.model.values.GradleNotNullValue in project android by JetBrains.

the class GradleBuildModel method appliedPlugins.

@NotNull
public List<GradleNotNullValue<String>> appliedPlugins() {
    ApplyDslElement applyDslElement = myGradleDslFile.getPropertyElement(APPLY_BLOCK_NAME, ApplyDslElement.class);
    if (applyDslElement == null) {
        return ImmutableList.of();
    }
    List<GradleNotNullValue<String>> listProperty = applyDslElement.getListProperty(PLUGIN, String.class);
    if (listProperty == null) {
        return ImmutableList.of();
    }
    List<GradleNotNullValue<String>> plugins = new ArrayList<>();
    Set<String> pluginValues = new HashSet<>();
    for (GradleNotNullValue<String> plugin : listProperty) {
        if (pluginValues.add(plugin.value())) {
            // Avoid duplicate plugin entries.
            plugins.add(plugin);
        }
    }
    for (GradleDslExpressionMap toBeAppliedPlugin : myToBeAppliedPlugins) {
        GradleNullableValue<String> plugin = toBeAppliedPlugin.getLiteralProperty(PLUGIN, String.class);
        assert plugin instanceof GradleNotNullValue;
        if (pluginValues.add(plugin.value())) {
            // Avoid duplicate plugin entries.
            plugins.add((GradleNotNullValue<String>) plugin);
        }
    }
    return plugins;
}
Also used : GradleDslExpressionMap(com.android.tools.idea.gradle.dsl.parser.elements.GradleDslExpressionMap) ApplyDslElement(com.android.tools.idea.gradle.dsl.parser.apply.ApplyDslElement) ArrayList(java.util.ArrayList) GradleNotNullValue(com.android.tools.idea.gradle.dsl.model.values.GradleNotNullValue) HashSet(com.intellij.util.containers.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GradleNotNullValue (com.android.tools.idea.gradle.dsl.model.values.GradleNotNullValue)9 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)7 ApplyDslElement (com.android.tools.idea.gradle.dsl.parser.apply.ApplyDslElement)1 GradleDslExpressionList (com.android.tools.idea.gradle.dsl.parser.elements.GradleDslExpressionList)1 GradleDslExpressionMap (com.android.tools.idea.gradle.dsl.parser.elements.GradleDslExpressionMap)1 Pair (com.android.utils.Pair)1 HashSet (com.intellij.util.containers.HashSet)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1