mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Added top level .Hugo variable with version, commit and generator information + docs
Added Version, CommitHash and BuildDate to hugolib/hugo.go and used it in build Removed commitHash and buildDate from commands/version.go and used hugolib vars Removed getDateFormat function from commands/version.go Conflicts: README.md docs/content/templates/variables.md
This commit is contained in:
parent
90afe41e49
commit
ac6b86aff8
7 changed files with 58 additions and 19 deletions
2
Makefile
2
Makefile
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
COMMIT_HASH=`git rev-parse --short HEAD 2>/dev/null`
|
COMMIT_HASH=`git rev-parse --short HEAD 2>/dev/null`
|
||||||
BUILD_DATE=`date +%FT%T%z`
|
BUILD_DATE=`date +%FT%T%z`
|
||||||
LDFLAGS=-ldflags "-X github.com/spf13/hugo/commands.commitHash ${COMMIT_HASH} -X github.com/spf13/hugo/commands.buildDate ${BUILD_DATE}"
|
LDFLAGS=-ldflags "-X github.com/spf13/hugo/hugolib.CommitHash ${COMMIT_HASH} -X github.com/spf13/hugo/hugolib.BuildDate ${BUILD_DATE}"
|
||||||
|
|
||||||
all: gitinfo
|
all: gitinfo
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# Hugo
|
# Hugo
|
||||||
A Fast and Flexible Static Site Generator built with love by [spf13](http://spf13.com)
|
A Fast and Flexible Static Site Generator built with love by [spf13](http://spf13.com)
|
||||||
and [friends](http://github.com/spf13/hugo/graphs/contributors) in Go.
|
and [friends](http://github.com/spf13/hugo/graphs/contributors) in Go.
|
||||||
|
|
||||||
[![Build Status](https://travis-ci.org/spf13/hugo.png)](https://travis-ci.org/spf13/hugo)
|
[![Build Status](https://travis-ci.org/spf13/hugo.png)](https://travis-ci.org/spf13/hugo)
|
||||||
|
@ -27,10 +27,10 @@ kind of website including blogs, tumbles and docs.
|
||||||
|
|
||||||
## Installing Hugo
|
## Installing Hugo
|
||||||
|
|
||||||
Hugo is written in Go with support for Windows, Linux, FreeBSD and OS X.
|
Hugo is written in Go with support for Windows, Linux, FreeBSD and OS X.
|
||||||
|
|
||||||
The latest release can be found at [Hugo Releases](https://github.com/spf13/hugo/releases).
|
The latest release can be found at [Hugo Releases](https://github.com/spf13/hugo/releases).
|
||||||
We currently build for Windows, Linux, FreeBSD and OS X for x64
|
We currently build for Windows, Linux, FreeBSD and OS X for x64
|
||||||
and i386 architectures.
|
and i386 architectures.
|
||||||
|
|
||||||
### Installing Hugo (binary)
|
### Installing Hugo (binary)
|
||||||
|
@ -89,7 +89,7 @@ Instead, it is possible to have the `version` sub-command return information abo
|
||||||
|
|
||||||
To do this, replace the `go build` command with the following *(replace `/path/to/hugo` with the actual path)*:
|
To do this, replace the `go build` command with the following *(replace `/path/to/hugo` with the actual path)*:
|
||||||
|
|
||||||
go build -ldflags "-X /path/to/hugo/commands.commitHash `git rev-parse --short HEAD 2>/dev/null` -X github.com/spf13/hugo/commands.buildDate `date +%FT%T%z`"
|
go build -ldflags "-X /path/to/hugo/hugolib.CommitHash `git rev-parse --short HEAD 2>/dev/null` -X github.com/spf13/hugo/hugolib.BuildDate `date +%FT%T%z`"
|
||||||
|
|
||||||
This will result in `hugo version` output that looks similar to:
|
This will result in `hugo version` output that looks similar to:
|
||||||
|
|
||||||
|
|
|
@ -22,29 +22,25 @@ import (
|
||||||
|
|
||||||
"bitbucket.org/kardianos/osext"
|
"bitbucket.org/kardianos/osext"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/hugo/hugolib"
|
||||||
)
|
)
|
||||||
|
|
||||||
var timeLayout string // the layout for time.Time
|
var timeLayout string // the layout for time.Time
|
||||||
|
|
||||||
var (
|
|
||||||
commitHash string
|
|
||||||
buildDate string
|
|
||||||
)
|
|
||||||
|
|
||||||
var version = &cobra.Command{
|
var version = &cobra.Command{
|
||||||
Use: "version",
|
Use: "version",
|
||||||
Short: "Print the version number of Hugo",
|
Short: "Print the version number of Hugo",
|
||||||
Long: `All software has versions. This is Hugo's`,
|
Long: `All software has versions. This is Hugo's`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if buildDate == "" {
|
if hugolib.BuildDate == "" {
|
||||||
setBuildDate() // set the build date from executable's mdate
|
setBuildDate() // set the build date from executable's mdate
|
||||||
} else {
|
} else {
|
||||||
formatBuildDate() // format the compile time
|
formatBuildDate() // format the compile time
|
||||||
}
|
}
|
||||||
if commitHash == "" {
|
if hugolib.CommitHash == "" {
|
||||||
fmt.Printf("Hugo Static Site Generator v0.13-DEV buildDate: %s\n", buildDate)
|
fmt.Printf("Hugo Static Site Generator v%s BuildDate: %s\n", hugolib.Version, hugolib.BuildDate)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Hugo Static Site Generator v0.13-DEV-%s buildDate: %s\n", strings.ToUpper(commitHash), buildDate)
|
fmt.Printf("Hugo Static Site Generator v%s-%s BuildDate: %s\n", hugolib.Version, strings.ToUpper(hugolib.CommitHash), hugolib.BuildDate)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -52,7 +48,7 @@ var version = &cobra.Command{
|
||||||
// setBuildDate checks the ModTime of the Hugo executable and returns it as a
|
// setBuildDate checks the ModTime of the Hugo executable and returns it as a
|
||||||
// formatted string. This assumes that the executable name is Hugo, if it does
|
// formatted string. This assumes that the executable name is Hugo, if it does
|
||||||
// not exist, an empty string will be returned. This is only called if the
|
// not exist, an empty string will be returned. This is only called if the
|
||||||
// buildDate wasn't set during compile time.
|
// hugolib.BuildDate wasn't set during compile time.
|
||||||
//
|
//
|
||||||
// osext is used for cross-platform.
|
// osext is used for cross-platform.
|
||||||
func setBuildDate() {
|
func setBuildDate() {
|
||||||
|
@ -68,12 +64,12 @@ func setBuildDate() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t := fi.ModTime()
|
t := fi.ModTime()
|
||||||
buildDate = t.Format(time.RFC3339)
|
hugolib.BuildDate = t.Format(time.RFC3339)
|
||||||
}
|
}
|
||||||
|
|
||||||
// formatBuildDate formats the buildDate according to the value in
|
// formatBuildDate formats the hugolib.BuildDate according to the value in
|
||||||
// .Params.DateFormat, if it's set.
|
// .Params.DateFormat, if it's set.
|
||||||
func formatBuildDate() {
|
func formatBuildDate() {
|
||||||
t, _ := time.Parse("2006-01-02T15:04:05-0700", buildDate)
|
t, _ := time.Parse("2006-01-02T15:04:05-0700", hugolib.BuildDate)
|
||||||
buildDate = t.Format(time.RFC3339)
|
hugolib.BuildDate = t.Format(time.RFC3339)
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,7 @@ matter, content or derived from file location.
|
||||||
**.IsNode** Always false for pages.<br>
|
**.IsNode** Always false for pages.<br>
|
||||||
**.IsPage** Always true for page.<br>
|
**.IsPage** Always true for page.<br>
|
||||||
**.Site** See site variables below.<br>
|
**.Site** See site variables below.<br>
|
||||||
|
**.Hugo** See site variables below<br>
|
||||||
|
|
||||||
## Page Params
|
## Page Params
|
||||||
|
|
||||||
|
@ -74,6 +75,7 @@ includes indexes, lists and the homepage.
|
||||||
**.IsNode** Always true for nodes.<br>
|
**.IsNode** Always true for nodes.<br>
|
||||||
**.IsPage** Always false for nodes.<br>
|
**.IsPage** Always false for nodes.<br>
|
||||||
**.Site** See site variables below<br>
|
**.Site** See site variables below<br>
|
||||||
|
**.Hugo** See site variables below<br>
|
||||||
|
|
||||||
## Site Variables
|
## Site Variables
|
||||||
|
|
||||||
|
@ -83,6 +85,7 @@ Also available is `.Site` which has the following:
|
||||||
**.Site.Taxonomies** The indexes for the entire site.<br>
|
**.Site.Taxonomies** The indexes for the entire site.<br>
|
||||||
**.Site.LastChange** The date of the last change of the most recent content.<br>
|
**.Site.LastChange** The date of the last change of the most recent content.<br>
|
||||||
**.Site.Recent** Array of all content ordered by Date, newest first.<br>
|
**.Site.Recent** Array of all content ordered by Date, newest first.<br>
|
||||||
|
<<<<<<< HEAD
|
||||||
**.Site.Params** A container holding the values from the `params` section of your site configuration file. For example, a TOML config file might look like this:
|
**.Site.Params** A container holding the values from the `params` section of your site configuration file. For example, a TOML config file might look like this:
|
||||||
|
|
||||||
baseurl = "http://yoursite.example.com/"
|
baseurl = "http://yoursite.example.com/"
|
||||||
|
@ -102,3 +105,12 @@ Also available is `.Site` which has the following:
|
||||||
**.Site.LastChange** A string representing the last time content has been updated.<br>
|
**.Site.LastChange** A string representing the last time content has been updated.<br>
|
||||||
**.Site.Permalinks** A string to override the default permalink format. Defined in the site configuration.<br>
|
**.Site.Permalinks** A string to override the default permalink format. Defined in the site configuration.<br>
|
||||||
**.Site.BuildDrafts** A boolean (Default: false) to indicate whether to build drafts. Defined in the site configuration.<br>
|
**.Site.BuildDrafts** A boolean (Default: false) to indicate whether to build drafts. Defined in the site configuration.<br>
|
||||||
|
|
||||||
|
## Hugo Variables
|
||||||
|
|
||||||
|
Also available is `.Hugo` which has the following:
|
||||||
|
|
||||||
|
**.Hugo.Generator** Meta tag for the version of Hugo that generated the site. Highly recommended to be included by default in all theme headers so we can start to track Hugo usage and popularity. e.g. `<meta name="generator" content="Hugo 0.13" />`<br>
|
||||||
|
**.Hugo.Version** The current version of the Hugo binary you are using e.g. `0.13-DEV`<br>
|
||||||
|
**.Hugo.CommitHash** The git commit hash of the current Hugo binary e.g. `0e8bed9ccffba0df554728b46c5bbf6d78ae5247`<br>
|
||||||
|
**.Hugo.BuildDate** The compile date of the current Hugo binary formatted with RFC 3339 e.g. `2002-10-02T10:00:00-05:00`<br>
|
||||||
|
|
25
hugolib/hugo.go
Normal file
25
hugolib/hugo.go
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package hugolib
|
||||||
|
|
||||||
|
const Version = "0.13-DEV"
|
||||||
|
|
||||||
|
var (
|
||||||
|
CommitHash string
|
||||||
|
BuildDate string
|
||||||
|
)
|
||||||
|
|
||||||
|
// Hugo contains all the information about the current Hugo environment
|
||||||
|
type HugoInfo struct {
|
||||||
|
Version string
|
||||||
|
Generator string
|
||||||
|
CommitHash string
|
||||||
|
BuildDate string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHugoInfo() HugoInfo {
|
||||||
|
return HugoInfo{
|
||||||
|
Version: Version,
|
||||||
|
CommitHash: CommitHash,
|
||||||
|
BuildDate: BuildDate,
|
||||||
|
Generator: `<meta name="generator" content="Hugo ` + Version + `" />`,
|
||||||
|
}
|
||||||
|
}
|
|
@ -29,6 +29,7 @@ type Node struct {
|
||||||
Params map[string]interface{}
|
Params map[string]interface{}
|
||||||
Date time.Time
|
Date time.Time
|
||||||
Sitemap Sitemap
|
Sitemap Sitemap
|
||||||
|
Hugo *HugoInfo
|
||||||
UrlPath
|
UrlPath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -67,6 +67,7 @@ type Site struct {
|
||||||
Taxonomies TaxonomyList
|
Taxonomies TaxonomyList
|
||||||
Source source.Input
|
Source source.Input
|
||||||
Sections Taxonomy
|
Sections Taxonomy
|
||||||
|
Hugo HugoInfo
|
||||||
Info SiteInfo
|
Info SiteInfo
|
||||||
Shortcodes map[string]ShortcodeFunc
|
Shortcodes map[string]ShortcodeFunc
|
||||||
Menus Menus
|
Menus Menus
|
||||||
|
@ -96,6 +97,7 @@ type SiteInfo struct {
|
||||||
Files []*source.File
|
Files []*source.File
|
||||||
Recent *Pages // legacy, should be identical to Pages
|
Recent *Pages // legacy, should be identical to Pages
|
||||||
Menus *Menus
|
Menus *Menus
|
||||||
|
Hugo *HugoInfo
|
||||||
Title string
|
Title string
|
||||||
Author map[string]interface{}
|
Author map[string]interface{}
|
||||||
LanguageCode string
|
LanguageCode string
|
||||||
|
@ -339,6 +341,7 @@ func (s *Site) initialize() (err error) {
|
||||||
|
|
||||||
s.Menus = Menus{}
|
s.Menus = Menus{}
|
||||||
|
|
||||||
|
s.Hugo = NewHugoInfo()
|
||||||
s.initializeSiteInfo()
|
s.initializeSiteInfo()
|
||||||
|
|
||||||
s.Shortcodes = make(map[string]ShortcodeFunc)
|
s.Shortcodes = make(map[string]ShortcodeFunc)
|
||||||
|
@ -366,6 +369,7 @@ func (s *Site) initializeSiteInfo() {
|
||||||
Menus: &s.Menus,
|
Menus: &s.Menus,
|
||||||
Params: params,
|
Params: params,
|
||||||
Permalinks: permalinks,
|
Permalinks: permalinks,
|
||||||
|
Hugo: &s.Hugo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1190,6 +1194,7 @@ func (s *Site) NewNode() *Node {
|
||||||
return &Node{
|
return &Node{
|
||||||
Data: make(map[string]interface{}),
|
Data: make(map[string]interface{}),
|
||||||
Site: &s.Info,
|
Site: &s.Info,
|
||||||
|
Hugo: &s.Hugo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue