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`
|
||||
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
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# 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.
|
||||
|
||||
[![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
|
||||
|
||||
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).
|
||||
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.
|
||||
|
||||
### 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)*:
|
||||
|
||||
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:
|
||||
|
||||
|
|
|
@ -22,29 +22,25 @@ import (
|
|||
|
||||
"bitbucket.org/kardianos/osext"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/hugo/hugolib"
|
||||
)
|
||||
|
||||
var timeLayout string // the layout for time.Time
|
||||
|
||||
var (
|
||||
commitHash string
|
||||
buildDate string
|
||||
)
|
||||
|
||||
var version = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Print the version number of Hugo",
|
||||
Long: `All software has versions. This is Hugo's`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if buildDate == "" {
|
||||
if hugolib.BuildDate == "" {
|
||||
setBuildDate() // set the build date from executable's mdate
|
||||
} else {
|
||||
formatBuildDate() // format the compile time
|
||||
}
|
||||
if commitHash == "" {
|
||||
fmt.Printf("Hugo Static Site Generator v0.13-DEV buildDate: %s\n", buildDate)
|
||||
if hugolib.CommitHash == "" {
|
||||
fmt.Printf("Hugo Static Site Generator v%s BuildDate: %s\n", hugolib.Version, hugolib.BuildDate)
|
||||
} 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
|
||||
// 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
|
||||
// buildDate wasn't set during compile time.
|
||||
// hugolib.BuildDate wasn't set during compile time.
|
||||
//
|
||||
// osext is used for cross-platform.
|
||||
func setBuildDate() {
|
||||
|
@ -68,12 +64,12 @@ func setBuildDate() {
|
|||
return
|
||||
}
|
||||
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.
|
||||
func formatBuildDate() {
|
||||
t, _ := time.Parse("2006-01-02T15:04:05-0700", buildDate)
|
||||
buildDate = t.Format(time.RFC3339)
|
||||
t, _ := time.Parse("2006-01-02T15:04:05-0700", hugolib.BuildDate)
|
||||
hugolib.BuildDate = t.Format(time.RFC3339)
|
||||
}
|
||||
|
|
|
@ -48,6 +48,7 @@ matter, content or derived from file location.
|
|||
**.IsNode** Always false for pages.<br>
|
||||
**.IsPage** Always true for page.<br>
|
||||
**.Site** See site variables below.<br>
|
||||
**.Hugo** See site variables below<br>
|
||||
|
||||
## Page Params
|
||||
|
||||
|
@ -74,6 +75,7 @@ includes indexes, lists and the homepage.
|
|||
**.IsNode** Always true for nodes.<br>
|
||||
**.IsPage** Always false for nodes.<br>
|
||||
**.Site** See site variables below<br>
|
||||
**.Hugo** See site variables below<br>
|
||||
|
||||
## Site Variables
|
||||
|
||||
|
@ -83,6 +85,7 @@ Also available is `.Site` which has the following:
|
|||
**.Site.Taxonomies** The indexes for the entire site.<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>
|
||||
<<<<<<< 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:
|
||||
|
||||
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.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>
|
||||
|
||||
## 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{}
|
||||
Date time.Time
|
||||
Sitemap Sitemap
|
||||
Hugo *HugoInfo
|
||||
UrlPath
|
||||
}
|
||||
|
||||
|
|
|
@ -67,6 +67,7 @@ type Site struct {
|
|||
Taxonomies TaxonomyList
|
||||
Source source.Input
|
||||
Sections Taxonomy
|
||||
Hugo HugoInfo
|
||||
Info SiteInfo
|
||||
Shortcodes map[string]ShortcodeFunc
|
||||
Menus Menus
|
||||
|
@ -96,6 +97,7 @@ type SiteInfo struct {
|
|||
Files []*source.File
|
||||
Recent *Pages // legacy, should be identical to Pages
|
||||
Menus *Menus
|
||||
Hugo *HugoInfo
|
||||
Title string
|
||||
Author map[string]interface{}
|
||||
LanguageCode string
|
||||
|
@ -339,6 +341,7 @@ func (s *Site) initialize() (err error) {
|
|||
|
||||
s.Menus = Menus{}
|
||||
|
||||
s.Hugo = NewHugoInfo()
|
||||
s.initializeSiteInfo()
|
||||
|
||||
s.Shortcodes = make(map[string]ShortcodeFunc)
|
||||
|
@ -366,6 +369,7 @@ func (s *Site) initializeSiteInfo() {
|
|||
Menus: &s.Menus,
|
||||
Params: params,
|
||||
Permalinks: permalinks,
|
||||
Hugo: &s.Hugo,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1190,6 +1194,7 @@ func (s *Site) NewNode() *Node {
|
|||
return &Node{
|
||||
Data: make(map[string]interface{}),
|
||||
Site: &s.Info,
|
||||
Hugo: &s.Hugo,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue