-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6f049b
commit 347bbe5
Showing
7 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env kotlin | ||
|
||
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-html-jvm:0.8.0") | ||
|
||
import kotlinx.html.*; import kotlinx.html.stream.* // ; import kotlinx.html.attributes.* | ||
|
||
val addressee = args.firstOrNull() ?: "World" | ||
|
||
print(createHTML().html { | ||
body { | ||
h1 { +"Hello, $addressee!" } | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
plugins { | ||
id("java") | ||
kotlin("jvm") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation(kotlin("scripting-common")) | ||
implementation(kotlin("scripting-jvm")) | ||
implementation(kotlin("scripting-dependencies")) | ||
implementation(kotlin("scripting-dependencies-maven")) | ||
implementation(kotlin("stdlib-jdk8")) | ||
|
||
// coroutines dependency is required for this particular definition | ||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1") | ||
|
||
testImplementation(platform("org.junit:junit-bom:5.10.0")) | ||
testImplementation("org.junit.jupiter:junit-jupiter") | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} | ||
|
||
kotlin { | ||
jvmToolchain(21) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
import kotlin.script.experimental.api.* | ||
import kotlin.script.experimental.dependencies.* | ||
import kotlin.script.experimental.dependencies.maven.MavenDependenciesResolver | ||
import kotlin.script.experimental.jvm.JvmDependency | ||
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext | ||
import kotlin.script.experimental.jvm.jvm | ||
import kotlin.script.experimental.annotations.KotlinScript | ||
import kotlinx.coroutines.runBlocking | ||
|
||
|
||
// @KotlinScript annotation marks a script definition class | ||
@KotlinScript( | ||
// File extension for the script type | ||
fileExtension = "webmonitor.kts", | ||
// Compilation configuration for the script type | ||
compilationConfiguration = ScriptWithMavenDepsConfiguration::class | ||
) | ||
abstract class ScriptWithMavenDeps | ||
|
||
object ScriptWithMavenDepsConfiguration : ScriptCompilationConfiguration( | ||
{ | ||
// Implicit imports for all scripts of this type | ||
defaultImports(DependsOn::class, Repository::class) | ||
jvm { | ||
// Extract the whole classpath from context classloader and use it as dependencies | ||
dependenciesFromCurrentContext(wholeClasspath = true) | ||
} | ||
// Callbacks | ||
refineConfiguration { | ||
// Process specified annotations with the provided handler | ||
onAnnotations(DependsOn::class, Repository::class, handler = ::configureMavenDepsOnAnnotations) | ||
} | ||
} | ||
) | ||
|
||
private val resolver = CompoundDependenciesResolver(FileSystemDependenciesResolver(), MavenDependenciesResolver()) | ||
|
||
// The handler that is called during script compilation in order to reconfigure compilation on the fly | ||
fun configureMavenDepsOnAnnotations(context: ScriptConfigurationRefinementContext): ResultWithDiagnostics<ScriptCompilationConfiguration> { | ||
val annotations = context.collectedData?.get(ScriptCollectedData.collectedAnnotations)?.takeIf { it.isNotEmpty() } | ||
?: return context.compilationConfiguration.asSuccess() // If no action is performed, the original configuration should be returned | ||
return runBlocking { | ||
// resolving maven artifacts using annotation arguments | ||
resolver.resolveFromScriptSourceAnnotations(annotations) | ||
}.onSuccess { | ||
context.compilationConfiguration.with { | ||
// updating the original configurations with the newly resolved artifacts as compilation dependencies | ||
dependencies.append(JvmDependency(it)) | ||
}.asSuccess() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
plugins { | ||
id("java") | ||
kotlin("jvm") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation(kotlin("stdlib-jdk8")) | ||
implementation(kotlin("scripting-common")) | ||
implementation(kotlin("scripting-jvm")) | ||
implementation(kotlin("scripting-jvm-host")) | ||
|
||
implementation(project(":script-definition")) // the script definition module | ||
|
||
testImplementation(platform("org.junit:junit-bom:5.10.0")) | ||
testImplementation("org.junit.jupiter:junit-jupiter") | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} | ||
kotlin { | ||
jvmToolchain(21) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
import java.io.File | ||
import kotlin.script.experimental.api.EvaluationResult | ||
import kotlin.script.experimental.api.ResultWithDiagnostics | ||
import kotlin.script.experimental.api.ScriptDiagnostic | ||
import kotlin.script.experimental.host.toScriptSource | ||
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost | ||
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate | ||
|
||
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 { | ||
if (it.severity > ScriptDiagnostic.Severity.DEBUG) { | ||
println(" : ${it.message}" + if (it.exception == null) "" else ": ${it.exception}") | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> { | ||
val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<ScriptWithMavenDeps>() | ||
return BasicJvmScriptingHost().eval(scriptFile.toScriptSource(), compilationConfiguration, null) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
plugins { | ||
id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0" | ||
} | ||
rootProject.name = providers.gradleProperty("project.name").get() | ||
include("script-definition") | ||
include("script-host") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env kotlin | ||
|
||
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-html-jvm:0.8.0") | ||
|
||
import kotlinx.html.*; import kotlinx.html.stream.*; import kotlinx.html.attributes.* | ||
|
||
val addressee = "World" | ||
|
||
print( | ||
createHTML().html { | ||
body { | ||
h1 { +"Hello, $addressee!" } | ||
} | ||
} | ||
) |