mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 16:28:12 -07:00
R.java compilation.
This commit is contained in:
parent
07da07bb6d
commit
ea9d574f39
5 changed files with 90 additions and 44 deletions
|
@ -28,6 +28,15 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
||||||
const val SOURCE_SET_MAIN = "main"
|
const val SOURCE_SET_MAIN = "main"
|
||||||
const val SOURCE_SET_TEST = "test"
|
const val SOURCE_SET_TEST = "test"
|
||||||
const val DOCS_DIRECTORY = "docs/javadoc"
|
const val DOCS_DIRECTORY = "docs/javadoc"
|
||||||
|
|
||||||
|
fun validateClasspath(cp: List<String>) {
|
||||||
|
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}")
|
lp(project, "No resources to copy for ${sourceSet}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun validateClasspath(cp: List<String>) {
|
|
||||||
cp.forEach {
|
|
||||||
if (! File(it).exists()) {
|
|
||||||
throw KobaltException("Couldn't find $it")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -91,7 +91,7 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String,
|
||||||
when (e.tagName) {
|
when (e.tagName) {
|
||||||
"groupId" -> groupId = e.textContent
|
"groupId" -> groupId = e.textContent
|
||||||
"artifactId" -> artifactId = e.textContent
|
"artifactId" -> artifactId = e.textContent
|
||||||
"packaging" -> packaging = e.textContent
|
"type" -> packaging = e.textContent
|
||||||
"version" -> version = e.textContent
|
"version" -> version = e.textContent
|
||||||
"optional" -> optional = "true".equals(e.textContent, true)
|
"optional" -> optional = "true".equals(e.textContent, true)
|
||||||
"scope" -> scope = e.textContent
|
"scope" -> scope = e.textContent
|
||||||
|
|
|
@ -8,8 +8,11 @@ import com.beust.kobalt.api.annotation.Directive
|
||||||
import com.beust.kobalt.api.annotation.Task
|
import com.beust.kobalt.api.annotation.Task
|
||||||
import com.beust.kobalt.internal.TaskResult
|
import com.beust.kobalt.internal.TaskResult
|
||||||
import com.beust.kobalt.maven.FileDependency
|
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.RunCommand
|
||||||
import com.beust.kobalt.misc.log
|
import com.beust.kobalt.misc.log
|
||||||
|
import com.beust.kobalt.plugin.java.JavaCompiler
|
||||||
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
|
||||||
|
@ -17,7 +20,8 @@ import java.nio.file.Path
|
||||||
import java.nio.file.Paths
|
import java.nio.file.Paths
|
||||||
|
|
||||||
class AndroidConfiguration(var compileSdkVersion : String = "23",
|
class AndroidConfiguration(var compileSdkVersion : String = "23",
|
||||||
var buildToolsVersion : String = "23.0.1")
|
var buildToolsVersion : String = "23.0.1",
|
||||||
|
var applicationId: String? = null)
|
||||||
|
|
||||||
@Directive
|
@Directive
|
||||||
fun Project.android(init: AndroidConfiguration.() -> Unit) : AndroidConfiguration {
|
fun Project.android(init: AndroidConfiguration.() -> Unit) : AndroidConfiguration {
|
||||||
|
@ -28,7 +32,7 @@ fun Project.android(init: AndroidConfiguration.() -> Unit) : AndroidConfiguratio
|
||||||
}
|
}
|
||||||
|
|
||||||
@Singleton
|
@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"
|
val ANDROID_HOME = "/Users/beust/android/adt-bundle-mac-x86_64-20140702/sdk"
|
||||||
override val name = "android"
|
override val name = "android"
|
||||||
|
|
||||||
|
@ -69,7 +73,7 @@ public class AndroidPlugin @Inject constructor() : BasePlugin() {
|
||||||
val compileSdkVersion = compileSdkVersion(project)
|
val compileSdkVersion = compileSdkVersion(project)
|
||||||
val buildToolsVersion = buildToolsVersion(project)
|
val buildToolsVersion = buildToolsVersion(project)
|
||||||
val androidJar = Paths.get(ANDROID_HOME, "platforms", "android-$compileSdkVersion", "android.jar")
|
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 intermediates = Paths.get(project.directory, "app", "build", "intermediates")
|
||||||
val manifestDir = Paths.get(project.directory, "app", "src", "main").toString()
|
val manifestDir = Paths.get(project.directory, "app", "src", "main").toString()
|
||||||
val manifestIntermediateDir = dirGet(intermediates, "manifests", "full", flavor)
|
val manifestIntermediateDir = dirGet(intermediates, "manifests", "full", flavor)
|
||||||
|
@ -107,9 +111,22 @@ public class AndroidPlugin @Inject constructor() : BasePlugin() {
|
||||||
"--custom-package", applicationId,
|
"--custom-package", applicationId,
|
||||||
"--output-text-symbols", dirGet(intermediates, "symbol", flavor))
|
"--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()
|
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<String>(), listOf<IClasspathDependency>(),
|
||||||
|
sourceFiles, buildDir)
|
||||||
|
return buildDir
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
55
src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt
Normal file
55
src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt
Normal file
|
@ -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<String>, cpList: List<IClasspathDependency>,
|
||||||
|
sourceFiles: List<String>, 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")
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -29,7 +29,8 @@ public class JavaPlugin @Inject constructor(
|
||||||
override val files: KFiles,
|
override val files: KFiles,
|
||||||
override val depFactory: DepFactory,
|
override val depFactory: DepFactory,
|
||||||
override val dependencyManager: DependencyManager,
|
override val dependencyManager: DependencyManager,
|
||||||
override val executors: KobaltExecutors)
|
override val executors: KobaltExecutors,
|
||||||
|
val javaCompiler: JavaCompiler)
|
||||||
: JvmCompilerPlugin(localRepo, files, depFactory, dependencyManager, executors) {
|
: JvmCompilerPlugin(localRepo, files, depFactory, dependencyManager, executors) {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
@ -48,37 +49,9 @@ public class JavaPlugin @Inject constructor(
|
||||||
|
|
||||||
private fun compilePrivate(project: Project, cpList: List<IClasspathDependency>, sourceFiles: List<String>,
|
private fun compilePrivate(project: Project, cpList: List<IClasspathDependency>, sourceFiles: List<String>,
|
||||||
outputDirectory: File): TaskResult {
|
outputDirectory: File): TaskResult {
|
||||||
outputDirectory.mkdirs()
|
val result = javaCompiler.compile(project.name!!, project.directory, compilerArgs, cpList, sourceFiles,
|
||||||
val jvm = JavaInfo.create(File(SystemProperties.javaBase))
|
outputDirectory)
|
||||||
val javac = jvm.javacExecutable
|
return result
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue