From ea9d574f39bda47b6ec74d29083d43574834e70b Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Thu, 29 Oct 2015 22:03:14 -0700 Subject: [PATCH] R.java compilation. --- .../kobalt/internal/JvmCompilerPlugin.kt | 17 +++--- src/main/kotlin/com/beust/kobalt/maven/Pom.kt | 2 +- .../kobalt/plugin/android/AndroidPlugin.kt | 23 +++++++- .../beust/kobalt/plugin/java/JavaCompiler.kt | 55 +++++++++++++++++++ .../beust/kobalt/plugin/java/JavaPlugin.kt | 37 ++----------- 5 files changed, 90 insertions(+), 44 deletions(-) create mode 100644 src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt diff --git a/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt b/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt index ac643cd1..828ba2dd 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt @@ -28,6 +28,15 @@ abstract class JvmCompilerPlugin @Inject constructor( const val SOURCE_SET_MAIN = "main" const val SOURCE_SET_TEST = "test" const val DOCS_DIRECTORY = "docs/javadoc" + + fun validateClasspath(cp: List) { + cp.forEach { + if (! File(it).exists()) { + throw KobaltException("Couldn't find $it") + } + } + } + } /** @@ -110,14 +119,6 @@ abstract class JvmCompilerPlugin @Inject constructor( lp(project, "No resources to copy for ${sourceSet}") } } - - protected fun validateClasspath(cp: List) { - cp.forEach { - if (! File(it).exists()) { - throw KobaltException("Couldn't find $it") - } - } - } } diff --git a/src/main/kotlin/com/beust/kobalt/maven/Pom.kt b/src/main/kotlin/com/beust/kobalt/maven/Pom.kt index 06c56858..a9147abe 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/Pom.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/Pom.kt @@ -91,7 +91,7 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String, when (e.tagName) { "groupId" -> groupId = e.textContent "artifactId" -> artifactId = e.textContent - "packaging" -> packaging = e.textContent + "type" -> packaging = e.textContent "version" -> version = e.textContent "optional" -> optional = "true".equals(e.textContent, true) "scope" -> scope = e.textContent diff --git a/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt index 6aeda674..d4fe56cd 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt @@ -8,8 +8,11 @@ import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.internal.TaskResult import com.beust.kobalt.maven.FileDependency +import com.beust.kobalt.maven.IClasspathDependency +import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.RunCommand import com.beust.kobalt.misc.log +import com.beust.kobalt.plugin.java.JavaCompiler import com.google.inject.Inject import com.google.inject.Singleton import java.io.File @@ -17,7 +20,8 @@ import java.nio.file.Path import java.nio.file.Paths class AndroidConfiguration(var compileSdkVersion : String = "23", - var buildToolsVersion : String = "23.0.1") + var buildToolsVersion : String = "23.0.1", + var applicationId: String? = null) @Directive fun Project.android(init: AndroidConfiguration.() -> Unit) : AndroidConfiguration { @@ -28,7 +32,7 @@ fun Project.android(init: AndroidConfiguration.() -> Unit) : AndroidConfiguratio } @Singleton -public class AndroidPlugin @Inject constructor() : BasePlugin() { +public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler) : BasePlugin() { val ANDROID_HOME = "/Users/beust/android/adt-bundle-mac-x86_64-20140702/sdk" override val name = "android" @@ -69,7 +73,7 @@ public class AndroidPlugin @Inject constructor() : BasePlugin() { val compileSdkVersion = compileSdkVersion(project) val buildToolsVersion = buildToolsVersion(project) val androidJar = Paths.get(ANDROID_HOME, "platforms", "android-$compileSdkVersion", "android.jar") - val applicationId = "com.beust.example" + val applicationId = configurations[project.name!!]?.applicationId!! val intermediates = Paths.get(project.directory, "app", "build", "intermediates") val manifestDir = Paths.get(project.directory, "app", "src", "main").toString() val manifestIntermediateDir = dirGet(intermediates, "manifests", "full", flavor) @@ -107,9 +111,22 @@ public class AndroidPlugin @Inject constructor() : BasePlugin() { "--custom-package", applicationId, "--output-text-symbols", dirGet(intermediates, "symbol", flavor)) ) + + val rDirectory = KFiles.joinDir(generated.toFile().path, "sources", "r", flavor, + applicationId.replace(".", File.separator)) + val generatedBuildDir = compile(project, rDirectory) + project.compileDependencies.add(FileDependency(generatedBuildDir.path)) return TaskResult() } + private fun compile(project: Project, rDirectory: String) : File { + val sourceFiles = arrayListOf(Paths.get(rDirectory, "R.java").toFile().path) + val buildDir = Paths.get(project.buildDirectory, "generated", "classes").toFile() + + javaCompiler.compile("Compiling R.java", null, listOf(), listOf(), + sourceFiles, buildDir) + return buildDir + } } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt new file mode 100644 index 00000000..ea706188 --- /dev/null +++ b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt @@ -0,0 +1,55 @@ +package com.beust.kobalt.plugin.java + +import com.beust.kobalt.JavaInfo +import com.beust.kobalt.SystemProperties +import com.beust.kobalt.api.Project +import com.beust.kobalt.internal.JvmCompilerPlugin +import com.beust.kobalt.internal.TaskResult +import com.beust.kobalt.maven.DependencyManager +import com.beust.kobalt.maven.IClasspathDependency +import com.beust.kobalt.misc.log +import com.google.inject.Inject +import com.google.inject.Singleton +import java.io.File + +@Singleton +class JavaCompiler @Inject constructor(val dependencyManager: DependencyManager){ + fun compile(name: String, directory: String?, compilerArgs: List, cpList: List, + sourceFiles: List, outputDirectory: File): TaskResult { + + outputDirectory.mkdirs() + val jvm = JavaInfo.create(File(SystemProperties.javaBase)) + val javac = jvm.javacExecutable + + val args = arrayListOf( + javac!!.absolutePath, + "-d", outputDirectory.absolutePath) + if (cpList.size > 0) { + val fullClasspath = dependencyManager.transitiveClosure(cpList) + val stringClasspath = fullClasspath.map { it.jarFile.get().absolutePath } + JvmCompilerPlugin.validateClasspath(stringClasspath) + args.add("-classpath") + args.add(stringClasspath.joinToString(File.pathSeparator)) + } + args.addAll(compilerArgs) + args.addAll(sourceFiles) + + val pb = ProcessBuilder(args) + if (directory != null) { + pb.directory(File(directory)) + } + pb.inheritIO() + // pb.redirectErrorStream(true) + // pb.redirectError(File("/tmp/kobalt-err")) + // pb.redirectOutput(File("/tmp/kobalt-out")) + val line = args.joinToString(" ") + log(1, " Compiling ${sourceFiles.size} files") + log(2, " Compiling $name:\n$line") + val process = pb.start() + val errorCode = process.waitFor() + + return if (errorCode == 0) TaskResult(true, "Compilation succeeded") + else TaskResult(false, "There were errors") + + } +} diff --git a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt index f0a5574a..51089a02 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt @@ -29,7 +29,8 @@ public class JavaPlugin @Inject constructor( override val files: KFiles, override val depFactory: DepFactory, override val dependencyManager: DependencyManager, - override val executors: KobaltExecutors) + override val executors: KobaltExecutors, + val javaCompiler: JavaCompiler) : JvmCompilerPlugin(localRepo, files, depFactory, dependencyManager, executors) { init { @@ -48,37 +49,9 @@ public class JavaPlugin @Inject constructor( private fun compilePrivate(project: Project, cpList: List, sourceFiles: List, outputDirectory: File): TaskResult { - outputDirectory.mkdirs() - val jvm = JavaInfo.create(File(SystemProperties.javaBase)) - val javac = jvm.javacExecutable - - val args = arrayListOf( - javac!!.absolutePath, - "-d", outputDirectory.absolutePath) - if (cpList.size > 0) { - val fullClasspath = dependencyManager.transitiveClosure(cpList) - val stringClasspath = fullClasspath.map { it.jarFile.get().absolutePath } - validateClasspath(stringClasspath) - args.add("-classpath") - args.add(stringClasspath.joinToString(File.pathSeparator)) - } - args.addAll(compilerArgs) - args.addAll(sourceFiles) - - val pb = ProcessBuilder(args) - pb.directory(File(project.directory)) - pb.inheritIO() - // pb.redirectErrorStream(true) - // pb.redirectError(File("/tmp/kobalt-err")) - // pb.redirectOutput(File("/tmp/kobalt-out")) - val line = args.joinToString(" ") - log(1, " Compiling ${sourceFiles.size} files") - log(2, " Compiling $project:\n$line") - val process = pb.start() - val errorCode = process.waitFor() - - return if (errorCode == 0) TaskResult(true, "Compilation succeeded") - else TaskResult(false, "There were errors") + val result = javaCompiler.compile(project.name!!, project.directory, compilerArgs, cpList, sourceFiles, + outputDirectory) + return result } /**