diff --git a/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt b/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt index ba6a80df..77dfd5fa 100644 --- a/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt @@ -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, val classLoader: ClassLoader) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt index a8b84ec3..82c4983b 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt @@ -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, outputDir: String, args: List) : 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) } } 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 2d811e03..1b2a0523 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt @@ -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, sourceFiles: List, - 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() diff --git a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt index 2443dd2f..03bbc3bf 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt @@ -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 = 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, otherClasspath: List, - source: List, outputDir: String, args: List) : TaskResult { + fun compile(project: Project?, context: KobaltContext?, compileDependencies: List, + otherClasspath: List, source: List, outputDir: String, args: List) : 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 = 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) = 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) } } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt index c76c61dd..41602924 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt @@ -96,7 +96,7 @@ class KotlinPlugin @Inject constructor( sourceFiles(sources) compilerArgs(compilerArgs) output = outputDirectory - }.compile(project) + }.compile(project, context) } }