<commit_before>package com.camnter.gradle.plugin.r2

import com.android.build.gradle.*
import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.internal.res.GenerateLibraryRFileTask
import com.android.build.gradle.internal.res.LinkApplicationAndroidResourcesTask
import com.android.build.gradle.internal.scope.VariantScope
import com.android.build.gradle.tasks.ProcessAndroidResources
import org.gradle.api.DomainObjectSet
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task

import java.lang.reflect.Field
import java.util.concurrent.atomic.AtomicBoolean

/**
 * @author CaMnter
 */

class R2Plugin implements Plugin<Project> {

    /**
     * Apply this plugin to the given target object.
     *
     * @param target The target object
     */
    @Override
    void apply(Project target) {
        println "[R2Plugin]   [apply]"
        target.plugins.all {
            if (it instanceof FeaturePlugin) {
                FeatureExtension featureExtension = target.extensions.getByType(
                        FeatureExtension.class)
                configureR2Generation(target, featureExtension.featureVariants, false)
                configureR2Generation(target, featureExtension.libraryVariants, false)
            } else if (it instanceof AppPlugin) {
                AppExtension appExtension = target.extensions.getByType(AppExtension.class)
                configureR2Generation(target, appExtension.applicationVariants, false)
            } else if (it instanceof LibraryPlugin) {
                LibraryExtension libraryExtension = target.extensions.getByType(
                        LibraryExtension.class)
                configureR2Generation(target, libraryExtension.libraryVariants, true)
            }
        }
    }

