|
| 1 | +/* |
| 2 | + * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. |
| 3 | + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. |
| 4 | + */ |
| 5 | + |
| 6 | +package org.jetbrains.kotlin.script.examples.jvm.resolve.maven |
| 7 | + |
| 8 | +import org.jetbrains.kotlin.script.util.DependsOn |
| 9 | +import org.jetbrains.kotlin.script.util.FilesAndMavenResolver |
| 10 | +import org.jetbrains.kotlin.script.util.Repository |
| 11 | +import java.io.File |
| 12 | +import kotlin.script.dependencies.ScriptContents |
| 13 | +import kotlin.script.dependencies.ScriptDependenciesResolver |
| 14 | +import kotlin.script.experimental.annotations.KotlinScript |
| 15 | +import kotlin.script.experimental.api.* |
| 16 | +import kotlin.script.experimental.jvm.JvmDependency |
| 17 | +import kotlin.script.experimental.jvm.compat.mapLegacyDiagnosticSeverity |
| 18 | +import kotlin.script.experimental.jvm.compat.mapLegacyScriptPosition |
| 19 | +import kotlin.script.experimental.jvm.dependenciesFromCurrentContext |
| 20 | +import kotlin.script.experimental.jvm.jvm |
| 21 | +import kotlin.script.experimental.jvm.withUpdatedClasspath |
| 22 | + |
| 23 | +@KotlinScript( |
| 24 | + fileExtension = "scriptwithdeps.kts", |
| 25 | + compilationConfiguration = ScriptWithMavenDepsConfiguration::class |
| 26 | +) |
| 27 | +abstract class ScriptWithMavenDeps |
| 28 | + |
| 29 | +object ScriptWithMavenDepsConfiguration : ScriptCompilationConfiguration( |
| 30 | + { |
| 31 | + defaultImports(DependsOn::class, Repository::class) |
| 32 | + jvm { |
| 33 | + dependenciesFromCurrentContext( |
| 34 | + "script", // script library jar name |
| 35 | + "kotlin-script-util" // DependsOn annotation is taken from script-util |
| 36 | + ) |
| 37 | + } |
| 38 | + refineConfiguration { |
| 39 | + onAnnotations(DependsOn::class, Repository::class, handler = ::configureMavenDepsOnAnnotations) |
| 40 | + } |
| 41 | + } |
| 42 | +) |
| 43 | + |
| 44 | +private val resolver = FilesAndMavenResolver() |
| 45 | + |
| 46 | +fun configureMavenDepsOnAnnotations(context: ScriptConfigurationRefinementContext): ResultWithDiagnostics<ScriptCompilationConfiguration> { |
| 47 | + val annotations = context.collectedData?.get(ScriptCollectedData.foundAnnotations)?.takeIf { it.isNotEmpty() } |
| 48 | + ?: return context.compilationConfiguration.asSuccess() |
| 49 | + val scriptContents = object : ScriptContents { |
| 50 | + override val annotations: Iterable<Annotation> = annotations |
| 51 | + override val file: File? = null |
| 52 | + override val text: CharSequence? = null |
| 53 | + } |
| 54 | + val diagnostics = arrayListOf<ScriptDiagnostic>() |
| 55 | + fun report(severity: ScriptDependenciesResolver.ReportSeverity, message: String, position: ScriptContents.Position?) { |
| 56 | + diagnostics.add( |
| 57 | + ScriptDiagnostic( |
| 58 | + message, |
| 59 | + mapLegacyDiagnosticSeverity(severity), |
| 60 | + context.script.locationId, |
| 61 | + mapLegacyScriptPosition(position) |
| 62 | + ) |
| 63 | + ) |
| 64 | + } |
| 65 | + return try { |
| 66 | + val newDepsFromResolver = resolver.resolve(scriptContents, emptyMap(), ::report, null).get() |
| 67 | + ?: return context.compilationConfiguration.asSuccess(diagnostics) |
| 68 | + val resolvedClasspath = newDepsFromResolver.classpath.toList().takeIf { it.isNotEmpty() } |
| 69 | + ?: return context.compilationConfiguration.asSuccess(diagnostics) |
| 70 | + context.compilationConfiguration.withUpdatedClasspath(resolvedClasspath).asSuccess(diagnostics) |
| 71 | + } catch (e: Throwable) { |
| 72 | + ResultWithDiagnostics.Failure(*diagnostics.toTypedArray(), e.asDiagnostics(path = context.script.locationId)) |
| 73 | + } |
| 74 | +} |
| 75 | + |
0 commit comments