diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-11-26 10:11:22 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-12-06 14:37:25 +0100 |
commit | 831d23cb4d1ca99cdc15ed31c8ee1f981497be8f (patch) | |
tree | 8fe47b1b1b9233448297f8015ce61bbb7da13fc7 | |
parent | 514e18dc27ce37a0e9a231741d616cf29d50d610 (diff) |
Add tpl/site and tpl/hugo
This means that the current `.Site` and ´.Hugo` is available as a globals, so you can do `site.IsServer`, `hugo.Version` etc.
Fixes #5470
Fixes #5467
Fixes #5503
35 files changed, 518 insertions, 162 deletions
diff --git a/.gitignore b/.gitignore index 568492d85..89244f128 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -hugo +/hugo docs/public* /.idea hugo.exe diff --git a/commands/commandeer.go b/commands/commandeer.go index cd2866a27..b722991ab 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -17,10 +17,11 @@ import ( "bytes" "errors" - "github.com/gohugoio/hugo/common/herrors" - "io/ioutil" + "github.com/gohugoio/hugo/common/herrors" + "github.com/gohugoio/hugo/common/hugo" + jww "github.com/spf13/jwalterweatherman" "os" @@ -105,7 +106,7 @@ func (c *commandeer) getErrorWithContext() interface{} { m := make(map[string]interface{}) m["Error"] = errors.New(removeErrorPrefixFromLog(c.logger.Errors())) - m["Version"] = hugoVersionString() + m["Version"] = hugo.BuildVersionString() fe := herrors.UnwrapErrorWithFileContext(c.buildErr) if fe != nil { @@ -379,7 +380,7 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error { if themeVersionMismatch { name := filepath.Base(dir) cfg.Logger.ERROR.Printf("%s theme does not support Hugo version %s. Minimum version required is %s\n", - strings.ToUpper(name), helpers.CurrentHugoVersion.ReleaseVersion(), minVersion) + strings.ToUpper(name), hugo.CurrentVersion.ReleaseVersion(), minVersion) } return nil diff --git a/commands/genman.go b/commands/genman.go index ac4eaf8d1..720046289 100644 --- a/commands/genman.go +++ b/commands/genman.go @@ -17,6 +17,7 @@ import ( "fmt" "strings" + "github.com/gohugoio/hugo/common/hugo" "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/hugofs" "github.com/spf13/cobra" @@ -45,7 +46,7 @@ in the "man" directory under the current directory.`, header := &doc.GenManHeader{ Section: "1", Manual: "Hugo Manual", - Source: fmt.Sprintf("Hugo %s", helpers.CurrentHugoVersion), + Source: fmt.Sprintf("Hugo %s", hugo.CurrentVersion), } if !strings.HasSuffix(cc.genmandir, helpers.FilePathSeparator) { cc.genmandir += helpers.FilePathSeparator diff --git a/commands/hugo.go b/commands/hugo.go index 0bb15c3d1..759efc17b 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -23,6 +23,7 @@ import ( "sort" "sync/atomic" + "github.com/gohugoio/hugo/common/hugo" "github.com/pkg/errors" "github.com/gohugoio/hugo/common/herrors" @@ -1041,7 +1042,7 @@ func (c *commandeer) isThemeVsHugoVersionMismatch(fs afero.Fs) (dir string, mism } if minVersion, ok := tomlMeta["min_version"]; ok { - if helpers.CompareVersion(minVersion) > 0 { + if hugo.CompareVersion(minVersion) > 0 { return absThemeDir, true, fmt.Sprint(minVersion) } } diff --git a/commands/version.go b/commands/version.go index b85f53725..287950a2d 100644 --- a/commands/version.go +++ b/commands/version.go @@ -14,16 +14,9 @@ package commands import ( - "fmt" - "runtime" - "strings" - - jww "github.com/spf13/jwalterweatherman" - - "github.com/gohugoio/hugo/helpers" - "github.com/gohugoio/hugo/hugolib" - "github.com/gohugoio/hugo/resource/tocss/scss" + "github.com/gohugoio/hugo/common/hugo" "github.com/spf13/cobra" + jww "github.com/spf13/jwalterweatherman" ) var _ cmder = (*versionCmd)(nil) @@ -47,29 +40,5 @@ func newVersionCmd() *versionCmd { } func printHugoVersion() { - jww.FEEDBACK.Println(hugoVersionString()) -} - -func hugoVersionString() string { - program := "Hugo Static Site Generator" - - version := "v" + helpers.CurrentHugoVersion.String() - if hugolib.CommitHash != "" { - version += "-" + strings.ToUpper(hugolib.CommitHash) - } - if scss.Supports() { - version += "/extended" - } - - osArch := runtime.GOOS + "/" + runtime.GOARCH - - var buildDate string - if hugolib.BuildDate != "" { - buildDate = hugolib.BuildDate - } else { - buildDate = "unknown" - } - - return fmt.Sprintf("%s %s %s BuildDate: %s", program, version, osArch, buildDate) - + jww.FEEDBACK.Println(hugo.BuildVersionString()) } diff --git a/hugolib/hugo_info.go b/common/hugo/hugo.go index 303231edb..b93b10bf1 100644 --- a/hugolib/hugo_info.go +++ b/common/hugo/hugo.go @@ -11,13 +11,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -package hugolib +package hugo import ( "fmt" "html/template" - - "github.com/gohugoio/hugo/helpers" ) var ( @@ -29,21 +27,19 @@ var ( BuildDate string ) -var hugoInfo *HugoInfo - -// HugoInfo contains information about the current Hugo environment -type HugoInfo struct { - Version helpers.HugoVersionString +// Info contains information about the current Hugo environment +type Info struct { + Version VersionString Generator template.HTML CommitHash string BuildDate string } -func init() { - hugoInfo = &HugoInfo{ - Version: helpers.CurrentHugoVersion.Version(), +func NewInfo() Info { + return Info{ + Version: CurrentVersion.Version(), CommitHash: CommitHash, BuildDate: BuildDate, - Generator: template.HTML(fmt.Sprintf(`<meta name="generator" content="Hugo %s" />`, helpers.CurrentHugoVersion.String())), + Generator: template.HTML(fmt.Sprintf(`<meta name="generator" content="Hugo %s" />`, CurrentVersion.String())), } } diff --git a/hugolib/hugo_info_test.go b/common/hugo/hugo_test.go index 0a34330ac..18a9b594f 100644 --- a/hugolib/hugo_info_test.go +++ b/common/hugo/hugo_test.go @@ -11,21 +11,22 @@ // See the License for the specific language governing permissions and // limitations under the License. -package hugolib +package hugo import ( "fmt" "testing" - "github.com/gohugoio/hugo/helpers" "github.com/stretchr/testify/require" ) func TestHugoInfo(t *testing.T) { assert := require.New(t) - assert.Equal(helpers.CurrentHugoVersion.Version(), hugoInfo.Version) - assert.IsType(helpers.HugoVersionString(""), hugoInfo.Version) + hugoInfo := NewInfo() + + assert.Equal(CurrentVersion.Version(), hugoInfo.Version) + assert.IsType(VersionString(""), hugoInfo.Version) assert.Equal(CommitHash, hugoInfo.CommitHash) assert.Equal(BuildDate, hugoInfo.BuildDate) assert.Contains(hugoInfo.Generator, fmt.Sprintf("Hugo %s", hugoInfo.Version)) diff --git a/common/hugo/site.go b/common/hugo/site.go new file mode 100644 index 000000000..08391858a --- /dev/null +++ b/common/hugo/site.go @@ -0,0 +1,24 @@ +// Copyright 2018 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package hugo + +import "github.com/gohugoio/hugo/langs" + +// Site represents a site in the build. This is currently a very narrow interface, +// but the actual implementation will be richer, see hugolib.SiteInfo. +type Site interface { + Language() *langs.Language + IsServer() bool + Hugo() Info +} diff --git a/common/hugo/vars_extended.go b/common/hugo/vars_extended.go new file mode 100644 index 000000000..20683b804 --- /dev/null +++ b/common/hugo/vars_extended.go @@ -0,0 +1,18 @@ +// Copyright 2018 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build extended + +package hugo + +var isExtended = true diff --git a/common/hugo/vars_regular.go b/common/hugo/vars_regular.go new file mode 100644 index 000000000..e1ece83fb --- /dev/null +++ b/common/hugo/vars_regular.go @@ -0,0 +1,18 @@ +// Copyright 2018 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !extended + +package hugo + +var isExtended = false diff --git a/helpers/hugo.go b/common/hugo/version.go index 3ad4f9379..204f8f747 100644 --- a/helpers/hugo.go +++ b/common/hugo/version.go @@ -1,4 +1,4 @@ -// Copyright 2015 The Hugo Authors. All rights reserved. +// Copyright 2018 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -11,18 +11,20 @@ // See the License for the specific language governing permissions and // limitations under the License. -package helpers +package hugo import ( "fmt" + + "runtime" "strings" "github.com/gohugoio/hugo/compare" "github.com/spf13/cast" ) -// HugoVersion represents the Hugo build version. -type HugoVersion struct { +// Version represents the Hugo build version. +type Version struct { // Major and minor version. Number float32 @@ -35,34 +37,34 @@ type HugoVersion struct { } var ( - _ compare.Eqer = (*HugoVersionString)(nil) - _ compare.Comparer = (*HugoVersionString)(nil) + _ compare.Eqer = (*VersionString)(nil) + _ compare.Comparer = (*VersionString)(nil) ) -func (v HugoVersion) String() string { - return hugoVersion(v.Number, v.PatchLevel, v.Suffix) +func (v Version) String() string { + return version(v.Number, v.PatchLevel, v.Suffix) } // Version returns the Hugo version. -func (v HugoVersion) Version() HugoVersionString { - return HugoVersionString(v.String()) +func (v Version) Version() VersionString { + return VersionString(v.String()) } -// HugoVersionString represents a Hugo version string. -type HugoVersionString string +// VersionString represents a Hugo version string. +type VersionString string -func (h HugoVersionString) String() string { +func (h VersionString) String() string { return string(h) } // Compare implements the compare.Comparer interface. -func (h HugoVersionString) Compare(other interface{}) int { - v := MustParseHugoVersion(h.String()) +func (h VersionString) Compare(other interface{}) int { + v := MustParseVersion(h.String()) return compareVersionsWithSuffix(v.Number, v.PatchLevel, v.Suffix, other) } // Eq implements the compare.Eqer interface. -func (h HugoVersionString) Eq(other interface{}) bool { +func (h VersionString) Eq(other interface{}) bool { s, err := cast.ToStringE(other) if err != nil { return false @@ -72,9 +74,9 @@ func (h HugoVersionString) Eq(other interface{}) bool { var versionSuffixes = []string{"-test", "-DEV"} -// ParseHugoVersion parses a version string. -func ParseHugoVersion(s string) (HugoVersion, error) { - var vv HugoVersion +// ParseVersion parses a version string. +func ParseVersion(s string) (Version, error) { + var vv Version for _, suffix := range versionSuffixes { if strings.HasSuffix(s, suffix) { vv.Suffix = suffix @@ -90,10 +92,10 @@ func ParseHugoVersion(s string) (HugoVersion, error) { return vv, nil } -// MustParseHugoVersion parses a version string +// MustParseVersion parses a version string // and panics if any error occurs. -func MustParseHugoVersion(s string) HugoVersion { - vv, err := ParseHugoVersion(s) +func MustParseVersion(s string) Version { + vv, err := ParseVersion(s) if err != nil { panic(err) } @@ -101,36 +103,54 @@ func MustParseHugoVersion(s string) HugoVersion { } // ReleaseVersion represents the release version. -func (v HugoVersion) ReleaseVersion() HugoVersion { +func (v Version) ReleaseVersion() Version { v.Suffix = "" return v } // Next returns the next Hugo release version. -func (v HugoVersion) Next() HugoVersion { - return HugoVersion{Number: v.Number + 0.01} +func (v Version) Next() Version { + return Version{Number: v.Number + 0.01} } // Prev returns the previous Hugo release version. -func (v HugoVersion) Prev() HugoVersion { - return HugoVersion{Number: v.Number - 0.01} +func (v Version) Prev() Version { + return Version{Number: v.Number - 0.01} } // NextPatchLevel returns the next patch/bugfix Hugo version. // This will be a patch increment on the previous Hugo version. -func (v HugoVersion) NextPatchLevel(level int) HugoVersion { - return HugoVersion{Number: v.Number - 0.01, PatchLevel: level} +func (v Version) NextPatchLevel(level int) Version { + return Version{Number: v.Number - 0.01, PatchLevel: level} } -// CurrentHugoVersion represents the current build version. -// This should be the only one. -var CurrentHugoVersion = HugoVersion{ - Number: 0.53, - PatchLevel: 0, - Suffix: "-DEV", +// BuildVersionString creates a version string. This is what you see when +// running "hugo version". +func BuildVersionString() string { + program := "Hugo Static Site Generator" + + version := "v" + CurrentVersion.String() + if CommitHash != "" { + version += "-" + strings.ToUpper(CommitHash) + } + if isExtended { + version += "/extended" + } + + osArch := runtime.GOOS + "/" + runtime.GOARCH + + var buildDate string + if BuildDate != "" { + buildDate = BuildDate + } else { + buildDate = "unknown" + } + + return fmt.Sprintf("%s %s %s BuildDate: %s", program, version, osArch, buildDate) + } -func hugoVersion(version float32, patchVersion int, suffix string) string { +func version(version float32, patchVersion int, suffix string) string { if patchVersion > 0 { return fmt.Sprintf("%.2f.%d%s", version, patchVersion, suffix) } @@ -142,7 +162,7 @@ func hugoVersion(version float32, patchVersion int, suffix string) string { // It returns -1 if the given version is less than, 0 if equal and 1 if greater than // the running version. func CompareVersion(version interface{}) int { - return compareVersionsWithSuffix(CurrentHugoVersion.Number, CurrentHugoVersion.PatchLevel, CurrentHugoVersion.Suffix, version) + return compareVersionsWithSuffix(CurrentVersion.Number, CurrentVersion.PatchLevel, CurrentVersion.Suffix, version) } func compareVersions(inVersion float32, inPatchVersion int, in interface{}) int { @@ -168,7 +188,7 @@ func compareVersionsWithSuffix(inVersion float32, inPatchVersion int, suffix str return -1 } - v, err := ParseHugoVersion(s) + v, err := ParseVersion(s) if err != nil { return -1 } diff --git a/common/hugo/version_current.go b/common/hugo/version_current.go new file mode 100644 index 000000000..0a42f3023 --- /dev/null +++ b/common/hugo/version_current.go @@ -0,0 +1,22 @@ +// Copyright 2018 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package hugo + +// CurrentVersion represents the current build version. +// This should be the only one. +var CurrentVersion = Version{ + Number: 0.53, + PatchLevel: 0, + Suffix: "-DEV", +} diff --git a/helpers/hugo_test.go b/common/hugo/version_test.go index 1c2d89619..fb28750ec 100644 --- a/helpers/hugo_test.go +++ b/common/hugo/version_test.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package helpers +package hugo import ( "testing" @@ -21,10 +21,10 @@ import ( ) func TestHugoVersion(t *testing.T) { - assert.Equal(t, "0.15-DEV", hugoVersion(0.15, 0, "-DEV")) - assert.Equal(t, "0.15.2-DEV", hugoVersion(0.15, 2, "-DEV")) + assert.Equal(t, "0.15-DEV", version(0.15, 0, "-DEV")) + assert.Equal(t, "0.15.2-DEV", version(0.15, 2, "-DEV")) - v := HugoVersion{Number: 0.21, PatchLevel: 0, Suffix: "-DEV"} + v := Version{Number: 0.21, PatchLevel: 0, Suffix: "-DEV"} require.Equal(t, v.ReleaseVersion().String(), "0.21") require.Equal(t, "0.21-DEV", v.String()) @@ -62,9 +62,9 @@ func TestCompareVersions(t *testing.T) { } func TestParseHugoVersion(t *testing.T) { - require.Equal(t, "0.25", MustParseHugoVersion("0.25").String()) - require.Equal(t, "0.25.2", MustParseHugoVersion("0.25.2").String()) - require.Equal(t, "0.25-test", MustParseHugoVersion("0.25-test").String()) - require.Equal(t, "0.25-DEV", MustParseHugoVersion("0.25-DEV").String()) + require.Equal(t, "0.25", MustParseVersion("0.25").String()) + require.Equal(t, "0.25.2", MustParseVersion("0.25.2").String()) + require.Equal(t, "0.25-test", MustParseVersion("0.25-test").String()) + require.Equal(t, "0.25-DEV", MustParseVersion("0.25-DEV").String()) } diff --git a/deps/deps.go b/deps/deps.go index de6d8b52a..46f4f7730 100644 --- a/deps/deps.go +++ b/deps/deps.go @@ -7,6 +7,7 @@ import ( "github.com/pkg/errors" "github.com/gohugoio/hugo/cache/filecache" + "github.com/gohugoio/hugo/common/hugo" "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/helpers" @@ -62,8 +63,12 @@ type Deps struct { // The translation func to use Translate func(translationID string, args ...interface{}) string `json:"-"` + // The language in use. TODO(bep) consolidate with site Language *langs.Language + // The site building. + Site hugo.Site + // All the output formats available for the current site. OutputFormatsConfig output.Formats @@ -230,6 +235,7 @@ func New(cfg DepsCfg) (*Deps, error) { ResourceSpec: resourceSpec, Cfg: cfg.Language, Language: cfg.Language, + Site: cfg.Site, FileCaches: fileCaches, BuildStartListeners: &Listeners{}, Timeout: time.Duration(timeoutms) * time.Millisecond, @@ -245,7 +251,7 @@ func New(cfg DepsCfg) (*Deps, error) { // ForLanguage creates a copy of the Deps with the language dependent // parts switched out. -func (d Deps) ForLanguage(cfg DepsCfg) (*Deps, error) { +func (d Deps) ForLanguage(cfg DepsCfg, onCreated func(d *Deps) error) (*Deps, error) { l := cfg.Language var err error @@ -259,6 +265,8 @@ func (d Deps) ForLanguage(cfg DepsCfg) (*Deps, error) { return nil, err } + d.Site = cfg.Site + // The resource cache is global so reuse. // TODO(bep) clean up these inits. resourceCache := d.ResourceS |