diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-10-29 17:14:04 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-10-30 09:41:05 +0100 |
commit | 173187e2633f3fc037c83e1e3de2902ae3c93b92 (patch) | |
tree | 92fbe7d0cd7dddf2eb229b074f24120c15d0407a /modules/config.go | |
parent | 8a1c637c4494751046142e0ef345fce38fc1431b (diff) |
Add module.replacements
Fixes #7904
Fixes #7908
Diffstat (limited to 'modules/config.go')
-rw-r--r-- | modules/config.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/modules/config.go b/modules/config.go index e0a0ea060..1ce8c9f02 100644 --- a/modules/config.go +++ b/modules/config.go @@ -18,6 +18,8 @@ import ( "path/filepath" "strings" + "github.com/pkg/errors" + "github.com/gohugoio/hugo/common/hugo" "github.com/gohugoio/hugo/config" @@ -40,6 +42,14 @@ var DefaultModuleConfig = Config{ // Comma separated glob list matching paths that should be // treated as private. Private: "*.*", + + // A list of replacement directives mapping a module path to a directory + // or a theme component in the themes folder. + // Note that this will turn the component into a traditional theme component + // that does not partake in vendoring etc. + // The syntax is the similar to the replacement directives used in go.mod, e.g: + // github.com/mod1 -> ../mod1,github.com/mod2 -> ../mod2 + Replacements: nil, } // ApplyProjectConfigDefaults applies default/missing module configuration for @@ -182,7 +192,12 @@ func ApplyProjectConfigDefaults(cfg config.Provider, mod Module) error { // DecodeConfig creates a modules Config from a given Hugo configuration. func DecodeConfig(cfg config.Provider) (Config, error) { + return decodeConfig(cfg, nil) +} + +func decodeConfig(cfg config.Provider, pathReplacements map[string]string) (Config, error) { c := DefaultModuleConfig + c.replacementsMap = pathReplacements if cfg == nil { return c, nil @@ -197,6 +212,37 @@ func DecodeConfig(cfg config.Provider) (Config, error) { return c, err } + if c.replacementsMap == nil { + + if len(c.Replacements) == 1 { + c.Replacements = strings.Split(c.Replacements[0], ",") + } + + for i, repl := range c.Replacements { + c.Replacements[i] = strings.TrimSpace(repl) + } + + c.replacementsMap = make(map[string]string) + for _, repl := range c.Replacements { + parts := strings.Split(repl, "->") + if len(parts) != 2 { + return c, errors.Errorf(`invalid module.replacements: %q; configure replacement pairs on the form "oldpath->newpath" `, repl) + } + + c.replacementsMap[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1]) + } + } + + if c.replacementsMap != nil && c.Imports != nil { + for i, imp := range c.Imports { + if newImp, found := c.replacementsMap[imp.Path]; found { + imp.Path = newImp + c.Imports[i] = imp + } + } + + } + for i, mnt := range c.Mounts { mnt.Source = filepath.Clean(mnt.Source) mnt.Target = filepath.Clean(mnt.Target) @@ -233,6 +279,9 @@ type Config struct { // "github.com/**". NoVendor string + Replacements []string + replacementsMap map[string]string + // Configures GOPROXY. Proxy string // Configures GONOPROXY. |