Skip to content

Commit

Permalink
More work
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Apr 23, 2023
1 parent 2d01b65 commit 8361894
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 107 deletions.
20 changes: 12 additions & 8 deletions config/allconfig/allconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ type RootConfig struct {
Watch bool
DisableLiveReload bool
LiveReloadPort int
IgnoreVendorPaths string

config.CommonDirs `mapstructure:",squash"`

Expand Down Expand Up @@ -385,7 +386,7 @@ type Configs struct {
configLangs []config.AllProvider
}

func (c *Configs) init() error {
func (c *Configs) Init() error {
c.configLangs = make([]config.AllProvider, len(c.Languages))
for i, l := range c.Languages {
c.configLangs[i] = ConfigLanguage{
Expand All @@ -406,12 +407,16 @@ func (c *Configs) init() error {
return nil
}

func (c *Configs) ConfigLangs() []config.AllProvider {
return c.configLangs
}

func (c *Configs) GetFirstLanguageConfig() config.AllProvider {
return c.configLangs[0]
}

// FromLoadConfigResult creates a new Config from res.
func FromLoadConfigResult(fs afero.Fs, res config.LoadConfigResult, activeModules modules.Modules) (Configs, error) {
func FromLoadConfigResult(fs afero.Fs, res config.LoadConfigResult) (Configs, error) {
if !res.Cfg.IsSet("languages") {
// We need at least one
lang := res.Cfg.GetString("defaultContentLanguage")
Expand All @@ -428,8 +433,6 @@ func FromLoadConfigResult(fs afero.Fs, res config.LoadConfigResult, activeModule
return Configs{}, err
}

// TODO1 make sure there's always one language in the map.

perLanguage := make(map[string]Config)

languagesConfig := root.GetStringMap("languages")
Expand Down Expand Up @@ -532,11 +535,8 @@ func FromLoadConfigResult(fs afero.Fs, res config.LoadConfigResult, activeModule
IsMultihost: isMultiHost,
Languages: languages,
LanguagesDefaultFirst: languagesDefaultFirst,
Modules: activeModules,
}
if err := cm.init(); err != nil {
return Configs{}, err
}

return cm, nil
}

Expand Down Expand Up @@ -601,6 +601,10 @@ func decodeConfigFromParams(fs afero.Fs, bcfg config.BaseConfig, cfg maps.Params
}
return nil
},
"module": func() error {
all.Module, err = modules.DecodeConfig(cfg)
return err
},
"permalinks": func() error {
all.Permalinks = cfg.GetStringMapString("permalinks")
return nil
Expand Down
13 changes: 0 additions & 13 deletions config/allconfig/allconfig_test.go

This file was deleted.

70 changes: 70 additions & 0 deletions config/allconfig/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package allconfig_test

import (
"testing"

qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/config/allconfig"
"github.com/gohugoio/hugo/hugolib"
)

func TestDirsMount(t *testing.T) {

files := `
-- hugo.toml --
baseURL = "https://example.com"
disableKinds = ["taxonomy", "term"]
[languages]
[languages.en]
weight = 1
[languages.sv]
weight = 2
[[module.mounts]]
source = 'content/en'
target = 'content'
lang = 'en'
[[module.mounts]]
source = 'content/sv'
target = 'content'
lang = 'sv'
-- content/en/p1.md --
---
title: "p1"
---
-- content/sv/p1.md --
---
title: "p1"
---
-- layouts/_default/single.html --
Title: {{ .Title }}
`

b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{T: t, TxtarString: files},
).Build()

//b.AssertFileContent("public/p1/index.html", "Title: p1")

sites := b.H.Sites
b.Assert(len(sites), qt.Equals, 2)

configs := b.H.Configs
mods := configs.Modules
b.Assert(len(mods), qt.Equals, 1)
mod := mods[0]
b.Assert(mod.Mounts(), qt.HasLen, 2)

enConcp := sites[0].Conf
enConf := enConcp.GetConfig().(allconfig.Config)

b.Assert(enConcp.BaseURL().String(), qt.Equals, "https://example.com")
modConf := enConf.Module
b.Assert(modConf.Mounts, qt.HasLen, 2)
b.Assert(modConf.Mounts[0].Source, qt.Equals, "content/en")
b.Assert(modConf.Mounts[0].Target, qt.Equals, "content")
b.Assert(modConf.Mounts[0].Lang, qt.Equals, "en")
b.Assert(modConf.Mounts[1].Source, qt.Equals, "content/sv")
b.Assert(modConf.Mounts[1].Target, qt.Equals, "content")
b.Assert(modConf.Mounts[1].Lang, qt.Equals, "sv")

}
18 changes: 17 additions & 1 deletion deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ type Deps struct {
*globalErrHandler
}

func (d Deps) Clone(s page.Site, conf config.AllProvider) (*Deps, error) {
d.Conf = conf
d.Site = s
d.Language = conf.Language()
d.ExecHelper = nil
d.PathSpec = nil
d.ContentSpec = nil
d.ResourceSpec = nil

if err := d.Init(); err != nil {
return nil, err
}

return &d, nil

}

func (d *Deps) Init() error {
if d.Log == nil {
d.Log = loggers.NewErrorLogger()
Expand Down Expand Up @@ -184,7 +201,6 @@ func (d *Deps) Compile() error {
if err := d.TranslationProvider.Update(d); err != nil {
return err
}

return nil
}

Expand Down
108 changes: 95 additions & 13 deletions hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,116 @@ var ErrNoConfigFile = errors.New("Unable to locate config file or config directo
// bookmark2 TODO1
// TODO1 remove the doWithConfig.
func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provider) error) (allconfig.Configs, error) {
res, moduleConfig, err := loadConfig(d, doWithConfig...)
if d.Environment == "" {
d.Environment = hugo.EnvironmentProduction
}

if len(d.Environ) == 0 && !hugo.IsRunningAsTest() {
d.Environ = os.Environ()
}

l := &configLoader{ConfigSourceDescriptor: d, cfg: config.New()}
// Make sure we always do this, even in error situations,
// as we have commands (e.g. "hugo mod init") that will
// use a partial configuration to do its job.
defer l.deleteMergeStrategies()

// TODO1 remove the module config loading from the main.
res, _, err := l.loadConfigMain(d, doWithConfig...)
if err != nil {
return allconfig.Configs{}, fmt.Errorf("failed to load config: %w", err)
}

configs, err := allconfig.FromLoadConfigResult(d.Fs, res, moduleConfig.ActiveModules)
configs, err := allconfig.FromLoadConfigResult(d.Fs, res)
if err != nil {
return allconfig.Configs{}, fmt.Errorf("failed to create config: %w", err)
}

moduleConfig, err := l.loadModules(configs)
if err != nil {
return allconfig.Configs{}, fmt.Errorf("failed to load modules: %w", err)
}
if len(l.ModulesConfigFiles) > 0 {
// Config merged in from modules.
// TODO1 improve this.
// Re-read the config.
configs, err = allconfig.FromLoadConfigResult(d.Fs, res)
if err != nil {
return allconfig.Configs{}, fmt.Errorf("failed to create config: %w", err)
}
}

configs.Modules = moduleConfig.ActiveModules

if err := configs.Init(); err != nil {
return allconfig.Configs{}, fmt.Errorf("failed to init config: %w", err)
}

return configs, nil

}

func loadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provider) error) (config.LoadConfigResult, modules.ModulesConfig, error) {
var res config.LoadConfigResult
func (l *configLoader) loadModules(configs allconfig.Configs) (modules.ModulesConfig, error) {
bcfg := configs.LoadingInfo.BaseConfig
conf := configs.Base
workingDir := bcfg.WorkingDir
themesDir := bcfg.ThemesDir
cfg := configs.LoadingInfo.Cfg

if d.Environment == "" {
d.Environment = hugo.EnvironmentProduction
var ignoreVendor glob.Glob
if s := conf.IgnoreVendorPaths; s != "" {
ignoreVendor, _ = hglob.GetGlob(hglob.NormalizePath(s))
}

if len(d.Environ) == 0 && !hugo.IsRunningAsTest() {
d.Environ = os.Environ()
ex := hexec.New(conf.Security)

hook := func(m *modules.ModulesConfig) error {
for _, tc := range m.ActiveModules {
if len(tc.ConfigFilenames()) > 0 {
if tc.Watch() {
l.ModulesConfigFiles = append(l.ModulesConfigFiles, tc.ConfigFilenames()...)
}

// Merge in the theme config using the configured
// merge strategy.
cfg.Merge("", tc.Cfg().Get(""))

}
}

return nil
}

l := &configLoader{ConfigSourceDescriptor: d, cfg: config.New()}
// Make sure we always do this, even in error situations,
// as we have commands (e.g. "hugo mod init") that will
// use a partial configuration to do its job.
defer l.deleteMergeStrategies()
modulesClient := modules.NewClient(modules.ClientConfig{
Fs: l.Fs,
Logger: l.Logger,
Exec: ex,
HookBeforeFinalize: hook,
WorkingDir: workingDir,
ThemesDir: themesDir,
Environment: l.Environment,
CacheDir: conf.Caches.CacheDirModules(),
ModuleConfig: conf.Module,
IgnoreVendor: ignoreVendor,
})

moduleConfig, err := modulesClient.Collect()

// We want to watch these for changes and trigger rebuild on version
// changes etc.
if moduleConfig.GoModulesFilename != "" {
l.ModulesConfigFiles = append(l.ModulesConfigFiles, moduleConfig.GoModulesFilename)
}

if moduleConfig.GoWorkspaceFilename != "" {
l.ModulesConfigFiles = append(l.ModulesConfigFiles, moduleConfig.GoWorkspaceFilename)
}

return moduleConfig, err
}

func (l *configLoader) loadConfigMain(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provider) error) (config.LoadConfigResult, modules.ModulesConfig, error) {
var res config.LoadConfigResult

if d.Flags != nil {
if err := l.normalizeCfg(d.Flags); err != nil {
Expand Down Expand Up @@ -173,6 +253,7 @@ func loadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
return res, l.ModulesConfig, err
}

// TODO1 remove.
modulesConfig, err := l.loadModulesConfig()
if err != nil {
return res, l.ModulesConfig, err
Expand Down Expand Up @@ -444,6 +525,7 @@ func (l configLoader) applyOsEnvOverrides(environ []string) error {
return nil
}

// TODO1 remove.
func (l *configLoader) collectModules(modConfig modules.Config, v1 config.Provider, hookBeforeFinalize func(m *modules.ModulesConfig) error) error {
workingDir := l.BaseConfig.WorkingDir
themesDir := cpaths.AbsPathify(l.WorkingDir, v1.GetString("themesDir"))
Expand Down
11 changes: 0 additions & 11 deletions hugolib/paths/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,6 @@ func New(fs *hugofs.Fs, cfg config.AllProvider) (*Paths, error) {
MultihostTargetBasePaths: multihostTargetBasePaths,
}

// TODO1
/*
if cfg.IsSet("allModules") {
p.AllModules = cfg.Get("allModules").(modules.Modules)
}
if cfg.IsSet("modulesClient") {
p.ModulesClient = cfg.Get("modulesClient").(*modules.Client)
}
*/

return p, nil
}

Expand Down
Loading

0 comments on commit 8361894

Please sign in to comment.