-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
14 changed files
with
254 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,63 @@ | ||
package teksturepako.pakku.io | ||
|
||
import com.github.michaelbull.result.get | ||
import com.github.michaelbull.result.runCatching | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import teksturepako.pakku.debug | ||
import teksturepako.pakku.debugIf | ||
import java.io.File | ||
import java.nio.file.FileSystems | ||
import java.nio.file.Path | ||
import java.nio.file.PathMatcher | ||
import kotlin.io.path.* | ||
|
||
@OptIn(ExperimentalPathApi::class) | ||
fun Path.listDirectoryEntriesRecursive(glob: String): Sequence<Path> | ||
{ | ||
val globPattern = "glob:${this.invariantSeparatorsPathString}/${glob.removePrefix("./")}" | ||
val matcher = FileSystems.getDefault().getPathMatcher(globPattern) | ||
suspend fun Path.walk(globPatterns: List<String>): Sequence<Pair<Path, Boolean>> = withContext(Dispatchers.IO) { | ||
val matchers: List<Pair<PathMatcher, Boolean>> = globPatterns.mapNotNull { input -> | ||
val negating = input.startsWith("!") | ||
val glob = (if (negating) input.removePrefix("!") else input).removePrefix("./") | ||
|
||
val globPattern = "glob:${this@walk.invariantSeparatorsPathString}/$glob" | ||
runCatching { FileSystems.getDefault().getPathMatcher(globPattern) to negating }.get() | ||
} | ||
|
||
return@withContext this@walk.walk(PathWalkOption.INCLUDE_DIRECTORIES) | ||
.mapNotNull { path -> | ||
val matcher = matchers.filter { (matcher) -> matcher.matches(path) } | ||
|
||
return this.walk(PathWalkOption.INCLUDE_DIRECTORIES) | ||
.filter { matcher.matches(it) } | ||
.map { Path(it.absolutePathString().removePrefix(this.absolutePathString()).removePrefix(File.separator)) } | ||
if (matcher.isEmpty()) return@mapNotNull null | ||
|
||
matcher to path | ||
}.map { (matchers, path) -> | ||
val resultPath = Path(path.absolutePathString().removePrefix(this@walk.absolutePathString()).removePrefix(File.separator)) | ||
val negating = matchers.any { it.second } | ||
|
||
resultPath to negating | ||
} | ||
} | ||
|
||
fun List<String>.expandWithGlob(inputPath: Path) = fold(listOf<String>()) { acc, glob -> | ||
if (glob.startsWith("!")) | ||
suspend fun List<String>.expandWithGlob(inputPath: Path): List<String> | ||
{ | ||
val paths = mutableSetOf<String>() | ||
|
||
val walk = inputPath.walk(this).toList().debug { | ||
for ((path, negating) in it) | ||
{ | ||
println("${if (negating) "!" else ""}$path") | ||
} | ||
} | ||
|
||
for ((path, _) in walk) | ||
{ | ||
acc - inputPath.listDirectoryEntriesRecursive(glob.removePrefix("!")).map { it.toString() }.toSet() | ||
paths += path.toString() | ||
} | ||
else | ||
|
||
for ((path, negating) in walk) | ||
{ | ||
acc + inputPath.listDirectoryEntriesRecursive(glob).map { it.toString() } | ||
if (negating) paths -= path.toString() | ||
} | ||
}.debugIf({ it.isNotEmpty() }) { println("expandWithGlob = $it") } | ||
|
||
return paths.toList().debugIf({ it.isNotEmpty() }) { println("expandWithGlob = $it") } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package teksturepako.pakku | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import teksturepako.pakku.api.data.generatePakkuId | ||
import teksturepako.pakku.api.data.workingPath | ||
import kotlin.io.path.* | ||
import kotlin.test.AfterTest | ||
import kotlin.test.BeforeTest | ||
|
||
open class PakkuTest | ||
{ | ||
private var testName: String = "" | ||
|
||
protected open suspend fun `on-set-up`() | ||
{ | ||
} | ||
|
||
protected open val teardown = true | ||
|
||
protected fun createTestFile(vararg path: String) | ||
{ | ||
val file = Path(workingPath, *path) | ||
runCatching { file.createParentDirectories() } | ||
runCatching { file.createFile() } | ||
} | ||
|
||
protected fun createTestDir(vararg path: String) | ||
{ | ||
val dir = Path(workingPath, *path) | ||
runCatching { dir.createParentDirectories() } | ||
runCatching { dir.createDirectory() } | ||
} | ||
|
||
@BeforeTest | ||
fun `set-up-test`() | ||
{ | ||
testName = this::class.simpleName ?: generatePakkuId() | ||
|
||
println("Setting up test: $testName") | ||
|
||
workingPath = "./build/test/$testName" | ||
runCatching { Path("./build/test/$testName").createParentDirectories() } | ||
runCatching { Path("./build/test/$testName").createDirectory() } | ||
|
||
runBlocking { this@PakkuTest.`on-set-up`() } | ||
} | ||
|
||
@AfterTest | ||
@OptIn(ExperimentalPathApi::class) | ||
fun `tear-down-test`() | ||
{ | ||
if (!teardown) return | ||
|
||
workingPath = "." | ||
|
||
println("Tearing down test: $testName") | ||
|
||
runCatching { Path("./build/test/$testName").deleteRecursively() } | ||
} | ||
} |
6 changes: 0 additions & 6 deletions
6
src/commonTest/kotlin/teksturepako/pakku/api/actions/export/TestExport.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.