Actual KotlinCompilerAdapter conversion

This commit is contained in:
Michael Bogdanov
2015-10-05 14:31:00 +03:00
parent bf18a7647a
commit 306ac73eb7
2 changed files with 71 additions and 93 deletions
@@ -14,152 +14,130 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.ant;
package org.jetbrains.kotlin.ant
import kotlin.KotlinPackage;
import kotlin.jvm.functions.Function1;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.MagicNames;
import org.apache.tools.ant.taskdefs.Javac;
import org.apache.tools.ant.taskdefs.compilers.Javac13;
import org.apache.tools.ant.taskdefs.condition.AntVersion;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Path;
import org.jetbrains.annotations.NotNull;
import org.apache.tools.ant.BuildException
import org.apache.tools.ant.MagicNames
import org.apache.tools.ant.Project.MSG_WARN
import org.apache.tools.ant.taskdefs.compilers.Javac13
import org.apache.tools.ant.taskdefs.condition.AntVersion
import org.apache.tools.ant.types.Commandline
import org.apache.tools.ant.types.Path
import java.io.File
import java.util.*
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class KotlinCompilerAdapter : Javac13() {
import static org.apache.tools.ant.Project.MSG_WARN;
var externalAnnotations: Path? = null
public class KotlinCompilerAdapter extends Javac13 {
private static final List<String> KOTLIN_EXTENSIONS = Arrays.asList("kt", "kts");
var moduleName: String? = null
private Path externalAnnotations;
var additionalArguments: MutableList<Commandline.Argument> = ArrayList(0)
private String moduleName;
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() {
fun createExternalAnnotations(): Path {
if (externalAnnotations == null) {
externalAnnotations = new Path(getProject());
externalAnnotations = Path(getProject())
}
return externalAnnotations.createPath();
return externalAnnotations!!.createPath()
}
public Commandline.Argument createCompilerArg() {
Commandline.Argument argument = new Commandline.Argument();
additionalArguments.add(argument);
return argument;
fun createCompilerArg(): Commandline.Argument {
val argument = Commandline.Argument()
additionalArguments.add(argument)
return argument
}
@Override
public String[] getSupportedFileExtensions() {
List<String> result = KotlinPackage.plus(Arrays.asList(super.getSupportedFileExtensions()), KOTLIN_EXTENSIONS);
//noinspection SSBasedInspection
return result.toArray(new String[result.size()]);
override fun getSupportedFileExtensions(): Array<String> {
return super.getSupportedFileExtensions() + KOTLIN_EXTENSIONS
}
@Override
public boolean execute() throws BuildException {
Javac javac = getJavac();
@Throws(BuildException::class)
override fun execute(): Boolean {
val javac = javac
checkAntVersion();
checkAntVersion()
Kotlin2JvmTask kotlinc = new Kotlin2JvmTask();
kotlinc.setFailOnError(javac.getFailonerror());
kotlinc.setOutput(javac.getDestdir());
val kotlinc = Kotlin2JvmTask()
kotlinc.failOnError = javac.failonerror
kotlinc.output = javac.destdir
Path classpath = javac.getClasspath();
val classpath = javac.classpath
if (classpath != null) {
kotlinc.setClasspath(classpath);
kotlinc.setClasspath(classpath)
}
// 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
kotlinc.setSrc(javac.getSrcdir());
kotlinc.src = javac.srcdir
kotlinc.setExternalAnnotations(externalAnnotations);
kotlinc.externalAnnotations = externalAnnotations
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
File[] compileListForJavac = filterOutKotlinSources(compileList);
val compileListForJavac = filterOutKotlinSources(compileList)
boolean hasKotlinFilesInSources = compileListForJavac.length < compileList.length;
val hasKotlinFilesInSources = compileListForJavac.size() < compileList.size()
if (hasKotlinFilesInSources) {
kotlinc.execute();
if (!Integer.valueOf(0).equals(kotlinc.getExitCode())) {
kotlinc.execute()
if (kotlinc.exitCode != 0) {
// Don't run javac if failOnError = false and there were errors on Kotlin sources
return false;
return false
}
}
else {
// 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) {
for (String arg : kotlinc.getArgs()) {
private fun addRuntimeToJavacClasspath(kotlinc: Kotlin2JvmTask) {
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".equals(arg)) return;
if ("-no-stdlib" == arg) return
}
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() {
AntVersion checkVersion = new AntVersion();
checkVersion.setAtLeast("1.8.2");
private fun checkAntVersion() {
val checkVersion = AntVersion()
checkVersion.atLeast = "1.8.2"
if (!checkVersion.eval()) {
getJavac().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 " +
"the output directory is clean before running this task. " +
"You have: " + getProject().getProperty(MagicNames.ANT_VERSION), MSG_WARN);
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 " +
"the output directory is clean before running this task. " +
"You have: " + getProject().getProperty(MagicNames.ANT_VERSION), MSG_WARN)
}
}
@NotNull
private static File[] filterOutKotlinSources(@NotNull File[] files) {
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;
}
});
companion object {
private val KOTLIN_EXTENSIONS = Arrays.asList("kt", "kts")
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() {
protected abstract val compilerFqName: String
protected val args: MutableList<String> = arrayListOf()
public val args: MutableList<String> = arrayListOf()
public var src: Path? = null
public var output: File? = null
@@ -40,7 +40,7 @@ public abstract class KotlinCompilerBaseTask : Task() {
public val additionalArguments: MutableList<Commandline.Argument> = arrayListOf()
internal var exitCode: Int? = null
public var exitCode: Int? = null
public fun createSrc(): Path {
val srcPath = src