hugo/magefile.go
Bjørn Erik Pedersen 9f5a92078a
Add Hugo Modules
This commit implements Hugo Modules.

This is a broad subject, but some keywords include:

* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`,  `hugo mod get`,  `hugo mod graph`,  `hugo mod tidy`, and  `hugo mod vendor`.

All of the above is backed by Go Modules.

Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-07-24 09:35:53 +02:00

322 lines
7.1 KiB
Go

// +build mage
package main
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/gohugoio/hugo/codegen"
"github.com/gohugoio/hugo/resources/page/page_generate"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
packageName = "github.com/gohugoio/hugo"
noGitLdflags = "-X $PACKAGE/common/hugo.buildDate=$BUILD_DATE"
)
var ldflags = "-X $PACKAGE/common/hugo.commitHash=$COMMIT_HASH -X $PACKAGE/common/hugo.buildDate=$BUILD_DATE"
// allow user to override go executable by running as GOEXE=xxx make ... on unix-like systems
var goexe = "go"
func init() {
if exe := os.Getenv("GOEXE"); exe != "" {
goexe = exe
}
// We want to use Go 1.11 modules even if the source lives inside GOPATH.
// The default is "auto".
os.Setenv("GO111MODULE", "on")
}
// Build hugo binary
func Hugo() error {
return sh.RunWith(flagEnv(), goexe, "build", "-ldflags", ldflags, "-tags", buildTags(), packageName)
}
// Build hugo binary with race detector enabled
func HugoRace() error {
return sh.RunWith(flagEnv(), goexe, "build", "-race", "-ldflags", ldflags, "-tags", buildTags(), packageName)
}
// Install hugo binary
func Install() error {
return sh.RunWith(flagEnv(), goexe, "install", "-ldflags", ldflags, "-tags", buildTags(), packageName)
}
func flagEnv() map[string]string {
hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return map[string]string{
"PACKAGE": packageName,
"COMMIT_HASH": hash,
"BUILD_DATE": time.Now().Format("2006-01-02T15:04:05Z0700"),
}
}
func Generate() error {
generatorPackages := []string{
"tpl/tplimpl/embedded/generate",
//"resources/page/generate",
}
for _, pkg := range generatorPackages {
if err := sh.RunWith(flagEnv(), goexe, "generate", path.Join(packageName, pkg)); err != nil {
return err
}
}
dir, _ := os.Getwd()
c := codegen.NewInspector(dir)
if err := page_generate.Generate(c); err != nil {
return err
}
goFmtPatterns := []string{
// TODO(bep) check: stat ./resources/page/*autogen*: no such file or directory
"./resources/page/page_marshaljson.autogen.go",
"./resources/page/page_wrappers.autogen.go",
"./resources/page/zero_file.autogen.go",
}
for _, pattern := range goFmtPatterns {
if err := sh.Run("gofmt", "-w", filepath.FromSlash(pattern)); err != nil {
return err
}
}
return nil
}
// Build hugo without git info
func HugoNoGitInfo() error {
ldflags = noGitLdflags
return Hugo()
}
var docker = sh.RunCmd("docker")
// Build hugo Docker container
func Docker() error {
if err := docker("build", "-t", "hugo", "."); err != nil {
return err
}
// yes ignore errors here
docker("rm", "-f", "hugo-build")
if err := docker("run", "--name", "hugo-build", "hugo ls /go/bin"); err != nil {
return err
}
if err := docker("cp", "hugo-build:/go/bin/hugo", "."); err != nil {
return err
}
return docker("rm", "hugo-build")
}
// Run tests and linters
func Check() {
if strings.Contains(runtime.Version(), "1.8") {
// Go 1.8 doesn't play along with go test ./... and /vendor.
// We could fix that, but that would take time.
fmt.Printf("Skip Check on %s\n", runtime.Version())
return
}
mg.Deps(Test386)
mg.Deps(Fmt, Vet)
// don't run two tests in parallel, they saturate the CPUs anyway, and running two
// causes memory issues in CI.
mg.Deps(TestRace)
}
func testGoFlags() string {
if isCI() {
return ""
}
return "-test.short"
}
// Run tests in 32-bit mode
// Note that we don't run with the extended tag. Currently not supported in 32 bit.
func Test386() error {
env := map[string]string{"GOARCH": "386", "GOFLAGS": testGoFlags()}
return sh.RunWith(env, goexe, "test", "./...")
}
// Run tests
func Test() error {
env := map[string]string{"GOFLAGS": testGoFlags()}
return sh.RunWith(env, goexe, "test", "./...", "-tags", buildTags())
}
// Run tests with race detector
func TestRace() error {
env := map[string]string{"GOFLAGS": testGoFlags()}
return sh.RunWith(env, goexe, "test", "-race", "./...", "-tags", buildTags())
}
// Run gofmt linter
func Fmt() error {
if !isGoLatest() {
return nil
}
pkgs, err := hugoPackages()
if err != nil {
return err
}
failed := false
first := true
for _, pkg := range pkgs {
files, err := filepath.Glob(filepath.Join(pkg, "*.go"))
if err != nil {
return nil
}
for _, f := range files {
// gofmt doesn't exit with non-zero when it finds unformatted code
// so we have to explicitly look for output, and if we find any, we
// should fail this target.
s, err := sh.Output("gofmt", "-l", f)
if err != nil {
fmt.Printf("ERROR: running gofmt on %q: %v\n", f, err)
failed = true
}
if s != "" {
if first {
fmt.Println("The following files are not gofmt'ed:")
first = false
}
failed = true
fmt.Println(s)
}
}
}
if failed {
return errors.New("improperly formatted go files")
}
return nil
}
var (
pkgPrefixLen = len("github.com/gohugoio/hugo")
pkgs []string
pkgsInit sync.Once
)
func hugoPackages() ([]string, error) {
var err error
pkgsInit.Do(func() {
var s string
s, err = sh.Output(goexe, "list", "./...")
if err != nil {
return
}
pkgs = strings.Split(s, "\n")
for i := range pkgs {
pkgs[i] = "." + pkgs[i][pkgPrefixLen:]
}
})
return pkgs, err
}
// Run golint linter
func Lint() error {
pkgs, err := hugoPackages()
if err != nil {
return err
}
failed := false
for _, pkg := range pkgs {
// We don't actually want to fail this target if we find golint errors,
// so we don't pass -set_exit_status, but we still print out any failures.
if _, err := sh.Exec(nil, os.Stderr, nil, "golint", pkg); err != nil {
fmt.Printf("ERROR: running go lint on %q: %v\n", pkg, err)
failed = true
}
}
if failed {
return errors.New("errors running golint")
}
return nil
}
// Run go vet linter
func Vet() error {
if err := sh.Run(goexe, "vet", "./..."); err != nil {
return fmt.Errorf("error running go vet: %v", err)
}
return nil
}
// Generate test coverage report
func TestCoverHTML() error {
const (
coverAll = "coverage-all.out"
cover = "coverage.out"
)
f, err := os.Create(coverAll)
if err != nil {
return err
}
defer f.Close()
if _, err := f.Write([]byte("mode: count")); err != nil {
return err
}
pkgs, err := hugoPackages()
if err != nil {
return err
}
for _, pkg := range pkgs {
if err := sh.Run(goexe, "test", "-coverprofile="+cover, "-covermode=count", pkg); err != nil {
return err
}
b, err := ioutil.ReadFile(cover)
if err != nil {
if os.IsNotExist(err) {
continue
}
return err
}
idx := bytes.Index(b, []byte{'\n'})
b = b[idx+1:]
if _, err := f.Write(b); err != nil {
return err
}
}
if err := f.Close(); err != nil {
return err
}
return sh.Run(goexe, "tool", "cover", "-html="+coverAll)
}
func isGoLatest() bool {
return strings.Contains(runtime.Version(), "1.11")
}
func isCI() bool {
return os.Getenv("CI") != ""
}
func buildTags() string {
// To build the extended Hugo SCSS/SASS enabled version, build with
// HUGO_BUILD_TAGS=extended mage install etc.
if envtags := os.Getenv("HUGO_BUILD_TAGS"); envtags != "" {
return envtags
}
return "none"
}