Rework the Destination filesystem to make --renderStaticToDisk work

See #9626
This commit is contained in:
Bjørn Erik Pedersen
2022-03-21 09:35:15 +01:00
parent b08193971a
commit d070bdf10f
75 changed files with 651 additions and 566 deletions

View File

@@ -38,8 +38,8 @@ import (
"github.com/gohugoio/hugo/modules"
hpaths "github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/hugolib/paths"
"github.com/spf13/afero"
)
@@ -68,12 +68,12 @@ type BaseFs struct {
// This usually maps to /my-project/public.
PublishFs afero.Fs
// A read-only filesystem starting from the project workDir.
WorkDir afero.Fs
// The filesystem used for renderStaticToDisk.
PublishFsStatic afero.Fs
// A read-only filesystem starting from the project workDir.
WorkDir afero.Fs
theBigFs *filesystemsCollector
// Locks.
@@ -434,21 +434,13 @@ func NewBase(p *paths.Paths, logger loggers.Logger, options ...func(*BaseFs) err
logger = loggers.NewWarningLogger()
}
// Make sure we always have the /public folder ready to use.
if err := fs.Destination.MkdirAll(p.AbsPublishDir, 0777); err != nil && !os.IsExist(err) {
return nil, err
}
publishFs := hugofs.NewBaseFileDecorator(afero.NewBasePathFs(fs.Destination, p.AbsPublishDir))
publishFs := hugofs.NewBaseFileDecorator(fs.PublishDir)
sourceFs := hugofs.NewBaseFileDecorator(afero.NewBasePathFs(fs.Source, p.WorkingDir))
publishFsStatic := afero.NewBasePathFs(fs.Source, p.AbsPublishDir)
// Same as sourceFs, but no decoration. This is what's used by os.ReadDir etc.
workDir := afero.NewBasePathFs(afero.NewReadOnlyFs(fs.Source), p.WorkingDir)
publishFsStatic := fs.PublishDirStatic
b := &BaseFs{
SourceFs: sourceFs,
WorkDir: workDir,
WorkDir: fs.WorkingDirReadOnly,
PublishFs: publishFs,
PublishFsStatic: publishFsStatic,
buildMu: lockedfile.MutexAt(filepath.Join(p.WorkingDir, lockFileBuild)),
@@ -638,7 +630,7 @@ func (b *sourceFilesystemsBuilder) createModFs(
if filepath.IsAbs(path) {
return "", path
}
return md.dir, paths.AbsPathify(md.dir, path)
return md.dir, hpaths.AbsPathify(md.dir, path)
}
for i, mount := range md.Mounts() {

View File

@@ -75,7 +75,7 @@ func initConfig(fs afero.Fs, cfg config.Provider) error {
func TestNewBaseFs(t *testing.T) {
c := qt.New(t)
v := config.New()
v := config.NewWithTestDefaults()
fs := hugofs.NewMem(v)
@@ -181,7 +181,7 @@ theme = ["atheme"]
}
func createConfig() config.Provider {
v := config.New()
v := config.NewWithTestDefaults()
v.Set("contentDir", "mycontent")
v.Set("i18nDir", "myi18n")
v.Set("staticDir", "mystatic")
@@ -219,22 +219,19 @@ func TestNewBaseFsEmpty(t *testing.T) {
func TestRealDirs(t *testing.T) {
c := qt.New(t)
v := createConfig()
root, themesDir := t.TempDir(), t.TempDir()
v.Set("workingDir", root)
v.Set("themesDir", themesDir)
v.Set("theme", "mytheme")
fs := hugofs.NewDefault(v)
sfs := fs.Source
root, err := afero.TempDir(sfs, "", "realdir")
c.Assert(err, qt.IsNil)
themesDir, err := afero.TempDir(sfs, "", "themesDir")
c.Assert(err, qt.IsNil)
defer func() {
os.RemoveAll(root)
os.RemoveAll(themesDir)
}()
v.Set("workingDir", root)
v.Set("themesDir", themesDir)
v.Set("theme", "mytheme")
c.Assert(sfs.MkdirAll(filepath.Join(root, "myassets", "scss", "sf1"), 0755), qt.IsNil)
c.Assert(sfs.MkdirAll(filepath.Join(root, "myassets", "scss", "sf2"), 0755), qt.IsNil)
c.Assert(sfs.MkdirAll(filepath.Join(themesDir, "mytheme", "assets", "scss", "sf2"), 0755), qt.IsNil)