Skip to content

Commit

Permalink
Fix glob pattern matching not working with workingPath
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj-hrivnak committed Oct 10, 2024
1 parent e3d1361 commit 50d30b0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import teksturepako.pakku.api.data.ConfigFile
import teksturepako.pakku.api.data.Dirs.cacheDir
import teksturepako.pakku.api.data.LockFile
import teksturepako.pakku.api.data.json
import teksturepako.pakku.api.data.workingPath
import teksturepako.pakku.api.http.Http
import teksturepako.pakku.api.overrides.OverrideType
import teksturepako.pakku.api.overrides.ProjectOverride
Expand Down Expand Up @@ -137,7 +138,7 @@ sealed class RuleContext(open val workingSubDir: String)
{
fun export(overridesDir: String? = type.folderName): RuleResult
{
val inputPath = Path(path)
val inputPath = Path(workingPath, path)
val outputPath = overridesDir?.let { getPath(it, path) } ?: getPath(path)

val message = "export $type '$inputPath' to '$outputPath'"
Expand Down Expand Up @@ -247,9 +248,8 @@ sealed class RuleContext(open val workingSubDir: String)
{
if (!project.redistributable && !force) return error(NotRedistributable(project))

val projectFile =
project.getLatestFile(Provider.providers - excludedProviders)
?: return error(NoFiles(project, lockFile))
val projectFile = project.getLatestFile(Provider.providers - excludedProviders)
?: return error(NoFiles(project, lockFile))

val result = onExport(
// Creates a callback to download the file lazily.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import teksturepako.pakku.api.projects.ProjectSide
import teksturepako.pakku.api.projects.ProjectType
import teksturepako.pakku.api.projects.UpdateStrategy
import teksturepako.pakku.io.*
import kotlin.io.path.Path

/**
* A config file (`pakku.json`) is a file used by the user to configure properties needed for modpack export.
Expand Down Expand Up @@ -97,9 +98,9 @@ data class ConfigFile(
this.overrides.clear()
}

fun getAllOverrides(): List<String> = filterOverrides(this.overrides.expandWithGlob())
fun getAllServerOverrides(): List<String> = filterOverrides(this.serverOverrides.expandWithGlob())
fun getAllClientOverrides(): List<String> = filterOverrides(this.clientOverrides.expandWithGlob())
fun getAllOverrides(): List<String> = filterOverrides(this.overrides.expandWithGlob(Path(workingPath)))
fun getAllServerOverrides(): List<String> = filterOverrides(this.serverOverrides.expandWithGlob(Path(workingPath)))
fun getAllClientOverrides(): List<String> = filterOverrides(this.clientOverrides.expandWithGlob(Path(workingPath)))

// -- PROJECTS --

Expand Down
24 changes: 13 additions & 11 deletions src/commonMain/kotlin/teksturepako/pakku/io/Glob.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package teksturepako.pakku.io

import teksturepako.pakku.debug
import teksturepako.pakku.debugIf
import java.io.File
import java.nio.file.FileSystems
import java.nio.file.Path
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.Path
import kotlin.io.path.PathWalkOption
import kotlin.io.path.walk
import kotlin.io.path.*

@OptIn(ExperimentalPathApi::class)
fun Path.listDirectoryEntriesRecursive(glob: String): Sequence<Path>
{
val matcher = FileSystems.getDefault().getPathMatcher("glob:${glob.removePrefix("./")}")
return walk(PathWalkOption.INCLUDE_DIRECTORIES).filter { matcher.matches(it) }
val globPattern = "glob:${this.invariantSeparatorsPathString}/${glob.removePrefix("./")}"
val matcher = FileSystems.getDefault().getPathMatcher(globPattern)

return this.walk(PathWalkOption.INCLUDE_DIRECTORIES)
.filter { matcher.matches(it) }
.map { Path(it.absolutePathString().removePrefix(this.absolutePathString()).removePrefix(File.separator)) }
}

fun List<String>.expandWithGlob() = fold(listOf<String>()) { acc, glob ->
fun List<String>.expandWithGlob(inputPath: Path) = fold(listOf<String>()) { acc, glob ->
if (glob.startsWith("!"))
{
acc - Path("").listDirectoryEntriesRecursive(glob.removePrefix("!")).map { it.toString() }.toSet()
acc - inputPath.listDirectoryEntriesRecursive(glob.removePrefix("!")).map { it.toString() }.toSet()
}
else
{
acc + Path("").listDirectoryEntriesRecursive(glob).map { it.toString() }
acc + inputPath.listDirectoryEntriesRecursive(glob).map { it.toString() }
}
}.debug(::println)
}.debugIf({ it.isNotEmpty() }) { println("expandWithGlob = $it") }.toList()

0 comments on commit 50d30b0

Please sign in to comment.