Skip to content

Commit 245b0ba

Browse files
committed
Infer kotlin and kotlinc binaries via KOTLIN_HOME when being set
1 parent 8b04ede commit 245b0ba

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/main/kotlin/kscript/app/resolver/CommandResolver.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ import java.nio.file.Path
99
import java.nio.file.Paths
1010
import kotlin.io.path.absolute
1111
import kotlin.io.path.absolutePathString
12+
import kotlin.io.path.div
1213

1314
class CommandResolver(private val config: Config, private val script: Script) {
1415
fun compileKotlin(jar: Path, dependencies: Set<Path>, filePaths: Set<Path>): String {
1516
val compilerOptsStr = resolveCompilerOpts(script.compilerOpts)
1617
val classpath = resolveClasspath(dependencies)
1718
val files = filePaths.joinToString(" ") { it.absolute().toString() }
1819

19-
return "kotlinc $compilerOptsStr $classpath -d '${jar.absolute()}' $files"
20+
val kotlinc = if (config.kotlinHome != null) (config.kotlinHome / "bin" / "kotlinc").toString() else "kotlinc"
21+
22+
return "$kotlinc $compilerOptsStr $classpath -d '${jar.absolute()}' $files"
2023
}
2124

2225
fun executeKotlin(jarArtifact: JarArtifact, dependencies: Set<Path>, userArgs: List<String>): String {
@@ -33,15 +36,19 @@ class CommandResolver(private val config: Config, private val script: Script) {
3336

3437
val classpath = resolveClasspath(dependenciesSet)
3538

36-
return "kotlin $kotlinOptsStr $classpath ${jarArtifact.execClassName} $userArgsStr"
39+
val kotlin = if (config.kotlinHome != null) (config.kotlinHome / "bin" / "kotlin").toString() else "kotlin"
40+
41+
return "$kotlin $kotlinOptsStr $classpath ${jarArtifact.execClassName} $userArgsStr"
3742
}
3843

3944
fun interactiveKotlinRepl(dependencies: Set<Path>): String {
4045
val compilerOptsStr = resolveCompilerOpts(script.compilerOpts)
4146
val kotlinOptsStr = resolveKotlinOpts(script.kotlinOpts)
4247
val classpath = resolveClasspath(dependencies)
4348

44-
return "kotlinc $compilerOptsStr $kotlinOptsStr $classpath"
49+
val kotlinc = if (config.kotlinHome != null) (config.kotlinHome / "bin" / "kotlinc").toString() else "kotlinc"
50+
51+
return "$kotlinc $compilerOptsStr $kotlinOptsStr $classpath"
4552
}
4653

4754
fun executeIdea(projectPath: Path): String {

src/main/kotlin/kscript/app/util/Executor.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import kscript.app.model.Config
55
import kscript.app.resolver.CommandResolver
66
import kscript.app.util.Logger.devMsg
77
import java.nio.file.Path
8+
import kotlin.io.path.div
89

910
class Executor(private val commandResolver: CommandResolver, private val config: Config) {
1011
fun compileKotlin(jar: Path, dependencies: Set<Path>, filePaths: Set<Path>) {
11-
if (!ShellUtils.isInPath("kotlinc")) {
12+
if (config.kotlinHome == null && !ShellUtils.isInPath("kotlinc")) {
1213
throw IllegalStateException("${"kotlinc"} is not in PATH")
1314
}
1415

@@ -24,8 +25,8 @@ class Executor(private val commandResolver: CommandResolver, private val config:
2425
}
2526

2627
fun executeKotlin(jarArtifact: JarArtifact, dependencies: Set<Path>, userArgs: List<String>) {
27-
if (config.kotlinHome == null) {
28-
throw IllegalStateException("KOTLIN_HOME is not set and could not be inferred from context")
28+
if (config.kotlinHome == null && !ShellUtils.isInPath("kotlin") ) {
29+
throw IllegalStateException("KOTLIN_HOME is not set and could not be inferred from context, and kotlin is not in PATH")
2930
}
3031

3132
val command = commandResolver.executeKotlin(jarArtifact, dependencies, userArgs)

0 commit comments

Comments
 (0)