diff --git a/README.md b/README.md index a781a44..8ac65c8 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# [Kotlin](https://kotlinlang.org/) Extension for [bld](https://rife2.com/bld) +# [Kotlin](https://kotlinlang.org/) Extension for [bld](https://rife2.com/bld) [![License](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![Java](https://img.shields.io/badge/java-17%2B-blue)](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html) @@ -8,7 +8,8 @@ [![Snapshot](https://flat.badgen.net/maven/v/metadata-url/repo.rife2.com/snapshots/com/uwyn/rife2/bld-kotlin/maven-metadata.xml?label=snapshot)](https://repo.rife2.com/#/snapshots/com/uwyn/rife2/bld-kotlin) [![GitHub CI](https://github.com/rife2/bld-kotlin/actions/workflows/bld.yml/badge.svg)](https://github.com/rife2/bld-kotlin/actions/workflows/bld.yml) -To install, please refer to the [extensions](https://github.com/rife2/bld/wiki/Extensions) and [support](https://github.com/rife2/bld/wiki/Kotlin-Support) +To install, please refer to the [extensions](https://github.com/rife2/bld/wiki/Extensions) +and [support](https://github.com/rife2/bld/wiki/Kotlin-Support) documentation. ## Compile Kotlin Source Code @@ -16,6 +17,7 @@ documentation. To compile the source code located in `src/main/kotlin` and `src/test/kotlin` from the current project: ```java + @BuildCommand(summary = "Compiles the Kotlin project") public void compile() throws Exception { new CompileKotlinOperation() @@ -30,16 +32,18 @@ public void compile() throws Exception { - [View Examples Project](https://github.com/rife2/bld-kotlin/tree/main/examples/) -Please check the [Compile Operation documentation](https://rife2.github.io/bld-kotlin/rife/bld/extension/CompileKotlinOperation.html#method-summary) +Please check +the [Compile Operation documentation](https://rife2.github.io/bld-kotlin/rife/bld/extension/CompileKotlinOperation.html#method-summary) for all available configuration options. ## Kotlin Compiler Requirement -Please make sure Kotlin is installed and that the `KOTLIN_HOME` environment variable is set. +Please make sure the Kotlin compiler is [installed](https://kotlinlang.org/docs/command-line.html#install-the-compiler). You can also manually configure the Kotlin home location as follows: ```java + @BuildCommand(summary = "Compiles the Kotlin project") public void compile() throws Exception { new CompileKotlinOperation() @@ -49,8 +53,23 @@ public void compile() throws Exception { } ``` -While older version of Kotlin are likely working with the extension, only version 1.9.24 or higher are officially supported. +The Kotlin compiler executable can also be specified directly: + +```java + +@BuildCommand(summary = "Compiles the Kotlin project") +public void compile() throws Exception { + new CompileKotlinOperation() + .fromProject(this) + .kotlinc("/usr/bin/kotlinc") + .execute(); +} +``` + +While older version of Kotlin are likely working with the extension, only version 1.9.24 or higher are officially +supported. ## Template Project -There is also a [Template Project](https://github.com/rife2/kotlin-bld-example) with support for the [Dokka](https://github.com/rife2/bld-dokka) and [Detekt](https://github.com/rife2/bld-detekt) extensions. +There is also a [Template Project](https://github.com/rife2/kotlin-bld-example) with support for +the [Dokka](https://github.com/rife2/bld-dokka) and [Detekt](https://github.com/rife2/bld-detekt) extensions. diff --git a/examples/README.md b/examples/README.md index 220d440..a029909 100644 --- a/examples/README.md +++ b/examples/README.md @@ -18,5 +18,4 @@ ## Requirements -- Kotlin installed -- `KOTLIN_HOME` environment variable set \ No newline at end of file +- A Kotlin compiler must be [installed](https://kotlinlang.org/docs/command-line.html#install-the-compiler). diff --git a/examples/src/bld/java/com/example/ExampleBuild.java b/examples/src/bld/java/com/example/ExampleBuild.java index da2b49a..b8bd68a 100644 --- a/examples/src/bld/java/com/example/ExampleBuild.java +++ b/examples/src/bld/java/com/example/ExampleBuild.java @@ -5,7 +5,6 @@ import rife.bld.Project; import rife.bld.extension.CompileKotlinOperation; import java.io.File; -import java.io.IOException; import java.util.List; import java.util.logging.ConsoleHandler; import java.util.logging.Level; @@ -43,14 +42,14 @@ public class ExampleBuild extends Project { public static void main(String[] args) { // Enable detailed logging for the Kotlin extension - var level = Level.ALL; - var logger = Logger.getLogger("rife.bld.extension"); - var consoleHandler = new ConsoleHandler(); + var level = Level.ALL; + var logger = Logger.getLogger("rife.bld.extension"); + var consoleHandler = new ConsoleHandler(); - consoleHandler.setLevel(level); - logger.addHandler(consoleHandler); - logger.setLevel(level); - logger.setUseParentHandlers(false); + consoleHandler.setLevel(level); + logger.addHandler(consoleHandler); + logger.setLevel(level); + logger.setUseParentHandlers(false); new ExampleBuild().start(args); } @@ -61,6 +60,8 @@ public class ExampleBuild extends Project { // The source code located in src/main/kotlin and src/test/kotlin will be compiled new CompileKotlinOperation() .fromProject(this) +// .kotlinHome("path/to/kotlin") +// .kotlinc("path/to/kotlinc") .execute(); // var op = new CompileKotlinOperation().fromProject(this); diff --git a/src/main/java/rife/bld/extension/CompileKotlinOperation.java b/src/main/java/rife/bld/extension/CompileKotlinOperation.java index 3b184ee..e97cd31 100644 --- a/src/main/java/rife/bld/extension/CompileKotlinOperation.java +++ b/src/main/java/rife/bld/extension/CompileKotlinOperation.java @@ -52,6 +52,7 @@ public class CompileKotlinOperation extends AbstractOperation classpath, Collection sources, File destination, File friendPaths) - throws ExitStatusException, InterruptedException, IOException { + throws ExitStatusException { if (sources.isEmpty() || destination == null) { return; } - var kotlinc = Path.of(kotlinHome_.getAbsolutePath(), "bin", "kotlinc").toFile(); + var args = new ArrayList(); - if (kotlinc.exists() && kotlinc.canExecute()) { - var args = new ArrayList(); + // kotlinc + args.add(kotlinCompiler()); - // kotlinc - args.add(kotlinc.getAbsolutePath()); + // classpath + args.add("-cp"); + args.add(FileUtils.joinPaths(classpath.stream().toList())); - // classpath - args.add("-cp"); - args.add(FileUtils.joinPaths(classpath.stream().toList())); + // destination + args.add("-d"); + args.add(destination.getAbsolutePath()); - // destination - args.add("-d"); - args.add(destination.getAbsolutePath()); + // friend-path + if (friendPaths != null && friendPaths.exists()) { + args.add("-Xfriend-paths=" + friendPaths.getAbsolutePath()); + } - // friend-path - if (friendPaths != null && friendPaths.exists()) { - args.add("-Xfriend-paths=" + friendPaths.getAbsolutePath()); - } + // options + if (compileOptions_ != null) { + args.addAll(compileOptions_.args()); + } - // options - if (compileOptions_ != null) { - args.addAll(compileOptions_.args()); - } + // plugins + if (!plugins_.isEmpty()) { + plugins_.forEach(p -> args.add("-Xplugin=" + p)); + } - // plugins - if (!plugins_.isEmpty()) { - plugins_.forEach(p -> args.add("-Xplugin=" + p)); - } + // sources + sources.forEach(f -> args.add(f.getAbsolutePath())); - // sources - sources.forEach(f -> args.add(f.getAbsolutePath())); + if (LOGGER.isLoggable(Level.FINE) && !silent()) { + LOGGER.fine(String.join(" ", args)); + } - if (LOGGER.isLoggable(Level.FINE) && !silent()) { - LOGGER.fine(String.join(" ", args)); - } - - var pb = new ProcessBuilder(); - pb.inheritIO(); - pb.command(args); - pb.directory(workDir_); + var pb = new ProcessBuilder(); + pb.inheritIO(); + pb.command(args); + pb.directory(workDir_); + try { var proc = pb.start(); proc.waitFor(); ExitStatusException.throwOnFailure(proc.exitValue()); - } else { + } catch (IOException | InterruptedException e) { if (LOGGER.isLoggable(Level.SEVERE) && !silent()) { - LOGGER.severe("The Kotlin compiler could not be found or executed: " + kotlinc.getAbsolutePath()); + LOGGER.severe(e.getLocalizedMessage()); } throw new ExitStatusException(ExitStatusException.EXIT_FAILURE); } @@ -315,7 +308,7 @@ public class CompileKotlinOperation extends AbstractOperation - * The {@link #kotlinHome()} should be set first. + * Provides compiler plugins located in the {@link #kotlinHome()} lib directory. * * @param plugins one or more plugins * @return this class instance diff --git a/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java b/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java index dc62990..351714b 100644 --- a/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java +++ b/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java @@ -54,6 +54,7 @@ class CompileKotlinOperationTest { var op = new CompileKotlinOperation() .fromProject(new Project()) .kotlinHome("/kotlin_home") + .kotlinc("kotlinc") .workDir("work_dir") .compileMainClasspath("path1", "path2") .compileOptions(new CompileOptions().jdkRelease("17").verbose(true)) @@ -75,6 +76,7 @@ class CompileKotlinOperationTest { CompilerPlugin.ALL_OPEN, CompilerPlugin.SAM_WITH_RECEIVER); assertThat(op.kotlinHome().getName()).as("kotlin_home").isEqualTo("kotlin_home"); + assertThat(op.kotlinc().getName()).as("kotlinc").isEqualTo("kotlinc"); assertThat(op.workDir().getName()).as("work_dir").isEqualTo("work_dir"); assertThat(op.compileMainClasspath()).as("compileMainClassPath")