<commit_before>package com.github.nomensvyat.switchbox

import com.android.build.gradle.*
import com.github.nomensvyat.switchbox.parser.FieldMapLoader
import org.gradle.api.*
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.StopExecutionException

class SwitchBoxPlugin implements Plugin<Project> {
    final Logger log = Logging.getLogger SwitchBoxPlugin
    private SwitchBoxExtension switchBoxExtension

    @Override
    void apply(Project project) {
        if (!hasAndroidPlugin(project)) {
            throw new StopExecutionException("Must be applied after 'android' or 'android-library' plugin.")
        }

        switchBoxExtension = project.extensions.create("switchBox", SwitchBoxExtension)

        project.afterEvaluate {
            doAfterEvaluate(project)
        }
    }

    def doAfterEvaluate(Project project) {
        def syncTask = project.task("syncSwitchBox")

        syncTask.doFirst { loadFields(project, it) }

        if (switchBoxExtension.syncOnBuild) {
            project.tasks.all { task ->
                if (task.name.contains("BuildConfig")) {
                    task.dependsOn syncTask
                }
            }
        } else {
            //done only on gradle sync
            loadFields(project, null)
        }
    }

    private void loadFields(Project project, @Nullable Task task) {
        def file = getFile(project, switchBoxExtension.filePath)

        log.info("Starting SwitchBox")

        //Remove up-to-date check for now
//        task?.setDidWork(false)
//        if (!FieldMapLoader.needToLoad(file)) {
//            log.info("SwitchBox UP-TO-DATE!!!")
//            return
//        }
//        task?.setDidWork(true)

        BuildConfigFieldAdder buildConfigFieldAdder = createBuildConfigAdder(getAndroidExtension(project))

        def cachedFieldMap = FieldMapLoader.getCached()
        if (cachedFieldMap != null) {
            buildConfigFieldAdder.remove(cachedFieldMap)
        }

        def fieldMap = FieldMapLoader.load(file)

        buildConfigFieldAdder.add(fieldMap)
    }

    private BuildConfigFieldAdder createBuildConfigAdder(BaseExtension androidExtension) {
        def builder = BuildConfigFieldAdder.builder()
        builder.addFieldContainer(FieldContainerFabric.createDefault(androidExtension.defaultConfig))
        androidExtension.buildTypes.all {
            builder.addFieldContainer(FieldContainerFabric.create(it))
        }
        androidExtension.productFlavors.all {
            builder.addFieldContainer(FieldContainerFabric.create(it))
        }

        def buildConfigFieldAdder = builder.build()
        buildConfigFieldAdder
    }

    BaseExtension getAndroidExtension(Project project) {
        def androidExtension
        try {
            androidExtension = project.extensions.getByType(AppExtension)
        } catch (UnknownDomainObjectException e) {
            androidExtension = project.extensions.getByType(LibraryExtension)
        }

        return androidExtension
    }

    static def getFile(Project project, String filePath) {
        try {
            return project.file(filePath)
        } catch (Exception e) {
            throw new Exception("File not found", e)
        }
    }

    static def hasAndroidPlugin(Project project) {
        return project.plugins.hasPlugin(AppPlugin) || project.plugins.hasPlugin(LibraryPlugin)
    }
}<commit_msg>Add onlyIf check not to skip if no filePath was set<commit_after>package com.github.nomensvyat.switchbox

import com.android.build.gradle.*
import com.github.nomensvyat.switchbox.parser.FieldMapLoader
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.StopExecutionException

class SwitchBoxPlugin implements Plugin<Project> {
    private static final String TASK_NAME = "syncSwitchBox"
    private static final String EXTENSION_NAME = "switchBox"

    private final Logger log = Logging.getLogger SwitchBoxPlugin
    private SwitchBoxExtension switchBoxExtension

    @Override
    void apply(Project project) {
        if (!hasAndroidPlugin(project)) {
            throw new StopExecutionException("Must be applied after 'android' or 'android-library' plugin.")
        }

        switchBoxExtension = project.extensions.create(EXTENSION_NAME, SwitchBoxExtension)

        project.afterEvaluate {
            doAfterEvaluate(project)
        }
    }

    def doAfterEvaluate(Project project) {
        def syncTask = project.task(TASK_NAME)

        syncTask.doFirst { loadFields(project) }

        syncTask.onlyIf { switchBoxExtension.filePath }

        if (switchBoxExtension.syncOnBuild) {
            project.tasks.all { task ->
                if (task.name.contains("BuildConfig")) {
                    task.dependsOn syncTask
                }
            }
        } else {
            //done only on gradle sync
            loadFields(project)
        }
    }

    private void loadFields(Project project) {
        if (switchBoxExtension.filePath.isEmpty()) {
            return
        }

        File file = getFile(project, switchBoxExtension.filePath)

        log.info("Starting SwitchBox")

        BuildConfigFieldAdder buildConfigFieldAdder = createBuildConfigAdder(getAndroidExtension(project))

        def cachedFieldMap = FieldMapLoader.getCached()
        if (cachedFieldMap != null) {
            buildConfigFieldAdder.remove(cachedFieldMap)
        }

        def fieldMap = FieldMapLoader.load(file)

        buildConfigFieldAdder.add(fieldMap)
    }

    private BuildConfigFieldAdder createBuildConfigAdder(BaseExtension androidExtension) {
        BuildConfigFieldAdder.Builder builder = BuildConfigFieldAdder.builder()
        builder.addFieldContainer(FieldContainerFabric.createDefault(androidExtension.defaultConfig))
        androidExtension.buildTypes.all {
            builder.addFieldContainer(FieldContainerFabric.create(it))
        }
        androidExtension.productFlavors.all {
            builder.addFieldContainer(FieldContainerFabric.create(it))
        }

        def buildConfigFieldAdder = builder.build()
        buildConfigFieldAdder
    }

    BaseExtension getAndroidExtension(Project project) {
        return project.extensions.findByType(AppExtension) ?: project.extensions.getByType(LibraryExtension)
    }

    static def getFile(Project project, String filePath) {
        try {
            return project.file(filePath)
        } catch (Exception e) {
            throw new Exception("File '${filePath}' not found", e)
        }
    }

    static def hasAndroidPlugin(Project project) {
        return project.plugins.hasPlugin(AppPlugin) || project.plugins.hasPlugin(LibraryPlugin)
    }
}
{'fraction_non_alphanumeric': 0.08172864912538585, 'fraction_numerical': 0.0, 'mean_word_length': 2.7018498367791075, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3141627', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1727, 'n_words': 339}