mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 16:28:12 -07:00
Fix source jar file.
This commit is contained in:
parent
2f840a44fd
commit
986f1b01be
7 changed files with 49 additions and 30 deletions
|
@ -10,10 +10,10 @@ import java.nio.file.attribute.BasicFileAttributes
|
||||||
* and GlobSpec (a spec defined by a glob, e.g. ** slash *Test.class)
|
* and GlobSpec (a spec defined by a glob, e.g. ** slash *Test.class)
|
||||||
*/
|
*/
|
||||||
sealed class IFileSpec {
|
sealed class IFileSpec {
|
||||||
abstract fun toFiles(filePath: String, excludes: List<Glob> = emptyList<Glob>()): List<File>
|
abstract fun toFiles(baseDir: String?, filePath: String, excludes: List<Glob> = emptyList<Glob>()): List<File>
|
||||||
|
|
||||||
class FileSpec(val spec: String) : IFileSpec() {
|
class FileSpec(val spec: String) : IFileSpec() {
|
||||||
override public fun toFiles(filePath: String, excludes: List<Glob>) = listOf(File(spec))
|
override public fun toFiles(baseDir: String?, filePath: String, excludes: List<Glob>) = listOf(File(spec))
|
||||||
|
|
||||||
override public fun toString() = spec
|
override public fun toString() = spec
|
||||||
}
|
}
|
||||||
|
@ -30,14 +30,14 @@ sealed class IFileSpec {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (includeMatchers.matches(rel)) {
|
if (includeMatchers.matches(rel)) {
|
||||||
log(2, "Including ${rel.toFile().absolutePath}")
|
log(2, "Including ${rel.toFile().path}")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
log(2, "Excluding ${rel.toFile()} (not matching any include pattern")
|
log(2, "Excluding ${rel.toFile()} (not matching any include pattern")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun toFiles(filePath: String, excludes: List<Glob>): List<File> {
|
override fun toFiles(baseDir: String?, filePath: String, excludes: List<Glob>): List<File> {
|
||||||
val result = arrayListOf<File>()
|
val result = arrayListOf<File>()
|
||||||
val includes = Glob(*spec.toTypedArray())
|
val includes = Glob(*spec.toTypedArray())
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ sealed class IFileSpec {
|
||||||
Files.walkFileTree(Paths.get(filePath), object : SimpleFileVisitor<Path>() {
|
Files.walkFileTree(Paths.get(filePath), object : SimpleFileVisitor<Path>() {
|
||||||
override public fun visitFile(path: Path, attrs: BasicFileAttributes): FileVisitResult {
|
override public fun visitFile(path: Path, attrs: BasicFileAttributes): FileVisitResult {
|
||||||
val rel = Paths.get(filePath).relativize(path)
|
val rel = Paths.get(filePath).relativize(path)
|
||||||
if (isIncluded(includes, excludes, rel)) {
|
if (isIncluded(includes, excludes, path)) {
|
||||||
result.add(rel.toFile())
|
result.add(rel.toFile())
|
||||||
}
|
}
|
||||||
return FileVisitResult.CONTINUE
|
return FileVisitResult.CONTINUE
|
||||||
|
|
|
@ -24,10 +24,10 @@ abstract class GenericTestRunner : ITestRunnerContributor {
|
||||||
else 0
|
else 0
|
||||||
|
|
||||||
protected fun findTestClasses(project: Project, testConfig: TestConfig): List<String> {
|
protected fun findTestClasses(project: Project, testConfig: TestConfig): List<String> {
|
||||||
val path = KFiles.joinDir(project.directory, project.buildDirectory, KFiles.TEST_CLASSES_DIR)
|
val path = KFiles.joinDir(project.buildDirectory, KFiles.TEST_CLASSES_DIR)
|
||||||
|
|
||||||
val result = IFileSpec.GlobSpec(toClassPaths(testConfig.testIncludes))
|
val result = IFileSpec.GlobSpec(toClassPaths(testConfig.testIncludes))
|
||||||
.toFiles(path, testConfig.testExcludes.map {
|
.toFiles(project.directory, path, testConfig.testExcludes.map {
|
||||||
Glob(it)
|
Glob(it)
|
||||||
}).map {
|
}).map {
|
||||||
it.toString().replace("/", ".").replace("\\", ".").replace(".class", "")
|
it.toString().replace("/", ".").replace("\\", ".").replace(".class", "")
|
||||||
|
|
|
@ -36,9 +36,16 @@ public class JarUtils {
|
||||||
expandJarFiles: Boolean, onError: (Exception) -> Unit = DEFAULT_HANDLER) {
|
expandJarFiles: Boolean, onError: (Exception) -> Unit = DEFAULT_HANDLER) {
|
||||||
val allFiles = file.allFromFiles(directory)
|
val allFiles = file.allFromFiles(directory)
|
||||||
allFiles.forEach { relSource ->
|
allFiles.forEach { relSource ->
|
||||||
val source =
|
val source = relSource
|
||||||
if (relSource.isAbsolute) relSource
|
|
||||||
else File(KFiles.joinDir(directory, file.from, relSource.path))
|
val entryFile = if (File(source.path).isAbsolute) File(source.path)
|
||||||
|
else if (! file.fromOriginal.isCurrentDir()) File(KFiles.joinDir(file.from, source.path))
|
||||||
|
else File(source.path)
|
||||||
|
|
||||||
|
if (!entryFile.exists()) {
|
||||||
|
throw AssertionError("File should exist: $entryFile")
|
||||||
|
}
|
||||||
|
|
||||||
if (source.isDirectory) {
|
if (source.isDirectory) {
|
||||||
log(2, "Writing contents of directory $source")
|
log(2, "Writing contents of directory $source")
|
||||||
|
|
||||||
|
@ -61,22 +68,21 @@ public class JarUtils {
|
||||||
} else {
|
} else {
|
||||||
if (expandJarFiles && source.name.endsWith(".jar") && ! source.path.contains("resources")) {
|
if (expandJarFiles && source.name.endsWith(".jar") && ! source.path.contains("resources")) {
|
||||||
log(2, "Writing contents of jar file $source")
|
log(2, "Writing contents of jar file $source")
|
||||||
val stream = JarInputStream(FileInputStream(source))
|
val stream = JarInputStream(FileInputStream(entryFile))
|
||||||
var entry = stream.nextEntry
|
var entry = stream.nextEntry
|
||||||
while (entry != null) {
|
while (entry != null) {
|
||||||
if (!entry.isDirectory && !KFiles.isExcluded(entry.name, DEFAULT_JAR_EXCLUDES)) {
|
if (!entry.isDirectory && !KFiles.isExcluded(entry.name, DEFAULT_JAR_EXCLUDES)) {
|
||||||
val ins = JarFile(source).getInputStream(entry)
|
val ins = JarFile(entryFile).getInputStream(entry)
|
||||||
addEntry(ins, JarEntry(entry), outputStream, onError)
|
addEntry(ins, JarEntry(entry), outputStream, onError)
|
||||||
}
|
}
|
||||||
entry = stream.nextEntry
|
entry = stream.nextEntry
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val entry = JarEntry(file.to + relSource.path.replace("\\", "/"))
|
val fixedSource = relSource.path.replace("\\", "/")
|
||||||
|
val entryFileName = if (file.toOriginal.isCurrentDir()) fixedSource
|
||||||
|
else file.to + fixedSource
|
||||||
|
val entry = JarEntry(entryFileName)
|
||||||
entry.time = source.lastModified()
|
entry.time = source.lastModified()
|
||||||
val entryFile = source
|
|
||||||
if (!entryFile.exists()) {
|
|
||||||
throw AssertionError("File should exist: $entryFile")
|
|
||||||
}
|
|
||||||
addEntry(FileInputStream(entryFile), entry, outputStream, onError)
|
addEntry(FileInputStream(entryFile), entry, outputStream, onError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -143,8 +149,12 @@ public class JarUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
open class Direction(open val p: String) {
|
open class Direction(open val p: String) {
|
||||||
override public fun toString() = path
|
override fun toString() = path
|
||||||
public val path: String get() = if (p.isEmpty() or p.endsWith("/")) p else p + "/"
|
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>) {
|
class IncludedFile(val fromOriginal: From, val toOriginal: To, val specs: List<IFileSpec>) {
|
||||||
|
@ -159,8 +169,8 @@ class IncludedFile(val fromOriginal: From, val toOriginal: To, val specs: List<I
|
||||||
fun allFromFiles(directory: String? = null): List<File> {
|
fun allFromFiles(directory: String? = null): List<File> {
|
||||||
val result = arrayListOf<File>()
|
val result = arrayListOf<File>()
|
||||||
specs.forEach { spec ->
|
specs.forEach { spec ->
|
||||||
val fullDir = if (directory == null) from else KFiles.joinDir(directory, from)
|
// val fullDir = if (directory == null) from else KFiles.joinDir(directory, from)
|
||||||
spec.toFiles(fullDir).forEach { source ->
|
spec.toFiles(directory, from).forEach { source ->
|
||||||
result.add(if (source.isAbsolute) source else File(source.path))
|
result.add(if (source.isAbsolute) source else File(source.path))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager)
|
||||||
filesNotExcluded.forEach {
|
filesNotExcluded.forEach {
|
||||||
fileSpecs.add(IFileSpec.FileSpec(it.path.toString().substring(prefixPath.toString().length + 1)))
|
fileSpecs.add(IFileSpec.FileSpec(it.path.toString().substring(prefixPath.toString().length + 1)))
|
||||||
}
|
}
|
||||||
result.add(IncludedFile(From(prefixPath.toString() + "/"), To(""), fileSpecs))
|
result.add(IncludedFile(From(project.directory + "/" + prefixPath.toString() + "/"), To(""), fileSpecs))
|
||||||
|
|
||||||
// Resources, if applicable
|
// Resources, if applicable
|
||||||
context.variant.resourceDirectories(project).forEach {
|
context.variant.resourceDirectories(project).forEach {
|
||||||
|
|
|
@ -48,9 +48,9 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
|
||||||
files.forEach { includedFile ->
|
files.forEach { includedFile ->
|
||||||
val includedSpecs = arrayListOf<IFileSpec>()
|
val includedSpecs = arrayListOf<IFileSpec>()
|
||||||
includedFile.specs.forEach { spec ->
|
includedFile.specs.forEach { spec ->
|
||||||
val fromPath = directory + "/" + includedFile.from
|
val fromPath = includedFile.from
|
||||||
if (File(fromPath).exists()) {
|
if (File(fromPath).exists()) {
|
||||||
spec.toFiles(fromPath).forEach { file ->
|
spec.toFiles(directory, fromPath).forEach { file ->
|
||||||
val fullFile = File(fromPath, file.path)
|
val fullFile = File(fromPath, file.path)
|
||||||
if (! fullFile.exists()) {
|
if (! fullFile.exists()) {
|
||||||
throw AssertionError("File should exist: $fullFile")
|
throw AssertionError("File should exist: $fullFile")
|
||||||
|
@ -245,7 +245,7 @@ class PackageConfig(val project: Project) : AttributeHolder {
|
||||||
jar {
|
jar {
|
||||||
name = "${project.name}-${project.version}-sources.jar"
|
name = "${project.name}-${project.version}-sources.jar"
|
||||||
project.sourceDirectories.forEach {
|
project.sourceDirectories.forEach {
|
||||||
include(from(it), to(""), glob("src/**"))
|
include(from(project.directory + "/" + it), to(""), glob("**"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
jar {
|
jar {
|
||||||
|
|
|
@ -6,11 +6,8 @@ import com.beust.kobalt.maven.DependencyManager
|
||||||
import com.google.inject.Inject
|
import com.google.inject.Inject
|
||||||
|
|
||||||
class ZipGenerator @Inject constructor(val dependencyManager: DependencyManager){
|
class ZipGenerator @Inject constructor(val dependencyManager: DependencyManager){
|
||||||
fun findIncludedFiles(project: Project, context: KobaltContext, zip: Zip)
|
|
||||||
= PackagingPlugin.findIncludedFiles(project.directory, zip.includedFiles, zip.excludes)
|
|
||||||
|
|
||||||
fun generateZip(project: Project, context: KobaltContext, zip: Zip) {
|
fun generateZip(project: Project, context: KobaltContext, zip: Zip) {
|
||||||
val allFiles = findIncludedFiles(project, context, zip)
|
val allFiles = PackagingPlugin.findIncludedFiles(project.directory, zip.includedFiles, zip.excludes)
|
||||||
PackagingPlugin.generateArchive(project, context, zip.name, ".zip", allFiles)
|
PackagingPlugin.generateArchive(project, context, zip.name, ".zip", allFiles)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package com.beust.kobalt
|
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.Assert
|
||||||
import org.testng.annotations.BeforeClass
|
import org.testng.annotations.BeforeClass
|
||||||
import org.testng.annotations.DataProvider
|
import org.testng.annotations.DataProvider
|
||||||
|
@ -45,7 +48,16 @@ class IncludeExcludeTest : KobaltTest() {
|
||||||
@Test(dataProvider = "dp")
|
@Test(dataProvider = "dp")
|
||||||
fun shouldInclude(root: File, includedSpec: List<String>, excludedSpec: List<String>, expectedFiles: List<String>) {
|
fun shouldInclude(root: File, includedSpec: List<String>, excludedSpec: List<String>, expectedFiles: List<String>) {
|
||||||
val g = IFileSpec.GlobSpec(includedSpec)
|
val g = IFileSpec.GlobSpec(includedSpec)
|
||||||
val files = g.toFiles(root.path, excludedSpec.map { Glob(it) })
|
val files = g.toFiles("", root.path, excludedSpec.map { Glob(it) })
|
||||||
Assert.assertEquals(files.map { it.name }, expectedFiles)
|
Assert.assertEquals(files.map { it.name }, expectedFiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
private fun f() {
|
||||||
|
val spec = IFileSpec.GlobSpec("src/**")
|
||||||
|
val files = spec.toFiles(homeDir("kotlin/kobalt"), "src/main/kotlin")
|
||||||
|
val inc = IncludedFile(From("src/main/kotlin"), To(""), listOf(IFileSpec.GlobSpec("**")))
|
||||||
|
// val files = inc.allFromFiles(homeDir("kotlin/kobalt"))
|
||||||
|
println("FILES: " + files)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue