<commit_before>package org.jetbrains.kotlin.jupyter

import jupyter.kotlin.DependsOn
import jupyter.kotlin.Repository
import org.jetbrains.kotlin.script.util.resolvers.DirectResolver
import org.jetbrains.kotlin.script.util.resolvers.FlatLibDirectoryResolver
import org.jetbrains.kotlin.script.util.resolvers.MavenResolver
import org.jetbrains.kotlin.script.util.resolvers.experimental.*
import java.io.File
import kotlin.script.dependencies.ScriptContents

open class JupyterScriptDependenciesResolver
{
    private var flatLibDirectoryResolver : GenericResolver = FlatLibDirectoryResolver()

    private var directResolver : GenericResolver = DirectResolver()

    private var mavenResolver : GenericResolver = MavenResolver()

    private val DependsOn.coordinates : GenericArtifactCoordinates
            get() = MavenArtifactCoordinates(value, groupId, artifactId, version)

    private val Repository.coordinates : GenericRepositoryCoordinates
            get () = BasicRepositoryCoordinates(value.takeIf { it.isNotBlank() } ?: url, id.takeIf { it.isNotBlank() })


    private val resolvers = sequenceOf(flatLibDirectoryResolver, directResolver, mavenResolver)

    fun resolveFromAnnotations(script: ScriptContents): List<File> {
        script.annotations.forEach { annotation ->
            when (annotation) {
                is Repository -> {
                    val coordinates = annotation.coordinates
                    if (!flatLibDirectoryResolver.tryAddRepository(coordinates) && !mavenResolver.tryAddRepository(coordinates))
                        throw IllegalArgumentException("Illegal argument for Repository annotation: $annotation")
                }
                is DependsOn -> {}
                else -> throw Exception("Unknown annotation ${annotation.javaClass}")
            }
        }
        return script.annotations.filterIsInstance(DependsOn::class.java).flatMap { dep ->
            resolvers.mapNotNull { it.tryResolve(dep.coordinates) }.firstOrNull() ?:
            throw Exception("Unable to resolve dependency $dep")
        }
    }
}
<commit_msg>use new dependency resolvers<commit_after>package org.jetbrains.kotlin.jupyter

import jupyter.kotlin.DependsOn
import jupyter.kotlin.Repository
import java.io.File
import kotlin.script.dependencies.ScriptContents
import kotlin.script.experimental.*
import kotlin.script.experimental.api.ResultWithDiagnostics
import kotlin.script.experimental.api.ScriptDiagnostic
import kotlin.script.experimental.api.asSuccess
import kotlin.script.experimental.api.makeFailureResult

open class JupyterScriptDependenciesResolver
{
    private val resolver : GenericDependenciesResolver = CompoundDependenciesResolver(DirectDependenciesResolver(), FlatLibDirectoryDependenciesResolver(), MavenDependenciesResolver())

    fun resolveFromAnnotations(script: ScriptContents): ResultWithDiagnostics<List<File>> {
        script.annotations.forEach { annotation ->
            when (annotation) {
                is Repository -> {
                    if (!resolver.tryAddRepository(annotation.value))
                        throw IllegalArgumentException("Illegal argument for Repository annotation: $annotation")
                }
                is DependsOn -> {}
                else -> throw Exception("Unknown annotation ${annotation.javaClass}")
            }
        }
        val scriptDiagnostics = mutableListOf<ScriptDiagnostic>()
        val classpath = mutableListOf<File>()
        for(annotation in script.annotations.filterIsInstance<DependsOn>()) {
            when (val result = resolver.resolve(annotation.value)) {
                is ResultWithDiagnostics.Failure -> scriptDiagnostics.add(ScriptDiagnostic("Failed to resolve dependencies:\n" + result.reports.joinToString("\n") { it.message }))
                is ResultWithDiagnostics.Success -> classpath.addAll(result.value)
            }
        }
        return if(scriptDiagnostics.isEmpty()) classpath.asSuccess()
               else makeFailureResult(scriptDiagnostics)
    }
}

{'fraction_non_alphanumeric': 0.08579221423258121, 'fraction_numerical': 0.0, 'mean_word_length': 3.3516720604099244, 'pattern_counts': {'":': 0, '<': 9, '<?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': '2765381', 'n_tokens_mistral': 1045, 'n_tokens_neox': 986, 'n_words': 191}