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

Extract CompilerActions in a val in both compilers.

This commit is contained in:
Cedric Beust 2015-10-30 12:41:06 -07:00
parent 45c95c2ca8
commit fe153d0ea3
5 changed files with 65 additions and 73 deletions

View file

@ -155,7 +155,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
classpath(files.kobaltJar)
sourceFiles(buildFile.path.toFile().absolutePath)
output = buildScriptJarFile.absolutePath
}.compile(null)
}.compile()
}
class BuildScriptInfo(val projects: List<Project>, val classLoader: ClassLoader)

View file

@ -16,6 +16,36 @@ import java.io.File
@Singleton
class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) {
val compilerAction = object : ICompilerAction {
override fun compile(info: CompilerActionInfo): TaskResult {
val jvm = JavaInfo.create(File(SystemProperties.javaBase))
val javac = jvm.javacExecutable
val allArgs = arrayListOf(
javac!!.absolutePath,
"-d", info.outputDir)
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)
pb.directory(File(info.outputDir))
pb.inheritIO()
val line = allArgs.joinToString(" ")
log(1, " Compiling ${info.sourceFiles.size} files with classpath size " + info.dependencies.size)
log(2, " Compiling $line")
val process = pb.start()
val errorCode = process.waitFor()
return if (errorCode == 0) TaskResult(true, "Compilation succeeded")
else TaskResult(false, "There were errors")
}
}
/**
* Create an ICompilerAction and a CompilerActionInfo suitable to be passed to doCompiler() to perform the
* actual compilation.
@ -24,35 +54,6 @@ class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) {
sourceFiles: List<String>, outputDir: String, args: List<String>) : TaskResult {
val info = CompilerActionInfo(dependencies, sourceFiles, outputDir, args)
val compilerAction = object : ICompilerAction {
override fun compile(info: CompilerActionInfo): TaskResult {
val jvm = JavaInfo.create(File(SystemProperties.javaBase))
val javac = jvm.javacExecutable
val allArgs = arrayListOf(
javac!!.absolutePath,
"-d", info.outputDir)
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)
pb.directory(File(info.outputDir))
pb.inheritIO()
val line = allArgs.joinToString(" ")
log(1, " Compiling ${info.sourceFiles.size} files with classpath size " + info.dependencies.size)
log(2, " Compiling ${project?.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")
}
}
return jvmCompiler.doCompile(project, context, compilerAction, info)
}
}

View file

@ -11,7 +11,6 @@ import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.internal.TaskResult
import com.beust.kobalt.maven.DepFactory
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.IClasspathDependency
import com.beust.kobalt.maven.LocalRepo
import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.KobaltExecutors
@ -46,13 +45,6 @@ public class JavaPlugin @Inject constructor(
override fun accept(project: Project) = project is JavaProject
private fun compilePrivate(project: Project, cpList: List<IClasspathDependency>, sourceFiles: List<String>,
outputDirectory: File): TaskResult {
val result = javaCompiler.compile(project, context, cpList, sourceFiles, outputDirectory.absolutePath,
compilerArgs)
return result
}
/**
* Replace all the .java files with their directories + *.java in order to limit the
* size of the command line (which blows up on Windows if there are a lot of files).
@ -105,7 +97,7 @@ public class JavaPlugin @Inject constructor(
{ it: String -> it.endsWith(".java") }
.map { File(projectDir, it).absolutePath }
val classpath = jvmCompiler.calculateDependencies(project, context!!, project.compileDependencies)
return compilePrivate(project, classpath, sourceFiles, buildDir)
return javaCompiler.compile(project, context, classpath, sourceFiles, buildDir.absolutePath, compilerArgs)
}
@Task(name = TASK_COMPILE_TEST, description = "Compile the tests", runAfter = arrayOf("compile"))
@ -113,16 +105,14 @@ public class JavaPlugin @Inject constructor(
copyResources(project, JvmCompilerPlugin.SOURCE_SET_TEST)
val projectDir = File(project.directory)
val absoluteSourceFiles = files.findRecursively(projectDir, project.sourceDirectoriesTest.map { File(it) })
val sourceFiles = files.findRecursively(projectDir, project.sourceDirectoriesTest.map { File(it) })
{ it: String -> it.endsWith(".java") }
.map { File(projectDir, it).absolutePath }
val result =
if (absoluteSourceFiles.size > 0) {
compilePrivate(project,
testDependencies(project),
absoluteSourceFiles,
makeOutputTestDir(project))
if (sourceFiles.size > 0) {
javaCompiler.compile(project, context, testDependencies(project), sourceFiles,
makeOutputTestDir(project).absolutePath, compilerArgs)
} else {
// No files to compile
TaskResult()

View file

@ -1,8 +1,12 @@
package com.beust.kobalt.plugin.kotlin;
import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project
import com.beust.kobalt.internal.*
import com.beust.kobalt.internal.CompilerActionInfo
import com.beust.kobalt.internal.ICompilerAction
import com.beust.kobalt.internal.JvmCompiler
import com.beust.kobalt.internal.TaskResult
import com.beust.kobalt.maven.*
import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.log
@ -18,16 +22,27 @@ import kotlin.properties.Delegates
* @since 08 03, 2015
*/
@Singleton
class KotlinCompiler @Inject constructor(override val localRepo : LocalRepo,
override val files: com.beust.kobalt.misc.KFiles,
override val depFactory: DepFactory,
override val dependencyManager: DependencyManager,
override val executors: KobaltExecutors,
override val jvmCompiler: JvmCompiler)
: JvmCompilerPlugin(localRepo, files, depFactory, dependencyManager, executors, jvmCompiler) {
class KotlinCompiler @Inject constructor(val localRepo : LocalRepo,
val files: com.beust.kobalt.misc.KFiles,
val depFactory: DepFactory,
val executors: KobaltExecutors,
val jvmCompiler: JvmCompiler) {
private val KOTLIN_VERSION = "1.0.0-beta-1038"
override val name = "kotlin"
val compilerAction = object: ICompilerAction {
override fun compile(info: CompilerActionInfo): TaskResult {
log(1, "Compiling ${info.sourceFiles.size} files")
val allArgs : Array<String> = arrayOf(
"-d", info.outputDir,
"-classpath", info.dependencies.map {it.jarFile.get()}.joinToString(File.pathSeparator),
*(info.compilerArgs.toTypedArray()),
info.sourceFiles.joinToString(" ")
)
log(2, "Calling kotlinc " + allArgs.joinToString(" "))
CLICompiler.doMainNoExit(K2JVMCompiler(), allArgs)
return TaskResult()
}
}
private fun getKotlinCompilerJar(name: String) : String {
val id = "org.jetbrains.kotlin:$name:$KOTLIN_VERSION"
@ -40,8 +55,8 @@ class KotlinCompiler @Inject constructor(override val localRepo : LocalRepo,
* Create an ICompilerAction and a CompilerActionInfo suitable to be passed to doCompiler() to perform the
* actual compilation.
*/
fun compile(project: Project?, compileDependencies: List<IClasspathDependency>, otherClasspath: List<String>,
source: List<String>, outputDir: String, args: List<String>) : TaskResult {
fun compile(project: Project?, context: KobaltContext?, compileDependencies: List<IClasspathDependency>,
otherClasspath: List<String>, source: List<String>, outputDir: String, args: List<String>) : TaskResult {
val executor = executors.newExecutor("KotlinCompiler", 10)
val compilerDep = depFactory.create("org.jetbrains.kotlin:kotlin-compiler-embeddable:$KOTLIN_VERSION", executor)
@ -62,20 +77,6 @@ class KotlinCompiler @Inject constructor(override val localRepo : LocalRepo,
.plus(classpathList)
.plus(otherClasspath.map { FileDependency(it)})
val info = CompilerActionInfo(dependencies, source, outputDir, args)
val compilerAction = object: ICompilerAction {
override fun compile(info: CompilerActionInfo): TaskResult {
log(1, "Compiling ${source.size} files")
val allArgs : Array<String> = arrayOf(
"-d", info.outputDir,
"-classpath", info.dependencies.map {it.jarFile.get()}.joinToString(File.pathSeparator),
*(info.compilerArgs.toTypedArray()),
info.sourceFiles.joinToString(" ")
)
log(2, "Calling kotlinc " + allArgs.joinToString(" "))
CLICompiler.doMainNoExit(K2JVMCompiler(), allArgs)
return TaskResult()
}
}
return jvmCompiler.doCompile(project, context, compilerAction, info)
}
}
@ -97,8 +98,8 @@ class KConfiguration @Inject constructor(val compiler: KotlinCompiler){
fun compilerArgs(s: List<String>) = args.addAll(s)
fun compile(project: Project?) : TaskResult {
return compiler.compile(project, dependencies, classpath, source, output, args)
fun compile(project: Project? = null, context: KobaltContext? = null) : TaskResult {
return compiler.compile(project, context, dependencies, classpath, source, output, args)
}
}

View file

@ -96,7 +96,7 @@ class KotlinPlugin @Inject constructor(
sourceFiles(sources)
compilerArgs(compilerArgs)
output = outputDirectory
}.compile(project)
}.compile(project, context)
}
}