2018-04-11 01:54:08 -04:00
|
|
|
// Copyright 2017 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 commands
|
|
|
|
|
|
|
|
import (
|
2018-10-03 08:58:09 -04:00
|
|
|
"github.com/gohugoio/hugo/common/loggers"
|
2018-04-11 01:54:08 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/spf13/nitro"
|
|
|
|
)
|
|
|
|
|
2018-04-13 02:42:29 -04:00
|
|
|
type commandsBuilder struct {
|
|
|
|
hugoBuilderCommon
|
|
|
|
|
|
|
|
commands []cmder
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCommandsBuilder() *commandsBuilder {
|
|
|
|
return &commandsBuilder{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *commandsBuilder) addCommands(commands ...cmder) *commandsBuilder {
|
|
|
|
b.commands = append(b.commands, commands...)
|
|
|
|
return b
|
2018-04-11 01:54:08 -04:00
|
|
|
}
|
|
|
|
|
2018-04-13 02:42:29 -04:00
|
|
|
func (b *commandsBuilder) addAll() *commandsBuilder {
|
|
|
|
b.addCommands(
|
|
|
|
b.newServerCmd(),
|
2018-04-11 01:54:08 -04:00
|
|
|
newVersionCmd(),
|
|
|
|
newEnvCmd(),
|
|
|
|
newConfigCmd(),
|
|
|
|
newCheckCmd(),
|
2018-04-13 02:42:29 -04:00
|
|
|
b.newBenchmarkCmd(),
|
2018-04-11 01:54:08 -04:00
|
|
|
newConvertCmd(),
|
|
|
|
newNewCmd(),
|
|
|
|
newListCmd(),
|
|
|
|
newImportCmd(),
|
|
|
|
newGenCmd(),
|
2018-04-16 02:23:32 -04:00
|
|
|
createReleaser(),
|
2018-04-11 01:54:08 -04:00
|
|
|
)
|
2018-04-13 02:42:29 -04:00
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *commandsBuilder) build() *hugoCmd {
|
|
|
|
h := b.newHugoCmd()
|
|
|
|
addCommands(h.getCommand(), b.commands...)
|
|
|
|
return h
|
2018-04-11 01:54:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func addCommands(root *cobra.Command, commands ...cmder) {
|
|
|
|
for _, command := range commands {
|
2018-04-16 02:23:32 -04:00
|
|
|
cmd := command.getCommand()
|
|
|
|
if cmd == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
root.AddCommand(cmd)
|
2018-04-11 01:54:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type baseCmd struct {
|
|
|
|
cmd *cobra.Command
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:42:29 -04:00
|
|
|
var _ commandsBuilderGetter = (*baseBuilderCmd)(nil)
|
|
|
|
|
|
|
|
// Used in tests.
|
|
|
|
type commandsBuilderGetter interface {
|
2018-11-06 02:45:19 -05:00
|
|
|
getCommandsBuilder() *commandsBuilder
|
2018-04-13 02:42:29 -04:00
|
|
|
}
|
2018-04-11 01:54:08 -04:00
|
|
|
type baseBuilderCmd struct {
|
|
|
|
*baseCmd
|
2018-04-13 02:42:29 -04:00
|
|
|
*commandsBuilder
|
|
|
|
}
|
|
|
|
|
2018-11-06 02:45:19 -05:00
|
|
|
func (b *baseBuilderCmd) getCommandsBuilder() *commandsBuilder {
|
2018-04-13 02:42:29 -04:00
|
|
|
return b.commandsBuilder
|
2018-04-11 01:54:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseCmd) getCommand() *cobra.Command {
|
|
|
|
return c.cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBaseCmd(cmd *cobra.Command) *baseCmd {
|
|
|
|
return &baseCmd{cmd: cmd}
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:42:29 -04:00
|
|
|
func (b *commandsBuilder) newBuilderCmd(cmd *cobra.Command) *baseBuilderCmd {
|
|
|
|
bcmd := &baseBuilderCmd{commandsBuilder: b, baseCmd: &baseCmd{cmd: cmd}}
|
2018-04-11 01:54:08 -04:00
|
|
|
bcmd.hugoBuilderCommon.handleFlags(cmd)
|
|
|
|
return bcmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseCmd) flagsToConfig(cfg config.Provider) {
|
|
|
|
initializeFlags(c.cmd, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
type hugoCmd struct {
|
|
|
|
*baseBuilderCmd
|
2018-04-11 14:17:28 -04:00
|
|
|
|
|
|
|
// Need to get the sites once built.
|
|
|
|
c *commandeer
|
2018-04-11 01:54:08 -04:00
|
|
|
}
|
|
|
|
|
2018-04-16 02:23:32 -04:00
|
|
|
var _ cmder = (*nilCommand)(nil)
|
|
|
|
|
|
|
|
type nilCommand struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nilCommand) getCommand() *cobra.Command {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nilCommand) flagsToConfig(cfg config.Provider) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:42:29 -04:00
|
|
|
func (b *commandsBuilder) newHugoCmd() *hugoCmd {
|
2018-04-11 01:54:08 -04:00
|
|
|
cc := &hugoCmd{}
|
|
|
|
|
2018-04-13 02:42:29 -04:00
|
|
|
cc.baseBuilderCmd = b.newBuilderCmd(&cobra.Command{
|
2018-04-11 01:54:08 -04:00
|
|
|
Use: "hugo",
|
|
|
|
Short: "hugo builds your site",
|
|
|
|
Long: `hugo is the main command, used to build your Hugo site.
|
|
|
|
|
|
|
|
Hugo is a Fast and Flexible Static Site Generator
|
|
|
|
built with love by spf13 and friends in Go.
|
|
|
|
|
|
|
|
Complete documentation is available at http://gohugo.io/.`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
cfgInit := func(c *commandeer) error {
|
|
|
|
if cc.buildWatch {
|
|
|
|
c.Set("disableLiveReload", true)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
c, err := initializeConfig(true, cc.buildWatch, &cc.hugoBuilderCommon, cc, cfgInit)
|
2018-04-11 01:54:08 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-11 14:17:28 -04:00
|
|
|
cc.c = c
|
2018-04-11 01:54:08 -04:00
|
|
|
|
|
|
|
return c.build()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
cc.cmd.PersistentFlags().StringVar(&cc.cfgFile, "config", "", "config file (default is path/config.yaml|json|toml)")
|
|
|
|
cc.cmd.PersistentFlags().BoolVar(&cc.quiet, "quiet", false, "build in quiet mode")
|
|
|
|
|
|
|
|
// Set bash-completion
|
|
|
|
validConfigFilenames := []string{"json", "js", "yaml", "yml", "toml", "tml"}
|
|
|
|
_ = cc.cmd.PersistentFlags().SetAnnotation("config", cobra.BashCompFilenameExt, validConfigFilenames)
|
|
|
|
|
|
|
|
cc.cmd.PersistentFlags().BoolVarP(&cc.verbose, "verbose", "v", false, "verbose output")
|
|
|
|
cc.cmd.PersistentFlags().BoolVarP(&cc.debug, "debug", "", false, "debug output")
|
|
|
|
cc.cmd.PersistentFlags().BoolVar(&cc.logging, "log", false, "enable Logging")
|
|
|
|
cc.cmd.PersistentFlags().StringVar(&cc.logFile, "logFile", "", "log File path (if set, logging enabled automatically)")
|
|
|
|
cc.cmd.PersistentFlags().BoolVar(&cc.verboseLog, "verboseLog", false, "verbose logging")
|
|
|
|
|
|
|
|
cc.cmd.Flags().BoolVarP(&cc.buildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
|
|
|
|
|
2018-04-11 03:57:43 -04:00
|
|
|
cc.cmd.Flags().Bool("renderToMemory", false, "render to memory (only useful for benchmark testing)")
|
|
|
|
|
2018-04-11 01:54:08 -04:00
|
|
|
// Set bash-completion
|
|
|
|
_ = cc.cmd.PersistentFlags().SetAnnotation("logFile", cobra.BashCompFilenameExt, []string{})
|
|
|
|
|
|
|
|
cc.cmd.SetGlobalNormalizationFunc(helpers.NormalizeHugoFlags)
|
|
|
|
cc.cmd.SilenceUsage = true
|
|
|
|
|
|
|
|
return cc
|
|
|
|
}
|
|
|
|
|
|
|
|
type hugoBuilderCommon struct {
|
|
|
|
source string
|
|
|
|
baseURL string
|
|
|
|
|
|
|
|
buildWatch bool
|
|
|
|
|
|
|
|
gc bool
|
|
|
|
|
|
|
|
// TODO(bep) var vs string
|
|
|
|
logging bool
|
|
|
|
verbose bool
|
|
|
|
verboseLog bool
|
|
|
|
debug bool
|
|
|
|
quiet bool
|
|
|
|
|
|
|
|
cfgFile string
|
|
|
|
logFile string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *hugoBuilderCommon) handleFlags(cmd *cobra.Command) {
|
|
|
|
cmd.Flags().Bool("cleanDestinationDir", false, "remove files from destination not found in static directories")
|
|
|
|
cmd.Flags().BoolP("buildDrafts", "D", false, "include content marked as draft")
|
|
|
|
cmd.Flags().BoolP("buildFuture", "F", false, "include content with publishdate in the future")
|
|
|
|
cmd.Flags().BoolP("buildExpired", "E", false, "include expired content")
|
|
|
|
cmd.Flags().StringVarP(&cc.source, "source", "s", "", "filesystem path to read files relative from")
|
|
|
|
cmd.Flags().StringP("contentDir", "c", "", "filesystem path to content directory")
|
|
|
|
cmd.Flags().StringP("layoutDir", "l", "", "filesystem path to layout directory")
|
|
|
|
cmd.Flags().StringP("cacheDir", "", "", "filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/")
|
|
|
|
cmd.Flags().BoolP("ignoreCache", "", false, "ignores the cache directory")
|
|
|
|
cmd.Flags().StringP("destination", "d", "", "filesystem path to write files to")
|
|
|
|
cmd.Flags().StringP("theme", "t", "", "theme to use (located in /themes/THEMENAME/)")
|
|
|
|
cmd.Flags().StringP("themesDir", "", "", "filesystem path to themes directory")
|
|
|
|
cmd.Flags().StringVarP(&cc.baseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. http://spf13.com/")
|
|
|
|
cmd.Flags().Bool("enableGitInfo", false, "add Git revision, date and author info to the pages")
|
|
|
|
cmd.Flags().BoolVar(&cc.gc, "gc", false, "enable to run some cleanup tasks (remove unused cache files) after the build")
|
|
|
|
|
|
|
|
cmd.Flags().BoolVar(&nitro.AnalysisOn, "stepAnalysis", false, "display memory and timing of different steps of the program")
|
|
|
|
cmd.Flags().Bool("templateMetrics", false, "display metrics about template executions")
|
|
|
|
cmd.Flags().Bool("templateMetricsHints", false, "calculate some improvement hints when combined with --templateMetrics")
|
|
|
|
cmd.Flags().BoolP("forceSyncStatic", "", false, "copy all files when static is changed.")
|
|
|
|
cmd.Flags().BoolP("noTimes", "", false, "don't sync modification time of files")
|
|
|
|
cmd.Flags().BoolP("noChmod", "", false, "don't sync permission mode of files")
|
|
|
|
cmd.Flags().BoolP("i18n-warnings", "", false, "print missing translations")
|
|
|
|
|
|
|
|
cmd.Flags().StringSlice("disableKinds", []string{}, "disable different kind of pages (home, RSS etc.)")
|
|
|
|
|
2018-11-04 12:39:14 -05:00
|
|
|
cmd.Flags().Bool("minify", false, "minify any supported output format (HTML, XML etc.)")
|
|
|
|
|
2018-04-11 01:54:08 -04:00
|
|
|
// Set bash-completion.
|
|
|
|
// Each flag must first be defined before using the SetAnnotation() call.
|
|
|
|
_ = cmd.Flags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
|
|
|
|
_ = cmd.Flags().SetAnnotation("cacheDir", cobra.BashCompSubdirsInDir, []string{})
|
|
|
|
_ = cmd.Flags().SetAnnotation("destination", cobra.BashCompSubdirsInDir, []string{})
|
|
|
|
_ = cmd.Flags().SetAnnotation("theme", cobra.BashCompSubdirsInDir, []string{"themes"})
|
|
|
|
}
|
2018-07-21 18:34:17 -04:00
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
func checkErr(logger *loggers.Logger, err error, s ...string) {
|
2018-07-21 18:34:17 -04:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(s) == 0 {
|
|
|
|
logger.CRITICAL.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, message := range s {
|
|
|
|
logger.ERROR.Println(message)
|
|
|
|
}
|
|
|
|
logger.ERROR.Println(err)
|
|
|
|
}
|