mirror of
https://github.com/gohugoio/hugo.git
synced 2025-09-02 22:52:51 +02:00
Move the mount duplicate filter to the modules package
Also simplify the mount validation logic. There are plenty of ways a user can create mount configs that behaves oddly.
This commit is contained in:
@@ -207,20 +207,28 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
|
||||
return v, configFiles, err
|
||||
}
|
||||
|
||||
mods, modulesConfigFiles, err := l.collectModules(modulesConfig, v)
|
||||
// Need to run these after the modules are loaded, but before
|
||||
// they are finalized.
|
||||
collectHook := func(m *modules.ModulesConfig) error {
|
||||
if err := loadLanguageSettings(v, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mods := m.ActiveModules
|
||||
|
||||
// Apply default project mounts.
|
||||
if err := modules.ApplyProjectConfigDefaults(v, mods[len(mods)-1]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
_, modulesConfigFiles, err := l.collectModules(modulesConfig, v, collectHook)
|
||||
if err != nil {
|
||||
return v, configFiles, err
|
||||
}
|
||||
|
||||
if err := loadLanguageSettings(v, nil); err != nil {
|
||||
return v, configFiles, err
|
||||
}
|
||||
|
||||
// Apply default project mounts.
|
||||
if err := modules.ApplyProjectConfigDefaults(v, mods[len(mods)-1]); err != nil {
|
||||
return v, configFiles, err
|
||||
}
|
||||
|
||||
if len(modulesConfigFiles) > 0 {
|
||||
configFiles = append(configFiles, modulesConfigFiles...)
|
||||
}
|
||||
@@ -406,7 +414,7 @@ func (l configLoader) loadModulesConfig(v1 *viper.Viper) (modules.Config, error)
|
||||
return modConfig, nil
|
||||
}
|
||||
|
||||
func (l configLoader) collectModules(modConfig modules.Config, v1 *viper.Viper) (modules.Modules, []string, error) {
|
||||
func (l configLoader) collectModules(modConfig modules.Config, v1 *viper.Viper, hookBeforeFinalize func(m *modules.ModulesConfig) error) (modules.Modules, []string, error) {
|
||||
workingDir := l.WorkingDir
|
||||
if workingDir == "" {
|
||||
workingDir = v1.GetString("workingDir")
|
||||
@@ -420,16 +428,40 @@ func (l configLoader) collectModules(modConfig modules.Config, v1 *viper.Viper)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
v1.Set("filecacheConfigs", filecacheConfigs)
|
||||
|
||||
var configFilenames []string
|
||||
|
||||
hook := func(m *modules.ModulesConfig) error {
|
||||
for _, tc := range m.ActiveModules {
|
||||
if tc.ConfigFilename() != "" {
|
||||
if tc.Watch() {
|
||||
configFilenames = append(configFilenames, tc.ConfigFilename())
|
||||
}
|
||||
if err := l.applyThemeConfig(v1, tc); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if hookBeforeFinalize != nil {
|
||||
return hookBeforeFinalize(m)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
modulesClient := modules.NewClient(modules.ClientConfig{
|
||||
Fs: l.Fs,
|
||||
Logger: l.Logger,
|
||||
WorkingDir: workingDir,
|
||||
ThemesDir: themesDir,
|
||||
CacheDir: filecacheConfigs.CacheDirModules(),
|
||||
ModuleConfig: modConfig,
|
||||
IgnoreVendor: ignoreVendor,
|
||||
Fs: l.Fs,
|
||||
Logger: l.Logger,
|
||||
HookBeforeFinalize: hook,
|
||||
WorkingDir: workingDir,
|
||||
ThemesDir: themesDir,
|
||||
CacheDir: filecacheConfigs.CacheDirModules(),
|
||||
ModuleConfig: modConfig,
|
||||
IgnoreVendor: ignoreVendor,
|
||||
})
|
||||
|
||||
v1.Set("modulesClient", modulesClient)
|
||||
@@ -442,22 +474,6 @@ func (l configLoader) collectModules(modConfig modules.Config, v1 *viper.Viper)
|
||||
// Avoid recreating these later.
|
||||
v1.Set("allModules", moduleConfig.ActiveModules)
|
||||
|
||||
if len(moduleConfig.ActiveModules) == 0 {
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
var configFilenames []string
|
||||
for _, tc := range moduleConfig.ActiveModules {
|
||||
if tc.ConfigFilename() != "" {
|
||||
if tc.Watch() {
|
||||
configFilenames = append(configFilenames, tc.ConfigFilename())
|
||||
}
|
||||
if err := l.applyThemeConfig(v1, tc); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if moduleConfig.GoModulesFilename != "" {
|
||||
// We want to watch this for changes and trigger rebuild on version
|
||||
// changes etc.
|
||||
|
@@ -18,7 +18,6 @@ package filesystems
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -454,7 +453,6 @@ func (b *sourceFilesystemsBuilder) createMainOverlayFs(p *paths.Paths) (*filesys
|
||||
// The theme components are ordered from left to right.
|
||||
// We need to revert it to get the
|
||||
// overlay logic below working as expected, with the project on top (last).
|
||||
|
||||
for i, mod := range mods {
|
||||
dir := mod.Dir()
|
||||
|
||||
@@ -463,11 +461,9 @@ func (b *sourceFilesystemsBuilder) createMainOverlayFs(p *paths.Paths) (*filesys
|
||||
}
|
||||
|
||||
isMainProject := mod.Owner() == nil
|
||||
// TODO(bep) embed mount + move any duplicate/overlap
|
||||
modsReversed[i] = mountsDescriptor{
|
||||
mounts: mod.Mounts(),
|
||||
Module: mod,
|
||||
dir: dir,
|
||||
watch: mod.Watch(),
|
||||
isMainProject: isMainProject,
|
||||
}
|
||||
}
|
||||
@@ -500,36 +496,7 @@ func (b *sourceFilesystemsBuilder) createModFs(
|
||||
return paths.AbsPathify(md.dir, path)
|
||||
}
|
||||
|
||||
seen := make(map[string]bool)
|
||||
|
||||
var mounts []modules.Mount
|
||||
|
||||
OUTER:
|
||||
for i, mount := range md.mounts {
|
||||
key := path.Join(mount.Lang, mount.Source, mount.Target)
|
||||
if seen[key] {
|
||||
continue
|
||||
}
|
||||
seen[key] = true
|
||||
|
||||
// Prevent overlapping mounts
|
||||
for j, mount2 := range md.mounts {
|
||||
if j == i || mount2.Target != mount.Target {
|
||||
continue
|
||||
}
|
||||
source := mount.Source
|
||||
if !strings.HasSuffix(source, filePathSeparator) {
|
||||
source += filePathSeparator
|
||||
}
|
||||
if strings.HasPrefix(mount2.Source, source) {
|
||||
continue OUTER
|
||||
}
|
||||
}
|
||||
|
||||
mounts = append(mounts, mount)
|
||||
}
|
||||
|
||||
for _, mount := range mounts {
|
||||
for _, mount := range md.Mounts() {
|
||||
|
||||
mountWeight := 1
|
||||
if md.isMainProject {
|
||||
@@ -540,7 +507,7 @@ OUTER:
|
||||
From: mount.Target,
|
||||
To: absPathify(mount.Source),
|
||||
Meta: hugofs.FileMeta{
|
||||
"watch": md.watch,
|
||||
"watch": md.Watch(),
|
||||
"mountWeight": mountWeight,
|
||||
},
|
||||
}
|
||||
@@ -703,9 +670,8 @@ func (c *filesystemsCollector) reverseFis(fis []hugofs.FileMetaInfo) {
|
||||
}
|
||||
|
||||
type mountsDescriptor struct {
|
||||
mounts []modules.Mount
|
||||
modules.Module
|
||||
dir string
|
||||
watch bool // whether this is a candidate for watching in server mode.
|
||||
isMainProject bool
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user