Support files in content mounts

This commit is a general improvement of handling if single file mounts.

Fixes #6684
Fixes #6696
This commit is contained in:
Bjørn Erik Pedersen
2019-12-30 10:50:00 +01:00
parent aa4ccb8a1e
commit ff6253bc7c
10 changed files with 273 additions and 55 deletions

View File

@@ -18,6 +18,7 @@ package filesystems
import (
"io"
"os"
"path"
"path/filepath"
"strings"
"sync"
@@ -55,6 +56,8 @@ type BaseFs struct {
theBigFs *filesystemsCollector
}
// TODO(bep) we can get regular files in here and that is fine, but
// we need to clean up the naming.
func (fs *BaseFs) WatchDirs() []hugofs.FileMetaInfo {
var dirs []hugofs.FileMetaInfo
for _, dir := range fs.AllDirs() {
@@ -62,7 +65,6 @@ func (fs *BaseFs) WatchDirs() []hugofs.FileMetaInfo {
dirs = append(dirs, dir)
}
}
return dirs
}
@@ -90,7 +92,7 @@ func (b *BaseFs) RelContentDir(filename string) string {
for _, dir := range b.SourceFilesystems.Content.Dirs {
dirname := dir.Meta().Filename()
if strings.HasPrefix(filename, dirname) {
rel := strings.TrimPrefix(filename, dirname)
rel := path.Join(dir.Meta().Path(), strings.TrimPrefix(filename, dirname))
return strings.TrimPrefix(rel, filePathSeparator)
}
}
@@ -298,8 +300,16 @@ func (d *SourceFilesystem) Contains(filename string) bool {
func (d *SourceFilesystem) Path(filename string) string {
for _, dir := range d.Dirs {
meta := dir.Meta()
if !dir.IsDir() {
if filename == meta.Filename() {
return meta.PathFile()
}
continue
}
if strings.HasPrefix(filename, meta.Filename()) {
p := strings.TrimPrefix(strings.TrimPrefix(filename, meta.Filename()), filePathSeparator)
p = path.Join(meta.PathFile(), p)
return p
}
}
@@ -530,11 +540,11 @@ func (b *sourceFilesystemsBuilder) createModFs(
fromToStatic []hugofs.RootMapping
)
absPathify := func(path string) string {
absPathify := func(path string) (string, string) {
if filepath.IsAbs(path) {
return path
return "", path
}
return paths.AbsPathify(md.dir, path)
return md.dir, paths.AbsPathify(md.dir, path)
}
for _, mount := range md.Mounts() {
@@ -544,9 +554,12 @@ func (b *sourceFilesystemsBuilder) createModFs(
mountWeight++
}
base, filename := absPathify(mount.Source)
rm := hugofs.RootMapping{
From: mount.Target,
To: absPathify(mount.Source),
From: mount.Target,
To: filename,
ToBasedir: base,
Meta: hugofs.FileMeta{
"watch": md.Watch(),
"mountWeight": mountWeight,
@@ -621,7 +634,8 @@ func (b *sourceFilesystemsBuilder) createModFs(
if md.isMainProject {
return b.p.AbsResourcesDir
}
return absPathify(files.FolderResources)
_, filename := absPathify(files.FolderResources)
return filename
}
if collector.overlayMounts == nil {