From 4b6c5eba306e6e69f3dd07a6c102bfc8040b38c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 31 Jul 2019 08:21:17 +0200 Subject: 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. --- modules/collect.go | 78 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 17 deletions(-) (limited to 'modules/collect.go') diff --git a/modules/collect.go b/modules/collect.go index 5ba7f74e2..808353608 100644 --- a/modules/collect.go +++ b/modules/collect.go @@ -20,6 +20,8 @@ import ( "path/filepath" "strings" + "github.com/gohugoio/hugo/common/loggers" + "github.com/spf13/cast" "github.com/gohugoio/hugo/common/maps" @@ -62,8 +64,25 @@ func CreateProjectModule(cfg config.Provider) (Module, error) { func (h *Client) Collect() (ModulesConfig, error) { mc, coll := h.collect(true) - return mc, coll.err + if coll.err != nil { + return mc, coll.err + } + + if err := (&mc).setActiveMods(h.logger); err != nil { + return mc, err + } + + if h.ccfg.HookBeforeFinalize != nil { + if err := h.ccfg.HookBeforeFinalize(&mc); err != nil { + return mc, err + } + } + if err := (&mc).finalize(h.logger); err != nil { + return mc, err + } + + return mc, nil } func (h *Client) collect(tidy bool) (ModulesConfig, *collector) { @@ -83,20 +102,8 @@ func (h *Client) collect(tidy bool) (ModulesConfig, *collector) { } } - // TODO(bep) consider --ignoreVendor vs removing from go.mod - var activeMods Modules - for _, mod := range c.modules { - if !mod.Config().HugoVersion.IsValid() { - h.logger.WARN.Printf(`Module %q is not compatible with this Hugo version; run "hugo mod graph" for more information.`, mod.Path()) - } - if !mod.Disabled() { - activeMods = append(activeMods, mod) - } - } - return ModulesConfig{ AllModules: c.modules, - ActiveModules: activeMods, GoModulesFilename: c.GoModulesFilename, }, c @@ -113,6 +120,43 @@ type ModulesConfig struct { GoModulesFilename string } +func (m *ModulesConfig) setActiveMods(logger *loggers.Logger) error { + var activeMods Modules + for _, mod := range m.AllModules { + if !mod.Config().HugoVersion.IsValid() { + logger.WARN.Printf(`Module %q is not compatible with this Hugo version; run "hugo mod graph" for more information.`, mod.Path()) + } + if !mod.Disabled() { + activeMods = append(activeMods, mod) + } + } + + m.ActiveModules = activeMods + + return nil +} + +func (m *ModulesConfig) finalize(logger *loggers.Logger) error { + for _, mod := range m.AllModules { + m := mod.(*moduleAdapter) + m.mounts = filterUnwantedMounts(m.mounts) + } + return nil +} + +func filterUnwantedMounts(mounts []Mount) []Mount { + // Remove duplicates + seen := make(map[Mount]bool) + tmp := mounts[:0] + for _, m := range mounts { + if !seen[m] { + tmp = append(tmp, m) + } + seen[m] = true + } + return tmp +} + type collected struct { // Pick the first and prevent circular loops. seen map[string]bool @@ -177,7 +221,7 @@ func (c *collector) add(owner *moduleAdapter, moduleImport Import, disabled bool modulePath := moduleImport.Path var realOwner Module = owner - if !c.ignoreVendor { + if !c.ccfg.IgnoreVendor { if err := c.collectModulesTXT(owner); err != nil { return nil, err } @@ -223,10 +267,10 @@ func (c *collector) add(owner *moduleAdapter, moduleImport Import, disabled bool // Fall back to /themes/ if moduleDir == "" { - moduleDir = filepath.Join(c.themesDir, modulePath) + moduleDir = filepath.Join(c.ccfg.ThemesDir, modulePath) if found, _ := afero.Exists(c.fs, moduleDir); !found { - c.err = c.wrapModuleNotFound(errors.Errorf(`module %q not found; either add it as a Hugo Module or store it in %q.`, modulePath, c.themesDir)) + c.err = c.wrapModuleNotFound(errors.Errorf(`module %q not found; either add it as a Hugo Module or store it in %q.`, modulePath, c.ccfg.ThemesDir)) return nil, nil } } @@ -427,7 +471,7 @@ func (c *collector) collect() { return } - projectMod := createProjectModule(c.gomods.GetMain(), c.workingDir, c.moduleConfig) + projectMod := createProjectModule(c.gomods.GetMain(), c.ccfg.WorkingDir, c.moduleConfig) if err := c.addAndRecurse(projectMod, false); err != nil { c.err = err -- cgit v1.2.3