Add method to find the Kotlin home

This commit is contained in:
Erik C. Thauvin 2025-04-01 11:07:08 -07:00
parent 23a97601db
commit dfa9a6124e
Signed by: erik
GPG key ID: 776702A6A2DA330E

View file

@ -68,7 +68,7 @@ public class CompileKotlinOperation extends AbstractOperation<CompileKotlinOpera
// Check bin subdirectory if it exists // Check bin subdirectory if it exists
var binDir = new File(directory, "bin"); var binDir = new File(directory, "bin");
if (binDir.exists() && binDir.isDirectory()) { if (binDir.isDirectory()) {
kotlinc = new File(binDir, KOTLINC_EXECUTABLE); kotlinc = new File(binDir, KOTLINC_EXECUTABLE);
if (isExecutable(kotlinc)) { if (isExecutable(kotlinc)) {
return kotlinc.getAbsolutePath(); return kotlinc.getAbsolutePath();
@ -639,6 +639,29 @@ public class CompileKotlinOperation extends AbstractOperation<CompileKotlinOpera
} }
} }
private File findKotlinHome() {
// Deduct from kotlinc location if provided
if (kotlinc_ != null) {
var parent = kotlinc_.getParentFile();
if (parent != null && parent.isDirectory()) {
if (parent.getPath().endsWith("bin")) {
if (parent.getParentFile() != null && parent.getParentFile().isDirectory()) {
return parent.getParentFile();
}
} else {
return parent;
}
}
} else {
var kotlinHome = System.getenv("KOTLIN_HOME");
if (kotlinHome != null) {
return new File(kotlinHome);
}
}
return null;
}
/** /**
* Configures a compile operation from a {@link BaseProject}. * Configures a compile operation from a {@link BaseProject}.
* <p> * <p>
@ -1022,15 +1045,17 @@ public class CompileKotlinOperation extends AbstractOperation<CompileKotlinOpera
* @see #plugins(File, CompilerPlugin...) * @see #plugins(File, CompilerPlugin...)
*/ */
public CompileKotlinOperation plugins(CompilerPlugin... plugins) { public CompileKotlinOperation plugins(CompilerPlugin... plugins) {
if (kotlinHome_ != null) { var kotlinHome = kotlinHome_;
var kotlinLib = new File(kotlinHome_, "lib"); if (kotlinHome == null) {
kotlinHome = findKotlinHome();
}
if (kotlinHome != null) {
var kotlinLib = new File(kotlinHome, "lib");
for (var plugin : plugins) { for (var plugin : plugins) {
plugins(kotlinLib, plugin); plugins(kotlinLib, plugin);
} }
} else { } else if (LOGGER.isLoggable(Level.WARNING) && !silent()) {
if (LOGGER.isLoggable(Level.WARNING) && !silent()) { LOGGER.warning("The Kotlin home must be set (or discovered) to specify compiler plugins directly.");
LOGGER.warning("The Kotlin home must be set to specify compiler plugins directly.");
}
} }
return this; return this;
} }