();
// classpath
args.add("-cp");
args.add(FileUtils.joinPaths(classpath.stream().toList()));
// destination
args.add("-d");
args.add(destination.getAbsolutePath());
// friend-path
if (friendPaths != null && friendPaths.exists()) {
args.add("-Xfriend-paths=" + friendPaths.getAbsolutePath());
}
// options
if (compileOptions_ != null) {
args.addAll(compileOptions_.args());
}
// plugins
if (!plugins_.isEmpty()) {
plugins_.forEach(p -> args.add("-Xplugin=" + p));
}
// sources
sources.forEach(f -> args.add(f.getAbsolutePath()));
if (LOGGER.isLoggable(Level.FINE) && !silent()) {
LOGGER.fine("kotlinc " + String.join(" ", args));
}
var exitCode = k2.exec(System.err, args.toArray(new String[0]));
if (exitCode.getCode() != 0) {
throw new IOException("Kotlin compilation failed.");
}
}
/**
* Part of the {@link #execute execute} operation, builds the test sources.
*
* @throws IOException if an error occurs
*/
@SuppressWarnings("PMD.SystemPrintln")
protected void executeBuildTestSources()
throws IOException {
if (!silent()) {
System.out.println("Compiling Kotlin test sources.");
}
executeBuildSources(
compileTestClasspath(),
sources(testSourceFiles(), testSourceDirectories()),
buildTestDirectory(),
buildMainDirectory());
}
/**
* Part of the {@link #execute execute} operation, creates the build directories.
*
* @throws IOException if an error occurs
*/
protected void executeCreateBuildDirectories() throws IOException {
if (buildMainDirectory() != null && !buildMainDirectory().exists() && !buildMainDirectory().mkdirs()) {
throw new IOException("Could not created build main directory: " + buildMainDirectory().getAbsolutePath());
}
if (buildTestDirectory() != null && !buildTestDirectory().exists() && !buildTestDirectory().mkdirs()) {
throw new IOException("Could not created build test directory: " + buildTestDirectory().getAbsolutePath());
}
}
/**
* Configures a compile operation from a {@link BaseProject}.
*
* Sets the following from the project:
*
* - {@link #buildMainDirectory() buildMainDirectory}
* - {@link #buildTestDirectory() buildTestDirectory}
* - {@link #compileMainClasspath() compileMainClassPath}
* - {@link #compileTestClasspath() compilesTestClassPath}
* - {@link #mainSourceFiles() mainSourceFiles} to the {@code kotlin} directory in
* {@link BaseProject#srcMainDirectory() srcMainDirectory}
* - {@link #testSourceFiles() testSourceFile} to the {@code kotlin} directory in
* {@link BaseProject#srcTestDirectory() srcTestDirectory}
* - {@link CompileKotlinOptions#jdkRelease jdkRelease} to {@link BaseProject#javaRelease() javaRelease}
* - {@link CompileKotlinOptions#noStdLib(boolean) noStdLib} to {@code true}
*
*
* @param project the project to configure the compile operation from
* @return this operation instance
*/
public CompileKotlinOperation fromProject(BaseProject project) {
project_ = project;
var op = buildMainDirectory(project.buildMainDirectory())
.buildTestDirectory(project.buildTestDirectory())
.compileMainClasspath(project.compileMainClasspath())
.compileTestClasspath(project.compileTestClasspath())
.mainSourceFiles(getKotlinFileList(new File(project.srcMainDirectory(), "kotlin")))
.testSourceFiles(getKotlinFileList(new File(project.srcTestDirectory(), "kotlin")));
if (project.javaRelease() != null && !compileOptions_.hasRelease()) {
compileOptions_.jdkRelease(project.javaRelease());
}
compileOptions_.noStdLib(true);
return op;
}
/**
* Provides main source directories that should be compiled.
*
* @param directories one or more main source directories
* @return this operation instance
*/
public CompileKotlinOperation mainSourceDirectories(File... directories) {
mainSourceDirectories_.addAll(List.of(directories));
return this;
}
/**
* Provides a list of main source directories that should be compiled.
*
* @param directories a list of main source directories
* @return this operation instance
*/
public CompileKotlinOperation mainSourceDirectories(Collection directories) {
mainSourceDirectories_.addAll(directories);
return this;
}
/**
* Retrieves the list of main source directories that should be compiled.
*
* @return the list of main source directories to compile
*/
public Collection mainSourceDirectories() {
return mainSourceDirectories_;
}
/**
* Provides main files that should be compiled.
*
* @param files one or more main files
* @return this operation instance
*/
public CompileKotlinOperation mainSourceFiles(File... files) {
mainSourceFiles_.addAll(Arrays.asList(files));
return this;
}
/**
* Provides a list of main files that should be compiled.
*
* @param files a list of main files
* @return this operation instance
*/
public CompileKotlinOperation mainSourceFiles(Collection files) {
mainSourceFiles_.addAll(files);
return this;
}
/**
* Retrieves the list of main files that should be compiled.
*
* @return the list of main files to compile
*/
public Collection mainSourceFiles() {
return mainSourceFiles_;
}
/**
* Provides compiler plugins.
*
* @param plugins one or more plugins
* @return this class instance
*/
public CompileKotlinOperation plugins(String... plugins) {
plugins_.addAll(List.of(plugins));
return this;
}
/**
* Provides compiler plugins.
*
* @param plugins a list of plugins
* @return this class instance
*/
public CompileKotlinOperation plugins(Collection plugins) {
plugins_.addAll(plugins);
return this;
}
/**
* Provides compiler plugins.
*
* @param directory the directory containing the plugin JARs
* @param plugins one or more plugins
* @return this class instance
*/
public CompileKotlinOperation plugins(File directory, CompileKotlinPlugin... plugins) {
for (var plugin : plugins) {
plugins_.addAll(getJarList(directory, plugin.label));
}
return this;
}
/**
* Provides compiler plugins.
*
* @param jars the list of plugin JARs
* @param plugins one or more plugins
* @return this class instance
*/
public CompileKotlinOperation plugins(Collection jars, CompileKotlinPlugin... plugins) {
jars.forEach(jar -> {
for (var plugin : plugins) {
if (jar.getName().matches(plugin.label)) {
plugins_.add(jar.getAbsolutePath());
break;
}
}
});
return this;
}
// Combine Kotlin sources
private Collection sources(Collection files, Collection directories) {
var sources = new ArrayList<>(files);
for (var directory : directories) {
sources.addAll(getKotlinFileList(directory));
}
return sources;
}
/**
* Provides test source directories that should be compiled.
*
* @param directories one or more test source directories
* @return this operation instance
*/
public CompileKotlinOperation testSourceDirectories(File... directories) {
testSourceDirectories_.addAll(List.of(directories));
return this;
}
/**
* Provides a list of test source directories that should be compiled.
*
* @param directories a list of test source directories
* @return this operation instance
*/
public CompileKotlinOperation testSourceDirectories(Collection directories) {
testSourceDirectories_.addAll(directories);
return this;
}
/**
* Retrieves the list of test source directories that should be compiled.
*
* @return the list of test source directories to compile
*/
public Collection testSourceDirectories() {
return testSourceDirectories_;
}
/**
* Provides test files that should be compiled.
*
* @param files one or more test files
* @return this operation instance
*/
public CompileKotlinOperation testSourceFiles(File... files) {
testSourceFiles_.addAll(Arrays.asList(files));
return this;
}
/**
* Provides a list of test files that should be compiled.
*
* @param files a list of test files
* @return this operation instance
*/
public CompileKotlinOperation testSourceFiles(Collection files) {
testSourceFiles_.addAll(files);
return this;
}
/**
* Retrieves the list of test files that should be compiled.
*
* @return the list of test files to compile
*/
public Collection testSourceFiles() {
return testSourceFiles_;
}
}