<commit_before>package com.beust.kobalt

import com.beust.jcommander.JCommander
import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.internal.*
import com.beust.kobalt.kotlin.BuildFile
import com.beust.kobalt.maven.*
import com.beust.kobalt.misc.*
import com.beust.kobalt.kotlin.ScriptCompiler2
import com.beust.kobalt.wrapper.Wrapper
import com.google.inject.Guice
import java.io.File
import java.nio.file.Paths
import java.util.*
import javax.inject.Inject

private fun parseArgs(argv: Array<String>): Main.RunInfo {
    val args = Args()
    val result = JCommander(args)
    result.parse(*argv)
    KobaltLogger.LOG_LEVEL = args.log
    return Main.RunInfo(result, args)
}

public fun main(argv: Array<String>) {
    val (jc, args) = parseArgs(argv)
    Kobalt.INJECTOR = Guice.createInjector(MainModule(args))
    Kobalt.INJECTOR.getInstance(Main::class.java).run(jc, args)
}

private class Main @Inject constructor(
        val script2: ScriptCompiler2.IFactory,
        val plugins: Plugins,
        val taskManager: TaskManager,
        val http: Http,
        val files: KFiles,
        val executors: KobaltExecutors,
        val localRepo: LocalRepo,
        val depFactory: DepFactory,
        val checkVersions: CheckVersions,
        val github: GithubApi,
        val updateKobalt: UpdateKobalt)
    : KobaltLogger {

    data class RunInfo(val jc: JCommander, val args: Args)

    public fun run(jc: JCommander, args: Args) {
        val latestVersionFuture = github.latestKobaltVersion
        benchmark("Build", {
            println(Banner.get() + Kobalt.version + "\n")
//            runTest()
            runWithArgs(jc, args)
            executors.shutdown()
            debug("All done")
        })

        // Check for new version
        val latestVersionString = latestVersionFuture.get()
        val latestVersion = Versions.toLongVersion(latestVersionString)
        val current = Versions.toLongVersion(Kobalt.version)
        if (latestVersion > current) {
            "***** ".let {
                log(1, it)
                log(1, "$it New Kobalt version available: $latestVersionString")
                log(1, "$it To update, run ./kobaltw --update")
                log(1, it )
            }
        }
    }

    public class Worker<T>(val runNodes: ArrayList<T>, val n: T) : IWorker<T>, KobaltLogger {
        override val priority = 0

        override fun call() : TaskResult2<T> {
            log(2, "Running node ${n}")
            runNodes.add(n)
            return TaskResult2(n != 3, n)
        }
    }

    private fun runTest() {
        with(Topological<String>()) {
            addEdge("b1", "a1")
            addEdge("b1", "a2")
            addEdge("b2", "a1")
            addEdge("b2", "a2")
            addEdge("c1", "b1")
            addEdge("c1", "b2")
            val sorted = sort(arrayListOf("a1", "a2", "b1", "b2", "c1", "x", "y"))
            println("Sorted: ${sorted}")
        }
    }

    private val SCRIPT_JAR = "buildScript.jar"

    private fun runWithArgs(jc: JCommander, args: Args) {
        val p = if (args.buildFile != null) File(args.buildFile) else findBuildFile()
        args.buildFile = p.absolutePath
        val buildFile = BuildFile(Paths.get(p.absolutePath), p.name)

        if (args.init) {
            //
            // --init: create a new build project and install the wrapper
            //
            Wrapper().install()
            ProjectGenerator().run(args)
        } else if (args.usage) {
            jc.usage()
        } else {
            if (! buildFile.exists()) {
                jc.usage()
            } else {
                val allProjects = script2.create(arrayListOf(buildFile)).findProjects()
//                // Install all the plugins found
//                val classLoaders = plugins.installDynamicPlugins(arrayListOf(buildFile))
//
//                // Compile the build script
//                val output = scriptCompilerFactory.create(plugins.pluginJarFiles,
//                        // @@
//                        { cl: ClassLoader, n: String -> plugins.instantiateClassName(classLoaders.get(0), n) }
//                    ).compile(buildFile, buildFile.lastModified(),
//                        KFiles.findBuildScriptLocation(buildFile, SCRIPT_JAR))

                //
                // Force each project.directory to be an absolute path, if it's not already
                //
                allProjects.forEach {<commit_msg>Not used.<commit_after>package com.beust.kobalt

import com.beust.jcommander.JCommander
import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.internal.*
import com.beust.kobalt.kotlin.BuildFile
import com.beust.kobalt.maven.*
import com.beust.kobalt.misc.*
import com.beust.kobalt.kotlin.ScriptCompiler2
import com.beust.kobalt.wrapper.Wrapper
import com.google.inject.Guice
import java.io.File
import java.nio.file.Paths
import java.util.*
import javax.inject.Inject

private fun parseArgs(argv: Array<String>): Main.RunInfo {
    val args = Args()
    val result = JCommander(args)
    result.parse(*argv)
    KobaltLogger.LOG_LEVEL = args.log
    return Main.RunInfo(result, args)
}

public fun main(argv: Array<String>) {
    val (jc, args) = parseArgs(argv)
    Kobalt.INJECTOR = Guice.createInjector(MainModule(args))
    Kobalt.INJECTOR.getInstance(Main::class.java).run(jc, args)
}

private class Main @Inject constructor(
        val script2: ScriptCompiler2.IFactory,
        val plugins: Plugins,
        val taskManager: TaskManager,
        val http: Http,
        val files: KFiles,
        val executors: KobaltExecutors,
        val localRepo: LocalRepo,
        val depFactory: DepFactory,
        val checkVersions: CheckVersions,
        val github: GithubApi,
        val updateKobalt: UpdateKobalt)
    : KobaltLogger {

    data class RunInfo(val jc: JCommander, val args: Args)

    public fun run(jc: JCommander, args: Args) {
        val latestVersionFuture = github.latestKobaltVersion
        benchmark("Build", {
            println(Banner.get() + Kobalt.version + "\n")
//            runTest()
            runWithArgs(jc, args)
            executors.shutdown()
            debug("All done")
        })

        // Check for new version
        val latestVersionString = latestVersionFuture.get()
        val latestVersion = Versions.toLongVersion(latestVersionString)
        val current = Versions.toLongVersion(Kobalt.version)
        if (latestVersion > current) {
            "***** ".let {
                log(1, it)
                log(1, "$it New Kobalt version available: $latestVersionString")
                log(1, "$it To update, run ./kobaltw --update")
                log(1, it )
            }
        }
    }

    public class Worker<T>(val runNodes: ArrayList<T>, val n: T) : IWorker<T>, KobaltLogger {
        override val priority = 0

        override fun call() : TaskResult2<T> {
            log(2, "Running node ${n}")
            runNodes.add(n)
            return TaskResult2(n != 3, n)
        }
    }

    private fun runTest() {
        with(Topological<String>()) {
            addEdge("b1", "a1")
            addEdge("b1", "a2")
            addEdge("b2", "a1")
            addEdge("b2", "a2")
            addEdge("c1", "b1")
            addEdge("c1", "b2")
            val sorted = sort(arrayListOf("a1", "a2", "b1", "b2", "c1", "x", "y"))
            println("Sorted: ${sorted}")
        }
    }

    private val SCRIPT_JAR = "buildScript.jar"

    private fun runWithArgs(jc: JCommander, args: Args) {
        val p = if (args.buildFile != null) File(args.buildFile) else findBuildFile()
        args.buildFile = p.absolutePath
        val buildFile = BuildFile(Paths.get(p.absolutePath), p.name)

        if (args.init) {
            //
            // --init: create a new build project and install the wrapper
            //
            Wrapper().install()
            ProjectGenerator().run(args)
        } else if (args.usage) {
            jc.usage()
        } else {
            if (! buildFile.exists()) {
                jc.usage()
            } else {
                val allProjects = script2.create(arrayListOf(buildFile)).findProjects()

                //
                // Force each project.directory to be an absolute path, if it's not already
                //
                allProjects.forEach {
{'fraction_non_alphanumeric': 0.11811490978157645, 'fraction_numerical': 0.007241215574548908, 'mean_word_length': 2.066982162358937, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 20, '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': '1201382', 'n_tokens_mistral': 2603, 'n_tokens_neox': 2520, 'n_words': 580}