From 2b179abf4991da55653bbcda35191f1dfc27776a Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Fri, 4 Mar 2016 21:42:50 +0400 Subject: [PATCH] Move archive generation to Archives. --- .../main/kotlin/com/beust/kobalt/Archives.kt | 70 +++++++++++++++++++ .../plugin/application/ApplicationPlugin.kt | 2 +- .../kobalt/plugin/packaging/JarGenerator.kt | 3 +- .../plugin/packaging/PackagingPlugin.kt | 60 +--------------- .../kobalt/plugin/packaging/WarGenerator.kt | 3 +- .../kobalt/plugin/packaging/ZipGenerator.kt | 3 +- 6 files changed, 78 insertions(+), 63 deletions(-) create mode 100644 modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Archives.kt diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Archives.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Archives.kt new file mode 100644 index 00000000..9860a7dd --- /dev/null +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Archives.kt @@ -0,0 +1,70 @@ +package com.beust.kobalt + +import com.beust.kobalt.api.KobaltContext +import com.beust.kobalt.api.Project +import com.beust.kobalt.api.annotation.ExportedProjectProperty +import com.beust.kobalt.misc.IncludedFile +import com.beust.kobalt.misc.JarUtils +import com.beust.kobalt.misc.KFiles +import com.beust.kobalt.misc.log +import java.io.File +import java.io.FileOutputStream +import java.io.OutputStream +import java.util.* +import java.util.zip.ZipOutputStream + +class Archives { + companion object { + @ExportedProjectProperty(doc = "The name of the jar file", type = "String") + const val JAR_NAME = "jarName" + + private val DEFAULT_STREAM_FACTORY = { os : OutputStream -> ZipOutputStream(os) } + + fun generateArchive(project: Project, + context: KobaltContext, + archiveName: String?, + suffix: String, + includedFiles: List, + expandJarFiles : Boolean = false, + outputStreamFactory: (OutputStream) -> ZipOutputStream = DEFAULT_STREAM_FACTORY) : File { + val fullArchiveName = context.variant.archiveName(project, archiveName, suffix) + val archiveDir = File(KFiles.libsDir(project)) + val result = File(archiveDir.path, fullArchiveName) + log(2, "Creating $result") + if (! Features.USE_TIMESTAMPS || isOutdated(project.directory, includedFiles, result)) { + val outStream = outputStreamFactory(FileOutputStream(result)) + JarUtils.addFiles(project.directory, includedFiles, outStream, expandJarFiles) + log(2, text = "Added ${includedFiles.size} files to $result") + outStream.flush() + outStream.close() + log(1, " Created $result") + } else { + log(2, " $result is up to date") + } + + project.projectProperties.put(JAR_NAME, result.absolutePath) + + return result + } + + private fun isOutdated(directory: String, includedFiles: List, output: File): Boolean { + if (! output.exists()) return true + + val lastModified = output.lastModified() + includedFiles.forEach { root -> + val allFiles = root.allFromFiles(directory) + allFiles.forEach { relFile -> + val file = File(KFiles.joinDir(directory, root.from, relFile.path)) + if (file.isFile) { + if (file.lastModified() > lastModified) { + log(2, " TS - Outdated $file and $output " + + Date(file.lastModified()) + " " + Date(output.lastModified())) + return true + } + } + } + } + return false + } + } +} diff --git a/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt index e8470eed..42420f0e 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt @@ -94,7 +94,7 @@ class ApplicationPlugin @Inject constructor(val configActor: ConfigActor val allDeps = arrayListOf(jarName) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/JarGenerator.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/JarGenerator.kt index ebebbbcd..86c5eb1c 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/JarGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/JarGenerator.kt @@ -1,5 +1,6 @@ package com.beust.kobalt.plugin.packaging +import com.beust.kobalt.Archives import com.beust.kobalt.IFileSpec import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Project @@ -93,7 +94,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) } val jarFactory = { os: OutputStream -> JarOutputStream(os, manifest) } - return PackagingPlugin.generateArchive(project, context, jar.name, ".jar", allFiles, + return Archives.generateArchive(project, context, jar.name, ".jar", allFiles, true /* expandJarFiles */, jarFactory) } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt index d7b206ba..715a9643 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt @@ -12,11 +12,7 @@ import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.PomGenerator import com.beust.kobalt.misc.* import java.io.File -import java.io.FileOutputStream -import java.io.OutputStream import java.nio.file.Paths -import java.util.* -import java.util.zip.ZipOutputStream import javax.inject.Inject import javax.inject.Singleton @@ -33,9 +29,6 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana @ExportedProjectProperty(doc = "Where the libraries are saved", type = "String") const val LIBS_DIR = "libsDir" - @ExportedProjectProperty(doc = "The name of the jar file", type = "String") - const val JAR_NAME = "jarName" - @ExportedProjectProperty(doc = "The list of packages produced for this project", type = "List") const val PACKAGES = "packages" @@ -76,57 +69,6 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana } return result } - - fun generateArchive(project: Project, - context: KobaltContext, - archiveName: String?, - suffix: String, - includedFiles: List, - expandJarFiles : Boolean = false, - outputStreamFactory: (OutputStream) -> ZipOutputStream = DEFAULT_STREAM_FACTORY) : File { - val fullArchiveName = context.variant.archiveName(project, archiveName, suffix) - val archiveDir = File(libsDir(project)) - val result = File(archiveDir.path, fullArchiveName) - log(2, "Creating $result") - if (! Features.USE_TIMESTAMPS || isOutdated(project.directory, includedFiles, result)) { - val outStream = outputStreamFactory(FileOutputStream(result)) - JarUtils.addFiles(project.directory, includedFiles, outStream, expandJarFiles) - log(2, text = "Added ${includedFiles.size} files to $result") - outStream.flush() - outStream.close() - log(1, " Created $result") - } else { - log(2, " $result is up to date") - } - - project.projectProperties.put(JAR_NAME, result.absolutePath) - - return result - } - - private val DEFAULT_STREAM_FACTORY = { os : OutputStream -> ZipOutputStream(os) } - - private fun isOutdated(directory: String, includedFiles: List, output: File): Boolean { - if (! output.exists()) return true - - val lastModified = output.lastModified() - includedFiles.forEach { root -> - val allFiles = root.allFromFiles(directory) - allFiles.forEach { relFile -> - val file = File(KFiles.joinDir(directory, root.from, relFile.path)) - if (file.isFile) { - if (file.lastModified() > lastModified) { - log(2, " TS - Outdated $file and $output " - + Date(file.lastModified()) + " " + Date(output.lastModified())) - return true - } - } - } - } - return false - } - - private fun libsDir(project: Project) = KFiles.makeDir(KFiles.buildDir(project).path, "libs").path } override val name = PLUGIN_NAME @@ -135,7 +77,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana override fun apply(project: Project, context: KobaltContext) { super.apply(project, context) - project.projectProperties.put(LIBS_DIR, libsDir(project)) + project.projectProperties.put(LIBS_DIR, KFiles.libsDir(project)) taskContributor.addVariantTasks(this, project, context, "assemble", runAfter = listOf("compile"), runTask = { doTaskAssemble(project) }) } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt index 81bb1a31..e3f463e2 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt @@ -1,5 +1,6 @@ package com.beust.kobalt.plugin.packaging +import com.beust.kobalt.Archives import com.beust.kobalt.IFileSpec import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.api.KobaltContext @@ -86,7 +87,7 @@ class WarGenerator @Inject constructor(val dependencyManager: DependencyManager) val allFiles = findIncludedFiles(project, context, war) val jarFactory = { os: OutputStream -> JarOutputStream(os, manifest) } - return PackagingPlugin.generateArchive(project, context, war.name, ".war", allFiles, + return Archives.generateArchive(project, context, war.name, ".war", allFiles, false /* don't expand jar files */, jarFactory) } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/ZipGenerator.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/ZipGenerator.kt index 0608b0ea..eb50dfae 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/ZipGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/ZipGenerator.kt @@ -1,5 +1,6 @@ package com.beust.kobalt.plugin.packaging +import com.beust.kobalt.Archives import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Project import com.beust.kobalt.maven.DependencyManager @@ -8,6 +9,6 @@ import com.google.inject.Inject class ZipGenerator @Inject constructor(val dependencyManager: DependencyManager){ fun generateZip(project: Project, context: KobaltContext, zip: Zip) { val allFiles = PackagingPlugin.findIncludedFiles(project.directory, zip.includedFiles, zip.excludes) - PackagingPlugin.generateArchive(project, context, zip.name, ".zip", allFiles) + Archives.generateArchive(project, context, zip.name, ".zip", allFiles) } }