hugo/modules/module.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

197 lines
4.2 KiB
Go

// 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 modules provides a client that can be used to manage Hugo Components,
// what's refered to as Hugo Modules. Hugo Modules is built on top of Go Modules,
// but also supports vendoring and components stored directly in the themes dir.
package modules
import (
"github.com/gohugoio/hugo/config"
"github.com/spf13/afero"
)
var _ Module = (*moduleAdapter)(nil)
type Module interface {
// Optional config read from the configFilename above.
Cfg() config.Provider
// The decoded module config and mounts.
Config() Config
// Optional configuration filename (e.g. "/themes/mytheme/config.json").
// This will be added to the special configuration watch list when in
// server mode.
ConfigFilename() string
// Directory holding files for this module.
Dir() string
// This module is disabled.
Disabled() bool
// Returns whether this is a Go Module.
IsGoMod() bool
// Any directory remappings.
Mounts() []Mount
// In the dependency tree, this is the first module that defines this module
// as a dependency.
Owner() Module
// Returns the path to this module.
// This will either be the module path, e.g. "github.com/gohugoio/myshortcodes",
// or the path below your /theme folder, e.g. "mytheme".
Path() string
// Replaced by this module.
Replace() Module
// Returns whether Dir points below the _vendor dir.
Vendor() bool
// The module version.
Version() string
// Whether this module's dir is a watch candidate.
Watch() bool
}
type Modules []Module
type moduleAdapter struct {
path string
dir string
version string
vendor bool
disabled bool
projectMod bool
owner Module
mounts []Mount
configFilename string
cfg config.Provider
config Config
// Set if a Go module.
gomod *goModule
}
func (m *moduleAdapter) Cfg() config.Provider {
return m.cfg
}
func (m *moduleAdapter) Config() Config {
return m.config
}
func (m *moduleAdapter) ConfigFilename() string {
return m.configFilename
}
func (m *moduleAdapter) Dir() string {
// This may point to the _vendor dir.
if !m.IsGoMod() || m.dir != "" {
return m.dir
}
return m.gomod.Dir
}
func (m *moduleAdapter) Disabled() bool {
return m.disabled
}
func (m *moduleAdapter) IsGoMod() bool {
return m.gomod != nil
}
func (m *moduleAdapter) Mounts() []Mount {
return m.mounts
}
func (m *moduleAdapter) Owner() Module {
return m.owner
}
func (m *moduleAdapter) Path() string {
if !m.IsGoMod() || m.path != "" {
return m.path
}
return m.gomod.Path
}
func (m *moduleAdapter) Replace() Module {
if m.IsGoMod() && !m.Vendor() && m.gomod.Replace != nil {
return &moduleAdapter{
gomod: m.gomod.Replace,
owner: m.owner,
}
}
return nil
}
func (m *moduleAdapter) Vendor() bool {
return m.vendor
}
func (m *moduleAdapter) Version() string {
if !m.IsGoMod() || m.version != "" {
return m.version
}
return m.gomod.Version
}
func (m *moduleAdapter) Watch() bool {
if m.Owner() == nil {
// Main project
return true
}
if !m.IsGoMod() {
// Module inside /themes
return true
}
if m.Replace() != nil {
// Version is not set when replaced by a local folder.
return m.Replace().Version() == ""
}
return false
}
func (m *moduleAdapter) validateAndApplyDefaults(fs afero.Fs) error {
/*if len(m.modImport.Mounts) == 0 {
// Create default mount points for every component folder that
// exists in the module.
for _, componentFolder := range files.ComponentFolders {
sourceDir := filepath.Join(dir, componentFolder)
_, err := fs.Stat(sourceDir)
if err == nil {
m.modImport.Mounts = append(m.modImport.Mounts, Mount{
Source: componentFolder,
Target: componentFolder,
})
}
}
}*/
return nil
}