1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 00:17:11 -07:00

Use the Java Compiler API.

This commit is contained in:
Cedric Beust 2015-12-10 01:41:21 +03:00
parent 553c050877
commit 82be14ae06
2 changed files with 15 additions and 19 deletions

View file

@ -9,7 +9,6 @@ Android:
General General
- [ ] Compile with javax.tool
- [ ] Apt should run from serviceloader - [ ] Apt should run from serviceloader
- [ ] Auto add variant - [ ] Auto add variant
- [ ] The test runner only selects classes with a parameterless constructor, which works for JUnit but not for TestNG - [ ] The test runner only selects classes with a parameterless constructor, which works for JUnit but not for TestNG
@ -43,6 +42,7 @@ General
Done: Done:
- [x] Compile with javax.tool
- [x] Android: multiple -source/-target flags - [x] Android: multiple -source/-target flags
- [x] Dokka: allow multiple format outputs e.g `outputFormat("html", "javadoc")` - [x] Dokka: allow multiple format outputs e.g `outputFormat("html", "javadoc")`
- [x] Finish abstracting everything in `JvmCompilerPlugin` - [x] Finish abstracting everything in `JvmCompilerPlugin`

View file

@ -9,12 +9,14 @@ import com.beust.kobalt.api.Project
import com.beust.kobalt.internal.ICompilerAction import com.beust.kobalt.internal.ICompilerAction
import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.internal.JvmCompiler
import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.log
import com.beust.kobalt.misc.warn import com.beust.kobalt.misc.warn
import com.beust.kobalt.plugin.android.forward
import com.google.inject.Inject import com.google.inject.Inject
import com.google.inject.Singleton import com.google.inject.Singleton
import java.io.File import java.io.File
import java.io.PrintWriter
import javax.tools.DiagnosticCollector
import javax.tools.JavaFileObject
import javax.tools.ToolProvider
@Singleton @Singleton
class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) { class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) {
@ -25,31 +27,25 @@ class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) {
return TaskResult() return TaskResult()
} }
info.outputDir.mkdirs()
val allArgs = arrayListOf( val allArgs = arrayListOf(
executable.absolutePath,
"-d", KFiles.makeDir(info.directory!!, info.outputDir.path).path) "-d", KFiles.makeDir(info.directory!!, info.outputDir.path).path)
if (info.dependencies.size > 0) { if (info.dependencies.size > 0) {
allArgs.add("-classpath") allArgs.add("-classpath")
allArgs.add(info.dependencies.map { it.jarFile.get() }.joinToString(File.pathSeparator)) allArgs.add(info.dependencies.map { it.jarFile.get() }.joinToString(File.pathSeparator))
} }
allArgs.addAll(info.compilerArgs) allArgs.addAll(info.compilerArgs)
allArgs.addAll(info.sourceFiles)
val pb = ProcessBuilder(allArgs) val compiler = ToolProvider.getSystemJavaCompiler()
// info.directory?.let { val fileManager = compiler.getStandardFileManager(null, null, null)
// pb.directory(File(it)) val fileObjects = fileManager.getJavaFileObjectsFromFiles(info.sourceFiles.map { File(it) })
// } val dc = DiagnosticCollector<JavaFileObject>()
pb.inheritIO() val classes = arrayListOf<String>()
val line = allArgs.joinToString(" ") val task = compiler.getTask(PrintWriter(System.out), fileManager, dc, allArgs, classes, fileObjects)
log(1, " Compiling ${info.sourceFiles.size} files") val result = task.call()
log(2, " Compiling ${line.forward()}")
val process = pb.start()
val errorCode = process.waitFor()
return if (errorCode == 0) TaskResult(true, "Compilation succeeded") return if (result) TaskResult(true, "Compilation succeeded")
else TaskResult(false, "There were errors") else TaskResult(false, "There were errors")
} }
} }