From c112d37ac1a739f6cb3a90de8edde5f6dd8d2661 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Thu, 14 May 2020 21:05:32 +0200 Subject: [PATCH] Consolidate resolving in the new lib, deprecate it in script-util also deprecate Import and CompileOptions annotations, because they do not seem generic enough. Create specific copie in the main-kts instead. --- .../jvm-maven-deps/script/build.gradle.kts | 5 +- .../examples/jvm/resolve/maven/scriptDef.kt | 28 ++-------- .../experimental/dependencies/annotations.kt | 54 +++++++++++++++++++ .../tools/kotlin-main-kts/build.gradle.kts | 2 - .../jetbrains/kotlin/mainKts/annotations.kt | 35 ++++++++++++ .../jetbrains/kotlin/mainKts/impl/resolve.kt | 36 ------------- .../org/jetbrains/kotlin/mainKts/scriptDef.kt | 10 +--- .../kotlin/script/util/annotations.kt | 16 ++---- .../jetbrains/kotlin/script/util/resolve.kt | 3 ++ .../kotlin/script/util/resolvers/basic.kt | 2 + .../resolvers/experimental/genericResolver.kt | 7 +++ .../kotlin/script/util/resolvers/maven.kt | 2 + .../util/resolvers/resolverInterface.kt | 1 + 13 files changed, 115 insertions(+), 86 deletions(-) create mode 100644 libraries/scripting/dependencies/src/kotlin/script/experimental/dependencies/annotations.kt create mode 100644 libraries/tools/kotlin-main-kts/src/org/jetbrains/kotlin/mainKts/annotations.kt delete mode 100644 libraries/tools/kotlin-main-kts/src/org/jetbrains/kotlin/mainKts/impl/resolve.kt diff --git a/libraries/examples/scripting/jvm-maven-deps/script/build.gradle.kts b/libraries/examples/scripting/jvm-maven-deps/script/build.gradle.kts index c461c0bc67e..f3528466c10 100644 --- a/libraries/examples/scripting/jvm-maven-deps/script/build.gradle.kts +++ b/libraries/examples/scripting/jvm-maven-deps/script/build.gradle.kts @@ -5,11 +5,8 @@ plugins { dependencies { compile(project(":kotlin-scripting-jvm")) + compile(project(":kotlin-scripting-dependencies")) compile(project(":kotlin-scripting-dependencies-maven")) - compile(project(":kotlin-script-util")) - runtime("com.jcabi:jcabi-aether:0.10.1") - runtime("org.sonatype.aether:aether-api:1.13.1") - runtime("org.apache.maven:maven-core:3.0.3") } sourceSets { diff --git a/libraries/examples/scripting/jvm-maven-deps/script/src/org/jetbrains/kotlin/script/examples/jvm/resolve/maven/scriptDef.kt b/libraries/examples/scripting/jvm-maven-deps/script/src/org/jetbrains/kotlin/script/examples/jvm/resolve/maven/scriptDef.kt index 3e054ba1017..15383a45cc3 100644 --- a/libraries/examples/scripting/jvm-maven-deps/script/src/org/jetbrains/kotlin/script/examples/jvm/resolve/maven/scriptDef.kt +++ b/libraries/examples/scripting/jvm-maven-deps/script/src/org/jetbrains/kotlin/script/examples/jvm/resolve/maven/scriptDef.kt @@ -6,14 +6,10 @@ package org.jetbrains.kotlin.script.examples.jvm.resolve.maven import kotlinx.coroutines.runBlocking -import org.jetbrains.kotlin.script.util.DependsOn -import org.jetbrains.kotlin.script.util.Repository import kotlin.script.experimental.annotations.KotlinScript import kotlin.script.experimental.api.* -import kotlin.script.experimental.dependencies.CompoundDependenciesResolver -import kotlin.script.experimental.dependencies.FileSystemDependenciesResolver +import kotlin.script.experimental.dependencies.* import kotlin.script.experimental.dependencies.maven.MavenDependenciesResolver -import kotlin.script.experimental.dependencies.tryAddRepository import kotlin.script.experimental.jvm.JvmDependency import kotlin.script.experimental.jvm.dependenciesFromCurrentContext import kotlin.script.experimental.jvm.jvm @@ -30,7 +26,7 @@ object ScriptWithMavenDepsConfiguration : ScriptCompilationConfiguration( jvm { dependenciesFromCurrentContext( "scripting-jvm-maven-deps", // script library jar name - "kotlin-script-util" // DependsOn annotation is taken from script-util + "kotlin-scripting-dependencies" // DependsOn annotation is taken from script-util ) } refineConfiguration { @@ -44,24 +40,8 @@ private val resolver = CompoundDependenciesResolver(FileSystemDependenciesResolv fun configureMavenDepsOnAnnotations(context: ScriptConfigurationRefinementContext): ResultWithDiagnostics { val annotations = context.collectedData?.get(ScriptCollectedData.foundAnnotations)?.takeIf { it.isNotEmpty() } ?: return context.compilationConfiguration.asSuccess() - annotations.forEach { annotation -> - when (annotation) { - is Repository -> { - val repositoryCoordinates = with(annotation) { value.takeIf { it.isNotBlank() } ?: url } - if (!resolver.tryAddRepository(repositoryCoordinates)) - return makeFailureResult("Unrecognized repository coordinates: $repositoryCoordinates") - } - is DependsOn -> {} - else -> return makeFailureResult("Unknown annotation ${annotation.javaClass}") - } - } - return annotations.filterIsInstance(DependsOn::class.java).flatMapSuccess { dep -> - val artifactCoordinates = - if (dep.value.isNotBlank()) dep.value - else listOf(dep.groupId, dep.artifactId, dep.version).filter { it.isNotBlank() }.joinToString(":") - runBlocking { - resolver.resolve(artifactCoordinates) - } + return runBlocking { + resolver.resolveFromAnnotations(annotations) }.onSuccess { context.compilationConfiguration.with { dependencies.append(JvmDependency(it)) diff --git a/libraries/scripting/dependencies/src/kotlin/script/experimental/dependencies/annotations.kt b/libraries/scripting/dependencies/src/kotlin/script/experimental/dependencies/annotations.kt new file mode 100644 index 00000000000..554bc6262dd --- /dev/null +++ b/libraries/scripting/dependencies/src/kotlin/script/experimental/dependencies/annotations.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package kotlin.script.experimental.dependencies + +import java.io.File +import kotlin.script.experimental.api.ResultWithDiagnostics +import kotlin.script.experimental.api.flatMapSuccess +import kotlin.script.experimental.api.makeFailureResult + +/** + * A common annotation that could be used in a script to denote a dependency + * The annotation could be processed by configuration refinement code and it's arguments passed to an ExternalDependenciesResolver + * for resolving dependencies + */ +@Target(AnnotationTarget.FILE) +@Repeatable +@Retention(AnnotationRetention.SOURCE) +annotation class DependsOn(vararg val artifactsCoordinates: String) + +/** + * A common annotation that could be used in a script to denote a repository for an ExternalDependenciesResolver + * The annotation could be processed by configuration refinement code and it's arguments passed to an ExternalDependenciesResolver + * for configuring repositories + */ +@Target(AnnotationTarget.FILE) +@Repeatable +@Retention(AnnotationRetention.SOURCE) +annotation class Repository(vararg val repositoriesCoordinates: String) + +/** + * An extension function that configures repositories and resolves artifacts denoted by the [Repository] and [DependsOn] annotations + */ +suspend fun ExternalDependenciesResolver.resolveFromAnnotations(annotations: Iterable): ResultWithDiagnostics> { + annotations.forEach { annotation -> + when (annotation) { + is Repository -> { + for (coordinates in annotation.repositoriesCoordinates) { + if (!tryAddRepository(coordinates)) + return makeFailureResult("Unrecognized repository coordinates: $coordinates") + } + } + is DependsOn -> {} + else -> return makeFailureResult("Unknown annotation ${annotation.javaClass}") + } + } + return annotations.filterIsInstance(DependsOn::class.java) + .flatMap { it.artifactsCoordinates.asIterable() } + .flatMapSuccess { artifactCoordinates -> + resolve(artifactCoordinates) + } +} diff --git a/libraries/tools/kotlin-main-kts/build.gradle.kts b/libraries/tools/kotlin-main-kts/build.gradle.kts index 50701fea34f..6f83d40d1aa 100644 --- a/libraries/tools/kotlin-main-kts/build.gradle.kts +++ b/libraries/tools/kotlin-main-kts/build.gradle.kts @@ -29,7 +29,6 @@ dependencies { compileOnly(project(":compiler:cli-common")) compileOnly(project(":kotlin-scripting-jvm-host")) compileOnly(project(":kotlin-scripting-dependencies")) - compileOnly(project(":kotlin-script-util")) runtime(project(":kotlin-compiler-embeddable")) runtime(project(":kotlin-scripting-compiler-embeddable")) runtime(project(":kotlin-scripting-jvm-host-embeddable")) @@ -38,7 +37,6 @@ dependencies { embedded(project(":kotlin-scripting-jvm")) { isTransitive = false } embedded(project(":kotlin-scripting-jvm-host")) { isTransitive = false } embedded(project(":kotlin-scripting-dependencies")) { isTransitive = false } - embedded(project(":kotlin-script-util")) { isTransitive = false } embedded("org.apache.ivy:ivy:2.5.0") embedded(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-core")) { isTransitive = false } embedded(commonDep("org.jetbrains.kotlinx:kotlinx-collections-immutable-jvm")) { diff --git a/libraries/tools/kotlin-main-kts/src/org/jetbrains/kotlin/mainKts/annotations.kt b/libraries/tools/kotlin-main-kts/src/org/jetbrains/kotlin/mainKts/annotations.kt new file mode 100644 index 00000000000..e7df7b65a8a --- /dev/null +++ b/libraries/tools/kotlin-main-kts/src/org/jetbrains/kotlin/mainKts/annotations.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.mainKts + +/** + * Import other script(s) + */ +@Target(AnnotationTarget.FILE) +@Repeatable +@Retention(AnnotationRetention.SOURCE) +annotation class Import(vararg val paths: String) + +/** + * Compiler options that will be applied on script compilation + * + * @see [kotlin.script.experimental.api.compilerOptions] + */ +@Target(AnnotationTarget.FILE) +@Repeatable +@Retention(AnnotationRetention.SOURCE) +annotation class CompilerOptions(vararg val options: String) diff --git a/libraries/tools/kotlin-main-kts/src/org/jetbrains/kotlin/mainKts/impl/resolve.kt b/libraries/tools/kotlin-main-kts/src/org/jetbrains/kotlin/mainKts/impl/resolve.kt deleted file mode 100644 index ded2ee02b4b..00000000000 --- a/libraries/tools/kotlin-main-kts/src/org/jetbrains/kotlin/mainKts/impl/resolve.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.mainKts.impl - -import org.jetbrains.kotlin.script.util.DependsOn -import org.jetbrains.kotlin.script.util.Repository -import java.io.File -import kotlin.script.experimental.api.ResultWithDiagnostics -import kotlin.script.experimental.api.flatMapSuccess -import kotlin.script.experimental.api.makeFailureResult -import kotlin.script.experimental.dependencies.ExternalDependenciesResolver -import kotlin.script.experimental.dependencies.tryAddRepository - -suspend fun resolveFromAnnotations(resolver: ExternalDependenciesResolver, annotations: Iterable): ResultWithDiagnostics> { - annotations.forEach { annotation -> - when (annotation) { - is Repository -> { - val repositoryCoordinates = with(annotation) { value.takeIf { it.isNotBlank() } ?: url } - if (!resolver.tryAddRepository(repositoryCoordinates)) - return makeFailureResult("Unrecognized repository coordinates: $repositoryCoordinates") - } - is DependsOn -> {} - else -> return makeFailureResult("Unknown annotation ${annotation.javaClass}") - } - } - return annotations.filterIsInstance(DependsOn::class.java).flatMapSuccess { dep -> - val artifactCoordinates = - if (dep.value.isNotBlank()) dep.value - else listOf(dep.groupId, dep.artifactId, dep.version).filter { it.isNotBlank() }.joinToString(":") - resolver.resolve(artifactCoordinates) - } -} - diff --git a/libraries/tools/kotlin-main-kts/src/org/jetbrains/kotlin/mainKts/scriptDef.kt b/libraries/tools/kotlin-main-kts/src/org/jetbrains/kotlin/mainKts/scriptDef.kt index 622256fb122..940ed15f39f 100644 --- a/libraries/tools/kotlin-main-kts/src/org/jetbrains/kotlin/mainKts/scriptDef.kt +++ b/libraries/tools/kotlin-main-kts/src/org/jetbrains/kotlin/mainKts/scriptDef.kt @@ -7,19 +7,13 @@ package org.jetbrains.kotlin.mainKts import kotlinx.coroutines.runBlocking import org.jetbrains.kotlin.mainKts.impl.IvyResolver -import org.jetbrains.kotlin.mainKts.impl.resolveFromAnnotations -import org.jetbrains.kotlin.script.util.CompilerOptions -import org.jetbrains.kotlin.script.util.DependsOn -import org.jetbrains.kotlin.script.util.Import -import org.jetbrains.kotlin.script.util.Repository import java.io.File import java.security.MessageDigest import kotlin.script.dependencies.ScriptContents import kotlin.script.dependencies.ScriptDependenciesResolver import kotlin.script.experimental.annotations.KotlinScript import kotlin.script.experimental.api.* -import kotlin.script.experimental.dependencies.CompoundDependenciesResolver -import kotlin.script.experimental.dependencies.FileSystemDependenciesResolver +import kotlin.script.experimental.dependencies.* import kotlin.script.experimental.host.FileBasedScriptSource import kotlin.script.experimental.host.FileScriptSource import kotlin.script.experimental.host.ScriptingHostConfiguration @@ -138,7 +132,7 @@ class MainKtsConfigurator : RefineScriptCompilationConfigurationHandler { val resolveResult = try { runBlocking { - resolveFromAnnotations(resolver, annotations.filter { it is DependsOn || it is Repository }) + resolver.resolveFromAnnotations( annotations.filter { it is DependsOn || it is Repository }) } } catch (e: Throwable) { ResultWithDiagnostics.Failure(*diagnostics.toTypedArray(), e.asDiagnostics(path = context.script.locationId)) diff --git a/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/annotations.kt b/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/annotations.kt index 803db866b62..a08a30d7355 100644 --- a/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/annotations.kt +++ b/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/annotations.kt @@ -16,33 +16,25 @@ package org.jetbrains.kotlin.script.util -// in case of flat or direct resolvers the value should be a direct path or file name of a jar respectively -// in case of maven resolver the maven coordinates string is accepted (resolved with com.jcabi.aether library) +@Deprecated("Use annotations an processing code from the kotlin-scripting-dependencies library") @Target(AnnotationTarget.FILE) @Repeatable @Retention(AnnotationRetention.SOURCE) annotation class DependsOn(val value: String = "", val groupId: String = "", val artifactId: String = "", val version: String = "") -// only flat directory repositories are supported now, so value should be a path to a directory with jars -// TODO: support other types of repos +@Deprecated("Use annotations an processing code from the kotlin-scripting-dependencies library") @Target(AnnotationTarget.FILE) @Repeatable @Retention(AnnotationRetention.SOURCE) annotation class Repository(val value: String = "", val id: String = "", val url: String = "") -/** - * Import other script(s) - */ +@Deprecated("Use your own annotations, this will be removed soon") @Target(AnnotationTarget.FILE) @Repeatable @Retention(AnnotationRetention.SOURCE) annotation class Import(vararg val paths: String) -/** - * Compiler options that will be applied on script compilation - * - * @see [kotlin.script.experimental.api.compilerOptions] - */ +@Deprecated("Use your own annotations, this will be removed soon") @Target(AnnotationTarget.FILE) @Repeatable @Retention(AnnotationRetention.SOURCE) diff --git a/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolve.kt b/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolve.kt index 5b5fe1267e8..9bdea470041 100644 --- a/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolve.kt +++ b/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolve.kt @@ -30,6 +30,7 @@ import kotlin.script.dependencies.ScriptDependenciesResolver import kotlin.script.dependencies.asFuture import kotlin.script.templates.AcceptedAnnotations +@Deprecated("Use new resolving classes from kotlin-scripting-dependencies and kotlin-scripting-dependencies-maven") open class KotlinAnnotatedScriptDependenciesResolver(val baseClassPath: List, resolvers: Iterable) : ScriptDependenciesResolver { @@ -75,9 +76,11 @@ open class KotlinAnnotatedScriptDependenciesResolver(val baseClassPath: List? = artifactCoordinates.string.takeUnless(String::isBlank) @@ -30,6 +31,7 @@ class DirectResolver : GenericRepositoryWithBridge { override fun tryAddRepository(repositoryCoordinates: GenericRepositoryCoordinates): Boolean = false } +@Deprecated("Use new resolving classes from kotlin-scripting-dependencies") class FlatLibDirectoryResolver(vararg paths: File) : GenericRepositoryWithBridge { private val localRepos = arrayListOf() diff --git a/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolvers/experimental/genericResolver.kt b/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolvers/experimental/genericResolver.kt index d4a2932aa70..a8e4642999c 100644 --- a/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolvers/experimental/genericResolver.kt +++ b/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolvers/experimental/genericResolver.kt @@ -13,10 +13,12 @@ import org.jetbrains.kotlin.script.util.resolvers.toRepositoryUrlOrNull import java.io.File import java.net.URL +@Deprecated("Use new resolving classes from kotlin-scripting-dependencies") interface GenericArtifactCoordinates { val string: String } +@Deprecated("Use new resolving classes from kotlin-scripting-dependencies") interface GenericRepositoryCoordinates { val string: String val name: String? get() = null @@ -24,6 +26,7 @@ interface GenericRepositoryCoordinates { val file: File? get() = (url?.takeIf { it.protocol == "file" }?.path ?: string).toRepositoryFileOrNull() } +@Deprecated("Use new resolving classes from kotlin-scripting-dependencies") interface GenericResolver { fun tryResolve(artifactCoordinates: GenericArtifactCoordinates): Iterable? fun tryAddRepository(repositoryCoordinates: GenericRepositoryCoordinates): Boolean @@ -34,10 +37,13 @@ interface GenericResolver { tryAddRepository(BasicRepositoryCoordinates(repositoryCoordinates, id)) } +@Deprecated("Use new resolving classes from kotlin-scripting-dependencies") open class BasicArtifactCoordinates(override val string: String) : GenericArtifactCoordinates +@Deprecated("Use new resolving classes from kotlin-scripting-dependencies") open class BasicRepositoryCoordinates(override val string: String, override val name: String? = null) : GenericRepositoryCoordinates +@Deprecated("Use new resolving classes from kotlin-scripting-dependencies") interface GenericRepositoryWithBridge : GenericResolver, Resolver { override fun tryResolve(dependsOn: DependsOn): Iterable? = tryResolve( @@ -55,6 +61,7 @@ interface GenericRepositoryWithBridge : GenericResolver, Resolver { } } +@Deprecated("Use new resolving classes from kotlin-scripting-dependencies") open class MavenArtifactCoordinates( val value: String?, val groupId: String?, diff --git a/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolvers/maven.kt b/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolvers/maven.kt index 1ddc3a8267b..eee357d85c3 100644 --- a/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolvers/maven.kt +++ b/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolvers/maven.kt @@ -31,8 +31,10 @@ import org.sonatype.aether.util.artifact.JavaScopes import java.io.File import java.util.* +@Deprecated("Use new resolving classes from kotlin-scripting-dependencies and kotlin-scripting-dependencies-maven") val mavenCentral = RemoteRepository("maven-central", "default", "https://repo.maven.apache.org/maven2/") +@Deprecated("Use kotlin.script.experimental.dependencies.maven.MavenDependenciesResolver from kotlin-scripting-dependencies-maven") class MavenResolver(val reportError: ((String) -> Unit)? = null): GenericRepositoryWithBridge { // TODO: make robust diff --git a/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolvers/resolverInterface.kt b/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolvers/resolverInterface.kt index 4ee4092cb16..5e6c7be1aa9 100644 --- a/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolvers/resolverInterface.kt +++ b/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/util/resolvers/resolverInterface.kt @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.script.util.DependsOn import org.jetbrains.kotlin.script.util.Repository import java.io.File +@Deprecated("Use new resolving classes from kotlin-scripting-dependencies") interface Resolver { fun tryResolve(dependsOn: DependsOn): Iterable? fun tryAddRepo(annotation: Repository): Boolean