Commit 80bc7c6
committed
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- .github/workflows
- src/main/kotlin/io/github/kscripting/kscript
- creator
- scripting
7 files changed
+742
-58
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
291 | 291 | | |
292 | 292 | | |
293 | 293 | | |
294 | | - | |
| 294 | + | |
295 | 295 | | |
296 | | - | |
297 | | - | |
298 | | - | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
299 | 301 | | |
300 | 302 | | |
301 | 303 | | |
| |||
0 commit comments