Permalink
Browse files

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
  • Loading branch information...
bep committed Nov 26, 2018
1 parent 514e18d commit 831d23cb4d1ca99cdc15ed31c8ee1f981497be8f
@@ -1,4 +1,4 @@
hugo
/hugo
docs/public*
/.idea
hugo.exe
@@ -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
@@ -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
@@ -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)
}
}
@@ -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())
}
@@ -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())),
}
}
@@ -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))
@@ -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
}
@@ -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
@@ -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
Oops, something went wrong.

0 comments on commit 831d23c

Please sign in to comment.