<commit_before>package com.beust.kobalt

import com.beust.kobalt.api.*
import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.log
import java.io.File

/**
 * Capture the product flavor and the build type of a build.
 */
class Variant(val initialProductFlavor: ProductFlavorConfig? = null,
        val initialBuildType: BuildTypeConfig? = null) {

    val productFlavor: ProductFlavorConfig by lazy {
        initialProductFlavor ?: Variant.DEFAULT_PRODUCT_FLAVOR
    }
    val buildType: BuildTypeConfig by lazy {
        initialBuildType ?: Variant.DEFAULT_BUILD_TYPE
    }

    val isDefault : Boolean
        get() = productFlavor == DEFAULT_PRODUCT_FLAVOR && buildType == DEFAULT_BUILD_TYPE

    fun toTask(taskName: String) = taskName + productFlavor.name.capitalize() + buildType.name.capitalize()

    fun sourceDirectories(project: Project) : List<File> {
        val sourceDirectories = project.sourceDirectories.map { File(it) }
        if (isDefault) return sourceDirectories
        else {
            val result = arrayListOf<File>()
            // The ordering of files is: 1) build type 2) product flavor 3) default
            buildType.let {
                val dir = File(KFiles.joinDir("src", it.name, project.projectInfo.sourceDirectory))
                log(2, "Adding source for build type ${it.name}: ${dir.path}")
                result.add(dir)
            }
            productFlavor.let {
                val dir = File(KFiles.joinDir("src", it.name, project.projectInfo.sourceDirectory))
                log(2, "Adding source for product flavor ${it.name}: ${dir.path}")
                result.add(dir)
            }

            // Generated directory, if applicable
            generatedSourceDirectory?.let {
                result.add(it)
            }

            // Now that all the variant source directories have been added, add the project's default ones
            result.addAll(sourceDirectories)
            return result
        }
    }

    fun archiveName(project: Project, archiveName: String?, suffix: String) : String {
        val result =
            if (isDefault) {
                archiveName ?: project.name + "-" + project.version + suffix
            } else {
                val base = if (archiveName != null) archiveName.substring(0, archiveName.length - suffix.length)
                else project.name + "-" + project.version
                val result: String =
                        base + "-${productFlavor.name}" + "-${buildType.name}"

                result
            }
        return result
    }

    val shortArchiveName = productFlavor.name + "-" + buildType.name

    val hasBuildConfig: Boolean
        get() {
            return productFlavor.buildConfig != null || buildType.buildConfig != null
        }

    var generatedSourceDirectory: File? = null

    /**
     * If either the Project or the current variant has a build config defined, generate BuildConfig.java
     */
    fun maybeGenerateBuildConfig(project: Project, context: KobaltContext) {
        fun generated(project: Project) = KFiles.joinDir(project.buildDirectory, "generated", "source")

        if (project.buildConfig != null || context.variant.hasBuildConfig) {
            val buildConfigs = arrayListOf<BuildConfig>()
            if (project.buildConfig != null) buildConfigs.add(project.buildConfig!!)
            with (context.variant) {
                if (buildType.buildConfig != null) buildConfigs.add(buildType.buildConfig!!)
                if (productFlavor.buildConfig != null) buildConfigs.add(productFlavor.buildConfig!!)
            }
            var pkg = project.packageName ?: project.group
                    ?: throw KobaltException(
                        "packageName needs to be defined on the project in order to generate BuildConfig")
            val code = project.projectInfo.generateBuildConfig(pkg, context.variant, buildConfigs)
            generatedSourceDirectory = KFiles.makeDir(generated(project), pkg.replace('.', File.separatorChar))
            val outputFile = File(generatedSourceDirectory, "BuildConfig" + project .sourceSuffix)
            KFiles.saveFile(outputFile, code)
            log(2, "Generated ${outputFile.path}")
        }
    }

    override fun toString() = toTask("")

    companion object {
        val DEFAULT_PRODUCT_FLAVOR = ProductFlavorConfig("")
        val DEFAULT_BUILD_TYPE = BuildTypeConfig("debug")

        fun allVariants(project: Project): List<Variant> {
            val result = arrayListOf<Variant>()
<commit_msg>Variant was not honoring the project directory.<commit_after>package com.beust.kobalt

import com.beust.kobalt.api.*
import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.log
import java.io.File

/**
 * Capture the product flavor and the build type of a build.
 */
class Variant(val initialProductFlavor: ProductFlavorConfig? = null,
        val initialBuildType: BuildTypeConfig? = null) {

    val productFlavor: ProductFlavorConfig by lazy {
        initialProductFlavor ?: Variant.DEFAULT_PRODUCT_FLAVOR
    }
    val buildType: BuildTypeConfig by lazy {
        initialBuildType ?: Variant.DEFAULT_BUILD_TYPE
    }

    val isDefault : Boolean
        get() = productFlavor == DEFAULT_PRODUCT_FLAVOR && buildType == DEFAULT_BUILD_TYPE

    fun toTask(taskName: String) = taskName + productFlavor.name.capitalize() + buildType.name.capitalize()

    fun sourceDirectories(project: Project) : List<File> {
        val sourceDirectories = project.sourceDirectories.map { File(it) }
        if (isDefault) return sourceDirectories
        else {
            val result = arrayListOf<File>()
            // The ordering of files is: 1) build type 2) product flavor 3) default
            buildType.let {
                val dir = File(KFiles.joinDir("src", it.name, project.projectInfo.sourceDirectory))
                log(2, "Adding source for build type ${it.name}: ${dir.path}")
                result.add(dir)
            }
            productFlavor.let {
                val dir = File(KFiles.joinDir("src", it.name, project.projectInfo.sourceDirectory))
                log(2, "Adding source for product flavor ${it.name}: ${dir.path}")
                result.add(dir)
            }

            // Generated directory, if applicable
            generatedSourceDirectory?.let {
                result.add(it)
            }

            // Now that all the variant source directories have been added, add the project's default ones
            result.addAll(sourceDirectories)
            return result
        }
    }

    fun archiveName(project: Project, archiveName: String?, suffix: String) : String {
        val result =
            if (isDefault) {
                archiveName ?: project.name + "-" + project.version + suffix
            } else {
                val base = if (archiveName != null) archiveName.substring(0, archiveName.length - suffix.length)
                else project.name + "-" + project.version
                val result: String =
                        base + "-${productFlavor.name}" + "-${buildType.name}"

                result
            }
        return result
    }

    val shortArchiveName = productFlavor.name + "-" + buildType.name

    val hasBuildConfig: Boolean
        get() {
            return productFlavor.buildConfig != null || buildType.buildConfig != null
        }

    var generatedSourceDirectory: File? = null

    /**
     * If either the Project or the current variant has a build config defined, generate BuildConfig.java
     */
    fun maybeGenerateBuildConfig(project: Project, context: KobaltContext) {
        fun generated(project: Project) = KFiles.joinDir(project.directory, project.buildDirectory, "generated",
                "source")

        if (project.buildConfig != null || context.variant.hasBuildConfig) {
            val buildConfigs = arrayListOf<BuildConfig>()
            if (project.buildConfig != null) buildConfigs.add(project.buildConfig!!)
            with (context.variant) {
                if (buildType.buildConfig != null) buildConfigs.add(buildType.buildConfig!!)
                if (productFlavor.buildConfig != null) buildConfigs.add(productFlavor.buildConfig!!)
            }
            var pkg = project.packageName ?: project.group
                    ?: throw KobaltException(
                        "packageName needs to be defined on the project in order to generate BuildConfig")
            val code = project.projectInfo.generateBuildConfig(pkg, context.variant, buildConfigs)
            generatedSourceDirectory = KFiles.makeDir(generated(project), pkg.replace('.', File.separatorChar))
            val outputFile = File(generatedSourceDirectory, "BuildConfig" + project .sourceSuffix)
            KFiles.saveFile(outputFile, code)
            log(2, "Generated ${outputFile.path}")
        }
    }

    override fun toString() = toTask("")

    companion object {
        val DEFAULT_PRODUCT_FLAVOR = ProductFlavorConfig("")
        val DEFAULT_BUILD_TYPE = BuildTypeConfig("debug")

        fun allVariants(project: Project): List<Variant> {
            val result = arrayListOf<Variant>()

{'fraction_non_alphanumeric': 0.09691485882481195, 'fraction_numerical': 0.001526218249209637, 'mean_word_length': 2.3311546840958606, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6071403', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2349, 'n_words': 607}