Skip to content

Commit

Permalink
Add script definition with dynamic dependencies downloading example
Browse files Browse the repository at this point in the history
  • Loading branch information
ligee committed Feb 7, 2020
1 parent 99ed79a commit 6586d05
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 0 deletions.
13 changes: 13 additions & 0 deletions jvm/basic/jvm-maven-deps/host/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

plugins {
kotlin("jvm")
}

val kotlinVersion: String by rootProject.extra

dependencies {
implementation(project(":jvm:basic:jvm-maven-deps:script"))
implementation("org.jetbrains.kotlin:kotlin-scripting-jvm-host:$kotlinVersion")
testImplementation("junit:junit:4.12")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2000-2018 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.script.examples.jvm.resolve.maven.host

import org.jetbrains.kotlin.script.examples.jvm.resolve.maven.ScriptWithMavenDeps
import java.io.File
import kotlin.script.experimental.api.EvaluationResult
import kotlin.script.experimental.api.ResultWithDiagnostics
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate

fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {

val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<ScriptWithMavenDeps>()

return BasicJvmScriptingHost().eval(scriptFile.toScriptSource(), compilationConfiguration, null)
}

fun main(vararg args: String) {
if (args.size != 1) {
println("usage: <app> <script file>")
} else {
val scriptFile = File(args[0])
println("Executing script $scriptFile")

val res = evalFile(scriptFile)

res.reports.forEach {
println(" : ${it.message}" + if (it.exception == null) "" else ": ${it.exception}")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2000-2018 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.script.examples.jvm.resolve.maven.test

import org.jetbrains.kotlin.script.examples.jvm.resolve.maven.host.evalFile
import org.junit.Assert
import org.junit.Test
import java.io.File
import kotlin.script.experimental.api.ResultWithDiagnostics

class ResolveTest {

@Test
fun testResolveJunit() {
val res = evalFile(File("testData/hello-maven-resolve-junit.scriptwithdeps.kts"))

Assert.assertTrue(
"test failed:\n ${res.reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception}" }}",
res is ResultWithDiagnostics.Success
)
}

@Test
fun testUnresolvedJunit() {
val res = evalFile(File("testData/hello-unresolved-junit.scriptwithdeps.kts"))

Assert.assertTrue(
"test failed - expecting a failure with the message \"Unresolved reference: junit\" but received " +
(if (res is ResultWithDiagnostics.Failure) "failure" else "success") +
":\n ${res.reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception}" }}",
res is ResultWithDiagnostics.Failure && res.reports.any { it.message.contains("Unresolved reference: junit") })
}

@Test
fun testResolveError() {
val res = evalFile(File("testData/hello-maven-resolve-error.scriptwithdeps.kts"))

Assert.assertTrue(
"test failed - expecting a failure with the message \"Unknown set of arguments to maven resolver: abracadabra\" but received " +
(if (res is ResultWithDiagnostics.Failure) "failure" else "success") +
":\n ${res.reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception}" }}",
res is ResultWithDiagnostics.Failure && res.reports.any { it.message.contains("Unknown set of arguments to maven resolver: abracadabra") })
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

@file:DependsOn("abracadabra")

println("Hello, World!")

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

@file:DependsOn("junit:junit:4.11")

org.junit.Assert.assertTrue(true)

println("Hello, World!")

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

org.junit.Assert.assertTrue(true)

println("Hello, World!")

14 changes: 14 additions & 0 deletions jvm/basic/jvm-maven-deps/script/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

plugins {
kotlin("jvm")
}

val kotlinVersion: String by rootProject.extra

dependencies {
implementation("org.jetbrains.kotlin:kotlin-scripting-jvm:$kotlinVersion")
implementation("org.jetbrains.kotlin:kotlin-script-util:$kotlinVersion")
runtimeOnly("com.jcabi:jcabi-aether:0.10.1")
runtimeOnly("org.sonatype.aether:aether-api:1.13.1")
runtimeOnly("org.apache.maven:maven-core:3.0.3")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2010-2018 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.script.examples.jvm.resolve.maven

import org.jetbrains.kotlin.script.util.DependsOn
import org.jetbrains.kotlin.script.util.FilesAndMavenResolver
import org.jetbrains.kotlin.script.util.Repository
import java.io.File
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.jvm.JvmDependency
import kotlin.script.experimental.jvm.compat.mapLegacyDiagnosticSeverity
import kotlin.script.experimental.jvm.compat.mapLegacyScriptPosition
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
import kotlin.script.experimental.jvm.jvm
import kotlin.script.experimental.jvm.withUpdatedClasspath

@KotlinScript(
fileExtension = "scriptwithdeps.kts",
compilationConfiguration = ScriptWithMavenDepsConfiguration::class
)
abstract class ScriptWithMavenDeps

object ScriptWithMavenDepsConfiguration : ScriptCompilationConfiguration(
{
defaultImports(DependsOn::class, Repository::class)
jvm {
dependenciesFromCurrentContext(
"script", // script library jar name
"kotlin-script-util" // DependsOn annotation is taken from script-util
)
}
refineConfiguration {
onAnnotations(DependsOn::class, Repository::class, handler = ::configureMavenDepsOnAnnotations)
}
}
)

private val resolver = FilesAndMavenResolver()

fun configureMavenDepsOnAnnotations(context: ScriptConfigurationRefinementContext): ResultWithDiagnostics<ScriptCompilationConfiguration> {
val annotations = context.collectedData?.get(ScriptCollectedData.foundAnnotations)?.takeIf { it.isNotEmpty() }
?: return context.compilationConfiguration.asSuccess()
val scriptContents = object : ScriptContents {
override val annotations: Iterable<Annotation> = annotations
override val file: File? = null
override val text: CharSequence? = null
}
val diagnostics = arrayListOf<ScriptDiagnostic>()
fun report(severity: ScriptDependenciesResolver.ReportSeverity, message: String, position: ScriptContents.Position?) {
diagnostics.add(
ScriptDiagnostic(
message,
mapLegacyDiagnosticSeverity(severity),
context.script.locationId,
mapLegacyScriptPosition(position)
)
)
}
return try {
val newDepsFromResolver = resolver.resolve(scriptContents, emptyMap(), ::report, null).get()
?: return context.compilationConfiguration.asSuccess(diagnostics)
val resolvedClasspath = newDepsFromResolver.classpath.toList().takeIf { it.isNotEmpty() }
?: return context.compilationConfiguration.asSuccess(diagnostics)
context.compilationConfiguration.withUpdatedClasspath(resolvedClasspath).asSuccess(diagnostics)
} catch (e: Throwable) {
ResultWithDiagnostics.Failure(*diagnostics.toTypedArray(), e.asDiagnostics(path = context.script.locationId))
}
}

2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ pluginManagement {
include("jvm:basic:jvm-simple-script:script")
include("jvm:basic:jvm-simple-script:host")

include("jvm:basic:jvm-maven-deps:script")
include("jvm:basic:jvm-maven-deps:host")

0 comments on commit 6586d05

Please sign in to comment.