diff --git a/.github/workflows/bld.yml b/.github/workflows/bld.yml index 769db33..ceddff2 100644 --- a/.github/workflows/bld.yml +++ b/.github/workflows/bld.yml @@ -9,7 +9,6 @@ jobs: strategy: matrix: java-version: [17, 21, 22] - kotlin-version: [1.9.24, 2.0.0] steps: - name: Checkout source repository diff --git a/.idea/libraries/bld.xml b/.idea/libraries/bld.xml index b04ed47..cde67cb 100644 --- a/.idea/libraries/bld.xml +++ b/.idea/libraries/bld.xml @@ -2,12 +2,12 @@ - + - + diff --git a/.vscode/settings.json b/.vscode/settings.json index 33a9922..d136e4d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,7 +9,7 @@ ], "java.configuration.updateBuildConfiguration": "automatic", "java.project.referencedLibraries": [ - "${HOME}/.bld/dist/bld-2.0.0-SNAPSHOT.jar", + "${HOME}/.bld/dist/bld-1.9.1.jar", "lib/**/*.jar" ] } diff --git a/README.md b/README.md index 8d39dc5..e1c974a 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![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) -[![Kotlin](https://img.shields.io/badge/kotlin-1.9%2B-7f52ff.svg)](https://kotlinlang.org) +[![Kotlin](https://img.shields.io/badge/kotlin-2.0.0-7f52ff.svg)](https://kotlinlang.org) [![bld](https://img.shields.io/badge/1.9.1-FA9052?label=bld&labelColor=2392FF)](https://rife2.com/bld) [![Release](https://flat.badgen.net/maven/v/metadata-url/repo.rife2.com/releases/com/uwyn/rife2/bld-kotlin/maven-metadata.xml?color=blue)](https://repo.rife2.com/#/releases/com/uwyn/rife2/bld-kotlin) [![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) @@ -33,24 +33,6 @@ public void compile() throws Exception { 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. - -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() - .fromProject(this) - .kotlinHome("path/to/kotlin") - .execute(); -} -``` - -While older version of Kotlin are likely working with the extension, only version 1.9 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. diff --git a/examples/.idea/libraries/bld.xml b/examples/.idea/libraries/bld.xml index 2fb5ff0..a2969be 100644 --- a/examples/.idea/libraries/bld.xml +++ b/examples/.idea/libraries/bld.xml @@ -2,12 +2,12 @@ - + - + diff --git a/examples/.vscode/settings.json b/examples/.vscode/settings.json index 33a9922..d136e4d 100644 --- a/examples/.vscode/settings.json +++ b/examples/.vscode/settings.json @@ -9,7 +9,7 @@ ], "java.configuration.updateBuildConfiguration": "automatic", "java.project.referencedLibraries": [ - "${HOME}/.bld/dist/bld-2.0.0-SNAPSHOT.jar", + "${HOME}/.bld/dist/bld-1.9.1.jar", "lib/**/*.jar" ] } diff --git a/examples/lib/bld/bld-wrapper.jar b/examples/lib/bld/bld-wrapper.jar index 68884a0..0bfc526 100644 Binary files a/examples/lib/bld/bld-wrapper.jar and b/examples/lib/bld/bld-wrapper.jar differ diff --git a/examples/lib/bld/bld-wrapper.properties b/examples/lib/bld/bld-wrapper.properties index 413d8f5..edf4d9d 100644 --- a/examples/lib/bld/bld-wrapper.properties +++ b/examples/lib/bld/bld-wrapper.properties @@ -4,4 +4,4 @@ bld.downloadLocation= bld.extension-kotlin=com.uwyn.rife2:bld-kotlin:1.0.0-SNAPSHOT bld.repositories=MAVEN_LOCAL,MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES bld.sourceDirectories= -bld.version=2.0.0-SNAPSHOT +bld.version=1.9.1 diff --git a/lib/bld/bld-wrapper.jar b/lib/bld/bld-wrapper.jar index 5c9583c..902333e 100644 Binary files a/lib/bld/bld-wrapper.jar and b/lib/bld/bld-wrapper.jar differ diff --git a/lib/bld/bld-wrapper.properties b/lib/bld/bld-wrapper.properties index 3037315..34f73c9 100644 --- a/lib/bld/bld-wrapper.properties +++ b/lib/bld/bld-wrapper.properties @@ -5,4 +5,4 @@ bld.extension-exec=com.uwyn.rife2:bld-exec:1.0.1 bld.extension-pmd=com.uwyn.rife2:bld-pmd:1.1.2 bld.repositories=MAVEN_CENTRAL,MAVEN_LOCAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES bld.sourceDirectories= -bld.version=2.0.0-SNAPSHOT +bld.version=1.9.1 diff --git a/scripts/checkcliargs.sh b/scripts/checkcliargs.sh index ef320da..7855fdc 100755 --- a/scripts/checkcliargs.sh +++ b/scripts/checkcliargs.sh @@ -1,11 +1,10 @@ #!/bin/bash -new=/tmp/checkcliargs-new -old=/tmp/checkcliargs-old +main=org.jetbrains.kotlin.cli.jvm.K2JVMCompiler -kotlinc -h 2>$new -~/.sdkman/candidates/kotlin/2.0.0/bin/kotlinc -h 2>$old +java -cp "lib/compile/*" $main -h 2>$new +java -cp "examples/lib/bld/*" $main -h 2>$old -code --diff --wait $old $new +diff $old $new rm -rf $new $old diff --git a/scripts/cliargs.sh b/scripts/cliargs.sh index 0427d9f..ebc70f7 100755 --- a/scripts/cliargs.sh +++ b/scripts/cliargs.sh @@ -1,5 +1,5 @@ #!/bin/bash -kotlinc -h 2> >(grep "^ ") |\ +java -cp "lib/compile/*" org.jetbrains.kotlin.cli.jvm.K2JVMCompiler -h 2> >(grep "^ ") |\ sed -e "s/^ //" -e "s/ .*//" -e "s/<.*//" -e '/-help/d' -e '/-version/d' -e '/^$/d'|\ sort > "src/test/resources/kotlinc-args.txt" diff --git a/src/bld/java/rife/bld/extension/CompileKotlinOperationBuild.java b/src/bld/java/rife/bld/extension/CompileKotlinOperationBuild.java index 655089f..465a49e 100644 --- a/src/bld/java/rife/bld/extension/CompileKotlinOperationBuild.java +++ b/src/bld/java/rife/bld/extension/CompileKotlinOperationBuild.java @@ -40,10 +40,26 @@ public class CompileKotlinOperationBuild extends Project { downloadSources = true; autoDownloadPurge = true; - repositories = List.of(MAVEN_LOCAL, MAVEN_CENTRAL, RIFE2_RELEASES, RIFE2_SNAPSHOTS); + repositories = List.of(MAVEN_LOCAL, MAVEN_CENTRAL, RIFE2_RELEASES); + var kotlin = version(2, 0, 0); scope(compile) - .include(dependency("com.uwyn.rife2", "bld", version(2, 0, 0, "SNAPSHOT"))); + .include(dependency("org.jetbrains.kotlin", "kotlin-compiler", kotlin)) + .include(dependency("org.jetbrains.kotlin", "kotlin-annotation-processing", kotlin)) + .include(dependency("org.jetbrains.kotlin", "kotlin-scripting-compiler", kotlin)) + .include(dependency("org.jetbrains.kotlin", "kotlin-reflect", kotlin)) + .include(dependency("org.jetbrains.kotlin", "kotlin-stdlib-common", kotlin)) + .include(dependency("org.jetbrains.kotlinx", "kotlinx-coroutines-core-jvm", version(1, 9, 0, "RC"))) + // Compiler Plugins + .include(dependency("org.jetbrains.kotlin", "kotlin-allopen-compiler-plugin", kotlin)) + .include(dependency("org.jetbrains.kotlin", "kotlin-assignment-compiler-plugin", kotlin)) + .include(dependency("org.jetbrains.kotlin", "kotlin-serialization-compiler-plugin", kotlin)) + .include(dependency("org.jetbrains.kotlin", "kotlin-lombok-compiler-plugin", kotlin)) + .include(dependency("org.jetbrains.kotlin", "kotlin-allopen-compiler-plugin", kotlin)) + .include(dependency("org.jetbrains.kotlin", "kotlin-noarg-compiler-plugin", kotlin)) + .include(dependency("org.jetbrains.kotlin", "kotlin-power-assert-compiler-plugin", kotlin)) + .include(dependency("org.jetbrains.kotlin", "kotlin-sam-with-receiver-compiler-plugin", kotlin)) + .include(dependency("com.uwyn.rife2", "bld", version(1, 9, 1))); scope(test) .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 3))) .include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 3))) diff --git a/src/main/java/rife/bld/extension/CompileKotlinOperation.java b/src/main/java/rife/bld/extension/CompileKotlinOperation.java index 3b184ee..0a69592 100644 --- a/src/main/java/rife/bld/extension/CompileKotlinOperation.java +++ b/src/main/java/rife/bld/extension/CompileKotlinOperation.java @@ -16,6 +16,7 @@ package rife.bld.extension; +import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler; import rife.bld.BaseProject; import rife.bld.extension.kotlin.CompileOptions; import rife.bld.extension.kotlin.CompilerPlugin; @@ -25,7 +26,6 @@ import rife.tools.FileUtils; import java.io.File; import java.io.IOException; -import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -51,9 +51,32 @@ public class CompileKotlinOperation extends AbstractOperation getJarList(File directory, String regex) { + var jars = new ArrayList(); + + if (directory.isDirectory()) { + var files = directory.listFiles(); + if (files != null) { + for (var f : files) { + if (!f.getName().endsWith("-sources.jar") && (!f.getName().endsWith("-javadoc.jar")) && + f.getName().matches(regex)) { + jars.add(f.getAbsolutePath()); + } + } + } + } + + return jars; + } /** * Determines if the given string is not blank. @@ -198,18 +221,6 @@ 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 k2 = new K2JVMCompiler(); + var args = new ArrayList(); - if (kotlinc.exists() && kotlinc.canExecute()) { - var args = new ArrayList(); + // classpath + args.add("-cp"); + args.add(FileUtils.joinPaths(classpath.stream().toList())); - // kotlinc - args.add(kotlinc.getAbsolutePath()); + // destination + args.add("-d"); + args.add(destination.getAbsolutePath()); - // classpath - args.add("-cp"); - args.add(FileUtils.joinPaths(classpath.stream().toList())); + // friend-path + if (friendPaths != null && friendPaths.exists()) { + args.add("-Xfriend-paths=" + friendPaths.getAbsolutePath()); + } - // destination - args.add("-d"); - args.add(destination.getAbsolutePath()); + // options + if (compileOptions_ != null) { + args.addAll(compileOptions_.args()); + } - // friend-path - if (friendPaths != null && friendPaths.exists()) { - args.add("-Xfriend-paths=" + friendPaths.getAbsolutePath()); - } + // plugins + if (!plugins_.isEmpty()) { + plugins_.forEach(p -> args.add("-Xplugin=" + p)); + } - // options - if (compileOptions_ != null) { - args.addAll(compileOptions_.args()); - } + // sources + sources.forEach(f -> args.add(f.getAbsolutePath())); - // plugins - if (!plugins_.isEmpty()) { - plugins_.forEach(p -> args.add("-Xplugin=" + p)); - } + if (LOGGER.isLoggable(Level.FINE) && !silent()) { + LOGGER.fine("kotlinc " + String.join(" ", args)); + } - // sources - sources.forEach(f -> args.add(f.getAbsolutePath())); - - if (LOGGER.isLoggable(Level.FINE) && !silent()) { - LOGGER.fine(String.join(" ", args)); - } - - var pb = new ProcessBuilder(); - pb.inheritIO(); - pb.command(args); - pb.directory(workDir_); - - var proc = pb.start(); - proc.waitFor(); - ExitStatusException.throwOnFailure(proc.exitValue()); - } else { - if (LOGGER.isLoggable(Level.SEVERE) && !silent()) { - LOGGER.severe("The Kotlin compiler could not be found or executed: " + kotlinc.getAbsolutePath()); - } - throw new ExitStatusException(ExitStatusException.EXIT_FAILURE); + var exitCode = k2.exec(System.err, args.toArray(String[]::new)); + if (exitCode.getCode() != 0) { + throw new ExitStatusException(exitCode.getCode()); } } @@ -315,7 +311,7 @@ public class CompileKotlinOperation extends AbstractOperation * Sets the following from the project: *
    - *
  • {@link #kotlinHome()} to the {@code KOTLIN_HOME} environment variable, if set.
  • - *
  • {@link #workDir()} to the project's directory.
  • *
  • {@link #buildMainDirectory() buildMainDirectory}
  • *
  • {@link #buildTestDirectory() buildTestDirectory}
  • *
  • {@link #compileMainClasspath() compileMainClassPath}
  • @@ -364,14 +358,6 @@ public class CompileKotlinOperation extends AbstractOperation - * The {@link #kotlinHome()} should be set first. * * @param plugins one or more plugins * @return this class instance */ public CompileKotlinOperation plugins(CompilerPlugin... plugins) { - if (kotlinHome_ != null) { - var kotlinLib = new File(kotlinHome_, "lib"); - for (var plugin : plugins) { - plugins(kotlinLib, plugin); - } - } else { - if (LOGGER.isLoggable(Level.WARNING) && !silent()) { - LOGGER.warning("The Kotlin home must be set to specify compiler plugins directly."); - } + for (var plugin : plugins) { + plugins_.addAll(getJarList(project_.libBldDirectory(), plugin.regex)); } return this; } @@ -577,7 +524,7 @@ public class CompileKotlinOperation extends AbstractOperation jars, CompilerPlugin... plugins) { jars.forEach(jar -> { for (var plugin : plugins) { - if (jar.getName().matches(plugin.jar)) { + if (jar.getName().matches(plugin.regex)) { plugins_.add(jar.getAbsolutePath()); break; } @@ -677,34 +624,4 @@ public class CompileKotlinOperation extends AbstractOperation testSourceFiles() { return testSourceFiles_; } - - /** - * Retrieves the working directory. - * - * @return the directory - */ - public File workDir() { - return workDir_; - } - - /** - * Provides the working directory, if it differs from the project's directory. - * - * @param dir the directory - * @return this operation instance - */ - public CompileKotlinOperation workDir(File dir) { - workDir_ = dir; - return this; - } - - /** - * Provides the working directory, if it differs from the project's directory. - * - * @param dir the directory path - * @return this operation instance - */ - public CompileKotlinOperation workDir(String dir) { - return workDir(new File(dir)); - } } diff --git a/src/main/java/rife/bld/extension/kotlin/CompilerPlugin.java b/src/main/java/rife/bld/extension/kotlin/CompilerPlugin.java index 17ed252..7a3d94e 100644 --- a/src/main/java/rife/bld/extension/kotlin/CompilerPlugin.java +++ b/src/main/java/rife/bld/extension/kotlin/CompilerPlugin.java @@ -17,24 +17,23 @@ package rife.bld.extension.kotlin; /** - * Defines the known Kotlin compiler plugin JARs. + * Defines the known Kotlin compiler plugins match (regex) strings. * * @author Erik C. Thauvin * @since 1.0 */ public enum CompilerPlugin { - ALL_OPEN("kotlin-allopen-compiler-plugin.jar"), - ASSIGNMENT("kotlin-assignment-compiler-plugin.jar"), - KOTLINX_SERIALIZATION("kotlinx-serialization-compiler-plugin.jar"), - KOTLIN_SERIALIZATION("kotlin-serialization-compiler-plugin.jar"), - LOMBOK("kotlin-lombok-compiler-plugin.jar"), - NOARG("kotlin-noarg-compiler-plugin.jar"), - POWER_ASSERT("kotlin-power-assert-compiler-plugin.jar"), - SAM_WITH_RECEIVER("kotlin-sam-with-receiver-compiler-plugin.jar"); + ALL_OPEN("^kotlin-allopen-compiler-plugin-.*$"), + ASSIGNMENT("^kotlin-assignment-compiler-plugin-.*$"), + KOTLIN_SERIALIZATION("^kotlin-serialization-compiler-plugin-.*$"), + LOMBOK("^kotlin-lombok-compiler-plugin-.*$"), + NOARG("^kotlin-noarg-compiler-plugin-.*$"), + POWER_ASSERT("^kotlin-power-assert-compiler-plugin-.*$"), + SAM_WITH_RECEIVER("^kotlin-sam-with-receiver-compiler-plugin-.*$"); - public final String jar; + public final String regex; - CompilerPlugin(String jar) { - this.jar = jar; + CompilerPlugin(String regex) { + this.regex = regex; } } diff --git a/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java b/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java index dc62990..51205be 100644 --- a/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java +++ b/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java @@ -53,8 +53,6 @@ class CompileKotlinOperationTest { void testCollections() { var op = new CompileKotlinOperation() .fromProject(new Project()) - .kotlinHome("/kotlin_home") - .workDir("work_dir") .compileMainClasspath("path1", "path2") .compileOptions(new CompileOptions().jdkRelease("17").verbose(true)) .mainSourceDirectories("dir1", "dir2") @@ -74,9 +72,6 @@ class CompileKotlinOperationTest { .plugins(Arrays.stream(Objects.requireNonNull(new File("lib/compile").listFiles())).toList(), CompilerPlugin.ALL_OPEN, CompilerPlugin.SAM_WITH_RECEIVER); - assertThat(op.kotlinHome().getName()).as("kotlin_home").isEqualTo("kotlin_home"); - assertThat(op.workDir().getName()).as("work_dir").isEqualTo("work_dir"); - assertThat(op.compileMainClasspath()).as("compileMainClassPath") .containsAll(List.of("path1", "path2")); assertThat(op.compileOptions().hasRelease()).as("hasRelease").isTrue(); @@ -93,9 +88,7 @@ class CompileKotlinOperationTest { assertThat(op.testSourceFiles()).as("testSourceFiles").containsOnly( new File("tfile1"), new File("tfile2"), new File("tfile3"), new File("tfile4"), new File("tfile5"), new File("tfile6")); - assertThat(op.plugins()).as("plugins").contains("/kotlin_home/lib/kotlin-serialization-compiler-plugin.jar", - "/kotlin_home/lib/kotlin-assignment-compiler-plugin.jar", "plugin2", "plugin3", "plugin4"); - assertThat(op.plugins()).as("plugins size").hasSize(8); + assertThat(op.plugins()).hasSize(10); } @Test