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

include() for install{}.

This commit is contained in:
Cedric Beust 2017-04-04 10:49:48 -07:00
parent 789c969a9b
commit 886b7a4bfa
18 changed files with 214 additions and 191 deletions

View file

@ -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

View file

@ -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<IncludedFile>()
@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)))
}
}

View file

@ -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<IFileSpec>,
val expandJarFiles: Boolean = false) {
constructor(specs: List<IFileSpec>, 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<File> {
val result = arrayListOf<File>()
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)

View file

@ -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

View file

@ -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<Glob>()
@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<IncludedFile>()
@Directive
open val attributes = arrayListOf(Pair("Manifest-Version", "1.0"))
override fun addAttribute(k: String, v: String) {
attributes.add(Pair(k, v))
}
}

View file

@ -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")

View file

@ -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<IClasspathDependency>,
dependencyFilter: DependencyFilter? = null,
filter: DependencyFilter = Filters.EXCLUDE_OPTIONAL_FILTER,
requiredBy: String? = null): List<IClasspathDependency> {
val result = arrayListOf<IClasspathDependency>()
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)
}
}

View file

@ -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<String> = 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<String> = hashSetOf<String>()) : List<String> {
val rr = resolve(id, scope, filter)
val children =

View file

@ -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<IFileSpec>,
val expandJarFiles: Boolean = false) {
constructor(specs: List<IFileSpec>, 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<File> {
val result = arrayListOf<File>()
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)

View file

@ -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<IncludedFile>) : List<File> {
val result = includedFiles.fold(arrayListOf<File>()) { 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<String, Boolean>): List<String> {