Skip to content

Commit 852c835

Browse files
committed
Add some comments to the script definitions
1 parent b131f63 commit 852c835

File tree

3 files changed

+43
-2
lines changed
  • jvm
    • basic
      • jvm-maven-deps/script/src/main/kotlin/org/jetbrains/kotlin/script/examples/jvm/resolve/maven
      • jvm-simple-script/script/src/main/kotlin/org/jetbrains/kotlin/script/examples/jvm/simple
    • simple-main-kts/simple-main-kts/src/main/kotlin/org/jetbrains/kotlin/script/examples/simpleMainKts

3 files changed

+43
-2
lines changed

jvm/basic/jvm-maven-deps/script/src/main/kotlin/org/jetbrains/kotlin/script/examples/jvm/resolve/maven/scriptDef.kt

+22-1
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,57 @@ import kotlin.script.experimental.jvm.JvmDependency
1414
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
1515
import 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
2129
abstract class ScriptWithMavenDeps
2230

2331
object 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

3856
private val resolver = CompoundDependenciesResolver(FileSystemDependenciesResolver(), MavenDependenciesResolver())
3957

58+
// The handler that is called during script compilation in order to reconfigure compilation on the fly
4059
fun 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
}

jvm/basic/jvm-simple-script/script/src/main/kotlin/org/jetbrains/kotlin/script/examples/jvm/simple/scriptDef.kt

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,14 @@ package org.jetbrains.kotlin.script.examples.jvm.simple
77

88
import kotlin.script.experimental.annotations.KotlinScript
99

10-
@KotlinScript(fileExtension = "simplescript.kts")
10+
// The KotlinScript annotation marks a class that can serve as a reference to the script definition for
11+
// `createJvmCompilationConfigurationFromTemplate` call as well as for the discovery mechanism
12+
// The marked class also become the base class for defined script type (unless redefined in the configuration)
13+
@KotlinScript(
14+
// file name extension by which this script type is recognized by mechanisms built into scripting compiler plugin
15+
// and IDE support, it is recommendend to use double extension with the last one being "kts", so some non-specific
16+
// scripting support could be used, e.g. in IDE, if the specific support is not installed.
17+
fileExtension = "simplescript.kts"
18+
)
19+
// the class is used as the script base class, therefore it should be open or abstract
1120
abstract class SimpleScript

jvm/simple-main-kts/simple-main-kts/src/main/kotlin/org/jetbrains/kotlin/script/examples/simpleMainKts/scriptDef.kt

+11
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,22 @@ import kotlin.script.experimental.jvm.*
2525
import kotlin.script.experimental.jvmhost.CompiledScriptJarsCache
2626

2727
@Suppress("unused")
28+
// The KotlinScript annotation marks a class that can serve as a reference to the script definition for
29+
// `createJvmCompilationConfigurationFromTemplate` call as well as for the discovery mechanism
30+
// The marked class also become the base class for defined script type (unless redefined in the configuration)
2831
@KotlinScript(
32+
// file name extension by which this script type is recognized by mechanisms built into scripting compiler plugin
33+
// and IDE support, it is recommendend to use double extension with the last one being "kts", so some non-specific
34+
// scripting support could be used, e.g. in IDE, if the specific support is not installed.
2935
fileExtension = "smain.kts",
36+
// the class or object that defines script compilation configuration for this type of scripts
3037
compilationConfiguration = SimpleMainKtsScriptDefinition::class,
38+
// the class or object that defines script evaluation configuration for this type of scripts
3139
evaluationConfiguration = MainKtsEvaluationConfiguration::class
3240
)
41+
// the class is used as the script base class, therefore it should be open or abstract. Also the constructor parameters
42+
// of the base class are copied to the script constructor, so with this definition the script will require `args` to be
43+
// passed to the constructor, and `args` could be used in the script as a defined variable.
3344
abstract class SimpleMainKtsScript(val args: Array<String>)
3445

3546
const val COMPILED_SCRIPTS_CACHE_DIR_ENV_VAR = "KOTLIN_SIMPLE_MAIN_KTS_COMPILED_SCRIPTS_CACHE_DIR"

0 commit comments

Comments
 (0)