@@ -14,36 +14,57 @@ import kotlin.script.experimental.jvm.JvmDependency
1414import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
1515import kotlin.script.experimental.jvm.jvm
1616
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)
1720@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.
1824 fileExtension = " scriptwithdeps.kts" ,
25+ // the class or object that defines script compilation configuration for this type of scripts
1926 compilationConfiguration = ScriptWithMavenDepsConfiguration ::class
2027)
28+ // the class is used as the script base class, therefore it should be open or abstract
2129abstract class ScriptWithMavenDeps
2230
2331object ScriptWithMavenDepsConfiguration : ScriptCompilationConfiguration(
2432 {
33+ // adds implicit import statements (in this case `implort kotlin.script.experimental.dependencies.DependsOn`, etc.)
34+ // to each script on compilation
2535 defaultImports(DependsOn ::class, Repository ::class)
36+
2637 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)`
2742 dependenciesFromCurrentContext(
2843 "script", // script library jar name
2944 "kotlin-scripting-dependencies" // DependsOn annotation is taken from this jar
3045 )
3146 }
47+ // section that callbacks during compilation
3248 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
3351 onAnnotations(DependsOn ::class, Repository ::class, handler = ::configureMavenDepsOnAnnotations)
3452 }
3553 }
3654)
3755
3856private val resolver = CompoundDependenciesResolver (FileSystemDependenciesResolver (), MavenDependenciesResolver ())
3957
58+ // The handler that is called during script compilation in order to reconfigure compilation on the fly
4059fun configureMavenDepsOnAnnotations (context : ScriptConfigurationRefinementContext ): ResultWithDiagnostics <ScriptCompilationConfiguration > {
4160 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
4362 return runBlocking {
63+ // resolving maven artifacts using annotation arguments
4464 resolver.resolveFromScriptSourceAnnotations(annotations)
4565 }.onSuccess {
4666 context.compilationConfiguration.with {
67+ // updating the original configurations with the newly resolved artifacts as compilation dependencies
4768 dependencies.append(JvmDependency (it))
4869 }.asSuccess()
4970 }
0 commit comments