diff --git a/kobalt/src/Build.kt b/kobalt/src/Build.kt index 65d29bc0..12298c53 100644 --- a/kobalt/src/Build.kt +++ b/kobalt/src/Build.kt @@ -1,3 +1,4 @@ + import com.beust.kobalt.* import com.beust.kobalt.api.Project import com.beust.kobalt.api.annotation.Task @@ -97,6 +98,7 @@ val kobaltPluginApi = project { dependencies { compile( + "org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}", "com.google.inject:guice:4.0", "com.google.inject.extensions:guice-assistedinject:4.0", "javax.inject:javax.inject:1", @@ -114,10 +116,10 @@ val kobaltPluginApi = project { *mavenResolver("api", "spi", "util", "impl", "connector-basic", "transport-http", "transport-file"), "org.apache.maven:maven-aether-provider:3.3.9", "org.testng.testng-remote:testng-remote:1.3.0", - "org.testng:testng:${Versions.testng}" + "org.testng:testng:${Versions.testng}", + "commons-io:commons-io:2.5" ) exclude(*aether("impl", "spi", "util", "api")) - compile("org.jetbrains.kotlin:kotlin-stdlib:1.1.1") } @@ -154,7 +156,9 @@ val kobaltApp = project(kobaltPluginApi, wrapper) { compile("org.jetbrains.kotlin:kotlin-compiler-embeddable:${Versions.kotlin}") // Used by the main app - compile("com.github.spullara.mustache.java:compiler:0.9.1", + compile( + "org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}", + "com.github.spullara.mustache.java:compiler:0.9.1", "javax.inject:javax.inject:1", "com.google.inject:guice:4.0", "com.google.inject.extensions:guice-assistedinject:4.0", @@ -181,16 +185,15 @@ val kobaltApp = project(kobaltPluginApi, wrapper) { // "org.glassfish.jersey.media:jersey-media-moxy:${Versions.jersey}", // "org.wasabi:wasabi:0.1.182" ) - compile("org.jetbrains.kotlin:kotlin-stdlib:1.1.1") } dependenciesTest { - compile("org.testng:testng:${Versions.testng}", + compile("org.jetbrains.kotlin:kotlin-test:${Versions.kotlin}", + "org.testng:testng:${Versions.testng}", "org.assertj:assertj-core:3.4.1", *mavenResolver("util") ) - compile("org.jetbrains.kotlin:kotlin-test:1.1.1") } assemble { @@ -202,12 +205,15 @@ val kobaltApp = project(kobaltPluginApi, wrapper) { } zip { val dir = "kobalt-$version" - include(from("dist"), to("$dir/bin"), "kobaltw") - include(from("dist"), to("$dir/bin"), "kobaltw.bat") - include(from("$buildDirectory/libs"), to("$dir/kobalt/wrapper"), - "$projectName-$version.jar") - include(from("modules/wrapper/$buildDirectory/libs"), to("$dir/kobalt/wrapper"), - "$projectName-wrapper.jar") + val files = listOf( + "dist", "$dir/bin", "kobaltw", + "dist", "$dir/bin", "kobaltw.bat", + "$buildDirectory/libs", "$dir/kobalt/wrapper", "$projectName-$version.jar", + "modules/wrapper/$buildDirectory/libs", "$dir/kobalt/wrapper", "$projectName-wrapper.jar") + + (0 .. files.size - 1 step 3).forEach { i -> + include(from(files[i]), To(files[i + 1]), files[i + 2]) + } } } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ArchiveGenerator.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ArchiveGenerator.kt index dc3fda2d..8158c642 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ArchiveGenerator.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ArchiveGenerator.kt @@ -3,7 +3,6 @@ package com.beust.kobalt import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Project import com.beust.kobalt.archive.Zip -import com.beust.kobalt.misc.IncludedFile import com.beust.kobalt.misc.KFiles import java.io.File diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/IncludeFromTo.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/IncludeFromTo.kt new file mode 100644 index 00000000..69b953b3 --- /dev/null +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/IncludeFromTo.kt @@ -0,0 +1,36 @@ +package com.beust.kobalt + +import com.beust.kobalt.api.annotation.Directive + +/** + * Base classes for directives that support install(from,to) (e.g. install{} or jar{}). + */ +open class IncludeFromTo { + /** + * Prefix path to be removed from the zip file. For example, if you add "build/lib/a.jar" to the zip + * file and the excludePrefix is "build/lib", then "a.jar" will be added at the root of the zip file. + */ + val includedFiles = arrayListOf() + + @Directive + fun from(s: String) = From(s) + + @Directive + fun to(s: String) = To(s) + + @Directive + fun include(vararg files: String) { + includedFiles.add(IncludedFile(files.map { IFileSpec.FileSpec(it) })) + } + + @Directive + fun include(from: From, to: To, vararg specs: String) { + includedFiles.add(IncludedFile(from, to, specs.map { IFileSpec.FileSpec(it) })) + } + + @Directive + fun include(from: From, to: To, vararg specs: IFileSpec.GlobSpec) { + includedFiles.add(IncludedFile(from, to, listOf(*specs))) + } +} + diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/IncludedFile.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/IncludedFile.kt new file mode 100644 index 00000000..46dea15e --- /dev/null +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/IncludedFile.kt @@ -0,0 +1,44 @@ +package com.beust.kobalt + +import com.beust.kobalt.misc.KFiles +import com.beust.kobalt.misc.toString +import java.io.File +import java.nio.file.Paths + +class IncludedFile(val fromOriginal: From, val toOriginal: To, val specs: List, + val expandJarFiles: Boolean = false) { + constructor(specs: List, expandJarFiles: Boolean = false) : this(From(""), To(""), specs, expandJarFiles) + fun from(s: String) = File(if (fromOriginal.isCurrentDir()) s else KFiles.joinDir(from, s)) + val from: String get() = fromOriginal.path.replace("\\", "/") + fun to(s: String) = File(if (toOriginal.isCurrentDir()) s else KFiles.joinDir(to, s)) + val to: String get() = toOriginal.path.replace("\\", "/") + override fun toString() = toString("IncludedFile", + "files - ", specs.map { it.toString() }, + "from", from, + "to", to) + + fun allFromFiles(directory: String? = null): List { + val result = arrayListOf() + specs.forEach { spec -> +// val fullDir = if (directory == null) from else KFiles.joinDir(directory, from) + spec.toFiles(directory, from).forEach { source -> + result.add(if (source.isAbsolute) source else File(source.path)) + } + } + return result.map { Paths.get(it.path).normalize().toFile()} + } +} + +open class Direction(open val p: String) { + override fun toString() = path + fun isCurrentDir() = path == "./" + + val path: String get() = + if (p.isEmpty()) "./" + else if (p.startsWith("/") || p.endsWith("/")) p + else p + "/" +} + +class From(override val p: String) : Direction(p) + +class To(override val p: String) : Direction(p) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Archives.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Archives.kt index 7d2e291f..19054e33 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Archives.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Archives.kt @@ -1,11 +1,12 @@ package com.beust.kobalt.archive -import com.beust.kobalt.Features -import com.beust.kobalt.IFileSpec +import 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.* +import com.beust.kobalt.misc.JarUtils +import com.beust.kobalt.misc.KFiles +import com.beust.kobalt.misc.kobaltLog import java.io.File import java.io.FileOutputStream import java.io.OutputStream diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Zip.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Zip.kt index 296eb784..41957218 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Zip.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Zip.kt @@ -1,23 +1,13 @@ package com.beust.kobalt.archive -import com.beust.kobalt.Glob -import com.beust.kobalt.IFileSpec +import com.beust.kobalt.* import com.beust.kobalt.api.Project import com.beust.kobalt.api.annotation.Directive -import com.beust.kobalt.misc.From -import com.beust.kobalt.misc.IncludedFile -import com.beust.kobalt.misc.To open class Zip(open val project: Project, open var name: String = Archives.defaultArchiveName(project) + ".zip", - open var fatJar: Boolean = false): AttributeHolder { + open var fatJar: Boolean = false): AttributeHolder, IncludeFromTo() { val excludes = arrayListOf() - @Directive - fun from(s: String) = From(s) - - @Directive - fun to(s: String) = To(s) - @Directive fun exclude(vararg files: String) { files.forEach { excludes.add(Glob(it)) } @@ -28,34 +18,10 @@ open class Zip(open val project: Project, open var name: String = Archives.defau specs.forEach { excludes.add(it) } } - @Directive - fun include(vararg files: String) { - includedFiles.add(IncludedFile(files.map { IFileSpec.FileSpec(it) })) - } - - @Directive - fun include(from: From, to: To, vararg specs: String) { - includedFiles.add(IncludedFile(from, to, specs.map { IFileSpec.FileSpec(it) })) - } - - @Directive - fun include(from: From, to: To, vararg specs: IFileSpec.GlobSpec) { - includedFiles.add(IncludedFile(from, to, listOf(*specs))) - } - - /** - * Prefix path to be removed from the zip file. For example, if you add "build/lib/a.jar" to the zip - * file and the excludePrefix is "build/lib", then "a.jar" will be added at the root of the zip file. - */ - val includedFiles = arrayListOf() - @Directive open val attributes = arrayListOf(Pair("Manifest-Version", "1.0")) override fun addAttribute(k: String, v: String) { attributes.add(Pair(k, v)) } - } - - diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/CompilerUtils.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/CompilerUtils.kt index cbc18dcf..e84b3de8 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/CompilerUtils.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/CompilerUtils.kt @@ -196,7 +196,7 @@ class CompilerUtils @Inject constructor(val files: KFiles, val dependencyManager .filter(File::exists) .forEach { context.logger.log(project.name, 2, "Copying from $it to $absOutputDir") - KFiles.copyRecursively(it, absOutputDir, deleteFirst = false) + KFiles.copyRecursively(it, absOutputDir, replaceExisting = true) } } else { context.logger.log(project.name, 2, "No resources to copy for $sourceSet") diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/DependencyManager.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/DependencyManager.kt index 081f9846..74d591ea 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/DependencyManager.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/DependencyManager.kt @@ -175,13 +175,13 @@ class DependencyManager @Inject constructor(val executors: KobaltExecutors, * TODO: This should be private, everyone should be calling calculateDependencies(). */ fun transitiveClosure(dependencies : List, - dependencyFilter: DependencyFilter? = null, + filter: DependencyFilter = Filters.EXCLUDE_OPTIONAL_FILTER, requiredBy: String? = null): List { val result = arrayListOf() dependencies.forEach { dependency -> result.add(dependency) if (dependency.isMaven) { - val resolved = resolver.resolveToIds(dependency.id, null, dependencyFilter).map { create(it) } + val resolved = resolver.resolveToIds(dependency.id, null, filter).map { create(it) } result.addAll(resolved) } } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/KobaltMavenResolver.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/KobaltMavenResolver.kt index 46c43272..48b3e836 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/KobaltMavenResolver.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/KobaltMavenResolver.kt @@ -34,10 +34,12 @@ class KobaltMavenResolver @Inject constructor(val settings: KobaltSettings, fun isRangeVersion(id: String) = id.contains(",") } - fun resolveToArtifact(id: String, scope: Scope? = null, filter: DependencyFilter? = null) : Artifact + fun resolveToArtifact(id: String, scope: Scope? = null, + filter: DependencyFilter = Filters.EXCLUDE_OPTIONAL_FILTER) : Artifact = resolve(id, scope, filter).root.artifact - fun resolve(id: String, scope: Scope? = null, filter: DependencyFilter? = null, + fun resolve(id: String, scope: Scope? = null, + filter: DependencyFilter = Filters.EXCLUDE_OPTIONAL_FILTER, repos: List = emptyList()): DependencyResult { val dependencyRequest = DependencyRequest(createCollectRequest(id, scope, repos), filter) val result = system.resolveDependencies(session, dependencyRequest) @@ -46,10 +48,12 @@ class KobaltMavenResolver @Inject constructor(val settings: KobaltSettings, return result } - fun resolve(artifact: Artifact, scope: Scope? = null, filter: DependencyFilter? = null) + fun resolve(artifact: Artifact, scope: Scope? = null, + filter: DependencyFilter = Filters.EXCLUDE_OPTIONAL_FILTER) = resolve(artifactToId(artifact), scope, filter) - fun resolveToIds(id: String, scope: Scope? = null, filter: DependencyFilter? = null, + fun resolveToIds(id: String, scope: Scope? = null, + filter: DependencyFilter = Filters.EXCLUDE_OPTIONAL_FILTER, seen: HashSet = hashSetOf()) : List { val rr = resolve(id, scope, filter) val children = diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/JarUtils.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/JarUtils.kt index ff282ae0..0837012d 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/JarUtils.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/JarUtils.kt @@ -1,10 +1,8 @@ package com.beust.kobalt.misc -import com.beust.kobalt.Glob -import com.beust.kobalt.IFileSpec +import com.beust.kobalt.* import com.google.common.io.CharStreams import java.io.* -import java.nio.file.Paths import java.util.jar.JarEntry import java.util.jar.JarFile import java.util.jar.JarInputStream @@ -134,39 +132,3 @@ class JarUtils { } } -open class Direction(open val p: String) { - override fun toString() = path - fun isCurrentDir() = path == "./" - val path: String get() = - if (p.isEmpty()) "./" - else if (p.startsWith("/") || p.endsWith("/")) p - else p + "/" -} - -class IncludedFile(val fromOriginal: From, val toOriginal: To, val specs: List, - val expandJarFiles: Boolean = false) { - constructor(specs: List, expandJarFiles: Boolean = false) : this(From(""), To(""), specs, expandJarFiles) - fun from(s: String) = File(if (fromOriginal.isCurrentDir()) s else KFiles.joinDir(from, s)) - val from: String get() = fromOriginal.path.replace("\\", "/") - fun to(s: String) = File(if (toOriginal.isCurrentDir()) s else KFiles.joinDir(to, s)) - val to: String get() = toOriginal.path.replace("\\", "/") - override fun toString() = toString("IncludedFile", - "files - ", specs.map { it.toString() }, - "from", from, - "to", to) - - fun allFromFiles(directory: String? = null): List { - val result = arrayListOf() - specs.forEach { spec -> -// val fullDir = if (directory == null) from else KFiles.joinDir(directory, from) - spec.toFiles(directory, from).forEach { source -> - result.add(if (source.isAbsolute) source else File(source.path)) - } - } - return result.map { Paths.get(it.path).normalize().toFile()} - } -} - -class From(override val p: String) : Direction(p) - -class To(override val p: String) : Direction(p) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt index b668b97a..d5e4b443 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt @@ -4,6 +4,7 @@ import com.beust.kobalt.* import com.beust.kobalt.api.Kobalt import com.beust.kobalt.api.Project import com.beust.kobalt.maven.Md5 +import org.apache.commons.io.FileUtils import java.io.* import java.nio.file.Files import java.nio.file.Path @@ -11,6 +12,7 @@ import java.nio.file.Paths import java.nio.file.StandardCopyOption import java.util.jar.JarInputStream + class KFiles { /** * This actually returns a list of strings because in development mode, we are not pointing to a single @@ -194,69 +196,6 @@ class KFiles { } } - fun copyRecursively(from: File, to: File, replaceExisting: Boolean = true, deleteFirst: Boolean = false, - onError: (File, IOException) -> OnErrorAction = { _, exception -> throw exception }) { - // Need to wait until copyRecursively supports an overwrite: Boolean = false parameter - // Until then, wipe everything first - if (deleteFirst) to.deleteRecursively() -// to.mkdirs() - hackCopyRecursively(from, to, replaceExisting = replaceExisting, onError = onError) - } - - /** Private exception class, used to terminate recursive copying */ - private class TerminateException(file: File) : FileSystemException(file) {} - - /** - * Copy/pasted from kotlin/io/Utils.kt to add support for overwriting. - */ - private fun hackCopyRecursively(from: File, dst: File, - replaceExisting: Boolean, - onError: (File, IOException) -> OnErrorAction = - { _, exception -> throw exception } - ): Boolean { - if (!from.exists()) { - return onError(from, NoSuchFileException(file = from, reason = "The source file doesn't exist")) != - OnErrorAction.TERMINATE - } - try { - // We cannot break for loop from inside a lambda, so we have to use an exception here - for (src in from.walkTopDown().onFail { f, e -> - if (onError(f, e) == OnErrorAction.TERMINATE) throw TerminateException(f) - }) { - if (!src.exists()) { - if (onError(src, NoSuchFileException(file = src, reason = "The source file doesn't exist")) == - OnErrorAction.TERMINATE) - return false - } else { - val relPath = src.relativeTo(from) - val dstFile = File(KFiles.joinDir(dst.path, relPath.path)) - if (dstFile.exists() && !replaceExisting && !(src.isDirectory && dstFile.isDirectory)) { - if (onError(dstFile, FileAlreadyExistsException(file = src, - other = dstFile, - reason = "The destination file already exists")) == OnErrorAction.TERMINATE) - return false - } else if (src.isDirectory) { - dstFile.mkdirs() - } else { - if (Features.USE_TIMESTAMPS && dstFile.exists() && Md5.toMd5(src) == Md5.toMd5(dstFile)) { - kobaltLog(3, " Identical files, not copying $src to $dstFile") - } else { - val target = src.copyTo(dstFile, true) - if (target.length() != src.length()) { - if (onError(src, - IOException("src.length() != dst.length()")) == OnErrorAction.TERMINATE) - return false - } - } - } - } - } - return true - } catch (e: TerminateException) { - return false - } - } - /** * The build location for build scripts is .kobalt/build */ @@ -386,6 +325,29 @@ class KFiles { } val dotKobaltDir = File(KFiles.joinAndMakeDir(KFiles.KOBALT_DOT_DIR)) + + /** + * Turn the IncludedFiles into actual Files + */ + fun materializeIncludedFiles(project: Project, includedFiles: List) : List { + val result = includedFiles.fold(arrayListOf()) { files, includedFile: IncludedFile -> + val foundFiles = includedFile.allFromFiles(project.directory) + val absFiles = foundFiles.map { + File(KFiles.joinDir(project.directory, includedFile.from, it.path)) + } + files.addAll(absFiles) + files + } + return result + } + + fun copyRecursively(from: File, to: File, replaceExisting: Boolean = true, deleteFirst: Boolean = false) { +// fun copy(relativePath: String, sourceDir: File, targetDir: File) = +// sourceDir.resolve(relativePath).copyRecursively(targetDir.resolve(relativePath), overwrite = true) + if (from.isFile) FileUtils.copyFileToDirectory(from, to) + else FileUtils.copyDirectory(from, to) + } + } fun findRecursively(directory: File, function: Function1): List { 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 74266e62..26a59672 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,11 @@ import com.beust.kobalt.internal.ParallelLogger import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.Md5 import com.beust.kobalt.maven.PomGenerator -import com.beust.kobalt.misc.IncludedFile import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.benchmarkMillis import java.io.File +import java.nio.file.Paths import javax.inject.Inject import javax.inject.Singleton @@ -107,14 +107,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana context.variant.archiveName(project, null, ".jar")) // Turn the IncludedFiles into actual Files - val inputFiles = allIncludedFiles.fold(arrayListOf()) { files, includedFile: IncludedFile -> - val foundFiles = includedFile.allFromFiles(project.directory) - val absFiles = foundFiles.map { - File(KFiles.joinDir(project.directory, includedFile.from, it.path)) - } - files.addAll(absFiles) - files - } + val inputFiles = KFiles.materializeIncludedFiles(project, allIncludedFiles) val inMd5 = Md5.toMd5Directories(inputFiles) val outMd5 = Md5.toMd5Directories(outputFiles) @@ -221,10 +214,24 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana val buildDir = project.projectProperties.getString(LIBS_DIR) val buildDirFile = File(buildDir) if (buildDirFile.exists()) { - context.logger.log(project.name, 1, "Installing from $buildDir to ${config.libDir}") - val toDir = KFiles.makeDir(config.libDir) - KFiles.copyRecursively(buildDirFile, toDir, deleteFirst = true) + if (config.includedFiles.isEmpty()) { + context.logger.log(project.name, 1, " Installing from $buildDir to ${config.libDir}") + val toDir = KFiles.makeDir(config.libDir) + File(buildDir).copyRecursively(toDir, overwrite = true) + } else { + config.includedFiles.forEach { inf -> + val target = inf.to + val targetFile = File(target) + targetFile.deleteRecursively() + targetFile.mkdirs() + val files = KFiles.materializeIncludedFiles(project, listOf(inf)) + files.forEach { + context.logger.log(project.name, 1, " Installing $it to $targetFile") + KFiles.copyRecursively(it, targetFile, true) + } + } + } } return TaskResult() @@ -242,7 +249,7 @@ fun Project.install(init: InstallConfig.() -> Unit) { } } -class InstallConfig(var libDir : String = "libs") +class InstallConfig(var libDir : String = "libs") : IncludeFromTo() @Directive fun Project.assemble(init: PackageConfig.(p: Project) -> Unit): PackageConfig = let { @@ -292,14 +299,14 @@ class PackageConfig(val project: Project) : AttributeHolder { name = "${project.name}-${project.version}-sources.jar" project.sourceDirectories.forEach { if (File(project.directory, it).exists()) { - include(from(it), to(""), glob("**")) + include(From(it), To(""), glob("**")) } } } jar { name = "${project.name}-${project.version}-javadoc.jar" val fromDir = KFiles.joinDir(project.buildDirectory, JvmCompilerPlugin.DOCS_DIRECTORY) - include(from(fromDir), to(""), glob("**")) + include(From(fromDir), To(""), glob("**")) } mainJarAttributes.forEach { @@ -331,3 +338,19 @@ class Pom { } +fun main(args: Array) { + val realSource = File("/tmp/a") + val sourceDir = File(realSource, "b").apply { mkdirs() } + val from = File(sourceDir, "foo").apply { writeText("I'm a file") } + val to = File("/tmp/to").apply { + deleteRecursively() + mkdirs() + } + val sourcePath = Paths.get(realSource.toURI()) + val targetPath = Paths.get(to.toURI()) +// Files.walkFileTree(sourcePath, KFiles.Companion.CopyFileVisitor(targetPath)) + + if (! to.isDirectory) { + throw AssertionError("Should be a directory") + } +} 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 295224c7..6556e033 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt @@ -1,8 +1,6 @@ package com.beust.kobalt.plugin.packaging -import com.beust.kobalt.ArchiveGenerator -import com.beust.kobalt.IFileSpec -import com.beust.kobalt.JarGenerator +import com.beust.kobalt.* import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Project @@ -10,10 +8,7 @@ import com.beust.kobalt.archive.Archives import com.beust.kobalt.archive.Zip import com.beust.kobalt.internal.ParallelLogger import com.beust.kobalt.maven.DependencyManager -import com.beust.kobalt.misc.From -import com.beust.kobalt.misc.IncludedFile import com.beust.kobalt.misc.KFiles -import com.beust.kobalt.misc.To import com.google.inject.Inject import java.io.File import java.io.OutputStream 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 e15c61ae..f70ad9a4 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/ZipGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/ZipGenerator.kt @@ -1,6 +1,7 @@ package com.beust.kobalt.plugin.packaging import com.beust.kobalt.ArchiveGenerator +import com.beust.kobalt.IncludedFile import com.beust.kobalt.JarGenerator import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Project @@ -8,7 +9,6 @@ import com.beust.kobalt.archive.Archives import com.beust.kobalt.archive.Zip import com.beust.kobalt.internal.ParallelLogger import com.beust.kobalt.maven.DependencyManager -import com.beust.kobalt.misc.IncludedFile import com.google.inject.Inject class ZipGenerator @Inject constructor(val dependencyManager: DependencyManager, val kobaltLog: ParallelLogger) diff --git a/src/test/kotlin/com/beust/kobalt/IncludeExcludeTest.kt b/src/test/kotlin/com/beust/kobalt/IncludeExcludeTest.kt index 81969a1c..1501c556 100644 --- a/src/test/kotlin/com/beust/kobalt/IncludeExcludeTest.kt +++ b/src/test/kotlin/com/beust/kobalt/IncludeExcludeTest.kt @@ -1,8 +1,5 @@ package com.beust.kobalt -import com.beust.kobalt.misc.From -import com.beust.kobalt.misc.IncludedFile -import com.beust.kobalt.misc.To import org.testng.Assert import org.testng.annotations.BeforeClass import org.testng.annotations.DataProvider diff --git a/src/test/kotlin/com/beust/kobalt/maven/FileSpecTest.kt b/src/test/kotlin/com/beust/kobalt/maven/FileSpecTest.kt index f0e8bc52..11e524af 100644 --- a/src/test/kotlin/com/beust/kobalt/maven/FileSpecTest.kt +++ b/src/test/kotlin/com/beust/kobalt/maven/FileSpecTest.kt @@ -1,11 +1,7 @@ package com.beust.kobalt.maven -import com.beust.kobalt.IFileSpec -import com.beust.kobalt.homeDir -import com.beust.kobalt.misc.From -import com.beust.kobalt.misc.IncludedFile +import com.beust.kobalt.* import com.beust.kobalt.misc.KFiles -import com.beust.kobalt.misc.To import org.testng.annotations.Test @Test(enabled = false) diff --git a/src/test/kotlin/com/beust/kobalt/misc/IncludedFileTest.kt b/src/test/kotlin/com/beust/kobalt/misc/IncludedFileTest.kt index 52a9eb69..2f98b54e 100644 --- a/src/test/kotlin/com/beust/kobalt/misc/IncludedFileTest.kt +++ b/src/test/kotlin/com/beust/kobalt/misc/IncludedFileTest.kt @@ -1,9 +1,14 @@ package com.beust.kobalt.misc +import com.beust.kobalt.From import com.beust.kobalt.IFileSpec +import com.beust.kobalt.IncludedFile +import com.beust.kobalt.To +import org.assertj.core.api.Assertions.assertThat import org.testng.Assert import org.testng.annotations.Test import java.io.File +import java.nio.file.Files @Test class IncludedFileTest { @@ -14,4 +19,28 @@ class IncludedFileTest { Assert.assertTrue(it.exists(), "Should exist: $it") } } + + fun shouldRecursivelyCopy() { + val TEXT = "I'm a file" + val ROOT = Files.createTempDirectory("kobalt-test").toFile() + val from = File(ROOT, "from") + File(from, "a/b").apply { mkdirs() } + val file = File("/a/b/foo") + File(from, file.path).apply { writeText(TEXT) } + val to = File(ROOT, "to").apply { + deleteRecursively() + mkdirs() + } + + val targetFile = File(to, file.path) + assertThat(targetFile).doesNotExist() + + KFiles.copyRecursively(from, to) + + assertThat(to).isDirectory() + with(targetFile) { + assertThat(this).exists() + assertThat(this.readText()).isEqualTo(TEXT) + } + } } diff --git a/src/test/kotlin/com/beust/kobalt/misc/JarUtilsTest.kt b/src/test/kotlin/com/beust/kobalt/misc/JarUtilsTest.kt index b4d1dc66..1cd3c4e2 100644 --- a/src/test/kotlin/com/beust/kobalt/misc/JarUtilsTest.kt +++ b/src/test/kotlin/com/beust/kobalt/misc/JarUtilsTest.kt @@ -1,6 +1,9 @@ package com.beust.kobalt.misc +import com.beust.kobalt.From import com.beust.kobalt.IFileSpec +import com.beust.kobalt.IncludedFile +import com.beust.kobalt.To import org.testng.Assert import org.testng.annotations.Test import javax.inject.Inject