mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-30 22:39:58 +02:00
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:
@@ -17,11 +17,13 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/gohugoio/hugo/modules"
|
||||
|
||||
"github.com/gohugoio/hugo/tpl/tplimpl"
|
||||
|
||||
"github.com/gohugoio/hugo/common/loggers"
|
||||
"github.com/gohugoio/hugo/htesting"
|
||||
"github.com/gohugoio/hugo/langs"
|
||||
"github.com/gohugoio/hugo/resources/page"
|
||||
"github.com/spf13/afero"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
@@ -199,7 +201,7 @@ func newDepsConfig(tp *TranslationProvider, cfg config.Provider, fs *hugofs.Fs)
|
||||
l.Set("i18nDir", "i18n")
|
||||
return deps.DepsCfg{
|
||||
Language: l,
|
||||
Site: htesting.NewTestHugoSite(),
|
||||
Site: page.NewDummyHugoSite(cfg),
|
||||
Cfg: cfg,
|
||||
Fs: fs,
|
||||
Logger: logger,
|
||||
@@ -219,6 +221,13 @@ func getConfig() *viper.Viper {
|
||||
v.Set("assetDir", "assets")
|
||||
v.Set("resourceDir", "resources")
|
||||
v.Set("publishDir", "public")
|
||||
langs.LoadLanguageSettings(v, nil)
|
||||
mod, err := modules.CreateProjectModule(v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
v.Set("allModules", modules.Modules{mod})
|
||||
|
||||
return v
|
||||
|
||||
}
|
||||
|
@@ -40,8 +40,7 @@ func NewTranslationProvider() *TranslationProvider {
|
||||
|
||||
// Update updates the i18n func in the provided Deps.
|
||||
func (tp *TranslationProvider) Update(d *deps.Deps) error {
|
||||
sp := source.NewSourceSpec(d.PathSpec, d.BaseFs.SourceFilesystems.I18n.Fs)
|
||||
src := sp.NewFilesystem("")
|
||||
spec := source.NewSourceSpec(d.PathSpec, nil)
|
||||
|
||||
i18nBundle := bundle.New()
|
||||
|
||||
@@ -51,26 +50,34 @@ func (tp *TranslationProvider) Update(d *deps.Deps) error {
|
||||
}
|
||||
var newLangs []string
|
||||
|
||||
for _, r := range src.Files() {
|
||||
currentSpec := language.GetPluralSpec(r.BaseFileName())
|
||||
if currentSpec == nil {
|
||||
// This may is a language code not supported by go-i18n, it may be
|
||||
// Klingon or ... not even a fake language. Make sure it works.
|
||||
newLangs = append(newLangs, r.BaseFileName())
|
||||
}
|
||||
}
|
||||
for _, dir := range d.BaseFs.I18n.Dirs {
|
||||
src := spec.NewFilesystemFromFileMetaInfo(dir)
|
||||
|
||||
if len(newLangs) > 0 {
|
||||
language.RegisterPluralSpec(newLangs, en)
|
||||
}
|
||||
|
||||
// The source files are ordered so the most important comes first. Since this is a
|
||||
// last key win situation, we have to reverse the iteration order.
|
||||
files := src.Files()
|
||||
for i := len(files) - 1; i >= 0; i-- {
|
||||
if err := addTranslationFile(i18nBundle, files[i]); err != nil {
|
||||
files, err := src.Files()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, r := range files {
|
||||
currentSpec := language.GetPluralSpec(r.BaseFileName())
|
||||
if currentSpec == nil {
|
||||
// This may is a language code not supported by go-i18n, it may be
|
||||
// Klingon or ... not even a fake language. Make sure it works.
|
||||
newLangs = append(newLangs, r.BaseFileName())
|
||||
}
|
||||
}
|
||||
|
||||
if len(newLangs) > 0 {
|
||||
language.RegisterPluralSpec(newLangs, en)
|
||||
}
|
||||
|
||||
// The source files are ordered so the most important comes first. Since this is a
|
||||
// last key win situation, we have to reverse the iteration order.
|
||||
for i := len(files) - 1; i >= 0; i-- {
|
||||
if err := addTranslationFile(i18nBundle, files[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tp.t = NewTranslator(i18nBundle, d.Cfg, d.Log)
|
||||
@@ -81,8 +88,8 @@ func (tp *TranslationProvider) Update(d *deps.Deps) error {
|
||||
|
||||
}
|
||||
|
||||
func addTranslationFile(bundle *bundle.Bundle, r source.ReadableFile) error {
|
||||
f, err := r.Open()
|
||||
func addTranslationFile(bundle *bundle.Bundle, r source.File) error {
|
||||
f, err := r.FileInfo().Meta().Open()
|
||||
if err != nil {
|
||||
return _errors.Wrapf(err, "failed to open translations file %q:", r.LogicalName())
|
||||
}
|
||||
@@ -101,14 +108,15 @@ func (tp *TranslationProvider) Clone(d *deps.Deps) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func errWithFileContext(inerr error, r source.ReadableFile) error {
|
||||
rfi, ok := r.FileInfo().(hugofs.RealFilenameInfo)
|
||||
func errWithFileContext(inerr error, r source.File) error {
|
||||
fim, ok := r.FileInfo().(hugofs.FileMetaInfo)
|
||||
if !ok {
|
||||
return inerr
|
||||
}
|
||||
|
||||
realFilename := rfi.RealFilename()
|
||||
f, err := r.Open()
|
||||
meta := fim.Meta()
|
||||
realFilename := meta.Filename()
|
||||
f, err := meta.Open()
|
||||
if err != nil {
|
||||
return inerr
|
||||
}
|
||||
|
Reference in New Issue
Block a user