Skip to content

Commit

Permalink
fix: use correct relative path when generating tags
Browse files Browse the repository at this point in the history
Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
  • Loading branch information
Ryex committed Oct 1, 2024
1 parent 3b67c47 commit 83b34ee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
10 changes: 8 additions & 2 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ func PathIsSub(parent string, sub string) (bool, error) {
}

var (
PackJSONPathRegex = regexp.MustCompile(`^res://packs/([\w\-. ]+).json`)
IDTrimPrefixRegex = regexp.MustCompile(`^([\w\-. ]+)/`)
PackJSONPathRegex = regexp.MustCompile(`^res://packs/([^/]+).json`)
IDTrimPrefixRegex = regexp.MustCompile(`^([^/]+)/`)
)

func NormalizeResourcePath(resPath string) string {
Expand All @@ -264,3 +264,9 @@ func NormalizeResourcePath(resPath string) string {
path = IDTrimPrefixRegex.ReplaceAllString(path, "")
return filepath.Clean(path)
}

func CleanRelativeResourcePath(path string) string {
path = strings.TrimPrefix(path, "res://packs/")
path = IDTrimPrefixRegex.ReplaceAllString(path, "")
return path
}
2 changes: 1 addition & 1 deletion pkg/ddpackage/auto_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (p *Package) generateTags(generator *GenerateTags, pcb func(p float64)) {
if fi.IsTaggable() {
tagsMap := generator.TagsFromPath(fi.CalcRelPath())
for tag, sets := range tagsMap {
p.Tags().Tag(tag, fi.ResPath)
p.Tags().Tag(tag, fi.RelPath)
for _, set := range sets.AsSlice() {
p.Tags().AddTagToSet(set, tag)
}
Expand Down
18 changes: 12 additions & 6 deletions pkg/structures/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func (pt *PackageTags) Tag(tag string, resources ...string) {
s = NewSet[string]()
pt.Tags[tag] = s
}
s.AddM(resources...)
relPaths := utils.Map(resources, utils.CleanRelativeResourcePath)
s.AddM(relPaths...)
}

func (pt *PackageTags) AllTags() []string {
Expand All @@ -57,17 +58,19 @@ func (pt *PackageTags) AllSets() []string {
func (pt *PackageTags) Untag(tag string, resources ...string) {
s, ok := pt.Tags[tag]
if ok {
s.RemoveM(resources...)
relPaths := utils.Map(resources, utils.CleanRelativeResourcePath)
s.RemoveM(relPaths...)
}
}

func (pt *PackageTags) Retag(resource string, tags ...string) {
ts := SetFrom(tags)
relPath := utils.CleanRelativeResourcePath(resource)
for tag := range pt.Tags {
if ts.Has(tag) {
pt.Tags[tag].Add(resource)
pt.Tags[tag].Add(relPath)
} else {
pt.Tags[tag].Remove(resource)
pt.Tags[tag].Remove(relPath)
}
}
}
Expand Down Expand Up @@ -126,7 +129,9 @@ func (pt *PackageTags) DeleteSet(set string) {

func (pt *PackageTags) TagsFor(resources ...string) *Set[string] {
res := NewSet[string]()
for i, resource := range resources {

relPaths := utils.Map(resources, utils.CleanRelativeResourcePath)
for i, resource := range relPaths {
cur := NewSet[string]()
for tag, s := range pt.Tags {
if s.Has(resource) {
Expand All @@ -143,7 +148,8 @@ func (pt *PackageTags) TagsFor(resources ...string) *Set[string] {
}

func (pt *PackageTags) ClearTagsFor(resources ...string) {
relPaths := utils.Map(resources, utils.CleanRelativeResourcePath)
for tag := range pt.Tags {
pt.Tags[tag].RemoveM(resources...)
pt.Tags[tag].RemoveM(relPaths...)
}
}

0 comments on commit 83b34ee

Please sign in to comment.