]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
commands: Load config before creating the filesystem
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 18 May 2023 11:29:33 +0000 (13:29 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 18 May 2023 13:38:25 +0000 (15:38 +0200)
To allow publishDir to be set in config file.

commands/commandeer.go
commands/hugobuilder.go
config/allconfig/load.go
modules/collect.go
testscripts/commands/hugo__flags.txt
testscripts/commands/hugo__publishdir_in_config.txt [new file with mode: 0644]

index 377f675539b990cf29e33446bf170e0efb93436e..8e768f045de9d5e0f4f852aae49eef4b89a8a040 100644 (file)
@@ -201,17 +201,37 @@ func (r *rootCommand) ConfigFromProvider(key int32, cfg config.Provider) (*commo
                if cfg == nil {
                        cfg = config.New()
                }
-               if !cfg.IsSet("publishDir") {
-                       cfg.Set("publishDir", "public")
-               }
+
                if !cfg.IsSet("renderToDisk") {
                        cfg.Set("renderToDisk", true)
                }
                if !cfg.IsSet("workingDir") {
                        cfg.Set("workingDir", dir)
+               } else {
+                       if err := os.MkdirAll(cfg.GetString("workingDir"), 0777); err != nil {
+                               return nil, fmt.Errorf("failed to create workingDir: %w", err)
+                       }
+               }
+
+               // Load the config first to allow publishDir to be configured in config file.
+               configs, err := allconfig.LoadConfig(
+                       allconfig.ConfigSourceDescriptor{
+                               Flags:       cfg,
+                               Fs:          hugofs.Os,
+                               Filename:    r.cfgFile,
+                               ConfigDir:   r.cfgDir,
+                               Environment: r.environment,
+                       },
+               )
+               if err != nil {
+                       return nil, err
                }
-               cfg.Set("publishDirStatic", cfg.Get("publishDir"))
-               cfg.Set("publishDirDynamic", cfg.Get("publishDir"))
+
+               base := configs.Base
+
+               cfg.Set("publishDir", base.PublishDir)
+               cfg.Set("publishDirStatic", base.PublishDir)
+               cfg.Set("publishDirDynamic", base.PublishDir)
 
                renderStaticToDisk := cfg.GetBool("renderStaticToDisk")
 
@@ -257,21 +277,6 @@ func (r *rootCommand) ConfigFromProvider(key int32, cfg config.Provider) (*commo
 
                }
 
-               configs, err := allconfig.LoadConfig(
-                       allconfig.ConfigSourceDescriptor{
-                               Flags:       cfg,
-                               Fs:          fs.Source,
-                               Filename:    r.cfgFile,
-                               ConfigDir:   r.cfgDir,
-                               Environment: r.environment,
-                       },
-               )
-               if err != nil {
-                       return nil, err
-               }
-
-               base := configs.Base
-
                if !base.C.Clock.IsZero() {
                        // TODO(bep) find a better place for this.
                        htime.Clock = clock.Start(configs.Base.C.Clock)
@@ -300,6 +305,7 @@ func (r *rootCommand) HugFromConfig(conf *commonConfig) (*hugolib.HugoSites, err
        h, _, err := r.hugoSites.GetOrCreate(r.configVersionID.Load(), func(key int32) (*hugolib.HugoSites, error) {
                conf.mu.Lock()
                defer conf.mu.Unlock()
+
                depsCfg := deps.DepsCfg{Configs: conf.configs, Fs: conf.fs, Logger: r.logger}
                return hugolib.NewHugoSites(depsCfg)
        })
index 5db0b3eff859abac2ae79d2661ef5509a4091690..28c21a9a7eb1d287867daa7defde62eade476d3c 100644 (file)
@@ -954,6 +954,7 @@ func (c *hugoBuilder) loadConfig(cd *simplecobra.Commandeer, running bool) error
        if err != nil {
                return err
        }
+
        c.setConf(conf)
        if c.onConfigLoaded != nil {
                if err := c.onConfigLoaded(false); err != nil {
index 2d1a84423acee0ef17b285fecc7538ce4767503b..51c1bf279b69188db570f08df0fd1088e540791f 100644 (file)
@@ -208,8 +208,9 @@ func (l configLoader) applyDefaultConfig() error {
 }
 
 func (l configLoader) normalizeCfg(cfg config.Provider) error {
-       minify := cfg.Get("minify")
-       if b, ok := minify.(bool); ok && b {
+       if b, ok := cfg.Get("minifyOutput").(bool); ok && b {
+               cfg.Set("minify.minifyOutput", true)
+       } else if b, ok := cfg.Get("minify").(bool); ok && b {
                cfg.Set("minify", maps.Params{"minifyOutput": true})
        }
 
index c69e7e7ee7f5e3eb86fef95ead24eeceafb13dd7..3340b332778481388b01d311008d14b41552aa49 100644 (file)
@@ -347,7 +347,7 @@ func (c *collector) addAndRecurse(owner *moduleAdapter, disabled bool) error {
        moduleConfig := owner.Config()
        if owner.projectMod {
                if err := c.applyMounts(Import{}, owner); err != nil {
-                       return fmt.Errorf("failed to apply mounts for project module: %w", err)
+                       return fmt.Errorf("failed to apply mounts for project: %w", err)
                }
        }
 
index 46243f8a954f624b07133e27fcf1945bd7c324d9..c2ea92c956474cdf62bcf6c96d37095627aec44c 100644 (file)
@@ -1,10 +1,12 @@
 # Test the hugo command.
 
-hugo --baseURL http://example.com/ --destination ${WORK}/newpublic --clock 2021-11-06T22:30:00.00+09:00 -e staging --config ${WORK}/myconfig --configDir ${WORK}/myconfigdir -s mysource
+hugo --baseURL http://example.com/ --minify --destination ${WORK}/newpublic --clock 2021-11-06T22:30:00.00+09:00 -e staging --config ${WORK}/myconfig --configDir ${WORK}/myconfigdir -s mysource
 stdout 'Pages.*|1'
 stdout 'Total in'
 grep 'Home: http://example.com/, Time: 2021-11-06' newpublic/index.html
 grep 'Environment: staging, foo: bar, bar: baz' newpublic/index.html
+# Verify that it's minified.
+grep '<body>Home' newpublic/index.html
 
 hugo --quiet
 ! stdout .
@@ -17,7 +19,9 @@ foo = "bar"
 -- myconfigdir/_default/params.toml --
 bar = "baz"
 -- mysource/layouts/index.html --
+<body>
 Home: {{ .Permalink }}, Time: {{ now }}
+</body>
 Environment: {{ hugo.Environment }}, foo: {{ .Site.Params.foo }}, bar: {{ .Site.Params.bar }}
 -- mysource/layouts/_default/single.html --
 Title: {{ .Title }}
diff --git a/testscripts/commands/hugo__publishdir_in_config.txt b/testscripts/commands/hugo__publishdir_in_config.txt
new file mode 100644 (file)
index 0000000..e57d4ad
--- /dev/null
@@ -0,0 +1,12 @@
+# Test the hugo command.
+
+hugo
+
+grep 'Home' newpublic/index.html
+
+-- hugo.toml --
+baseURL = "http://example.org/"
+disableKinds = ["RSS", "sitemap", "robotsTXT", "404", "taxonomy", "term"]
+publishDir = "newpublic"
+-- layouts/index.html --
+Home.