Skip to content

Commit

Permalink
Add tpl/site and tpl/hugo
Browse files Browse the repository at this point in the history
This means that the current `Site` and ´.Hugo` is available as a globals, so you can do `site.IsServer` `hugo.Version` etc.

Fixes gohugoio#5470
Fixes gohugoio#5467
Fixes gohugoio#5503
  • Loading branch information
bep committed Dec 6, 2018
1 parent 514e18d commit 878de0b
Show file tree
Hide file tree
Showing 36 changed files with 518 additions and 162 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
hugo
./hugo
docs/public*
/.idea
hugo.exe
Expand Down
9 changes: 5 additions & 4 deletions commands/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion commands/genman.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sort"
"sync/atomic"

"github.com/gohugoio/hugo/common/hugo"
"github.com/pkg/errors"

"github.com/gohugoio/hugo/common/herrors"
Expand Down Expand Up @@ -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)
}
}
Expand Down
37 changes: 3 additions & 34 deletions commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
}
20 changes: 8 additions & 12 deletions hugolib/hugo_info.go → common/hugo/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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())),
}
}
9 changes: 5 additions & 4 deletions hugolib/hugo_info_test.go → common/hugo/hugo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
24 changes: 24 additions & 0 deletions common/hugo/site.go
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 18 additions & 0 deletions common/hugo/vars_extended.go
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions common/hugo/vars_regular.go
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 878de0b

Please sign in to comment.