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:
parent
553c050877
commit
82be14ae06
2 changed files with 15 additions and 19 deletions
2
TODO.md
2
TODO.md
|
@ -9,7 +9,6 @@ Android:
|
|||
|
||||
General
|
||||
|
||||
- [ ] Compile with javax.tool
|
||||
- [ ] Apt should run from serviceloader
|
||||
- [ ] Auto add variant
|
||||
- [ ] The test runner only selects classes with a parameterless constructor, which works for JUnit but not for TestNG
|
||||
|
@ -43,6 +42,7 @@ General
|
|||
|
||||
Done:
|
||||
|
||||
- [x] Compile with javax.tool
|
||||
- [x] Android: multiple -source/-target flags
|
||||
- [x] Dokka: allow multiple format outputs e.g `outputFormat("html", "javadoc")`
|
||||
- [x] Finish abstracting everything in `JvmCompilerPlugin`
|
||||
|
|
|
@ -9,12 +9,14 @@ import com.beust.kobalt.api.Project
|
|||
import com.beust.kobalt.internal.ICompilerAction
|
||||
import com.beust.kobalt.internal.JvmCompiler
|
||||
import com.beust.kobalt.misc.KFiles
|
||||
import com.beust.kobalt.misc.log
|
||||
import com.beust.kobalt.misc.warn
|
||||
import com.beust.kobalt.plugin.android.forward
|
||||
import com.google.inject.Inject
|
||||
import com.google.inject.Singleton
|
||||
import java.io.File
|
||||
import java.io.PrintWriter
|
||||
import javax.tools.DiagnosticCollector
|
||||
import javax.tools.JavaFileObject
|
||||
import javax.tools.ToolProvider
|
||||
|
||||
@Singleton
|
||||
class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) {
|
||||
|
@ -25,31 +27,25 @@ class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) {
|
|||
return TaskResult()
|
||||
}
|
||||
|
||||
info.outputDir.mkdirs()
|
||||
|
||||
val allArgs = arrayListOf(
|
||||
executable.absolutePath,
|
||||
"-d", KFiles.makeDir(info.directory!!, info.outputDir.path).path)
|
||||
if (info.dependencies.size > 0) {
|
||||
allArgs.add("-classpath")
|
||||
allArgs.add(info.dependencies.map { it.jarFile.get() }.joinToString(File.pathSeparator))
|
||||
}
|
||||
allArgs.addAll(info.compilerArgs)
|
||||
allArgs.addAll(info.sourceFiles)
|
||||
|
||||
val pb = ProcessBuilder(allArgs)
|
||||
// info.directory?.let {
|
||||
// pb.directory(File(it))
|
||||
// }
|
||||
pb.inheritIO()
|
||||
val line = allArgs.joinToString(" ")
|
||||
log(1, " Compiling ${info.sourceFiles.size} files")
|
||||
log(2, " Compiling ${line.forward()}")
|
||||
val process = pb.start()
|
||||
val errorCode = process.waitFor()
|
||||
val compiler = ToolProvider.getSystemJavaCompiler()
|
||||
val fileManager = compiler.getStandardFileManager(null, null, null)
|
||||
val fileObjects = fileManager.getJavaFileObjectsFromFiles(info.sourceFiles.map { File(it) })
|
||||
val dc = DiagnosticCollector<JavaFileObject>()
|
||||
val classes = arrayListOf<String>()
|
||||
val task = compiler.getTask(PrintWriter(System.out), fileManager, dc, allArgs, classes, fileObjects)
|
||||
val result = task.call()
|
||||
|
||||
return if (errorCode == 0) TaskResult(true, "Compilation succeeded")
|
||||
return if (result) TaskResult(true, "Compilation succeeded")
|
||||
else TaskResult(false, "There were errors")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue