From 6714a24c6ac8363ab07eee8e38db500436a3f445 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Fri, 30 Oct 2015 17:38:51 -0700 Subject: [PATCH] Move javadoc under ICompilerAction. --- .../com/beust/kobalt/internal/JvmCompiler.kt | 3 +- .../beust/kobalt/kotlin/BuildFileCompiler.kt | 6 +- .../com/beust/kobalt/maven/PomGenerator.kt | 7 ++- .../kotlin/com/beust/kobalt/misc/KFiles.kt | 10 +--- .../kobalt/plugin/android/AndroidPlugin.kt | 2 +- .../beust/kobalt/plugin/java/JavaCompiler.kt | 24 ++++---- .../beust/kobalt/plugin/java/JavaPlugin.kt | 59 ++++++------------- .../kobalt/plugin/kotlin/KotlinCompiler.kt | 10 ++-- .../kobalt/plugin/kotlin/KotlinPlugin.kt | 8 +-- 9 files changed, 52 insertions(+), 77 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt b/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt index b7471b7c..f36b3b2e 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt @@ -20,7 +20,6 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager) */ fun doCompile(project: Project?, context: KobaltContext?, action: ICompilerAction, info: CompilerActionInfo) : TaskResult { - File(info.outputDir).mkdirs() val allDependencies = info.dependencies + calculateDependencies(project, context, info.dependencies) @@ -64,7 +63,7 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager) } data class CompilerActionInfo(val dependencies: List, - val sourceFiles: List, val outputDir: String, val compilerArgs: List) + val sourceFiles: List, val outputDir: File, val compilerArgs: List) interface ICompilerAction { fun compile(info: CompilerActionInfo): TaskResult diff --git a/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt b/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt index 6f63cc8f..35a4976b 100644 --- a/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt @@ -88,8 +88,8 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b classpath(files.kobaltJar) classpath(pluginUrls.map { it.file }) sourceFiles(listOf(buildFile.path.toFile().absolutePath)) - output = buildScriptJarFile.absolutePath - }.compile(null /* no projects yet */) + output = buildScriptJarFile + }.compile() } } @@ -154,7 +154,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b kotlinCompilePrivate { classpath(files.kobaltJar) sourceFiles(buildFile.path.toFile().absolutePath) - output = buildScriptJarFile.absolutePath + output = File(buildScriptJarFile.absolutePath) }.compile() } diff --git a/src/main/kotlin/com/beust/kobalt/maven/PomGenerator.kt b/src/main/kotlin/com/beust/kobalt/maven/PomGenerator.kt index 23ab1d43..ffb39ef2 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/PomGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/PomGenerator.kt @@ -1,7 +1,8 @@ package com.beust.kobalt.maven -import com.beust.kobalt.api.Project import com.beust.kobalt.SystemProperties +import com.beust.kobalt.api.Project +import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.log import com.google.common.base.Preconditions import com.google.inject.assistedinject.Assisted @@ -52,8 +53,8 @@ public class PomGenerator @Inject constructor(@Assisted val project: Project) { val s = StringWriter() MavenXpp3Writer().write(s, m) - val buildDir = com.beust.kobalt.misc.KFiles.makeDir(project.directory, project.buildDirectory!!) - val outputDir = com.beust.kobalt.misc.KFiles.makeDir(buildDir.path, "libs") + val buildDir = KFiles.makeDir(project.directory, project.buildDirectory!!) + val outputDir = KFiles.makeDir(buildDir.path, "libs") val mavenId = MavenId.create(project.group!!, project.artifactId!!, project.packaging, project.version!!) val pomFile = SimpleDep(mavenId).toPomFileName() val outputFile = File(outputDir, pomFile) diff --git a/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt b/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt index c4f011b3..5548463a 100644 --- a/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt +++ b/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt @@ -1,9 +1,9 @@ package com.beust.kobalt.misc +import com.beust.kobalt.SystemProperties import com.beust.kobalt.api.Kobalt import com.beust.kobalt.homeDir import com.beust.kobalt.kotlin.BuildFile -import com.beust.kobalt.SystemProperties import java.io.File import java.io.IOException import java.nio.file.Files @@ -57,11 +57,8 @@ public class KFiles { public fun joinDir(vararg ts: String): String = ts.toArrayList().joinToString(File.separator) - public fun makeDir(dir: String, s: String) : File { - val result = File(dir, s) - result.mkdirs() - return result - } + fun makeDir(dir: String, s: String? = null) = + (if (s != null) File(dir, s) else File(dir)).apply { mkdirs() } public fun findRecursively(rootDir: File) : List = findRecursively(rootDir, arrayListOf(), { s -> true }) @@ -192,5 +189,4 @@ public class KFiles { os.close() } } - } 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 709096f6..3f006d17 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt @@ -146,7 +146,7 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler) : val sourceFiles = arrayListOf(Paths.get(rDirectory, "R.java").toFile().path) val buildDir = Paths.get(project.buildDirectory, "generated", "classes").toFile() - javaCompiler.compile(project, context, listOf(), sourceFiles, buildDir.absolutePath, listOf()) + javaCompiler.compile(project, context, listOf(), sourceFiles, buildDir, listOf()) 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 index d9c4c3b6..57d50b5c 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt @@ -16,15 +16,12 @@ import java.io.File @Singleton class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) { - val compilerAction = object : ICompilerAction { + fun compilerAction(executable: File) = 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) + executable.absolutePath, + "-d", info.outputDir.absolutePath) if (info.dependencies.size > 0) { allArgs.add("-classpath") allArgs.add(info.dependencies.map {it.jarFile.get()}.joinToString(File.pathSeparator)) @@ -33,7 +30,7 @@ class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) { allArgs.addAll(info.sourceFiles) val pb = ProcessBuilder(allArgs) - pb.directory(File(info.outputDir)) + pb.directory(info.outputDir) pb.inheritIO() val line = allArgs.joinToString(" ") log(1, " Compiling ${info.sourceFiles.size} files with classpath size " + info.dependencies.size) @@ -50,9 +47,16 @@ class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) { * Create an ICompilerAction based on the parameters and send it to JvmCompiler.doCompile(). */ fun compile(project: Project?, context: KobaltContext?, dependencies: List, - sourceFiles: List, outputDir: String, args: List) : TaskResult { - + sourceFiles: List, outputDir: File, args: List) : TaskResult { val info = CompilerActionInfo(dependencies, sourceFiles, outputDir, args) - return jvmCompiler.doCompile(project, context, compilerAction, info) + val jvm = JavaInfo.create(File(SystemProperties.javaBase)) + return jvmCompiler.doCompile(project, context, compilerAction(jvm.javacExecutable!!), info) + } + + fun javadoc(project: Project?, context: KobaltContext?, dependencies: List, + sourceFiles: List, outputDir: File, args: List) : TaskResult { + val info = CompilerActionInfo(dependencies, sourceFiles, outputDir, args) + val jvm = JavaInfo.create(File(SystemProperties.javaBase)) + return jvmCompiler.doCompile(project, context, compilerAction(jvm.javadocExecutable!!), 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 1b2a0523..3aea9f7d 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt @@ -1,7 +1,5 @@ package com.beust.kobalt.plugin.java -import com.beust.kobalt.JavaInfo -import com.beust.kobalt.SystemProperties import com.beust.kobalt.api.Kobalt import com.beust.kobalt.api.Project import com.beust.kobalt.api.annotation.Directive @@ -59,60 +57,32 @@ public class JavaPlugin @Inject constructor( @Task(name = TASK_JAVADOC, description = "Run Javadoc") fun taskJavadoc(project: Project) : TaskResult { val projectDir = File(project.directory) - val outputDir = File(projectDir, - project.buildDirectory + File.separator + JvmCompilerPlugin.DOCS_DIRECTORY) - outputDir.mkdirs() - val jvm = JavaInfo.create(File(SystemProperties.javaBase)) - val javadoc = jvm.javadocExecutable - - val sourceFiles = files.findRecursively(projectDir, project.sourceDirectories.map { File(it) }) - { it: String -> it.endsWith(".java") } - .map { File(projectDir, it).absolutePath } - val classpath = jvmCompiler.calculateDependencies(project, context!!, project.compileDependencies) - val args = arrayListOf( - javadoc!!.absolutePath, - "-classpath", classpath.map { it.jarFile.get().absolutePath }.joinToString(File.pathSeparator), - "-d", outputDir.absolutePath) - val compressed = sourcesToDirectories(sourceFiles, project.sourceSuffix) - args.addAll(compressed) - - val pb = ProcessBuilder(args) - pb.directory(File(project.directory)) - pb.inheritIO() - val process = pb.start() - val errorCode = process.waitFor() - - return if (errorCode == 0) TaskResult(true, "Compilation succeeded") - else TaskResult(false, "There were errors") - + val sourceFiles = findSourceFiles(project.directory, project.sourceDirectories) + val buildDir = File(projectDir, + project.buildDirectory + File.separator + JvmCompilerPlugin.DOCS_DIRECTORY).apply { mkdirs() } + return javaCompiler.javadoc(project, context, project.compileDependencies, sourceFiles, + buildDir, compilerArgs) } @Task(name = TASK_COMPILE, description = "Compile the project") fun taskCompile(project: Project) : TaskResult { copyResources(project, JvmCompilerPlugin.SOURCE_SET_MAIN) val projectDir = File(project.directory) - val buildDir = File(projectDir, - project.buildDirectory + File.separator + "classes") - val sourceFiles = files.findRecursively(projectDir, project.sourceDirectories.map { File(it) }) - { it: String -> it.endsWith(".java") } - .map { File(projectDir, it).absolutePath } - val classpath = jvmCompiler.calculateDependencies(project, context!!, project.compileDependencies) - return javaCompiler.compile(project, context, classpath, sourceFiles, buildDir.absolutePath, compilerArgs) + val sourceFiles = findSourceFiles(project.directory, project.sourceDirectories) + val buildDir = File(projectDir, project.buildDirectory + File.separator + "classes") + return javaCompiler.compile(project, context, project.compileDependencies, sourceFiles, + buildDir, compilerArgs) } @Task(name = TASK_COMPILE_TEST, description = "Compile the tests", runAfter = arrayOf("compile")) fun taskCompileTest(project: Project): TaskResult { copyResources(project, JvmCompilerPlugin.SOURCE_SET_TEST) - val projectDir = File(project.directory) - - val sourceFiles = files.findRecursively(projectDir, project.sourceDirectoriesTest.map { File(it) }) - { it: String -> it.endsWith(".java") } - .map { File(projectDir, it).absolutePath } + val sourceFiles = findSourceFiles(project.directory, project.sourceDirectoriesTest) val result = if (sourceFiles.size > 0) { javaCompiler.compile(project, context, testDependencies(project), sourceFiles, - makeOutputTestDir(project).absolutePath, compilerArgs) + makeOutputTestDir(project), compilerArgs) } else { // No files to compile TaskResult() @@ -120,6 +90,13 @@ public class JavaPlugin @Inject constructor( return result } + private fun findSourceFiles(dir: String, sourceDirectories: List): List { + val projectDir = File(dir) + return files.findRecursively(projectDir, + sourceDirectories.map { File(it) }) { it: String -> it.endsWith(".java") } + .map { File(projectDir, it).absolutePath } + } + private val compilerArgs = arrayListOf() fun addCompilerArgs(vararg args: String) { 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 3c19785d..ba59e622 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt @@ -33,10 +33,10 @@ class KotlinCompiler @Inject constructor(val localRepo : LocalRepo, override fun compile(info: CompilerActionInfo): TaskResult { log(1, "Compiling ${info.sourceFiles.size} files") val allArgs : Array = arrayOf( - "-d", info.outputDir, + "-d", info.outputDir.path, "-classpath", info.dependencies.map {it.jarFile.get()}.joinToString(File.pathSeparator), *(info.compilerArgs.toTypedArray()), - info.sourceFiles.joinToString(" ") + *(info.sourceFiles.toTypedArray()) ) log(2, "Calling kotlinc " + allArgs.joinToString(" ")) CLICompiler.doMainNoExit(K2JVMCompiler(), allArgs) @@ -55,7 +55,7 @@ class KotlinCompiler @Inject constructor(val localRepo : LocalRepo, * Create an ICompilerAction based on the parameters and send it to JvmCompiler.doCompile(). */ fun compile(project: Project?, context: KobaltContext?, compileDependencies: List, - otherClasspath: List, source: List, outputDir: String, args: List) : TaskResult { + otherClasspath: List, sourceFiles: List, outputDir: File, args: List) : TaskResult { val executor = executors.newExecutor("KotlinCompiler", 10) val compilerDep = depFactory.create("org.jetbrains.kotlin:kotlin-compiler-embeddable:$KOTLIN_VERSION", executor) @@ -72,7 +72,7 @@ class KotlinCompiler @Inject constructor(val localRepo : LocalRepo, .map { FileDependency(it) } val dependencies = compileDependencies + classpathList + otherClasspath.map { FileDependency(it)} - val info = CompilerActionInfo(dependencies, source, outputDir, args) + val info = CompilerActionInfo(dependencies, sourceFiles, outputDir, args) return jvmCompiler.doCompile(project, context, compilerAction, info) } } @@ -81,7 +81,7 @@ class KConfiguration @Inject constructor(val compiler: KotlinCompiler){ val classpath = arrayListOf() val dependencies = arrayListOf() var source = arrayListOf() - var output: String by Delegates.notNull() + var output: File by Delegates.notNull() val args = arrayListOf() fun sourceFiles(s: String) = source.add(s) 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 41602924..9fad4553 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt @@ -59,7 +59,7 @@ class KotlinPlugin @Inject constructor( File(projectDirectory, it).absolutePath } - compilePrivate(project, classpath, absoluteSourceFiles, buildDirectory.absolutePath) + compilePrivate(project, classpath, absoluteSourceFiles, buildDirectory) lp(project, "Compilation succeeded") return TaskResult() } @@ -79,16 +79,14 @@ class KotlinPlugin @Inject constructor( compilePrivate(project, testDependencies(project), absoluteSourceFiles, - makeOutputTestDir(project).absolutePath) + makeOutputTestDir(project)) lp(project, "Compilation of tests succeeded") return TaskResult() } private fun compilePrivate(project: Project, cpList: List, sources: List, - outputDirectory: String): TaskResult { - File(outputDirectory).mkdirs() - + outputDirectory: File): TaskResult { log(1, " Compiling ${sources.size} files with classpath size ${cpList.size}") return kotlinCompilePrivate {