Skip to content

Commit 9f945de

Browse files
authored
Merge pull request #8 from jnorthrup/feat/python-npm-packaging
Feat/python npm packaging
2 parents 0e81106 + 528edfa commit 9f945de

File tree

11 files changed

+392
-31
lines changed

11 files changed

+392
-31
lines changed

README.adoc

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ In particular this wrapper around `kotlinc` adds
2424
Taken all these features together, `kscript` provides an easy-to-use, very flexible, and almost zero-overhead solution
2525
to write self-contained mini-applications with Kotlin.
2626
27-
*Good News*: Kotlin https://kotlinlang.org/docs/reference/whatsnew14.html#scripting-and-repl[v1.4] finally ships with a much improved - and needed - scripting integration. See https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/main-kts/MainKts.md[here] for examples and documentation. Still, we think that `kscript` has various benefits compared this new platform-bundled improved toolstack, so we'll plan to support `kscript` until the kotlin platform will ship with an even more rich and versatile kotlin scripting interpreter.
28-
29-
*https://holgerbrandl.github.io/kscript_kotlinconf_2017/kscript_kotlinconf.html[`kscript` presentation from KotlinConf2017!]*
27+
Kotlin's native scripting capabilities have continually improved, providing a solid foundation. `kscript` builds upon this by offering a rich set of features to enhance the scripting experience, including simplified dependency management, compiled script caching, flexible script input modes, and deployment options. While Kotlin's own scripting support is powerful, `kscript` aims to provide additional conveniences and power tools for scripters.
3028
3129
'''
3230
* <<Installation>>
@@ -64,6 +62,8 @@ Once Kotlin is ready, you can install `kscript` with
6462
sdk install kscript
6563
----
6664
65+
Package managers like SDKMAN, Homebrew, and Scoop provide convenient ways to install `kscript`. Note that the version they provide might not always be the absolute latest. For the most recent updates or to use `kscript` with very new Kotlin/Java versions, consider the <<Build it yourself>> section.
66+
6767
To test your installation simply run
6868
6969
[source,bash]
@@ -169,8 +169,11 @@ To install `scoop` use the https://github.com/ScoopInstaller/Install[official gu
169169
170170
=== Build it yourself
171171
172-
To build `kscript` yourself, simply clone the repo and do
172+
To build `kscript` yourself, which can be useful for accessing the very latest features or using it with specific modern Kotlin/Java versions (like Kotlin 2.2.0+ and Java 21+ which have been tested):
173+
174+
Ensure you have a modern JDK installed (e.g., JDK 17 or newer, JDK 21 recommended for recent Kotlin versions). The build uses the Gradle wrapper (`gradlew`), which will download the appropriate Gradle version.
173175
176+
Clone the repository and then run:
174177
[source,bash]
175178
----
176179
./gradlew assemble
@@ -445,9 +448,9 @@ dependencies:
445448
----
446449
io.github.kscripting:kscript-annotations:1.5
447450
----
451+
// (Note: The `kscript-annotations` artifact version `1.5` is from an older release cycle. While it may still work for basic annotations, for projects using newer Kotlin versions (like 2.x), you might need to check for a more recent version of `kscript-annotations` if available, or be mindful of potential Kotlin standard library version misalignments if this artifact pulls in an older one.)
448452

449-
`kscript` will automatically detect an annotation-driven script, and if so will declare a dependency on this artifact
450-
internally.
453+
`kscript` will automatically detect an annotation-driven script, and if so will declare a dependency on `io.github.kscripting:kscript-annotations` (historically version 1.5) internally.
451454

452455
Note, that if a script is located in a package other than the root package, you need to import the annotations with (
453456
e.g. `import DependsOn`).
@@ -729,21 +732,7 @@ the `@file:Entry`.
729732

730733
=== What are performance and resource usage difference between scripting with kotlin and python?
731734

732-
Kotlin is a compiled language, so there is a compilation overhead when you run a script/application written in Kotlin
733-
for the first time.
734-
735-
Kotlin runs (mainly) on the JVM which needs some time (~200ms) to start up. In contrast, the python interpreter has
736-
close to zero warmup time.
737-
738-
I think there is a consensus that JVM programs execute much faster than python equivalents. Still, python might be
739-
faster depending on your specific usecase. Also, with kotlin-native becoming more mature, you could compile into native
740-
binaries directly, which should bring it close to C/C++ performance.
741-
742-
Main motivations for using Kotlin over Python for scripting and development are
743-
744-
* Kotlin is the better designed, more fluent language with much better tooling around it
745-
* The JVM dependency ecosystem allows for strict versioning. No more messing around with virtualenv, e.g. to run a short
746-
10liner against a specific version of numpy.
735+
Kotlin scripts involve a JVM startup and, for the first run of a script, a compilation step. While the JVM offers excellent peak performance for longer-running or complex tasks, the initial overhead might be noticeable for very short-lived, simple scripts when compared to languages like Python that have minimal startup time. The best choice often depends on the specific use case, script complexity, access to Java/Kotlin libraries, and developer familiarity with the ecosystems.
747736

748737
=== Does kscript work with java?
749738

@@ -774,16 +763,7 @@ Help to spread the word. Great community articles about `kscript` include
774763
-using kscript
775764

776765
You could also show your support by upvoting `kscript` here on github, or by voting for issues in Intellij IDEA which
777-
impact `kscript`ing. Here are our top 2 tickets/annoyances that we would love to see fixed:
778-
779-
* https://youtrack.jetbrains.com/issue/KT-13347[KT-13347] Good code is red in injected kotlin language snippets
780-
781-
To allow for more interactive script development, you could also vote/comment on the most annoying REPL issues.
782-
783-
* https://youtrack.jetbrains.net/issue/KT-24789[KT-24789] "Unresolved reference" when running a script which is a
784-
symlink to a script outside of source roots
785-
* https://youtrack.jetbrains.com/issue/KT-12583[KT-12583] IDE REPL should run in project root directory
786-
* https://youtrack.jetbrains.com/issue/KT-11409[KT-11409] Allow to "Send Selection To Kotlin Console"
766+
impact `kscript`ing. For specific kscript issues or feature proposals, please use the kscript GitHub issue tracker. For broader Kotlin language or IDE-related issues, the official JetBrains YouTrack is the appropriate place.
787767

788768
== Acknowledgements
789769

src/main/kotlin/io/github/kscripting/kscript/Kscript.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,46 @@ fun main(args: Array<String>) {
9393
return
9494
}
9595

96+
// Handle --export-to-gradle-project
97+
if (parsedOptions.containsKey("export-to-gradle-project")) {
98+
val outputDirPathValue = parsedOptions["export-to-gradle-project"]
99+
val scriptPathValue = parsedOptions["script"] // Script path is taken from remaining args
100+
101+
// Basic validation
102+
if (scriptPathValue == null || scriptPathValue.isBlank()) {
103+
errorMsg("The <script> argument is required for --export-to-gradle-project.")
104+
exitProcess(1)
105+
}
106+
if (outputDirPathValue == null || outputDirPathValue.isBlank()) {
107+
errorMsg("The <output_dir> argument for --export-to-gradle-project is required.")
108+
exitProcess(1)
109+
}
110+
111+
val scriptFile = java.nio.file.Paths.get(scriptPathValue).toFile()
112+
if (!scriptFile.exists()) {
113+
errorMsg("Script file not found: $scriptPathValue")
114+
exitProcess(1)
115+
}
116+
117+
// scriptPathValue is already validated to be non-null/blank
118+
// outputDirPathValue is also validated
119+
120+
try {
121+
io.github.kscripting.kscript.generator.exportToGradleProject(
122+
scriptFilePathString = scriptPathValue!!, // scriptPathValue is a String
123+
outputDir = java.nio.file.Paths.get(outputDirPathValue!!),
124+
cliOptions = parsedOptions.toMap(),
125+
config = config // Pass the config object
126+
)
127+
info("Gradle project export process finished for '$scriptPathValue' to '$outputDirPathValue'.")
128+
exitProcess(0)
129+
} catch (e: Exception) {
130+
errorMsg("Error during --export-to-gradle-project for script '$scriptPathValue': ${e.message}")
131+
e.printStackTrace() // For detailed debugging during development
132+
exitProcess(1)
133+
}
134+
}
135+
96136
KscriptHandler(executor, config, parsedOptions).handle(userArgs)
97137
} catch (e: Exception) {
98138
errorMsg(e)

0 commit comments

Comments
 (0)