    private void configureR2Generation(Project project, DomainObjectSet<BaseVariant> variants,
            boolean isLibrary) {
        println "[R2Plugin]   [configureR2Generation]"
        // 遍历 DomainObjectSet<out BaseVariant>
        try {
            variants.all {
                if (null == it) {
                    return
                }
                // 获取每个 output 文件夹 File
                File outputDir = new File(
                        FileUtils.resolve(project.buildDir, "generated/source/r2/${it.dirName}"))
                // 创建对应的 R2 任务
                Task task = project.tasks.create("generate${it.name.capitalize()}R2")
                // 设置 R2 任务的 输出目录 r File
                task.outputs.dir(outputDir)
                // 注册任务
                it.registerJavaGeneratingTask(task, outputDir)

                AtomicBoolean once = new AtomicBoolean()

                // 遍历 DomainObjectCollection<BaseVariantOutput>
                it.outputs.all { output ->
                    // ProcessAndroidResources
                    def processResources = output.processResources

                    // 区分 android gradle plugin 版本
                    String version = ''
                    String alpha = ''
                    project.rootProject
                            .buildscript
                            .configurations
                            .classpath
                            .resolvedConfiguration
                            .firstLevelModuleDependencies.
                            each {
                                def name = it.name
                                if (name.contains('com.android.tools.build:gradle')) {
                                    def moduleVersion = it.moduleVersion
                                    // alpha ?
                                    if (moduleVersion.contains("-")) {
                                        def versionArray = moduleVersion.split("-")
                                        println "[R2Plugin]   [versionArray] = ${versionArray}"
                                        version = versionArray[0]
                                        alpha = versionArray[1]
                                    } else {
                                        version = moduleVersion
                                    }
                                }
                            }
                    def firstVersion = version.substring(0, 1) as Integer
                    // 拿到 R 文件夹路径
                    def rPackage = ''
                    if (version == '3.1.0') {
                        if ('' != alpha) {
                            switch (alpha) {
                                case "alpha01":
                                    rPackage = getPackageForRFromProcessAndroidResources(
                                            processResources)
                                    break
                                case "alpha02":
                                    rPackage = isLibrary ?
                                            (processResources as GenerateLibraryRFileTask).packageForR :
                                            getPackageForRFromLinkApplicationAndroidResourcesTask(
                                                    processResources as LinkApplicationAndroidResourcesTask)
                                    break
                            }
                        } else {
                            // TODO 3.1.0 final version
                        }
                    } else if (version == '3.0.0' || firstVersion < 3) {
                        // less than or equal to 3.0.0
                        rPackage = processResources.packageForR<commit_msg>[Modify] adaptive android gradle plugin 3.1.0-alpha03 in r2-plugin module<commit_after>package com.camnter.gradle.plugin.r2

import com.android.build.gradle.*
import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.internal.res.GenerateLibraryRFileTask
import com.android.build.gradle.internal.res.LinkApplicationAndroidResourcesTask
import com.android.build.gradle.internal.scope.VariantScope
import com.android.build.gradle.tasks.ProcessAndroidResources
import org.gradle.api.DomainObjectSet
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task

import java.lang.reflect.Field
import java.util.concurrent.atomic.AtomicBoolean

/**
 * @author CaMnter
 */

class R2Plugin implements Plugin<Project> {

    /**
     * Apply this plugin to the given target object.
     *
     * @param target The target object
     */
    @Override
    void apply(Project target) {
        println "[R2Plugin]   [apply]"
        target.plugins.all {
            if (it instanceof FeaturePlugin) {
                FeatureExtension featureExtension = target.extensions.getByType(
                        FeatureExtension.class)
                configureR2Generation(target, featureExtension.featureVariants, false)
                configureR2Generation(target, featureExtension.libraryVariants, false)
            } else if (it instanceof AppPlugin) {
                AppExtension appExtension = target.extensions.getByType(AppExtension.class)
                configureR2Generation(target, appExtension.applicationVariants, false)
            } else if (it instanceof LibraryPlugin) {
                LibraryExtension libraryExtension = target.extensions.getByType(
                        LibraryExtension.class)
                configureR2Generation(target, libraryExtension.libraryVariants, true)
            }
        }
    }

    private void configureR2Generation(Project project, DomainObjectSet<BaseVariant> variants,
            boolean isLibrary) {
        println "[R2Plugin]   [configureR2Generation]"
        // 遍历 DomainObjectSet<out BaseVariant>
        try {
            variants.all {
                if (null == it) {
                    return
                }
                // 获取每个 output 文件夹 File
                File outputDir = new File(
                        FileUtils.resolve(project.buildDir, "generated/source/r2/${it.dirName}"))
                // 创建对应的 R2 任务
                Task task = project.tasks.create("generate${it.name.capitalize()}R2")
                // 设置 R2 任务的 输出目录 r File
                task.outputs.dir(outputDir)
                // 注册任务
                it.registerJavaGeneratingTask(task, outputDir)

                AtomicBoolean once = new AtomicBoolean()

                // 遍历 DomainObjectCollection<BaseVariantOutput>
                it.outputs.all { output ->
                    // ProcessAndroidResources
                    def processResources = output.processResources

                    // 区分 android gradle plugin 版本
                    String version = ''
                    String alpha = ''
                    project.rootProject
                            .buildscript
                            .configurations
                            .classpath
                            .resolvedConfiguration
                            .firstLevelModuleDependencies.
                            each {
                                def name = it.name
                                if (name.contains('com.android.tools.build:gradle')) {
                                    def moduleVersion = it.moduleVersion
                                    // alpha ?
                                    if (moduleVersion.contains("-")) {
                                        def versionArray = moduleVersion.split("-")
                                        println "[R2Plugin]   [versionArray] = ${versionArray}"
                                        version = versionArray[0]
                                        alpha = versionArray[1]
                                    } else {
                                        version = moduleVersion
                                    }
                                }
                            }
                    def firstVersion = version.substring(0, 1) as Integer
                    // 拿到 R 文件夹路径
                    def rPackage = ''
                    if (version == '3.1.0') {
                        if ('' != alpha) {
                            if (alpha == "alpha01") {
                                rPackage =
                                        getPackageForRFromProcessAndroidResources(processResources)
                            } else {
                                // "alpha02", "alpha03"...
                                rPackage = isLibrary ?
                                        (processResources as GenerateLibraryRFileTask).packageForR :
                                        getPackageForRFromLinkApplicationAndroidResourcesTask(
                                                processResources as LinkApplicationAndroidResourcesTask)
                            }
                        } else {
                            // TODO 3.1.0 final version
                        }
                    } else if (version == '3.0.0' || firstVersion < 3) {
                        // less than or equal to 3.0.0
                        rPackage = processResources.packageForR
{'fraction_non_alphanumeric': 0.07040203747498636, 'fraction_numerical': 0.007276696379843551, 'mean_word_length': 1.1871891784364432, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 46, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3877393', 'n_tokens_mistral': 2473, 'n_tokens_neox': 2327, 'n_words': 492}