GroovyGradleParser extends CodeVisitorSupport { public static GradleContext.Builder parseGradleBuildFile( String content, int defaultMinSdkVersion, @Nullable AndroidPluginVersion defaultAndroidPluginVersion) { List<ASTNode> astNodes = new AstBuilder().buildFromString(CompilePhase.CONVERSION, content); GroovyGradleParser parser = new GroovyGradleParser(content, defaultMinSdkVersion, defaultAndroidPluginVersion); for (ASTNode node : astNodes) { if (node instanceof ClassNode) { continue; } node.visit(parser); } return parser.getGradleContextBuilder(); } private  GroovyGradleParser(
      String content, int defaultMinSdkVersion, AndroidPluginVersion defaultAndroidPluginVersion); static GradleContext.Builder parseGradleBuildFile(
      String content,
      int defaultMinSdkVersion,
      @Nullable AndroidPluginVersion defaultAndroidPluginVersion); GradleContext.Builder getGradleContextBuilder(); @Override void visitMethodCallExpression(MethodCallExpression expression); @Override void visitTupleExpression(TupleExpression tupleExpression); @Override void visitBinaryExpression(BinaryExpression binaryExpression);  }
@Test public void parsesPluginType_Application() throws Exception { File buildFile = TestUtils.getTestDataFile(APP_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); assertThat(context.getPluginType()).isEqualTo(GradleContext.PluginType.APPLICATION); }
@Test public void parsesPluginType_DynamicFeature() throws Exception { File buildFile = TestUtils.getTestDataFile(DYNAMIC_FEATURE_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); assertThat(context.getPluginType()).isEqualTo(GradleContext.PluginType.DYNAMIC_FEATURE); }
@Test public void parsesPluginType_Unknown() throws Exception { File buildFile = TestUtils.getTestDataFile(PROJECT_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); assertThat(context.getPluginType()).isEqualTo(GradleContext.PluginType.UNKNOWN); }
@Test public void parsesMinSdkVersion() throws Exception { File buildFile = TestUtils.getTestDataFile(APP_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); assertThat(context.getMinSdkVersion()).isEqualTo(15); }
@Test public void parsesMinSdkVersionInTupleExpression() throws Exception { File buildFile = TestUtils.getTestDataFile(DYNAMIC_FEATURE_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); assertThat(context.getMinSdkVersion()).isEqualTo(14); }
@Test public void parsesProguardConfigs() throws Exception { File buildFile = TestUtils.getTestDataFile(PROGUARD_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); ImmutableMap<String, ProguardConfig> expectedMap = ImmutableMap.of( "release", ProguardConfig.builder() .setMinifyEnabled(true) .setHasProguardRules(true) .setObfuscationEnabled(true) .build(), "shrinkOnly", ProguardConfig.builder() .setMinifyEnabled(true) .setHasProguardRules(true) .setObfuscationEnabled(false) .build(), "debug", ProguardConfig.builder() .setMinifyEnabled(false) .setHasProguardRules(true) .setObfuscationEnabled(true) .build()); assertThat(context.getProguardConfigs()).isEqualTo(expectedMap); }
@Test public void parsesComplexBuildFile() throws Exception { File buildFile = TestUtils.getTestDataFile(COMPLEX_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); ImmutableMap<String, ProguardConfig> expectedMap = ImmutableMap.of( "release", ProguardConfig.builder() .setMinifyEnabled(true) .setHasProguardRules(true) .setObfuscationEnabled(true) .build(), "debug", ProguardConfig.builder() .setMinifyEnabled(true) .setHasProguardRules(true) .setObfuscationEnabled(true) .build()); assertThat(context.getMinSdkVersion()).isEqualTo(19); assertThat(context.getPluginType()).isEqualTo(GradleContext.PluginType.APPLICATION); assertThat(context.getProguardConfigs()).isEqualTo(expectedMap); }
@Test public void variableMinSdkVersionDefaultsToDefaultvalue() throws Exception { File buildFile = TestUtils.getTestDataFile(VARIABLE_MINSDK_BUILD_FILE); int defaultMinSdkVersion = 345; String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, defaultMinSdkVersion, null).build(); assertThat(context.getMinSdkVersion()).isEqualTo(defaultMinSdkVersion); }
@Test public void setsAndroidPluginVersion() throws Exception { File buildFile = TestUtils.getTestDataFile(PROJECT_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); assertThat(context.getAndroidPluginVersion()).isNotNull(); assertThat(context.getAndroidPluginVersion().getMajorVersion()).isEqualTo(3); assertThat(context.getAndroidPluginVersion().getMinorVersion()).isEqualTo(4); }
@Test public void setsBundleConfigAllFalse() throws Exception { File buildFile = TestUtils.getTestDataFile(DISABLE_SPLITS_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); assertThat(context.getBundleConfig().getAbiSplitEnabled()).isFalse(); assertThat(context.getBundleConfig().getDensitySplitEnabled()).isFalse(); assertThat(context.getBundleConfig().getLanguageSplitEnabled()).isFalse(); }
@Test public void setsBundleConfigEnabledByDefault() throws Exception { File buildFile = TestUtils.getTestDataFile(APP_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); assertThat(context.getBundleConfig().getAbiSplitEnabled()).isTrue(); assertThat(context.getBundleConfig().getDensitySplitEnabled()).isTrue(); assertThat(context.getBundleConfig().getLanguageSplitEnabled()).isTrue(); }
@Test public void setsBundleConfigMixEnabled() throws Exception { File buildFile = TestUtils.getTestDataFile(MIX_BUNDLE_SPLITS_ENABLED_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); assertThat(context.getBundleConfig().getAbiSplitEnabled()).isTrue(); assertThat(context.getBundleConfig().getDensitySplitEnabled()).isTrue(); assertThat(context.getBundleConfig().getLanguageSplitEnabled()).isFalse(); }
@Test public void setsBundleConfigLineNumbersAll() throws Exception { File buildFile = TestUtils.getTestDataFile(DISABLE_SPLITS_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); assertThat(context.getBundleConfig().getBundleConfigLocation().getAbiSplitLineNumber()) .isEqualTo(21); assertThat(context.getBundleConfig().getBundleConfigLocation().getDensitySplitLineNumber()) .isEqualTo(18); assertThat(context.getBundleConfig().getBundleConfigLocation().getLanguageSplitLineNumber()) .isEqualTo(15); }
@Test public void setsBundleConfigLineNumberForNone() throws Exception { File buildFile = TestUtils.getTestDataFile(APP_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); assertThat(context.getBundleConfig().getBundleConfigLocation().getAbiSplitLineNumber()) .isNull(); assertThat(context.getBundleConfig().getBundleConfigLocation().getDensitySplitLineNumber()) .isNull(); assertThat(context.getBundleConfig().getBundleConfigLocation().getLanguageSplitLineNumber()) .isNull(); }
@Test public void setsBundleConfigLineNumberMixed() throws Exception { File buildFile = TestUtils.getTestDataFile(MIX_BUNDLE_SPLITS_ENABLED_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); assertThat(context.getBundleConfig().getBundleConfigLocation().getAbiSplitLineNumber()) .isNull(); assertThat(context.getBundleConfig().getBundleConfigLocation().getDensitySplitLineNumber()) .isEqualTo(18); assertThat(context.getBundleConfig().getBundleConfigLocation().getLanguageSplitLineNumber()) .isEqualTo(15); }
@Test public void parseToplevelMethodCall() throws Exception { File buildFile = TestUtils.getTestDataFile(TOP_LEVEL_METHOD_CALL_GRADLE_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); assertThat(context).isNotNull(); }
@Test public void setsLibraryDependencies() throws Exception { File buildFile = TestUtils.getTestDataFile(LIBRARY_DEPENDENCY_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); ImmutableSet<Library> expectedSet = ImmutableSet.of( Library.newBuilder() .setMavenLibrary( MavenLibrary.newBuilder() .setGroupId("com.google.inject") .setArtifactId("guice") .setVersion("3.0") .build()) .build(), Library.newBuilder() .setMavenLibrary( MavenLibrary.newBuilder() .setGroupId("com.android.support.constraint") .setArtifactId("constraint-layout") .setVersion("1.1.3") .build()) .build(), Library.newBuilder() .setMavenLibrary( MavenLibrary.newBuilder() .setGroupId("com.android.support") .setArtifactId("appcompat-v7") .setVersion("28.0.0") .build()) .build(), Library.newBuilder() .setMavenLibrary( MavenLibrary.newBuilder() .setGroupId("com.google.code.guice.test") .setArtifactId("guice") .setVersion("1.0") .build()) .build(), Library.newBuilder() .setMavenLibrary( MavenLibrary.newBuilder() .setGroupId("foo") .setArtifactId("bar") .setVersion("1.0.0") .build()) .build(), Library.newBuilder() .setMavenLibrary( MavenLibrary.newBuilder() .setGroupId("org.myorg") .setArtifactId("someLib") .setVersion("1.0") .build()) .build()); assertThat(context.getLibraryDependencies()).hasSize(6); assertThat(context.getLibraryDependencies()).isEqualTo(expectedSet); }
@Test public void setsEmbedsWearApk() throws Exception { File buildFile = TestUtils.getTestDataFile(EMBEDS_WEAR_APK_GRADLE_BUILD_FILE); String content = Files.asCharSource(buildFile, UTF_8).read(); GradleContext context = GroovyGradleParser.parseGradleBuildFile(content, 1, null).build(); assertThat(context.getEmbedsWearApk()).isTrue(); }