Clean up and moved compile options and plugins to sub-package

This commit is contained in:
Erik C. Thauvin 2024-06-19 08:17:30 -07:00
parent c151a49935
commit c595632732
Signed by: erik
GPG key ID: 776702A6A2DA330E
2 changed files with 219 additions and 75 deletions

View file

@ -14,13 +14,13 @@
* limitations under the License. * limitations under the License.
*/ */
package rife.bld.extension; package rife.bld.extension.kotlin;
import rife.bld.extension.CompileKotlinOperation;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.*;
import java.util.Collection; import java.util.stream.Collectors;
import java.util.Collections;
import java.util.List;
import static rife.bld.extension.CompileKotlinOperation.isNotBlank; import static rife.bld.extension.CompileKotlinOperation.isNotBlank;
@ -30,30 +30,30 @@ import static rife.bld.extension.CompileKotlinOperation.isNotBlank;
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a> * @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0 * @since 1.0
*/ */
public class CompileKotlinOptions { public class CompileOptions {
private final List<String> advancedOptions_ = new ArrayList<>(); private final Collection<String> advancedOptions_ = new ArrayList<>();
private final List<String> argFile_ = new ArrayList<>(); private final Collection<File> argFile_ = new ArrayList<>();
private final List<String> classpath_ = new ArrayList<>(); private final Collection<File> classpath_ = new ArrayList<>();
private final List<String> jvmOptions_ = new ArrayList<>(); private final Collection<String> jvmOptions_ = new ArrayList<>();
private final List<String> optIn_ = new ArrayList<>(); private final Collection<String> optIn_ = new ArrayList<>();
private final List<String> options_ = new ArrayList<>(); private final Collection<String> options_ = new ArrayList<>();
private final List<String> plugin_ = new ArrayList<>(); private final Collection<String> plugin_ = new ArrayList<>();
private final List<String> scriptTemplates_ = new ArrayList<>(); private final Collection<String> scriptTemplates_ = new ArrayList<>();
private String apiVersion_; private String apiVersion_;
private String expression_; private String expression_;
private boolean includeRuntime_; private boolean includeRuntime_;
private boolean javaParameters_; private boolean javaParameters_;
private String jdkHome_; private File jdkHome_;
private String jdkRelease_; private String jdkRelease_;
private String jvmTarget_; private String jvmTarget_;
private String kotlinHome_; private File kotlinHome_;
private String languageVersion_; private String languageVersion_;
private String moduleName_; private String moduleName_;
private boolean noJdk_; private boolean noJdk_;
private boolean noReflect_; private boolean noReflect_;
private boolean noStdLib_; private boolean noStdLib_;
private boolean noWarn_; private boolean noWarn_;
private String path_; private File path_;
private boolean progressive_; private boolean progressive_;
private boolean verbose_; private boolean verbose_;
private boolean wError_; private boolean wError_;
@ -64,7 +64,7 @@ public class CompileKotlinOptions {
* @param options one or more advanced options * @param options one or more advanced options
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions advancedOptions(String... options) { public CompileOptions advancedOptions(String... options) {
Collections.addAll(advancedOptions_, options); Collections.addAll(advancedOptions_, options);
return this; return this;
} }
@ -75,18 +75,27 @@ public class CompileKotlinOptions {
* @param options list of compiler options * @param options list of compiler options
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions advancedOptions(Collection<String> options) { public CompileOptions advancedOptions(Collection<String> options) {
advancedOptions_.addAll(options); advancedOptions_.addAll(options);
return this; return this;
} }
/**
* Retrieves advanced compiler options.
*
* @return the advanced compiler options
*/
public Collection<String> advancedOptions() {
return advancedOptions_;
}
/** /**
* Allow using declarations only from the specified version of Kotlin bundled libraries. * Allow using declarations only from the specified version of Kotlin bundled libraries.
* *
* @param version the api version * @param version the api version
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions apiVersion(String version) { public CompileOptions apiVersion(String version) {
apiVersion_ = version; apiVersion_ = version;
return this; return this;
} }
@ -97,7 +106,7 @@ public class CompileKotlinOptions {
* @param version the api version * @param version the api version
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions apiVersion(int version) { public CompileOptions apiVersion(int version) {
apiVersion_ = String.valueOf(version); apiVersion_ = String.valueOf(version);
return this; return this;
} }
@ -120,11 +129,37 @@ public class CompileKotlinOptions {
* @param files one or more files * @param files one or more files
* @return this operation instance * @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.
* <p>
* 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:
* <ul>
* <li>{@code -include-runtime -d hello.jar hello.kt}</li>
* </ul>
* 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 (\).
* <ul>
* <li>{@code -include-runtime -d 'My folder'}</li>
* </ul>
* 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); Collections.addAll(argFile_, files);
return this; return this;
} }
/** /**
* Read the compiler options from the given files. * Read the compiler options from the given files.
* *
@ -132,11 +167,20 @@ public class CompileKotlinOptions {
* @return this operation instance * @return this operation instance
* @see #argFile(String...) * @see #argFile(String...)
*/ */
public CompileKotlinOptions argFile(Collection<String> files) { public CompileOptions argFile(Collection<File> files) {
argFile_.addAll(files); argFile_.addAll(files);
return this; return this;
} }
/**
* Retrieves the files containing compiler options.
*
* @return the list of files
*/
public Collection<File> argFile() {
return argFile_;
}
/** /**
* Returns the formatted arguments. * Returns the formatted arguments.
* *
@ -153,13 +197,13 @@ public class CompileKotlinOptions {
// @argfile // @argfile
if (!argFile_.isEmpty()) { if (!argFile_.isEmpty()) {
argFile_.forEach(f -> args.add("@" + f)); argFile_.forEach(f -> args.add("@" + f.getAbsolutePath()));
} }
// classpath // classpath
if (!classpath_.isEmpty()) { if (!classpath_.isEmpty()) {
args.add("-classpath"); args.add("-classpath");
args.add(String.join(File.pathSeparator, classpath_)); args.add(classpath_.stream().map(File::getAbsolutePath).collect(Collectors.joining(File.pathSeparator)));
} }
// expression // expression
@ -185,9 +229,9 @@ public class CompileKotlinOptions {
} }
// jdk-home // jdk-home
if (isNotBlank(jdkHome_)) { if (jdkHome_ != null) {
args.add("-jdk-home"); args.add("-jdk-home");
args.add(jdkHome_); args.add(jdkHome_.getAbsolutePath());
} }
// jdk-release // jdk-release
@ -201,9 +245,9 @@ public class CompileKotlinOptions {
} }
// kotlin-home // kotlin-home
if (isNotBlank(kotlinHome_)) { if (kotlinHome_ != null) {
args.add("-kotlin-home"); args.add("-kotlin-home");
args.add(kotlinHome_); args.add(kotlinHome_.getAbsolutePath());
} }
// language-version // language-version
@ -250,9 +294,9 @@ public class CompileKotlinOptions {
} }
// path // path
if (isNotBlank(path_)) { if (path_ != null) {
args.add("-d"); args.add("-d");
args.add(path_); args.add(path_.getAbsolutePath());
} }
// plugin // plugin
@ -298,7 +342,22 @@ public class CompileKotlinOptions {
* @param paths one pr more paths * @param paths one pr more paths
* @return this operation instance * @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.
* <p>
* 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); Collections.addAll(classpath_, paths);
return this; return this;
} }
@ -311,18 +370,27 @@ public class CompileKotlinOptions {
* @param paths the list of paths * @param paths the list of paths
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions classpath(Collection<String> paths) { public CompileOptions classpath(Collection<File> paths) {
classpath_.addAll(paths); classpath_.addAll(paths);
return this; return this;
} }
/**
* Retrieves the class files classpath.
*
* @return the list of classpath
*/
public Collection<File> classpath() {
return classpath_;
}
/** /**
* Evaluate the given string as a Kotlin script. * Evaluate the given string as a Kotlin script.
* *
* @param expression the expression * @param expression the expression
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions expression(String expression) { public CompileOptions expression(String expression) {
expression_ = expression; expression_ = expression;
return this; return this;
} }
@ -343,18 +411,27 @@ public class CompileKotlinOptions {
* @param includeRuntime {@code true} or {@code false} * @param includeRuntime {@code true} or {@code false}
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions includeRuntime(boolean includeRuntime) { public CompileOptions includeRuntime(boolean includeRuntime) {
includeRuntime_ = includeRuntime; includeRuntime_ = includeRuntime;
return this; 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. * Generate metadata for Java 1.8 reflection on method parameters.
* *
* @param javaParameters {@code true} or {@code false} * @param javaParameters {@code true} or {@code false}
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions javaParameters(boolean javaParameters) { public CompileOptions javaParameters(boolean javaParameters) {
javaParameters_ = javaParameters; javaParameters_ = javaParameters;
return this; return this;
} }
@ -365,7 +442,18 @@ public class CompileKotlinOptions {
* @param jdkHome the JDK home path * @param jdkHome the JDK home path
* @return this operation instance * @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; jdkHome_ = jdkHome;
return this; return this;
} }
@ -381,7 +469,7 @@ public class CompileKotlinOptions {
* @param version the target version * @param version the target version
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions jdkRelease(String version) { public CompileOptions jdkRelease(String version) {
jdkRelease_ = version; jdkRelease_ = version;
return this; return this;
} }
@ -393,7 +481,7 @@ public class CompileKotlinOptions {
* @return this operation instance * @return this operation instance
* @see #jdkRelease(String) * @see #jdkRelease(String)
*/ */
public CompileKotlinOptions jdkRelease(int version) { public CompileOptions jdkRelease(int version) {
jdkRelease_ = String.valueOf(version); jdkRelease_ = String.valueOf(version);
return this; return this;
} }
@ -404,18 +492,27 @@ public class CompileKotlinOptions {
* @param jvmOptions one or more JVM option * @param jvmOptions one or more JVM option
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions jvmOptions(String... jvmOptions) { public CompileOptions jvmOptions(String... jvmOptions) {
Collections.addAll(jvmOptions_, jvmOptions); Collections.addAll(jvmOptions_, jvmOptions);
return this; return this;
} }
/**
* Retrieves the JVM options.
*
* @return the list of options
*/
public Collection<String> jvmOptions() {
return jvmOptions_;
}
/** /**
* Pass an option directly to JVM * Pass an option directly to JVM
* *
* @param jvmOptions the list JVM options * @param jvmOptions the list JVM options
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions jvmOptions(Collection<String> jvmOptions) { public CompileOptions jvmOptions(Collection<String> jvmOptions) {
jvmOptions_.addAll(jvmOptions); jvmOptions_.addAll(jvmOptions);
return this; return this;
} }
@ -428,7 +525,7 @@ public class CompileKotlinOptions {
* @param target the target version * @param target the target version
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions jvmTarget(String target) { public CompileOptions jvmTarget(String target) {
jvmTarget_ = target; jvmTarget_ = target;
return this; return this;
} }
@ -440,7 +537,7 @@ public class CompileKotlinOptions {
* @return this operation instance * @return this operation instance
* @see #jvmTarget(String) * @see #jvmTarget(String)
*/ */
public CompileKotlinOptions jvmTarget(int target) { public CompileOptions jvmTarget(int target) {
jvmTarget_ = String.valueOf(target); jvmTarget_ = String.valueOf(target);
return this; return this;
} }
@ -451,18 +548,29 @@ public class CompileKotlinOptions {
* @param path the Kotlin home path * @param path the Kotlin home path
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions kotlinHome(String path) { public CompileOptions kotlinHome(File path) {
kotlinHome_ = path; kotlinHome_ = path;
return this; 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. * Provide source compatibility with the specified version of Kotlin.
* *
* @param version the language version * @param version the language version
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions languageVersion(String version) { public CompileOptions languageVersion(String version) {
languageVersion_ = version; languageVersion_ = version;
return this; return this;
} }
@ -473,7 +581,7 @@ public class CompileKotlinOptions {
* @param name the module name * @param name the module name
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions moduleName(String name) { public CompileOptions moduleName(String name) {
moduleName_ = name; moduleName_ = name;
return this; return this;
} }
@ -484,7 +592,7 @@ public class CompileKotlinOptions {
* @param noJdk {@code true} or {@code false} * @param noJdk {@code true} or {@code false}
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions noJdk(boolean noJdk) { public CompileOptions noJdk(boolean noJdk) {
noJdk_ = noJdk; noJdk_ = noJdk;
return this; return this;
} }
@ -495,7 +603,7 @@ public class CompileKotlinOptions {
* @param noReflect {@code true} or {@code false} * @param noReflect {@code true} or {@code false}
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions noReflect(boolean noReflect) { public CompileOptions noReflect(boolean noReflect) {
noReflect_ = noReflect; noReflect_ = noReflect;
return this; return this;
} }
@ -507,7 +615,7 @@ public class CompileKotlinOptions {
* @param noStdLib {@code true} or {@code false} * @param noStdLib {@code true} or {@code false}
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions noStdLib(boolean noStdLib) { public CompileOptions noStdLib(boolean noStdLib) {
noStdLib_ = noStdLib; noStdLib_ = noStdLib;
return this; return this;
} }
@ -518,7 +626,7 @@ public class CompileKotlinOptions {
* @param noWarn {@code true} or {@code false} * @param noWarn {@code true} or {@code false}
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions noWarn(boolean noWarn) { public CompileOptions noWarn(boolean noWarn) {
noWarn_ = noWarn; noWarn_ = noWarn;
return this; return this;
} }
@ -529,18 +637,27 @@ public class CompileKotlinOptions {
* @param annotations one or more annotation names * @param annotations one or more annotation names
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions optIn(String... annotations) { public CompileOptions optIn(String... annotations) {
Collections.addAll(optIn_, annotations); Collections.addAll(optIn_, annotations);
return this; return this;
} }
/**
* Retrieves the opt-in fully qualified names.
*
* @return the list of fully qualified names
*/
public Collection<String> optIn() {
return optIn_;
}
/** /**
* Enable usages of API that requires opt-in with a requirement annotation with the given fully qualified name. * Enable usages of API that requires opt-in with a requirement annotation with the given fully qualified name.
* *
* @param annotations list of annotation names * @param annotations list of annotation names
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions optIn(Collection<String> annotations) { public CompileOptions optIn(Collection<String> annotations) {
optIn_.addAll(annotations); optIn_.addAll(annotations);
return this; return this;
} }
@ -551,18 +668,27 @@ public class CompileKotlinOptions {
* @param options one or more compiler options * @param options one or more compiler options
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions options(String... options) { public CompileOptions options(String... options) {
Collections.addAll(options_, options); Collections.addAll(options_, options);
return this; return this;
} }
/**
* Retrieves additional compiler options.
*
* @return the list of options
*/
public Collection<String> options() {
return options_;
}
/** /**
* Specify additional compiler options. * Specify additional compiler options.
* *
* @param options list of compiler options * @param options list of compiler options
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions options(Collection<String> options) { public CompileOptions options(Collection<String> options) {
options_.addAll(options); options_.addAll(options);
return this; return this;
} }
@ -575,8 +701,8 @@ public class CompileKotlinOptions {
* @param path the location path * @param path the location path
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions path(File path) { public CompileOptions path(File path) {
path_ = path.getAbsolutePath(); path_ = path;
return this; return this;
} }
@ -588,8 +714,8 @@ public class CompileKotlinOptions {
* @param path the location path * @param path the location path
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions path(String path) { public CompileOptions path(String path) {
path_ = path; path_ = new File(path);
return this; return this;
} }
@ -601,18 +727,27 @@ public class CompileKotlinOptions {
* @param value the plugin option value * @param value the plugin option value
* @return this operation instance * @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); plugin_.add(id + ':' + optionName + ':' + value);
return this; return this;
} }
/**
* Retrieves the plugin options.
*
* @return the list ofoptions.
*/
public Collection<String> plugin() {
return plugin_;
}
/** /**
* Allow using declarations only from the specified version of Kotlin bundled libraries. * Allow using declarations only from the specified version of Kotlin bundled libraries.
* *
* @param progressive {@code true} or {@code false} * @param progressive {@code true} or {@code false}
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions progressive(boolean progressive) { public CompileOptions progressive(boolean progressive) {
progressive_ = progressive; progressive_ = progressive;
return this; return this;
} }
@ -625,11 +760,20 @@ public class CompileKotlinOptions {
* @param classNames one or more class names * @param classNames one or more class names
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions scriptTemplates(String... classNames) { public CompileOptions scriptTemplates(String... classNames) {
Collections.addAll(scriptTemplates_, classNames); Collections.addAll(scriptTemplates_, classNames);
return this; return this;
} }
/**
* Retrieves the script templates.
*
* @return the list of templates.
*/
public Collection<String> scriptTemplates() {
return scriptTemplates_;
}
/** /**
* Script definition template classes. * Script definition template classes.
* <p> * <p>
@ -638,7 +782,7 @@ public class CompileKotlinOptions {
* @param classNames the list class names * @param classNames the list class names
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions scriptTemplates(Collection<String> classNames) { public CompileOptions scriptTemplates(Collection<String> classNames) {
scriptTemplates_.addAll(classNames); scriptTemplates_.addAll(classNames);
return this; return this;
} }
@ -649,7 +793,7 @@ public class CompileKotlinOptions {
* @param verbose {@code true} or {@code false} * @param verbose {@code true} or {@code false}
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions verbose(boolean verbose) { public CompileOptions verbose(boolean verbose) {
verbose_ = verbose; verbose_ = verbose;
return this; return this;
} }
@ -660,7 +804,7 @@ public class CompileKotlinOptions {
* @param wError {@code true} or {@code false} * @param wError {@code true} or {@code false}
* @return this operation instance * @return this operation instance
*/ */
public CompileKotlinOptions wError(boolean wError) { public CompileOptions wError(boolean wError) {
wError_ = wError; wError_ = wError;
return this; return this;
} }

View file

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package rife.bld.extension; package rife.bld.extension.kotlin;
/** /**
* Defines the known Kotlin compiler plugins match (regex) strings. * Defines the known Kotlin compiler plugins match (regex) strings.
@ -22,18 +22,18 @@ package rife.bld.extension;
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a> * @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0 * @since 1.0
*/ */
public enum CompileKotlinPlugin { public enum CompilerPlugin {
ALL_OPEN("^allopen-compiler-plugin-.*$"), ALL_OPEN("^kotlin-allopen-compiler-plugin-.*$"),
ASSIGNMENT("^assignment-compiler-plugin-.*$"), ASSIGNMENT("^kotlin-assignment-compiler-plugin-.*$"),
KOTLIN_SERIALIZATION("^kotlin-serialization-compiler-plugin-.*$"), KOTLIN_SERIALIZATION("^kotlin-serialization-compiler-plugin-.*$"),
LOMBOK("^lombok-compiler-plugin-.*$"), LOMBOK("^kotlin-lombok-compiler-plugin-.*$"),
NOARG("^noarg-compiler-plugin-.*$"), NOARG("^kotlin-noarg-compiler-plugin-.*$"),
POWER_ASSERT("^power-assert-compiler-plugin-.*$"), POWER_ASSERT("^kotlin-power-assert-compiler-plugin-.*$"),
SAM_WITH_RECEIVER("^sam-with-receiver-compiler-plugin-.*$"); SAM_WITH_RECEIVER("^kotlin-sam-with-receiver-compiler-plugin-.*$");
public final String label; public final String regex;
CompileKotlinPlugin(String label) { CompilerPlugin(String regex) {
this.label = label; this.regex = regex;
} }
} }