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
This commit is contained in:
Bjørn Erik Pedersen
2019-05-03 09:16:58 +02:00
parent 47953148b6
commit 9f5a92078a
158 changed files with 9895 additions and 5433 deletions

View File

@@ -14,11 +14,14 @@
package source
import (
"os"
"path/filepath"
"strings"
"sync"
"github.com/gohugoio/hugo/hugofs/files"
"github.com/pkg/errors"
"github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/hugofs"
@@ -28,8 +31,7 @@ import (
// fileInfo implements the File interface.
var (
_ File = (*FileInfo)(nil)
_ ReadableFile = (*FileInfo)(nil)
_ File = (*FileInfo)(nil)
)
// File represents a source file.
@@ -90,13 +92,7 @@ type FileWithoutOverlap interface {
// Hugo content files being one of them, considered to be unique.
UniqueID() string
FileInfo() os.FileInfo
}
// A ReadableFile is a File that is readable.
type ReadableFile interface {
File
Open() (hugio.ReadSeekCloser, error)
FileInfo() hugofs.FileMetaInfo
}
// FileInfo describes a source file.
@@ -107,7 +103,7 @@ type FileInfo struct {
sp *SourceSpec
fi os.FileInfo
fi hugofs.FileMetaInfo
// Derived from filename
ext string // Extension without any "."
@@ -179,13 +175,14 @@ func (fi *FileInfo) UniqueID() string {
}
// FileInfo returns a file's underlying os.FileInfo.
func (fi *FileInfo) FileInfo() os.FileInfo { return fi.fi }
func (fi *FileInfo) FileInfo() hugofs.FileMetaInfo { return fi.fi }
func (fi *FileInfo) String() string { return fi.BaseFileName() }
// Open implements ReadableFile.
func (fi *FileInfo) Open() (hugio.ReadSeekCloser, error) {
f, err := fi.sp.SourceFs.Open(fi.Filename())
f, err := fi.fi.Meta().Open()
return f, err
}
@@ -225,40 +222,47 @@ func NewTestFile(filename string) *FileInfo {
}
}
// NewFileInfo returns a new FileInfo structure.
func (sp *SourceSpec) NewFileInfo(baseDir, filename string, isLeafBundle bool, fi os.FileInfo) *FileInfo {
var lang, translationBaseName, relPath string
if fp, ok := fi.(hugofs.FilePather); ok {
filename = fp.Filename()
baseDir = fp.BaseDir()
relPath = fp.Path()
func (sp *SourceSpec) NewFileInfoFrom(path, filename string) (*FileInfo, error) {
meta := hugofs.FileMeta{
"filename": filename,
"path": path,
}
if fl, ok := fi.(hugofs.LanguageAnnouncer); ok {
lang = fl.Lang()
translationBaseName = fl.TranslationBaseName()
return sp.NewFileInfo(hugofs.NewFileMetaInfo(nil, meta))
}
func (sp *SourceSpec) NewFileInfo(fi hugofs.FileMetaInfo) (*FileInfo, error) {
m := fi.Meta()
filename := m.Filename()
relPath := m.Path()
isLeafBundle := m.Classifier() == files.ContentClassLeaf
if relPath == "" || strings.Contains(relPath, "TODO") {
return nil, errors.Errorf("no Path provided by %v (%T)", m, m.Fs())
}
dir, name := filepath.Split(filename)
if filename == "" || strings.Contains(filename, "TODO") {
return nil, errors.Errorf("no Filename provided by %v (%T)", m, m.Fs())
}
relDir := filepath.Dir(relPath)
if relDir == "." {
relDir = ""
}
if !strings.HasSuffix(relDir, helpers.FilePathSeparator) {
relDir = relDir + helpers.FilePathSeparator
}
lang := m.Lang()
translationBaseName := m.GetString("translationBaseName")
dir, name := filepath.Split(relPath)
if !strings.HasSuffix(dir, helpers.FilePathSeparator) {
dir = dir + helpers.FilePathSeparator
}
baseDir = strings.TrimSuffix(baseDir, helpers.FilePathSeparator)
relDir := ""
if dir != baseDir {
relDir = strings.TrimPrefix(dir, baseDir)
}
relDir = strings.TrimPrefix(relDir, helpers.FilePathSeparator)
if relPath == "" {
relPath = filepath.Join(relDir, name)
}
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(name), "."))
baseName := helpers.Filename(name)
@@ -277,14 +281,14 @@ func (sp *SourceSpec) NewFileInfo(baseDir, filename string, isLeafBundle bool, f
lang: lang,
ext: ext,
dir: dir,
relDir: relDir,
relPath: relPath,
relDir: relDir, // Dir()
relPath: relPath, // Path()
name: name,
baseName: baseName,
baseName: baseName, // BaseFileName()
translationBaseName: translationBaseName,
isLeafBundle: isLeafBundle,
}
return f
return f, nil
}