Skip to content

Add composite script examples showcasing @file:Import usage in Kotlin scripts #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ allprojects {
}
}

dependencies {
// adding these dependencies here just to have it locally available after gradle project import
// can be useful when running performance tests for .main.kts scripts
implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.8.0")
implementation("eu.jrie.jetbrains:kotlin-shell-core:0.2.1")
}

1 change: 1 addition & 0 deletions jvm/main-kts/MainKts.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ with easy interaction with Kotlin code and can be executed:
```
kotlin kotlin-shell.main.kts main-kts/example.txt
```
- [`composite/composite.main.kts`](scripts/composite/composite.main.kts) demonstrates script composition capabilities via `@file:Import` annotation

23 changes: 23 additions & 0 deletions jvm/main-kts/scripts/composite/composite.main.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env kotlin

/**
* A script demonstrating the composition functionality of .main.kts scripts using imports.
*
* Key features:
* - Supports both relative and absolute paths for imports
* - Multiple imports using repeated annotations
* - All public top-level declarations (variables, functions, classes) from imported scripts
* become available in the importing script
* - Each imported script is evaluated when imported, not just its declarations are included
* - Imported scripts are evaluated in the order they appear
*
* Note: Import resolution is performed relative to the importing script's location
* when using relative paths.
*/


@file:Import("kts/simple.kts")
@file:Import("imported.main.kts")

sharedMainKtsVar++
println("simple.kts members: $ktsScriptClassMembers")
4 changes: 4 additions & 0 deletions jvm/main-kts/scripts/composite/imported.main.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var sharedMainKtsVar = 2

// The __FILE__ variable contains the absolute path of the current .main.kts script
println("Hi from $__FILE__")
11 changes: 11 additions & 0 deletions jvm/main-kts/scripts/composite/kts/simple.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import kotlin.reflect.full.declaredMembers

// Kotlin scripts (.kts files) automatically include these dependencies:
// 1. kotlin-script-runtime.jar - provides script-specific functionality
val scriptArgs = args

// 2. kotlin-stdlib.jar - provides the Kotlin standard library
println("Hello from simple.kts! Args=$scriptArgs")

// 3. kotlin-reflect.jar - provides reflection capabilities
val ktsScriptClassMembers = this::class.declaredMembers
21 changes: 21 additions & 0 deletions jvm/main-kts/scripts/kotlin-shell.main.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
#!/usr/bin/env kotlin

/**
* A command-line utility script that provides directory listing and word counting functionality.
*
* Usage:
* - Without arguments: Lists files in the current directory (equivalent to `ls -l`)
* - With file argument: Performs word count analysis (similar to `wc` command)
* Shows both custom implementation and system `wc` command results
*
* Example usage:
* ```
* ./kotlin-shell.main.kts # Lists directory contents
* ./kotlin-shell.main.kts file.txt # Counts lines, words, and characters in file.txt
* ```
*
* Dependencies:
* - kotlin-shell-core:0.2.1
*
* @param args - command line arguments. If provided, the first argument should be a file path
*/


@file:DependsOn("eu.jrie.jetbrains:kotlin-shell-core:0.2.1")
@file:CompilerOptions("-Xopt-in=kotlin.RequiresOptIn")
@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
Expand Down
13 changes: 12 additions & 1 deletion jvm/main-kts/scripts/kotlinx-html.main.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
#!/usr/bin/env kotlin

/**
* A demonstration script showing HTML generation using kotlinx.html DSL.
* This script creates and prints a simple HTML document with a "Hello, World!" heading.
*
* Usage:
* - Run the script to generate HTML output
* - No command line arguments needed
*
* Dependencies:
* - kotlinx-html-jvm:0.8.0
*/

@file:DependsOn("org.jetbrains.kotlinx:kotlinx-html-jvm:0.8.0")

import kotlinx.html.*
Expand All @@ -12,4 +24,3 @@ print(createHTML().html {
h1 { +"Hello, $addressee!" }
}
})

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy