Skip to content

Commit 80bc7c6

Browse files
Refactor kscript for Kotlin 2.0.0 and Sandbox Execution
This commit represents a significant refactoring of kscript's core functionality to modernize its dependency management and script execution by leveraging the Kotlin Scripting API and a sandboxed environment. **Summary of Changes:** 1. **Upgraded to Kotlin 2.0.0:** * The project's `build.gradle.kts` has been updated to use Kotlin version `2.0.0`. * The GitHub Actions CI workflow (`.github/workflows/build.yml`) has been updated to use Kotlin `2.0.0`. 2. **New Dependency Resolution Mechanism:** * I introduced `KscriptDefinition.kt`, which defines a custom script type for kscript using `@KotlinScript`. * I implemented `KscriptCompilationConfiguration` to use Kotlin's `refineConfigurationOnAnnotations` mechanism. * I created `DependencyResolver.kt` with a handler (`resolveKscriptDependencies`) that processes `@file:DependsOn` and `@file:Repository` annotations. * This handler utilizes `org.jetbrains.kotlin.scripting.dependencies.maven.MavenDependenciesResolver` to resolve dependencies against Maven repositories. Resolved dependencies are paths to JARs in the local Maven cache (`~/.m2/repository`). 3. **Sandboxed Script Execution:** * I modified `KscriptHandler.kt` to orchestrate a new sandboxed execution flow: * A unique temporary sandbox directory is created for each script run. * Resolved dependencies (from the local Maven cache) are copied into a `lib/` subdirectory within the sandbox. * I refactored `JarArtifactCreator.kt` to be "sandbox-aware": * It no longer generates custom wrapper classes for KTS files, relying on the Kotlin Scripting API compilation based on `KscriptBase`. * It compiles your script and produces `scriplet.jar` within an `out/` subdirectory of the sandbox. The compile-time classpath for this step uses the paths from the local Maven cache. * The script (`scriplet.jar`) is then executed from within the sandbox. The runtime classpath correctly points to `sandbox/out/scriplet.jar` and the JARs in `sandbox/lib/*`. * The sandbox directory is cleaned up after script execution. 4. **Build System Dependencies:** * I added necessary `kotlin-scripting-*` dependencies (`common`, `jvm`, `dependencies`, `dependencies-maven`) and updated `kotlinx-coroutines-core` in `build.gradle.kts` to support these changes. **Overall Impact:** This refactoring aims to: * Modernize kscript by aligning it with the official Kotlin Scripting APIs. * Simplify and make more robust kscript's internal dependency resolution logic by leveraging Maven/Ivy resolvers provided by Kotlin. * Improve script execution isolation and classpath management through sandboxing. * Lay the groundwork for future enhancements by building on a standard Kotlin foundation. **Current Status & Next Steps:** I refactored the core execution path for scripts. I updated the CI environment to Kotlin 2.0.0. The immediate next step, which I was working on, is to run these changes through CI to: * Verify that kscript builds successfully with Kotlin 2.0.0 and the new dependencies. * Assess the impact on existing tests. Many tests, particularly those related to dependency resolution and execution, will likely require significant updates. * Address any build failures or test failures. Features like interactive mode (`--interactive`), packaging (`--package`), and Idea project generation (`--idea`) are currently bypassed in the new flow and will need to be refactored to be sandbox-aware in subsequent work. This captures the foundational changes. Further work is needed to adapt all features and thoroughly test the new architecture.
1 parent ebe1989 commit 80bc7c6

File tree

7 files changed

+742
-58
lines changed

7 files changed

+742
-58
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
- name: Setup Kotlin
4040
uses: fwilhe2/setup-kotlin@main
4141
with:
42-
version: 1.7.21
42+
version: 2.0.0
4343

4444
- name: Setup Gradle
4545
uses: gradle/[email protected]

build.gradle.kts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
44
import java.time.ZoneOffset
55
import java.time.ZonedDateTime
66

7-
val kotlinVersion: String = "1.7.21"
7+
val kotlinVersion: String = "2.0.0"
88

99
plugins {
10-
kotlin("jvm") version "1.7.21"
10+
kotlin("jvm") version "2.0.0"
1111
application
1212
id("com.adarshr.test-logger") version "3.2.0"
1313
id("com.github.gmazzo.buildconfig") version "3.1.0"
@@ -291,11 +291,13 @@ dependencies {
291291

292292
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion")
293293
implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
294-
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
294+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0") // Updated version
295295

296-
implementation("org.jetbrains.kotlin:kotlin-scripting-common:$kotlinVersion")
297-
implementation("org.jetbrains.kotlin:kotlin-scripting-jvm:$kotlinVersion")
298-
implementation("org.jetbrains.kotlin:kotlin-scripting-dependencies-maven-all:$kotlinVersion")
296+
implementation("org.jetbrains.kotlin:kotlin-scripting-common:$kotlinVersion") // Retained, will use new kotlinVersion
297+
implementation("org.jetbrains.kotlin:kotlin-scripting-jvm:$kotlinVersion") // Retained, will use new kotlinVersion
298+
// Replaced kotlin-scripting-dependencies-maven-all with more specific artifacts as per subtask
299+
implementation("org.jetbrains.kotlin:kotlin-scripting-dependencies:$kotlinVersion")
300+
implementation("org.jetbrains.kotlin:kotlin-scripting-dependencies-maven:$kotlinVersion")
299301

300302
implementation("org.apache.commons:commons-lang3:3.12.0")
301303
implementation("commons-io:commons-io:2.11.0")

0 commit comments

Comments
 (0)