]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Add hugo.WorkingDir
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 20 May 2023 13:04:22 +0000 (15:04 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 20 May 2023 15:45:56 +0000 (17:45 +0200)
Fixes #10969

commands/hugobuilder.go
common/hugo/hugo.go
common/hugo/hugo_test.go
config/allconfig/configlanguage.go
config/configProvider.go
hugolib/config_test.go
hugolib/site_new.go
langs/i18n/i18n_test.go
resources/page/page_matcher_test.go
resources/page/site.go
tpl/cast/docshelper.go

index 20a3228f9f9d813b9710852f318d6a6a04f3a918..20e800aca5d2aa47e64a4e98844b6cd3f3d2506e 100644 (file)
@@ -990,7 +990,9 @@ func (c *hugoBuilder) loadConfig(cd *simplecobra.Commandeer, running bool) error
        cfg := config.New()
        cfg.Set("renderToDisk", (c.s == nil && !c.r.renderToMemory) || (c.s != nil && c.s.renderToDisk))
        watch := c.r.buildWatch || (c.s != nil && c.s.serverWatch)
-       cfg.Set("environment", c.r.environment)
+       if c.r.environment != "" {
+               cfg.Set("environment", c.r.environment)
+       }
 
        cfg.Set("internal", maps.Params{
                "running": running,
index 6402d7b88de74299681926bab22847059fe31b57..4769852a096c72088fe7782b35d3774c49851cda 100644 (file)
@@ -60,6 +60,7 @@ type HugoInfo struct {
        // version of go that the Hugo binary was built with
        GoVersion string
 
+       conf ConfigProvider
        deps []*Dependency
 }
 
@@ -81,15 +82,26 @@ func (i HugoInfo) IsExtended() bool {
        return IsExtended
 }
 
+// WorkingDir returns the project working directory.
+func (i HugoInfo) WorkingDir() string {
+       return i.conf.WorkingDir()
+}
+
 // Deps gets a list of dependencies for this Hugo build.
 func (i HugoInfo) Deps() []*Dependency {
        return i.deps
 }
 
+// ConfigProvider represents the config options that are relevant for HugoInfo.
+type ConfigProvider interface {
+       Environment() string
+       WorkingDir() string
+}
+
 // NewInfo creates a new Hugo Info object.
-func NewInfo(environment string, deps []*Dependency) HugoInfo {
-       if environment == "" {
-               environment = EnvironmentProduction
+func NewInfo(conf ConfigProvider, deps []*Dependency) HugoInfo {
+       if conf.Environment() == "" {
+               panic("environment not set")
        }
        var (
                commitHash string
@@ -107,7 +119,8 @@ func NewInfo(environment string, deps []*Dependency) HugoInfo {
        return HugoInfo{
                CommitHash:  commitHash,
                BuildDate:   buildDate,
-               Environment: environment,
+               Environment: conf.Environment(),
+               conf:        conf,
                deps:        deps,
                GoVersion:   goVersion,
        }
index f2ad0f5c1ef4057203b7fe3eaa5d26fad1f132d5..b0279f11137120d3b4ff165aa5da59f8b2cad405 100644 (file)
@@ -23,10 +23,12 @@ import (
 func TestHugoInfo(t *testing.T) {
        c := qt.New(t)
 
-       hugoInfo := NewInfo("", nil)
+       conf := testConfig{environment: "production", workingDir: "/mywork"}
+       hugoInfo := NewInfo(conf, nil)
 
        c.Assert(hugoInfo.Version(), qt.Equals, CurrentVersion.Version())
        c.Assert(fmt.Sprintf("%T", VersionString("")), qt.Equals, fmt.Sprintf("%T", hugoInfo.Version()))
+       c.Assert(hugoInfo.WorkingDir(), qt.Equals, "/mywork")
 
        bi := getBuildInfo()
        if bi != nil {
@@ -39,6 +41,19 @@ func TestHugoInfo(t *testing.T) {
        c.Assert(hugoInfo.IsProduction(), qt.Equals, true)
        c.Assert(hugoInfo.IsExtended(), qt.Equals, IsExtended)
 
-       devHugoInfo := NewInfo("development", nil)
+       devHugoInfo := NewInfo(testConfig{environment: "development"}, nil)
        c.Assert(devHugoInfo.IsProduction(), qt.Equals, false)
 }
+
+type testConfig struct {
+       environment string
+       workingDir  string
+}
+
+func (c testConfig) Environment() string {
+       return c.environment
+}
+
+func (c testConfig) WorkingDir() string {
+       return c.workingDir
+}
index b28d547697760299663b90d7d8fd70a84c7b52c1..95c5c7edfbc47e2be3cf4d6cd8f9ae1d4d0bd935 100644 (file)
@@ -101,6 +101,10 @@ func (c ConfigLanguage) DirsBase() config.CommonDirs {
        return c.m.Base.CommonDirs
 }
 
+func (c ConfigLanguage) WorkingDir() string {
+       return c.m.Base.WorkingDir
+}
+
 func (c ConfigLanguage) Quiet() bool {
        return c.m.Base.Internal.Quiet
 }
index ac00c747696d8762dde3981e21934ef629db2e15..8ed0728bd8af60097e14bdea9668afccf8540b41 100644 (file)
@@ -64,6 +64,7 @@ type AllProvider interface {
        Timeout() time.Duration
        StaticDirs() []string
        IgnoredErrors() map[string]bool
+       WorkingDir() string
 }
 
 // Provider provides the configuration settings for Hugo.
index 24275025ccc7e68d50995a5383ae1a91b48d2e66..4af9b7998c4c00cd86b06debd2a85b50fee170b9 100644 (file)
@@ -898,6 +898,29 @@ mainSections: []
 
 }
 
+func TestConfigHugoWorkingDir(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+-- layouts/index.html --
+WorkingDir: {{ hugo.WorkingDir }}|
+
+`
+       b := NewIntegrationTestBuilder(
+               IntegrationTestConfig{
+                       T:           t,
+                       TxtarString: files,
+                       WorkingDir:  "myworkingdir",
+               },
+       ).Build()
+
+       b.AssertFileContent("public/index.html", `
+WorkingDir: myworkingdir|
+`)
+
+}
+
 func TestConfigMergeLanguageDeepEmptyLefSide(t *testing.T) {
        t.Parallel()
 
index b0b34e45782b83e8df4bced508f694a95d7cce90..1262cc3aeea1c967938ab4e6c6edd812e1c1b8f3 100644 (file)
@@ -261,7 +261,7 @@ func newHugoSitesNew(cfg deps.DepsCfg, d *deps.Deps, sites []*Site) (*HugoSites,
                dependencies = append(dependencies, depFromMod(m))
        }
 
-       h.hugoInfo = hugo.NewInfo(h.Configs.Base.Environment, dependencies)
+       h.hugoInfo = hugo.NewInfo(h.Configs.GetFirstLanguageConfig(), dependencies)
 
        var prototype *deps.Deps
        for i, s := range sites {
index 491ae3373a673fe2d26aeacba0dcb151e0307ab3..1ac6144ddaa48c18bf132a5c16858dd99e088827 100644 (file)
@@ -480,7 +480,7 @@ func prepareDeps(afs afero.Fs, cfg config.Provider) (*deps.Deps, *TranslationPro
        translationProvider := NewTranslationProvider()
        d.TemplateProvider = tplimpl.DefaultTemplateProvider
        d.TranslationProvider = translationProvider
-       d.Site = page.NewDummyHugoSite(cfg)
+       d.Site = page.NewDummyHugoSite(d.Conf)
        if err := d.Compile(nil); err != nil {
                panic(err)
        }
index 990312ed1ebe0ab75e44fe5d3b9929f89a595769..da47843db980e830eea572ceffb8699a71c8bb32 100644 (file)
@@ -25,8 +25,8 @@ import (
 
 func TestPageMatcher(t *testing.T) {
        c := qt.New(t)
-       developmentTestSite := testSite{h: hugo.NewInfo("development", nil)}
-       productionTestSite := testSite{h: hugo.NewInfo("production", nil)}
+       developmentTestSite := testSite{h: hugo.NewInfo(testConfig{environment: "development"}, nil)}
+       productionTestSite := testSite{h: hugo.NewInfo(testConfig{environment: "production"}, nil)}
 
        p1, p2, p3 :=
                &testPage{path: "/p1", kind: "section", lang: "en", site: developmentTestSite},
@@ -156,3 +156,16 @@ func TestDecodeCascadeConfig(t *testing.T) {
        c.Assert(got, qt.IsNotNil)
 
 }
+
+type testConfig struct {
+       environment string
+       workingDir  string
+}
+
+func (c testConfig) Environment() string {
+       return c.environment
+}
+
+func (c testConfig) WorkingDir() string {
+       return c.workingDir
+}
index 16e070160a48ecd8d119633018af7cb625594fad..da97dfe6e543bf41a4013a6623f346f1c7464459 100644 (file)
@@ -444,9 +444,9 @@ func (s testSite) Param(key any) (any, error) {
 }
 
 // NewDummyHugoSite creates a new minimal test site.
-func NewDummyHugoSite(cfg config.Provider) Site {
+func NewDummyHugoSite(conf config.AllProvider) Site {
        return testSite{
-               h: hugo.NewInfo(hugo.EnvironmentProduction, nil),
+               h: hugo.NewInfo(conf, nil),
                l: &langs.Language{
                        Lang: "en",
                },
index 2ed28e3c51c53a3fcf62066da03a04b7a4a4dbfb..981c515517a2eecaea557d3192fffeb780252ac3 100644 (file)
@@ -29,7 +29,8 @@ func init() {
                if err := d.Init(); err != nil {
                        panic(err)
                }
-               d.Site = page.NewDummyHugoSite(newTestConfig())
+               conf := testconfig.GetTestConfig(nil, newTestConfig())
+               d.Site = page.NewDummyHugoSite(conf)
 
                var namespaces internal.TemplateFuncsNamespaces