@@ -14,36 +14,57 @@ import kotlin.script.experimental.jvm.JvmDependency
14
14
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
15
15
import kotlin.script.experimental.jvm.jvm
16
16
17
+ // The KotlinScript annotation marks a class that can serve as a reference to the script definition for
18
+ // `createJvmCompilationConfigurationFromTemplate` call as well as for the discovery mechanism
19
+ // The marked class also become the base class for defined script type (unless redefined in the configuration)
17
20
@KotlinScript(
21
+ // file name extension by which this script type is recognized by mechanisms built into scripting compiler plugin
22
+ // and IDE support, it is recommendend to use double extension with the last one being "kts", so some non-specific
23
+ // scripting support could be used, e.g. in IDE, if the specific support is not installed.
18
24
fileExtension = " scriptwithdeps.kts" ,
25
+ // the class or object that defines script compilation configuration for this type of scripts
19
26
compilationConfiguration = ScriptWithMavenDepsConfiguration ::class
20
27
)
28
+ // the class is used as the script base class, therefore it should be open or abstract
21
29
abstract class ScriptWithMavenDeps
22
30
23
31
object ScriptWithMavenDepsConfiguration : ScriptCompilationConfiguration(
24
32
{
33
+ // adds implicit import statements (in this case `implort kotlin.script.experimental.dependencies.DependsOn`, etc.)
34
+ // to each script on compilation
25
35
defaultImports(DependsOn ::class, Repository ::class)
36
+
26
37
jvm {
38
+ // the dependenciesFromCurrentContext helper function extracts the classpath from current thread classloader
39
+ // and take jars with mentioned names to the compilation classpath via `dependencies` key.
40
+ // to add the whole classpath for the classloader without check for jar presense, use
41
+ // `dependenciesFromCurrentContext(wholeClasspath = true)`
27
42
dependenciesFromCurrentContext(
28
43
"script", // script library jar name
29
44
"kotlin-scripting-dependencies" // DependsOn annotation is taken from this jar
30
45
)
31
46
}
47
+ // section that callbacks during compilation
32
48
refineConfiguration {
49
+ // the callback called than any of the listed file-level annotations are encountered in the compiled script
50
+ // the processing is defined by the `handler`, that may return refined configuration depending on the annotations
33
51
onAnnotations(DependsOn ::class, Repository ::class, handler = ::configureMavenDepsOnAnnotations)
34
52
}
35
53
}
36
54
)
37
55
38
56
private val resolver = CompoundDependenciesResolver (FileSystemDependenciesResolver (), MavenDependenciesResolver ())
39
57
58
+ // The handler that is called during script compilation in order to reconfigure compilation on the fly
40
59
fun configureMavenDepsOnAnnotations (context : ScriptConfigurationRefinementContext ): ResultWithDiagnostics <ScriptCompilationConfiguration > {
41
60
val annotations = context.collectedData?.get(ScriptCollectedData .collectedAnnotations)?.takeIf { it.isNotEmpty() }
42
- ? : return context.compilationConfiguration.asSuccess()
61
+ ? : return context.compilationConfiguration.asSuccess() // If no action is performed, the original configuration should be returned
43
62
return runBlocking {
63
+ // resolving maven artifacts using annotation arguments
44
64
resolver.resolveFromScriptSourceAnnotations(annotations)
45
65
}.onSuccess {
46
66
context.compilationConfiguration.with {
67
+ // updating the original configurations with the newly resolved artifacts as compilation dependencies
47
68
dependencies.append(JvmDependency (it))
48
69
}.asSuccess()
49
70
}
0 commit comments