Actual KotlinCompilerAdapter conversion
This commit is contained in:
@@ -14,152 +14,130 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.ant;
|
package org.jetbrains.kotlin.ant
|
||||||
|
|
||||||
import kotlin.KotlinPackage;
|
import org.apache.tools.ant.BuildException
|
||||||
import kotlin.jvm.functions.Function1;
|
import org.apache.tools.ant.MagicNames
|
||||||
import org.apache.tools.ant.BuildException;
|
import org.apache.tools.ant.Project.MSG_WARN
|
||||||
import org.apache.tools.ant.MagicNames;
|
import org.apache.tools.ant.taskdefs.compilers.Javac13
|
||||||
import org.apache.tools.ant.taskdefs.Javac;
|
import org.apache.tools.ant.taskdefs.condition.AntVersion
|
||||||
import org.apache.tools.ant.taskdefs.compilers.Javac13;
|
import org.apache.tools.ant.types.Commandline
|
||||||
import org.apache.tools.ant.taskdefs.condition.AntVersion;
|
import org.apache.tools.ant.types.Path
|
||||||
import org.apache.tools.ant.types.Commandline;
|
import java.io.File
|
||||||
import org.apache.tools.ant.types.Path;
|
import java.util.*
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.io.File;
|
class KotlinCompilerAdapter : Javac13() {
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.apache.tools.ant.Project.MSG_WARN;
|
var externalAnnotations: Path? = null
|
||||||
|
|
||||||
public class KotlinCompilerAdapter extends Javac13 {
|
var moduleName: String? = null
|
||||||
private static final List<String> KOTLIN_EXTENSIONS = Arrays.asList("kt", "kts");
|
|
||||||
|
|
||||||
private Path externalAnnotations;
|
var additionalArguments: MutableList<Commandline.Argument> = ArrayList(0)
|
||||||
|
|
||||||
private String moduleName;
|
fun createExternalAnnotations(): Path {
|
||||||
|
|
||||||
public List<Commandline.Argument> additionalArguments = new ArrayList<Commandline.Argument>(0);
|
|
||||||
|
|
||||||
public void setExternalAnnotations(Path externalAnnotations) {
|
|
||||||
this.externalAnnotations = externalAnnotations;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setModuleName(String moduleName) {
|
|
||||||
this.moduleName = moduleName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Path createExternalAnnotations() {
|
|
||||||
if (externalAnnotations == null) {
|
if (externalAnnotations == null) {
|
||||||
externalAnnotations = new Path(getProject());
|
externalAnnotations = Path(getProject())
|
||||||
}
|
}
|
||||||
return externalAnnotations.createPath();
|
return externalAnnotations!!.createPath()
|
||||||
}
|
}
|
||||||
|
|
||||||
public Commandline.Argument createCompilerArg() {
|
fun createCompilerArg(): Commandline.Argument {
|
||||||
Commandline.Argument argument = new Commandline.Argument();
|
val argument = Commandline.Argument()
|
||||||
additionalArguments.add(argument);
|
additionalArguments.add(argument)
|
||||||
return argument;
|
return argument
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
override fun getSupportedFileExtensions(): Array<String> {
|
||||||
public String[] getSupportedFileExtensions() {
|
return super.getSupportedFileExtensions() + KOTLIN_EXTENSIONS
|
||||||
List<String> result = KotlinPackage.plus(Arrays.asList(super.getSupportedFileExtensions()), KOTLIN_EXTENSIONS);
|
|
||||||
//noinspection SSBasedInspection
|
|
||||||
return result.toArray(new String[result.size()]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Throws(BuildException::class)
|
||||||
public boolean execute() throws BuildException {
|
override fun execute(): Boolean {
|
||||||
Javac javac = getJavac();
|
val javac = javac
|
||||||
|
|
||||||
checkAntVersion();
|
checkAntVersion()
|
||||||
|
|
||||||
Kotlin2JvmTask kotlinc = new Kotlin2JvmTask();
|
val kotlinc = Kotlin2JvmTask()
|
||||||
kotlinc.setFailOnError(javac.getFailonerror());
|
kotlinc.failOnError = javac.failonerror
|
||||||
kotlinc.setOutput(javac.getDestdir());
|
kotlinc.output = javac.destdir
|
||||||
|
|
||||||
Path classpath = javac.getClasspath();
|
val classpath = javac.classpath
|
||||||
if (classpath != null) {
|
if (classpath != null) {
|
||||||
kotlinc.setClasspath(classpath);
|
kotlinc.setClasspath(classpath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// We use the provided src dir instead of compileList, because the latter is insane:
|
// We use the provided src dir instead of compileList, because the latter is insane:
|
||||||
// it is constructed only of sources which are newer than classes with the same name
|
// it is constructed only of sources which are newer than classes with the same name
|
||||||
kotlinc.setSrc(javac.getSrcdir());
|
kotlinc.src = javac.srcdir
|
||||||
|
|
||||||
kotlinc.setExternalAnnotations(externalAnnotations);
|
kotlinc.externalAnnotations = externalAnnotations
|
||||||
|
|
||||||
if (moduleName == null) {
|
if (moduleName == null) {
|
||||||
moduleName = AntPackage.getDefaultModuleName(javac);
|
moduleName = javac.defaultModuleName
|
||||||
}
|
}
|
||||||
kotlinc.setModuleName(moduleName);
|
kotlinc.moduleName = moduleName
|
||||||
|
|
||||||
kotlinc.getAdditionalArguments().addAll(additionalArguments);
|
kotlinc.additionalArguments.addAll(additionalArguments)
|
||||||
|
|
||||||
// Javac13#execute passes everything in compileList to javac, which doesn't recognize .kt files
|
// Javac13#execute passes everything in compileList to javac, which doesn't recognize .kt files
|
||||||
File[] compileListForJavac = filterOutKotlinSources(compileList);
|
val compileListForJavac = filterOutKotlinSources(compileList)
|
||||||
|
|
||||||
boolean hasKotlinFilesInSources = compileListForJavac.length < compileList.length;
|
val hasKotlinFilesInSources = compileListForJavac.size() < compileList.size()
|
||||||
|
|
||||||
if (hasKotlinFilesInSources) {
|
if (hasKotlinFilesInSources) {
|
||||||
kotlinc.execute();
|
kotlinc.execute()
|
||||||
if (!Integer.valueOf(0).equals(kotlinc.getExitCode())) {
|
if (kotlinc.exitCode != 0) {
|
||||||
// Don't run javac if failOnError = false and there were errors on Kotlin sources
|
// Don't run javac if failOnError = false and there were errors on Kotlin sources
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// This is needed for addRuntimeToJavacClasspath, where kotlinc arguments will be used.
|
// This is needed for addRuntimeToJavacClasspath, where kotlinc arguments will be used.
|
||||||
kotlinc.fillArguments();
|
kotlinc.fillArguments()
|
||||||
}
|
}
|
||||||
|
|
||||||
javac.log("Running javac...");
|
javac.log("Running javac...")
|
||||||
|
|
||||||
compileList = compileListForJavac;
|
compileList = compileListForJavac
|
||||||
|
|
||||||
addRuntimeToJavacClasspath(kotlinc);
|
addRuntimeToJavacClasspath(kotlinc)
|
||||||
|
|
||||||
return compileList.length == 0 || super.execute();
|
return compileList.isEmpty() || super.execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addRuntimeToJavacClasspath(@NotNull Kotlin2JvmTask kotlinc) {
|
private fun addRuntimeToJavacClasspath(kotlinc: Kotlin2JvmTask) {
|
||||||
for (String arg : kotlinc.getArgs()) {
|
for (arg in kotlinc.args) {
|
||||||
// If "-no-stdlib" was specified explicitly, probably the user also wanted the javac classpath to not have it
|
// If "-no-stdlib" was specified explicitly, probably the user also wanted the javac classpath to not have it
|
||||||
if ("-no-stdlib".equals(arg)) return;
|
if ("-no-stdlib" == arg) return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (compileClasspath == null) {
|
if (compileClasspath == null) {
|
||||||
compileClasspath = new Path(getProject());
|
compileClasspath = Path(getProject())
|
||||||
}
|
}
|
||||||
compileClasspath.add(new Path(getProject(), KotlinAntTaskUtil.INSTANCE$.getRuntimeJar().getAbsolutePath()));
|
compileClasspath.add(Path(getProject(), KotlinAntTaskUtil.runtimeJar.absolutePath))
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkAntVersion() {
|
private fun checkAntVersion() {
|
||||||
AntVersion checkVersion = new AntVersion();
|
val checkVersion = AntVersion()
|
||||||
checkVersion.setAtLeast("1.8.2");
|
checkVersion.atLeast = "1.8.2"
|
||||||
if (!checkVersion.eval()) {
|
if (!checkVersion.eval()) {
|
||||||
getJavac().log("<withKotlin> task requires Ant of version at least 1.8.2 to operate reliably. " +
|
javac.log("<withKotlin> task requires Ant of version at least 1.8.2 to operate reliably. " +
|
||||||
"Please upgrade or, as a workaround, make sure you have at least one Java source and " +
|
"Please upgrade or, as a workaround, make sure you have at least one Java source and " +
|
||||||
"the output directory is clean before running this task. " +
|
"the output directory is clean before running this task. " +
|
||||||
"You have: " + getProject().getProperty(MagicNames.ANT_VERSION), MSG_WARN);
|
"You have: " + getProject().getProperty(MagicNames.ANT_VERSION), MSG_WARN)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
companion object {
|
||||||
private static File[] filterOutKotlinSources(@NotNull File[] files) {
|
private val KOTLIN_EXTENSIONS = Arrays.asList("kt", "kts")
|
||||||
List<File> nonKotlinSources = KotlinPackage.filterNot(files, new Function1<File, Boolean>() {
|
|
||||||
@Override
|
|
||||||
public Boolean invoke(File file) {
|
|
||||||
for (String extension : KOTLIN_EXTENSIONS) {
|
|
||||||
if (file.getPath().endsWith("." + extension)) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return nonKotlinSources.toArray(new File[nonKotlinSources.size()]);
|
private fun filterOutKotlinSources(files: Array<File>): Array<File> {
|
||||||
|
return files.filterNot {
|
||||||
|
for (extension in KOTLIN_EXTENSIONS) {
|
||||||
|
if (it.path.endsWith("." + extension)) return@filterNot true
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}.toTypedArray()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ import java.io.PrintStream
|
|||||||
public abstract class KotlinCompilerBaseTask : Task() {
|
public abstract class KotlinCompilerBaseTask : Task() {
|
||||||
protected abstract val compilerFqName: String
|
protected abstract val compilerFqName: String
|
||||||
|
|
||||||
protected val args: MutableList<String> = arrayListOf()
|
public val args: MutableList<String> = arrayListOf()
|
||||||
|
|
||||||
public var src: Path? = null
|
public var src: Path? = null
|
||||||
public var output: File? = null
|
public var output: File? = null
|
||||||
@@ -40,7 +40,7 @@ public abstract class KotlinCompilerBaseTask : Task() {
|
|||||||
|
|
||||||
public val additionalArguments: MutableList<Commandline.Argument> = arrayListOf()
|
public val additionalArguments: MutableList<Commandline.Argument> = arrayListOf()
|
||||||
|
|
||||||
internal var exitCode: Int? = null
|
public var exitCode: Int? = null
|
||||||
|
|
||||||
public fun createSrc(): Path {
|
public fun createSrc(): Path {
|
||||||
val srcPath = src
|
val srcPath = src
|
||||||
|
|||||||
Reference in New Issue
Block a user