Skip to content

Commit

Permalink
fix: should always use / as separator
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxilly committed Jan 29, 2024
1 parent 760db4a commit 3387a4c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
15 changes: 7 additions & 8 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package gore

import (
"fmt"
"path/filepath"
"runtime/debug"
"sort"
"strings"
Expand Down Expand Up @@ -118,8 +117,8 @@ type PackageClassifier interface {
func NewPathPackageClassifier(mainPkgFilepath string) *PathPackageClassifier {
return &PathPackageClassifier{
mainFilepath: mainPkgFilepath, mainFolders: []string{
filepath.Dir(mainPkgFilepath),
filepath.Clean(mainPkgFilepath),
osAwarePathDir(mainPkgFilepath),
osAwarePathClean(mainPkgFilepath),
},
}
}
Expand Down Expand Up @@ -160,11 +159,11 @@ func (c *PathPackageClassifier) Classify(pkg *Package) PackageClass {
return ClassVendor
}

parentFolder := filepath.Dir(pkg.Filepath)
parentFolder := osAwarePathDir(pkg.Filepath)

if strings.HasPrefix(pkg.Filepath, c.mainFilepath+"/vendor/") ||
strings.HasPrefix(pkg.Filepath, filepath.Dir(c.mainFilepath)+"/vendor/") ||
strings.HasPrefix(pkg.Filepath, filepath.Dir(filepath.Dir(c.mainFilepath))+"/vendor/") {
strings.HasPrefix(pkg.Filepath, osAwarePathDir(c.mainFilepath)+"/vendor/") ||
strings.HasPrefix(pkg.Filepath, osAwarePathDir(osAwarePathDir(c.mainFilepath))+"/vendor/") {
return ClassVendor
}

Expand All @@ -188,11 +187,11 @@ func (c *PathPackageClassifier) Classify(pkg *Package) PackageClass {

// If the path does not contain the "vendor" in a path but has the main package folder name, assume part of main.
if !strings.Contains(pkg.Filepath, "vendor/") &&
(filepath.Base(filepath.Dir(pkg.Filepath)) == filepath.Base(c.mainFilepath)) {
(osAwarePathBase(osAwarePathDir(pkg.Filepath)) == osAwarePathBase(c.mainFilepath)) {
return ClassMain
}
// Special case for entry point package.
if pkg.Name == "" && filepath.Base(pkg.Filepath) == "runtime" {
if pkg.Name == "" && osAwarePathBase(pkg.Filepath) == "runtime" {
return ClassSTD
}

Expand Down
4 changes: 4 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ func osAwarePathDir(s string) string {
func osAwarePathBase(s string) string {
return filepath.Base(s)
}

func osAwarePathClean(s string) string {
return filepath.Clean(s)
}
8 changes: 8 additions & 0 deletions utils_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ func osAwarePathBase(s string) string {
return s
}
}

func osAwarePathClean(s string) string {
if strings.Contains(s, "/") {
return strings.ReplaceAll(filepath.Clean(s), "\\", "/")
} else {
return s
}
}

0 comments on commit 3387a4c

Please sign in to comment.