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:
@@ -20,6 +20,7 @@ import (
|
||||
|
||||
"github.com/gohugoio/hugo/config"
|
||||
"github.com/gohugoio/hugo/langs"
|
||||
"github.com/gohugoio/hugo/modules"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/gohugoio/hugo/hugofs"
|
||||
@@ -39,7 +40,6 @@ type Paths struct {
|
||||
// Directories
|
||||
// TODO(bep) when we have trimmed down mos of the dirs usage outside of this package, make
|
||||
// these into an interface.
|
||||
ContentDir string
|
||||
ThemesDir string
|
||||
WorkingDir string
|
||||
|
||||
@@ -62,8 +62,9 @@ type Paths struct {
|
||||
UglyURLs bool
|
||||
CanonifyURLs bool
|
||||
|
||||
Language *langs.Language
|
||||
Languages langs.Languages
|
||||
Language *langs.Language
|
||||
Languages langs.Languages
|
||||
LanguagesDefaultFirst langs.Languages
|
||||
|
||||
// The PathSpec looks up its config settings in both the current language
|
||||
// and then in the global Viper config.
|
||||
@@ -74,8 +75,8 @@ type Paths struct {
|
||||
DefaultContentLanguage string
|
||||
multilingual bool
|
||||
|
||||
themes []string
|
||||
AllThemes []ThemeConfig
|
||||
AllModules modules.Modules
|
||||
ModulesClient *modules.Client
|
||||
}
|
||||
|
||||
func New(fs *hugofs.Fs, cfg config.Provider) (*Paths, error) {
|
||||
@@ -91,12 +92,6 @@ func New(fs *hugofs.Fs, cfg config.Provider) (*Paths, error) {
|
||||
resourceDir := filepath.Clean(cfg.GetString("resourceDir"))
|
||||
publishDir := filepath.Clean(cfg.GetString("publishDir"))
|
||||
|
||||
if contentDir == "" {
|
||||
return nil, fmt.Errorf("contentDir not set")
|
||||
}
|
||||
if resourceDir == "" {
|
||||
return nil, fmt.Errorf("resourceDir not set")
|
||||
}
|
||||
if publishDir == "" {
|
||||
return nil, fmt.Errorf("publishDir not set")
|
||||
}
|
||||
@@ -104,8 +99,9 @@ func New(fs *hugofs.Fs, cfg config.Provider) (*Paths, error) {
|
||||
defaultContentLanguage := cfg.GetString("defaultContentLanguage")
|
||||
|
||||
var (
|
||||
language *langs.Language
|
||||
languages langs.Languages
|
||||
language *langs.Language
|
||||
languages langs.Languages
|
||||
languagesDefaultFirst langs.Languages
|
||||
)
|
||||
|
||||
if l, ok := cfg.(*langs.Language); ok {
|
||||
@@ -117,6 +113,12 @@ func New(fs *hugofs.Fs, cfg config.Provider) (*Paths, error) {
|
||||
languages = l
|
||||
}
|
||||
|
||||
if l, ok := cfg.Get("languagesSortedDefaultFirst").(langs.Languages); ok {
|
||||
languagesDefaultFirst = l
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
if len(languages) == 0 {
|
||||
// We have some old tests that does not test the entire chain, hence
|
||||
// they have no languages. So create one so we get the proper filesystem.
|
||||
@@ -156,33 +158,30 @@ func New(fs *hugofs.Fs, cfg config.Provider) (*Paths, error) {
|
||||
UglyURLs: cfg.GetBool("uglyURLs"),
|
||||
CanonifyURLs: cfg.GetBool("canonifyURLs"),
|
||||
|
||||
ContentDir: contentDir,
|
||||
ThemesDir: cfg.GetString("themesDir"),
|
||||
WorkingDir: workingDir,
|
||||
|
||||
AbsResourcesDir: absResourcesDir,
|
||||
AbsPublishDir: absPublishDir,
|
||||
|
||||
themes: config.GetStringSlicePreserveString(cfg, "theme"),
|
||||
|
||||
multilingual: cfg.GetBool("multilingual"),
|
||||
defaultContentLanguageInSubdir: cfg.GetBool("defaultContentLanguageInSubdir"),
|
||||
DefaultContentLanguage: defaultContentLanguage,
|
||||
|
||||
Language: language,
|
||||
Languages: languages,
|
||||
LanguagesDefaultFirst: languagesDefaultFirst,
|
||||
MultihostTargetBasePaths: multihostTargetBasePaths,
|
||||
|
||||
PaginatePath: cfg.GetString("paginatePath"),
|
||||
}
|
||||
|
||||
if !cfg.IsSet("theme") && cfg.IsSet("allThemes") {
|
||||
p.AllThemes = cfg.Get("allThemes").([]ThemeConfig)
|
||||
} else {
|
||||
p.AllThemes, err = collectThemeNames(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cfg.IsSet("allModules") {
|
||||
p.AllModules = cfg.Get("allModules").(modules.Modules)
|
||||
}
|
||||
|
||||
if cfg.IsSet("modulesClient") {
|
||||
p.ModulesClient = cfg.Get("modulesClient").(*modules.Client)
|
||||
}
|
||||
|
||||
// TODO(bep) remove this, eventually
|
||||
@@ -207,15 +206,6 @@ func (p *Paths) Lang() string {
|
||||
return p.Language.Lang
|
||||
}
|
||||
|
||||
// ThemeSet checks whether a theme is in use or not.
|
||||
func (p *Paths) ThemeSet() bool {
|
||||
return len(p.themes) > 0
|
||||
}
|
||||
|
||||
func (p *Paths) Themes() []string {
|
||||
return p.themes
|
||||
}
|
||||
|
||||
func (p *Paths) GetTargetLanguageBasePath() string {
|
||||
if p.Languages.IsMultihost() {
|
||||
// In a multihost configuration all assets will be published below the language code.
|
||||
@@ -269,6 +259,18 @@ func (p *Paths) AbsPathify(inPath string) string {
|
||||
return AbsPathify(p.WorkingDir, inPath)
|
||||
}
|
||||
|
||||
// RelPathify trims any WorkingDir prefix from the given filename. If
|
||||
// the filename is not considered to be absolute, the path is just cleaned.
|
||||
func (p *Paths) RelPathify(filename string) string {
|
||||
filename = filepath.Clean(filename)
|
||||
if !filepath.IsAbs(filename) {
|
||||
return filename
|
||||
}
|
||||
|
||||
return strings.TrimPrefix(strings.TrimPrefix(filename, p.WorkingDir), FilePathSeparator)
|
||||
|
||||
}
|
||||
|
||||
// AbsPathify creates an absolute path if given a working dir and arelative path.
|
||||
// If already absolute, the path is just cleaned.
|
||||
func AbsPathify(workingDir, inPath string) string {
|
||||
|
@@ -16,6 +16,8 @@ package paths
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gohugoio/hugo/langs"
|
||||
|
||||
"github.com/gohugoio/hugo/hugofs"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -27,14 +29,19 @@ func TestNewPaths(t *testing.T) {
|
||||
v := viper.New()
|
||||
fs := hugofs.NewMem(v)
|
||||
|
||||
v.Set("languages", map[string]interface{}{
|
||||
"no": map[string]interface{}{},
|
||||
"en": map[string]interface{}{},
|
||||
})
|
||||
v.Set("defaultContentLanguageInSubdir", true)
|
||||
v.Set("defaultContentLanguage", "no")
|
||||
v.Set("multilingual", true)
|
||||
v.Set("contentDir", "content")
|
||||
v.Set("workingDir", "work")
|
||||
v.Set("resourceDir", "resources")
|
||||
v.Set("publishDir", "public")
|
||||
|
||||
langs.LoadLanguageSettings(v, nil)
|
||||
|
||||
p, err := New(fs, v)
|
||||
assert.NoError(err)
|
||||
|
||||
|
@@ -1,154 +0,0 @@
|
||||
// Copyright 2019 The Hugo Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package paths
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gohugoio/hugo/config"
|
||||
"github.com/spf13/afero"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
type ThemeConfig struct {
|
||||
// The theme name as provided by the folder name below /themes.
|
||||
Name string
|
||||
|
||||
// Optional configuration filename (e.g. "/themes/mytheme/config.json").
|
||||
ConfigFilename string
|
||||
|
||||
// Optional config read from the ConfigFile above.
|
||||
Cfg config.Provider
|
||||
}
|
||||
|
||||
// Create file system, an ordered theme list from left to right, no duplicates.
|
||||
type themesCollector struct {
|
||||
themesDir string
|
||||
fs afero.Fs
|
||||
seen map[string]bool
|
||||
themes []ThemeConfig
|
||||
}
|
||||
|
||||
func (c *themesCollector) isSeen(theme string) bool {
|
||||
loki := strings.ToLower(theme)
|
||||
if c.seen[loki] {
|
||||
return true
|
||||
}
|
||||
c.seen[loki] = true
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *themesCollector) addAndRecurse(themes ...string) error {
|
||||
for i := 0; i < len(themes); i++ {
|
||||
theme := themes[i]
|
||||
configFilename := c.getConfigFileIfProvided(theme)
|
||||
if !c.isSeen(theme) {
|
||||
tc, err := c.add(theme, configFilename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.addThemeNamesFromTheme(tc); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *themesCollector) add(name, configFilename string) (ThemeConfig, error) {
|
||||
var cfg config.Provider
|
||||
var tc ThemeConfig
|
||||
|
||||
if configFilename != "" {
|
||||
var err error
|
||||
cfg, err = config.FromFile(c.fs, configFilename)
|
||||
if err != nil {
|
||||
return tc, err
|
||||
}
|
||||
}
|
||||
|
||||
tc = ThemeConfig{Name: name, ConfigFilename: configFilename, Cfg: cfg}
|
||||
c.themes = append(c.themes, tc)
|
||||
return tc, nil
|
||||
|
||||
}
|
||||
|
||||
func collectThemeNames(p *Paths) ([]ThemeConfig, error) {
|
||||
return CollectThemes(p.Fs.Source, p.AbsPathify(p.ThemesDir), p.Themes())
|
||||
|
||||
}
|
||||
|
||||
func CollectThemes(fs afero.Fs, themesDir string, themes []string) ([]ThemeConfig, error) {
|
||||
if len(themes) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
c := &themesCollector{
|
||||
fs: fs,
|
||||
themesDir: themesDir,
|
||||
seen: make(map[string]bool)}
|
||||
|
||||
for i := 0; i < len(themes); i++ {
|
||||
theme := themes[i]
|
||||
if err := c.addAndRecurse(theme); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return c.themes, nil
|
||||
|
||||
}
|
||||
|
||||
func (c *themesCollector) getConfigFileIfProvided(theme string) string {
|
||||
configDir := filepath.Join(c.themesDir, theme)
|
||||
|
||||
var (
|
||||
configFilename string
|
||||
exists bool
|
||||
)
|
||||
|
||||
// Viper supports more, but this is the sub-set supported by Hugo.
|
||||
for _, configFormats := range config.ValidConfigFileExtensions {
|
||||
configFilename = filepath.Join(configDir, "config."+configFormats)
|
||||
exists, _ = afero.Exists(c.fs, configFilename)
|
||||
if exists {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !exists {
|
||||
// No theme config set.
|
||||
return ""
|
||||
}
|
||||
|
||||
return configFilename
|
||||
|
||||
}
|
||||
|
||||
func (c *themesCollector) addThemeNamesFromTheme(theme ThemeConfig) error {
|
||||
if theme.Cfg != nil && theme.Cfg.IsSet("theme") {
|
||||
v := theme.Cfg.Get("theme")
|
||||
switch vv := v.(type) {
|
||||
case []string:
|
||||
return c.addAndRecurse(vv...)
|
||||
case []interface{}:
|
||||
return c.addAndRecurse(cast.ToStringSlice(vv)...)
|
||||
default:
|
||||
return c.addAndRecurse(cast.ToString(vv))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user