diff --git a/src/main/java/rife/bld/extension/CompileKotlinOptions.java b/src/main/java/rife/bld/extension/kotlin/CompileOptions.java similarity index 68% rename from src/main/java/rife/bld/extension/CompileKotlinOptions.java rename to src/main/java/rife/bld/extension/kotlin/CompileOptions.java index 890f3c0..7703105 100644 --- a/src/main/java/rife/bld/extension/CompileKotlinOptions.java +++ b/src/main/java/rife/bld/extension/kotlin/CompileOptions.java @@ -14,13 +14,13 @@ * limitations under the License. */ -package rife.bld.extension; +package rife.bld.extension.kotlin; + +import rife.bld.extension.CompileKotlinOperation; import java.io.File; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import java.util.*; +import java.util.stream.Collectors; import static rife.bld.extension.CompileKotlinOperation.isNotBlank; @@ -30,30 +30,30 @@ import static rife.bld.extension.CompileKotlinOperation.isNotBlank; * @author Erik C. Thauvin * @since 1.0 */ -public class CompileKotlinOptions { - private final List advancedOptions_ = new ArrayList<>(); - private final List argFile_ = new ArrayList<>(); - private final List classpath_ = new ArrayList<>(); - private final List jvmOptions_ = new ArrayList<>(); - private final List optIn_ = new ArrayList<>(); - private final List options_ = new ArrayList<>(); - private final List plugin_ = new ArrayList<>(); - private final List scriptTemplates_ = new ArrayList<>(); +public class CompileOptions { + private final Collection advancedOptions_ = new ArrayList<>(); + private final Collection argFile_ = new ArrayList<>(); + private final Collection classpath_ = new ArrayList<>(); + private final Collection jvmOptions_ = new ArrayList<>(); + private final Collection optIn_ = new ArrayList<>(); + private final Collection options_ = new ArrayList<>(); + private final Collection plugin_ = new ArrayList<>(); + private final Collection scriptTemplates_ = new ArrayList<>(); private String apiVersion_; private String expression_; private boolean includeRuntime_; private boolean javaParameters_; - private String jdkHome_; + private File jdkHome_; private String jdkRelease_; private String jvmTarget_; - private String kotlinHome_; + private File kotlinHome_; private String languageVersion_; private String moduleName_; private boolean noJdk_; private boolean noReflect_; private boolean noStdLib_; private boolean noWarn_; - private String path_; + private File path_; private boolean progressive_; private boolean verbose_; private boolean wError_; @@ -64,7 +64,7 @@ public class CompileKotlinOptions { * @param options one or more advanced options * @return this operation instance */ - public CompileKotlinOptions advancedOptions(String... options) { + public CompileOptions advancedOptions(String... options) { Collections.addAll(advancedOptions_, options); return this; } @@ -75,18 +75,27 @@ public class CompileKotlinOptions { * @param options list of compiler options * @return this operation instance */ - public CompileKotlinOptions advancedOptions(Collection options) { + public CompileOptions advancedOptions(Collection options) { advancedOptions_.addAll(options); return this; } + /** + * Retrieves advanced compiler options. + * + * @return the advanced compiler options + */ + public Collection advancedOptions() { + return advancedOptions_; + } + /** * Allow using declarations only from the specified version of Kotlin bundled libraries. * * @param version the api version * @return this operation instance */ - public CompileKotlinOptions apiVersion(String version) { + public CompileOptions apiVersion(String version) { apiVersion_ = version; return this; } @@ -97,7 +106,7 @@ public class CompileKotlinOptions { * @param version the api version * @return this operation instance */ - public CompileKotlinOptions apiVersion(int version) { + public CompileOptions apiVersion(int version) { apiVersion_ = String.valueOf(version); return this; } @@ -120,11 +129,37 @@ public class CompileKotlinOptions { * @param files one or more files * @return this operation instance */ - public CompileKotlinOptions argFile(String... files) { + public CompileOptions argFile(String... files) { + Collections.addAll(argFile_, Arrays.stream(files) + .map(File::new) + .toArray(File[]::new)); + return this; + } + + /** + * Read the compiler options from the given files. + *

+ * Such a file can contain compiler options with values and paths to the source files. + * Options and paths should be separated by whitespaces. For example: + *

    + *
  • {@code -include-runtime -d hello.jar hello.kt}
  • + *
+ * To pass values that contain whitespaces, surround them with single ({@code '}) or double ({@code "}) quotes. + * If a value contains quotation marks in it, escape them with a backslash (\). + *
    + *
  • {@code -include-runtime -d 'My folder'}
  • + *
+ * If the files reside in locations different from the current directory, use relative paths. + * + * @param files one or more files + * @return this operation instance + */ + public CompileOptions argFile(File... files) { Collections.addAll(argFile_, files); return this; } + /** * Read the compiler options from the given files. * @@ -132,11 +167,20 @@ public class CompileKotlinOptions { * @return this operation instance * @see #argFile(String...) */ - public CompileKotlinOptions argFile(Collection files) { + public CompileOptions argFile(Collection files) { argFile_.addAll(files); return this; } + /** + * Retrieves the files containing compiler options. + * + * @return the list of files + */ + public Collection argFile() { + return argFile_; + } + /** * Returns the formatted arguments. * @@ -153,13 +197,13 @@ public class CompileKotlinOptions { // @argfile if (!argFile_.isEmpty()) { - argFile_.forEach(f -> args.add("@" + f)); + argFile_.forEach(f -> args.add("@" + f.getAbsolutePath())); } // classpath if (!classpath_.isEmpty()) { args.add("-classpath"); - args.add(String.join(File.pathSeparator, classpath_)); + args.add(classpath_.stream().map(File::getAbsolutePath).collect(Collectors.joining(File.pathSeparator))); } // expression @@ -185,9 +229,9 @@ public class CompileKotlinOptions { } // jdk-home - if (isNotBlank(jdkHome_)) { + if (jdkHome_ != null) { args.add("-jdk-home"); - args.add(jdkHome_); + args.add(jdkHome_.getAbsolutePath()); } // jdk-release @@ -201,9 +245,9 @@ public class CompileKotlinOptions { } // kotlin-home - if (isNotBlank(kotlinHome_)) { + if (kotlinHome_ != null) { args.add("-kotlin-home"); - args.add(kotlinHome_); + args.add(kotlinHome_.getAbsolutePath()); } // language-version @@ -250,9 +294,9 @@ public class CompileKotlinOptions { } // path - if (isNotBlank(path_)) { + if (path_ != null) { args.add("-d"); - args.add(path_); + args.add(path_.getAbsolutePath()); } // plugin @@ -298,7 +342,22 @@ public class CompileKotlinOptions { * @param paths one pr more paths * @return this operation instance */ - public CompileKotlinOptions classpath(String... paths) { + public CompileOptions classpath(String... paths) { + Collections.addAll(classpath_, Arrays.stream(paths) + .map(File::new) + .toArray(File[]::new)); + return this; + } + + /** + * Search for class files in the specified paths. + *

+ * The classpath can contain file and directory paths, ZIP, or JAR files. + * + * @param paths one or more path + * @return this operation instance + */ + public CompileOptions classpath(File... paths) { Collections.addAll(classpath_, paths); return this; } @@ -311,18 +370,27 @@ public class CompileKotlinOptions { * @param paths the list of paths * @return this operation instance */ - public CompileKotlinOptions classpath(Collection paths) { + public CompileOptions classpath(Collection paths) { classpath_.addAll(paths); return this; } + /** + * Retrieves the class files classpath. + * + * @return the list of classpath + */ + public Collection classpath() { + return classpath_; + } + /** * Evaluate the given string as a Kotlin script. * * @param expression the expression * @return this operation instance */ - public CompileKotlinOptions expression(String expression) { + public CompileOptions expression(String expression) { expression_ = expression; return this; } @@ -343,18 +411,27 @@ public class CompileKotlinOptions { * @param includeRuntime {@code true} or {@code false} * @return this operation instance */ - public CompileKotlinOptions includeRuntime(boolean includeRuntime) { + public CompileOptions includeRuntime(boolean includeRuntime) { includeRuntime_ = includeRuntime; return this; } + /** + * Indicates whether {@link #verbose(boolean)} was set. + * + * @return {@code true} if verbose was set; or {@code false} otherwise + */ + public boolean isVerbose() { + return verbose_; + } + /** * Generate metadata for Java 1.8 reflection on method parameters. * * @param javaParameters {@code true} or {@code false} * @return this operation instance */ - public CompileKotlinOptions javaParameters(boolean javaParameters) { + public CompileOptions javaParameters(boolean javaParameters) { javaParameters_ = javaParameters; return this; } @@ -365,7 +442,18 @@ public class CompileKotlinOptions { * @param jdkHome the JDK home path * @return this operation instance */ - public CompileKotlinOptions jdkHome(String jdkHome) { + public CompileOptions jdkHome(String jdkHome) { + jdkHome_ = new File(jdkHome); + return this; + } + + /** + * Use a custom JDK home directory to include into the classpath if it differs from the default {@code JAVA_HOME}. + * + * @param jdkHome the JDK home path + * @return this operation instance + */ + public CompileOptions jdkHome(File jdkHome) { jdkHome_ = jdkHome; return this; } @@ -381,7 +469,7 @@ public class CompileKotlinOptions { * @param version the target version * @return this operation instance */ - public CompileKotlinOptions jdkRelease(String version) { + public CompileOptions jdkRelease(String version) { jdkRelease_ = version; return this; } @@ -393,7 +481,7 @@ public class CompileKotlinOptions { * @return this operation instance * @see #jdkRelease(String) */ - public CompileKotlinOptions jdkRelease(int version) { + public CompileOptions jdkRelease(int version) { jdkRelease_ = String.valueOf(version); return this; } @@ -404,18 +492,27 @@ public class CompileKotlinOptions { * @param jvmOptions one or more JVM option * @return this operation instance */ - public CompileKotlinOptions jvmOptions(String... jvmOptions) { + public CompileOptions jvmOptions(String... jvmOptions) { Collections.addAll(jvmOptions_, jvmOptions); return this; } + /** + * Retrieves the JVM options. + * + * @return the list of options + */ + public Collection jvmOptions() { + return jvmOptions_; + } + /** * Pass an option directly to JVM * * @param jvmOptions the list JVM options * @return this operation instance */ - public CompileKotlinOptions jvmOptions(Collection jvmOptions) { + public CompileOptions jvmOptions(Collection jvmOptions) { jvmOptions_.addAll(jvmOptions); return this; } @@ -428,7 +525,7 @@ public class CompileKotlinOptions { * @param target the target version * @return this operation instance */ - public CompileKotlinOptions jvmTarget(String target) { + public CompileOptions jvmTarget(String target) { jvmTarget_ = target; return this; } @@ -440,7 +537,7 @@ public class CompileKotlinOptions { * @return this operation instance * @see #jvmTarget(String) */ - public CompileKotlinOptions jvmTarget(int target) { + public CompileOptions jvmTarget(int target) { jvmTarget_ = String.valueOf(target); return this; } @@ -451,18 +548,29 @@ public class CompileKotlinOptions { * @param path the Kotlin home path * @return this operation instance */ - public CompileKotlinOptions kotlinHome(String path) { + public CompileOptions kotlinHome(File path) { kotlinHome_ = path; return this; } + /** + * Specify a custom path to the Kotlin compiler used for the discovery of runtime libraries. + * + * @param path the Kotlin home path + * @return this operation instance + */ + public CompileOptions kotlinHome(String path) { + kotlinHome_ = new File(path); + return this; + } + /** * Provide source compatibility with the specified version of Kotlin. * * @param version the language version * @return this operation instance */ - public CompileKotlinOptions languageVersion(String version) { + public CompileOptions languageVersion(String version) { languageVersion_ = version; return this; } @@ -473,7 +581,7 @@ public class CompileKotlinOptions { * @param name the module name * @return this operation instance */ - public CompileKotlinOptions moduleName(String name) { + public CompileOptions moduleName(String name) { moduleName_ = name; return this; } @@ -484,7 +592,7 @@ public class CompileKotlinOptions { * @param noJdk {@code true} or {@code false} * @return this operation instance */ - public CompileKotlinOptions noJdk(boolean noJdk) { + public CompileOptions noJdk(boolean noJdk) { noJdk_ = noJdk; return this; } @@ -495,7 +603,7 @@ public class CompileKotlinOptions { * @param noReflect {@code true} or {@code false} * @return this operation instance */ - public CompileKotlinOptions noReflect(boolean noReflect) { + public CompileOptions noReflect(boolean noReflect) { noReflect_ = noReflect; return this; } @@ -507,7 +615,7 @@ public class CompileKotlinOptions { * @param noStdLib {@code true} or {@code false} * @return this operation instance */ - public CompileKotlinOptions noStdLib(boolean noStdLib) { + public CompileOptions noStdLib(boolean noStdLib) { noStdLib_ = noStdLib; return this; } @@ -518,7 +626,7 @@ public class CompileKotlinOptions { * @param noWarn {@code true} or {@code false} * @return this operation instance */ - public CompileKotlinOptions noWarn(boolean noWarn) { + public CompileOptions noWarn(boolean noWarn) { noWarn_ = noWarn; return this; } @@ -529,18 +637,27 @@ public class CompileKotlinOptions { * @param annotations one or more annotation names * @return this operation instance */ - public CompileKotlinOptions optIn(String... annotations) { + public CompileOptions optIn(String... annotations) { Collections.addAll(optIn_, annotations); return this; } + /** + * Retrieves the opt-in fully qualified names. + * + * @return the list of fully qualified names + */ + public Collection optIn() { + return optIn_; + } + /** * Enable usages of API that requires opt-in with a requirement annotation with the given fully qualified name. * * @param annotations list of annotation names * @return this operation instance */ - public CompileKotlinOptions optIn(Collection annotations) { + public CompileOptions optIn(Collection annotations) { optIn_.addAll(annotations); return this; } @@ -551,18 +668,27 @@ public class CompileKotlinOptions { * @param options one or more compiler options * @return this operation instance */ - public CompileKotlinOptions options(String... options) { + public CompileOptions options(String... options) { Collections.addAll(options_, options); return this; } + /** + * Retrieves additional compiler options. + * + * @return the list of options + */ + public Collection options() { + return options_; + } + /** * Specify additional compiler options. * * @param options list of compiler options * @return this operation instance */ - public CompileKotlinOptions options(Collection options) { + public CompileOptions options(Collection options) { options_.addAll(options); return this; } @@ -575,8 +701,8 @@ public class CompileKotlinOptions { * @param path the location path * @return this operation instance */ - public CompileKotlinOptions path(File path) { - path_ = path.getAbsolutePath(); + public CompileOptions path(File path) { + path_ = path; return this; } @@ -588,8 +714,8 @@ public class CompileKotlinOptions { * @param path the location path * @return this operation instance */ - public CompileKotlinOptions path(String path) { - path_ = path; + public CompileOptions path(String path) { + path_ = new File(path); return this; } @@ -601,18 +727,27 @@ public class CompileKotlinOptions { * @param value the plugin option value * @return this operation instance */ - public CompileKotlinOptions plugin(String id, String optionName, String value) { + public CompileOptions plugin(String id, String optionName, String value) { plugin_.add(id + ':' + optionName + ':' + value); return this; } + /** + * Retrieves the plugin options. + * + * @return the list ofoptions. + */ + public Collection plugin() { + return plugin_; + } + /** * Allow using declarations only from the specified version of Kotlin bundled libraries. * * @param progressive {@code true} or {@code false} * @return this operation instance */ - public CompileKotlinOptions progressive(boolean progressive) { + public CompileOptions progressive(boolean progressive) { progressive_ = progressive; return this; } @@ -625,11 +760,20 @@ public class CompileKotlinOptions { * @param classNames one or more class names * @return this operation instance */ - public CompileKotlinOptions scriptTemplates(String... classNames) { + public CompileOptions scriptTemplates(String... classNames) { Collections.addAll(scriptTemplates_, classNames); return this; } + /** + * Retrieves the script templates. + * + * @return the list of templates. + */ + public Collection scriptTemplates() { + return scriptTemplates_; + } + /** * Script definition template classes. *

@@ -638,7 +782,7 @@ public class CompileKotlinOptions { * @param classNames the list class names * @return this operation instance */ - public CompileKotlinOptions scriptTemplates(Collection classNames) { + public CompileOptions scriptTemplates(Collection classNames) { scriptTemplates_.addAll(classNames); return this; } @@ -649,7 +793,7 @@ public class CompileKotlinOptions { * @param verbose {@code true} or {@code false} * @return this operation instance */ - public CompileKotlinOptions verbose(boolean verbose) { + public CompileOptions verbose(boolean verbose) { verbose_ = verbose; return this; } @@ -660,7 +804,7 @@ public class CompileKotlinOptions { * @param wError {@code true} or {@code false} * @return this operation instance */ - public CompileKotlinOptions wError(boolean wError) { + public CompileOptions wError(boolean wError) { wError_ = wError; return this; } diff --git a/src/main/java/rife/bld/extension/CompileKotlinPlugin.java b/src/main/java/rife/bld/extension/kotlin/CompilerPlugin.java similarity index 63% rename from src/main/java/rife/bld/extension/CompileKotlinPlugin.java rename to src/main/java/rife/bld/extension/kotlin/CompilerPlugin.java index 16c0424..7a3d94e 100644 --- a/src/main/java/rife/bld/extension/CompileKotlinPlugin.java +++ b/src/main/java/rife/bld/extension/kotlin/CompilerPlugin.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package rife.bld.extension; +package rife.bld.extension.kotlin; /** * Defines the known Kotlin compiler plugins match (regex) strings. @@ -22,18 +22,18 @@ package rife.bld.extension; * @author Erik C. Thauvin * @since 1.0 */ -public enum CompileKotlinPlugin { - ALL_OPEN("^allopen-compiler-plugin-.*$"), - ASSIGNMENT("^assignment-compiler-plugin-.*$"), +public enum CompilerPlugin { + ALL_OPEN("^kotlin-allopen-compiler-plugin-.*$"), + ASSIGNMENT("^kotlin-assignment-compiler-plugin-.*$"), KOTLIN_SERIALIZATION("^kotlin-serialization-compiler-plugin-.*$"), - LOMBOK("^lombok-compiler-plugin-.*$"), - NOARG("^noarg-compiler-plugin-.*$"), - POWER_ASSERT("^power-assert-compiler-plugin-.*$"), - SAM_WITH_RECEIVER("^sam-with-receiver-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 label; + public final String regex; - CompileKotlinPlugin(String label) { - this.label = label; + CompilerPlugin(String regex) { + this.regex = regex; } }