Skip to content

Commit

Permalink
TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Dec 30, 2023
1 parent de89ce9 commit cc73dc1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
34 changes: 23 additions & 11 deletions hugofs/rootmapping_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"path/filepath"
"strings"

"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/paths"

iofs "io/fs"
Expand Down Expand Up @@ -88,8 +89,6 @@ func NewRootMappingFs(fs afero.Fs, rms ...RootMapping) (*RootMappingFs, error) {
rm.From = dirFrom
rm.To = dirTo

fmt.Println("rm.From", rm.From)

rm.Meta.Rename = func(name string, toFrom bool) string {
if toFrom {
if name == nameTo {
Expand All @@ -115,7 +114,7 @@ func NewRootMappingFs(fs afero.Fs, rms ...RootMapping) (*RootMappingFs, error) {
// Refresh the FileInfo object.
fi, err = fs.Stat(rm.To)
if err != nil {
if os.IsNotExist(err) {
if herrors.IsNotExist(err) {
continue
}
return nil, err
Expand Down Expand Up @@ -342,9 +341,9 @@ type ReverseLookupProvder interface {
ReverseLookup(filename string) ([]ComponentPath, error)
}

func (fs *RootMappingFs) ReverseLookup(filename string) ([]ComponentPath, error) {
filename = fs.cleanName(filename)
key := filepathSeparator + filename
func (fs *RootMappingFs) ReverseLookup(in string) ([]ComponentPath, error) {
in = fs.cleanName(in)
key := filepathSeparator + in

s, roots := fs.getRootsReverse(key)

Expand All @@ -355,16 +354,28 @@ func (fs *RootMappingFs) ReverseLookup(filename string) ([]ComponentPath, error)

var cps []ComponentPath

for _, first := range roots {
base := strings.TrimPrefix(key, s)
dir, name := filepath.Split(base)
base := strings.TrimPrefix(key, s)
dir, name := filepath.Split(base)

for _, first := range roots {
if first.Meta.Rename != nil {
name = first.Meta.Rename(name, true)
}

// TODO1 regular files, improve.

// Now we know that this file _could_ be in this fs.
filename := filepathSeparator + filepath.Join(first.path, dir, name)

// Confirm that it exists.
_, err := fs.Stat(first.FromBase + filename)
if err != nil {
continue
}

cps = append(cps, ComponentPath{
Component: first.FromBase,
Path: paths.ToSlashTrimLeading(filepath.Join(first.path, dir, name)),
Path: paths.ToSlashTrimLeading(filename),
})
}

Expand Down Expand Up @@ -400,7 +411,8 @@ func (fs *RootMappingFs) getRootsReverse(key string) (string, []RootMapping) {

func (fs *RootMappingFs) getRootsIn(key string, tree *radix.Tree) (string, []RootMapping) {
s, v, found := tree.LongestPrefix(key)
if !found || (s == filepathSeparator && key != filepathSeparator) {

if !found {
return "", nil
}
return s, v.([]RootMapping)
Expand Down
1 change: 1 addition & 0 deletions hugolib/filesystems/basefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ func (d *SourceFilesystem) MakePathRelative(filename string) (string, bool) {
if len(cps) == 0 {
return "", false
}

return filepath.FromSlash(cps[0].Path), true
}

Expand Down
27 changes: 18 additions & 9 deletions hugolib/filesystems/basefs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,23 +405,32 @@ func TestMakePathRelative(t *testing.T) {
c.Assert(makeRel(filepath.Join(workDir, "dust", "d3", "foo.txt")), qt.Equals, filepath.FromSlash("foo/bar/d3/foo.txt"))
}

func TestMakePathRelativeWithJSConfigInTheme(t *testing.T) {
func TestMakePathRelativeWithFileMountedInRoot(t *testing.T) {
files := `
-- hugo.toml --
theme = "t1"
-- themes/t1/package.json --
JS.
-- themes/t1/assets/js/main.js --
[[module.mounts]]
source = "bar.txt"
target = "assets/foso/baz.txt"
[[module.imports]]
path = "t1"
[[module.imports.mounts]]
source = "src"
target = "assets/foo/bar"
-- bar.txt --
Bar.
-- themes/t1/src/main.js --
Main.
`
b := hugolib.Test(t, files)

rel, found := b.H.BaseFs.Assets.MakePathRelative(filepath.FromSlash("/themes/t1/assets/js/main.js"))
rel, found := b.H.BaseFs.Assets.MakePathRelative(filepath.FromSlash("/themes/t1/src/main.js"))
b.Assert(found, qt.Equals, true)
b.Assert(rel, qt.Equals, filepath.FromSlash("js/main.js"))
rel, found = b.H.BaseFs.Assets.MakePathRelative(filepath.FromSlash("/themes/t1/package.json"))
b.Assert(rel, qt.Equals, filepath.FromSlash("foo/bar/main.js"))

/*rel, found = b.H.BaseFs.Assets.MakePathRelative(filepath.FromSlash("/bar.txt"))
b.Assert(found, qt.Equals, true)
b.Assert(rel, qt.Equals, filepath.FromSlash("_jsconfig/package.json"))
b.Assert(rel, qt.Equals, filepath.FromSlash("foo/bar.txt"))
*/
}

func checkFileCount(fs afero.Fs, dirname string, c *qt.C, expected int) {
Expand Down
1 change: 1 addition & 0 deletions resources/resource_transformers/js/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func createBuildPlugins(depsManager identity.Manager, c *Client, opts Options) (
// ESBuild resolve this.
return api.OnResolveResult{}, nil
}

relDir = filepath.Dir(rel)
} else {
relDir = opts.sourceDir
Expand Down

0 comments on commit cc73dc1

Please sign in to comment.