2016-02-24 18:52:11 -05:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2013-09-29 02:09:03 -04:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2013-09-29 02:09:03 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2013-09-29 02:09:03 -04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2015-12-16 22:41:33 -05:00
|
|
|
// Package commands defines and implements command-line commands and flags
|
|
|
|
// used by Hugo. Commands and flags are implemented using Cobra.
|
2013-09-29 02:09:03 -04:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2014-02-17 05:54:15 -05:00
|
|
|
"fmt"
|
2017-01-03 10:57:43 -05:00
|
|
|
"io/ioutil"
|
2017-03-25 14:48:28 -04:00
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
2017-03-25 14:48:28 -04:00
|
|
|
|
2017-01-03 11:28:51 -05:00
|
|
|
"log"
|
2014-05-16 17:49:27 -04:00
|
|
|
"net/http"
|
2014-02-17 05:54:15 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-06-12 14:29:47 -04:00
|
|
|
"runtime"
|
2014-02-17 05:54:15 -05:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2014-03-31 13:23:34 -04:00
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
2016-04-18 12:37:32 -04:00
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/parser"
|
2016-02-05 12:41:40 -05:00
|
|
|
flag "github.com/spf13/pflag"
|
2015-11-09 23:31:52 -05:00
|
|
|
|
2015-12-04 08:17:48 -05:00
|
|
|
"regexp"
|
|
|
|
|
2016-04-20 10:28:26 -04:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
"github.com/gohugoio/hugo/hugolib"
|
|
|
|
"github.com/gohugoio/hugo/livereload"
|
|
|
|
"github.com/gohugoio/hugo/utils"
|
|
|
|
"github.com/gohugoio/hugo/watcher"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/fsync"
|
2014-03-31 13:23:34 -04:00
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
|
|
|
"github.com/spf13/nitro"
|
2014-04-05 01:26:43 -04:00
|
|
|
"github.com/spf13/viper"
|
2013-09-29 02:09:03 -04:00
|
|
|
)
|
|
|
|
|
2016-07-26 04:24:27 -04:00
|
|
|
// Hugo represents the Hugo sites to build. This variable is exported as it
|
2016-03-24 09:24:22 -04:00
|
|
|
// is used by at least one external library (the Hugo caddy plugin). We should
|
|
|
|
// provide a cleaner external API, but until then, this is it.
|
2016-07-28 03:30:58 -04:00
|
|
|
var Hugo *hugolib.HugoSites
|
2015-12-22 00:10:01 -05:00
|
|
|
|
2016-08-16 06:50:26 -04:00
|
|
|
// Reset resets Hugo ready for a new full build. This is mainly only useful
|
|
|
|
// for benchmark testing etc. via the CLI commands.
|
|
|
|
func Reset() error {
|
2016-08-16 06:49:07 -04:00
|
|
|
Hugo = nil
|
2016-08-16 06:50:26 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-12 05:32:11 -04:00
|
|
|
// commandError is an error used to signal different error situations in command handling.
|
2015-12-02 05:42:53 -05:00
|
|
|
type commandError struct {
|
|
|
|
s string
|
|
|
|
userError bool
|
|
|
|
}
|
|
|
|
|
2016-09-12 05:32:11 -04:00
|
|
|
func (c commandError) Error() string {
|
|
|
|
return c.s
|
2015-12-02 05:42:53 -05:00
|
|
|
}
|
|
|
|
|
2016-09-12 05:32:11 -04:00
|
|
|
func (c commandError) isUserError() bool {
|
|
|
|
return c.userError
|
2015-12-02 05:42:53 -05:00
|
|
|
}
|
|
|
|
|
2015-12-02 17:37:40 -05:00
|
|
|
func newUserError(a ...interface{}) commandError {
|
|
|
|
return commandError{s: fmt.Sprintln(a...), userError: true}
|
2015-12-02 05:42:53 -05:00
|
|
|
}
|
|
|
|
|
2015-12-02 17:37:40 -05:00
|
|
|
func newSystemError(a ...interface{}) commandError {
|
|
|
|
return commandError{s: fmt.Sprintln(a...), userError: false}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSystemErrorF(format string, a ...interface{}) commandError {
|
|
|
|
return commandError{s: fmt.Sprintf(format, a...), userError: false}
|
2015-12-02 05:42:53 -05:00
|
|
|
}
|
|
|
|
|
2016-09-12 05:32:11 -04:00
|
|
|
// Catch some of the obvious user errors from Cobra.
|
2015-12-02 05:42:53 -05:00
|
|
|
// We don't want to show the usage message for every error.
|
|
|
|
// The below may be to generic. Time will show.
|
|
|
|
var userErrorRegexp = regexp.MustCompile("argument|flag|shorthand")
|
|
|
|
|
|
|
|
func isUserError(err error) bool {
|
|
|
|
if cErr, ok := err.(commandError); ok && cErr.isUserError() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return userErrorRegexp.MatchString(err.Error())
|
|
|
|
}
|
|
|
|
|
2016-01-13 01:34:29 -05:00
|
|
|
// HugoCmd is Hugo's root command.
|
|
|
|
// Every other command attached to HugoCmd is a child command to it.
|
2016-02-06 06:40:16 -05:00
|
|
|
var HugoCmd = &cobra.Command{
|
2014-02-17 05:54:15 -05:00
|
|
|
Use: "hugo",
|
2015-05-21 07:30:01 -04:00
|
|
|
Short: "hugo builds your site",
|
2015-08-04 05:15:12 -04:00
|
|
|
Long: `hugo is the main command, used to build your Hugo site.
|
2013-09-29 02:09:03 -04:00
|
|
|
|
2015-08-04 05:15:12 -04:00
|
|
|
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/.`,
|
2015-12-02 05:42:53 -05:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-01-03 11:28:51 -05:00
|
|
|
cfg, err := InitializeConfig()
|
|
|
|
if err != nil {
|
2015-12-02 05:42:53 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-25 09:37:04 -04:00
|
|
|
c, err := newCommandeer(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2016-02-05 12:41:40 -05:00
|
|
|
if buildWatch {
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg.Cfg.Set("disableLiveReload", true)
|
2017-01-10 04:55:03 -05:00
|
|
|
c.watchConfig()
|
2016-01-13 01:34:29 -05:00
|
|
|
}
|
2015-12-02 05:42:53 -05:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
return c.build()
|
2014-02-17 05:54:15 -05:00
|
|
|
},
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
2014-05-16 17:49:27 -04:00
|
|
|
|
2013-11-12 18:36:23 -05:00
|
|
|
var hugoCmdV *cobra.Command
|
|
|
|
|
2015-12-02 13:56:36 -05:00
|
|
|
// Flags that are to be added to commands.
|
2016-02-05 12:41:40 -05:00
|
|
|
var (
|
2016-11-03 06:33:53 -04:00
|
|
|
buildWatch bool
|
|
|
|
logging bool
|
|
|
|
renderToMemory bool // for benchmark testing
|
|
|
|
verbose bool
|
|
|
|
verboseLog bool
|
|
|
|
quiet bool
|
2016-02-05 12:41:40 -05:00
|
|
|
)
|
|
|
|
|
2016-02-05 16:58:17 -05:00
|
|
|
var (
|
2017-02-04 22:20:06 -05:00
|
|
|
baseURL string
|
|
|
|
cacheDir string
|
|
|
|
contentDir string
|
|
|
|
layoutDir string
|
|
|
|
cfgFile string
|
|
|
|
destination string
|
|
|
|
logFile string
|
|
|
|
theme string
|
|
|
|
themesDir string
|
|
|
|
source string
|
|
|
|
logI18nWarnings bool
|
2017-02-18 04:02:12 -05:00
|
|
|
disableKinds []string
|
2016-02-05 16:58:17 -05:00
|
|
|
)
|
2013-09-29 02:09:03 -04:00
|
|
|
|
2015-12-02 13:56:36 -05:00
|
|
|
// Execute adds all child commands to the root command HugoCmd and sets flags appropriately.
|
2013-09-29 02:09:03 -04:00
|
|
|
func Execute() {
|
2016-02-06 06:40:16 -05:00
|
|
|
HugoCmd.SetGlobalNormalizationFunc(helpers.NormalizeHugoFlags)
|
2015-12-02 05:42:53 -05:00
|
|
|
|
2016-02-06 06:40:16 -05:00
|
|
|
HugoCmd.SilenceUsage = true
|
2015-12-02 05:42:53 -05:00
|
|
|
|
2014-02-17 05:54:15 -05:00
|
|
|
AddCommands()
|
2015-12-02 05:42:53 -05:00
|
|
|
|
2016-02-06 06:40:16 -05:00
|
|
|
if c, err := HugoCmd.ExecuteC(); err != nil {
|
2015-12-02 05:42:53 -05:00
|
|
|
if isUserError(err) {
|
|
|
|
c.Println("")
|
|
|
|
c.Println(c.UsageString())
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:26:49 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
|
|
|
|
2015-12-02 13:56:36 -05:00
|
|
|
// AddCommands adds child commands to the root command HugoCmd.
|
2013-09-29 02:09:03 -04:00
|
|
|
func AddCommands() {
|
2016-02-06 06:40:16 -05:00
|
|
|
HugoCmd.AddCommand(serverCmd)
|
|
|
|
HugoCmd.AddCommand(versionCmd)
|
2016-10-05 04:20:25 -04:00
|
|
|
HugoCmd.AddCommand(envCmd)
|
2016-02-06 06:40:16 -05:00
|
|
|
HugoCmd.AddCommand(configCmd)
|
|
|
|
HugoCmd.AddCommand(checkCmd)
|
|
|
|
HugoCmd.AddCommand(benchmarkCmd)
|
|
|
|
HugoCmd.AddCommand(convertCmd)
|
|
|
|
HugoCmd.AddCommand(newCmd)
|
|
|
|
HugoCmd.AddCommand(listCmd)
|
|
|
|
HugoCmd.AddCommand(undraftCmd)
|
|
|
|
HugoCmd.AddCommand(importCmd)
|
|
|
|
|
|
|
|
HugoCmd.AddCommand(genCmd)
|
2015-11-23 10:51:12 -05:00
|
|
|
genCmd.AddCommand(genautocompleteCmd)
|
|
|
|
genCmd.AddCommand(gendocCmd)
|
|
|
|
genCmd.AddCommand(genmanCmd)
|
2017-04-05 10:18:53 -04:00
|
|
|
genCmd.AddCommand(createGenDocsHelper().cmd)
|
2015-12-02 14:00:47 -05:00
|
|
|
}
|
|
|
|
|
2016-02-05 12:41:40 -05:00
|
|
|
// initHugoBuilderFlags initializes all common flags, typically used by the
|
2016-02-06 10:31:20 -05:00
|
|
|
// core build commands, namely hugo itself, server, check and benchmark.
|
2016-02-05 12:41:40 -05:00
|
|
|
func initHugoBuilderFlags(cmd *cobra.Command) {
|
|
|
|
initHugoBuildCommonFlags(cmd)
|
|
|
|
}
|
|
|
|
|
2016-07-07 12:06:48 -04:00
|
|
|
func initRootPersistentFlags() {
|
|
|
|
HugoCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is path/config.yaml|json|toml)")
|
2016-10-08 12:26:16 -04:00
|
|
|
HugoCmd.PersistentFlags().BoolVar(&quiet, "quiet", false, "build in quiet mode")
|
2016-02-06 10:31:20 -05:00
|
|
|
|
|
|
|
// Set bash-completion
|
2015-12-02 14:00:47 -05:00
|
|
|
validConfigFilenames := []string{"json", "js", "yaml", "yml", "toml", "tml"}
|
2017-04-06 11:39:20 -04:00
|
|
|
_ = HugoCmd.PersistentFlags().SetAnnotation("config", cobra.BashCompFilenameExt, validConfigFilenames)
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 10:31:20 -05:00
|
|
|
// initHugoBuildCommonFlags initialize common flags related to the Hugo build.
|
|
|
|
// Called by initHugoBuilderFlags.
|
2016-02-05 12:41:40 -05:00
|
|
|
func initHugoBuildCommonFlags(cmd *cobra.Command) {
|
2017-04-26 15:58:10 -04:00
|
|
|
cmd.Flags().Bool("cleanDestinationDir", false, "remove files from destination not found in static directories")
|
2016-11-03 06:33:53 -04:00
|
|
|
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")
|
2017-04-26 15:58:10 -04:00
|
|
|
cmd.Flags().Bool("disable404", false, "do not render 404 page")
|
|
|
|
cmd.Flags().Bool("disableRSS", false, "do not build RSS files")
|
|
|
|
cmd.Flags().Bool("disableSitemap", false, "do not build Sitemap file")
|
2016-02-05 16:58:17 -05:00
|
|
|
cmd.Flags().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from")
|
2015-12-09 01:45:24 -05:00
|
|
|
cmd.Flags().StringVarP(&contentDir, "contentDir", "c", "", "filesystem path to content directory")
|
|
|
|
cmd.Flags().StringVarP(&layoutDir, "layoutDir", "l", "", "filesystem path to layout directory")
|
2016-02-05 16:58:17 -05:00
|
|
|
cmd.Flags().StringVarP(&cacheDir, "cacheDir", "", "", "filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/")
|
2017-04-26 15:58:10 -04:00
|
|
|
cmd.Flags().BoolP("ignoreCache", "", false, "ignores the cache directory")
|
2016-02-05 16:58:17 -05:00
|
|
|
cmd.Flags().StringVarP(&destination, "destination", "d", "", "filesystem path to write files to")
|
|
|
|
cmd.Flags().StringVarP(&theme, "theme", "t", "", "theme to use (located in /themes/THEMENAME/)")
|
2016-12-22 02:39:58 -05:00
|
|
|
cmd.Flags().StringVarP(&themesDir, "themesDir", "", "", "filesystem path to themes directory")
|
2016-11-03 06:33:53 -04:00
|
|
|
cmd.Flags().Bool("uglyURLs", false, "if true, use /filename.html instead of /filename/")
|
|
|
|
cmd.Flags().Bool("canonifyURLs", false, "if true, all relative URLs will be canonicalized using baseURL")
|
2016-02-05 16:58:17 -05:00
|
|
|
cmd.Flags().StringVarP(&baseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. http://spf13.com/")
|
2017-04-26 15:58:10 -04:00
|
|
|
cmd.Flags().Bool("enableGitInfo", false, "add Git revision, date and author info to the pages")
|
2016-02-05 12:41:40 -05:00
|
|
|
|
|
|
|
cmd.Flags().BoolVar(&nitro.AnalysisOn, "stepAnalysis", false, "display memory and timing of different steps of the program")
|
2017-04-26 15:58:10 -04:00
|
|
|
cmd.Flags().Bool("pluralizeListTitles", true, "pluralize titles in lists using inflect")
|
|
|
|
cmd.Flags().Bool("preserveTaxonomyNames", false, `preserve taxonomy names as written ("Gérard Depardieu" vs "gerard-depardieu")`)
|
|
|
|
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().BoolVarP(&logI18nWarnings, "i18n-warnings", "", false, "print missing translations")
|
2016-02-05 12:41:40 -05:00
|
|
|
|
2017-04-26 15:58:10 -04:00
|
|
|
cmd.Flags().StringSliceVar(&disableKinds, "disableKinds", []string{}, "disable different kind of pages (home, RSS etc.)")
|
2017-02-18 04:02:12 -05:00
|
|
|
|
2016-02-06 10:31:20 -05:00
|
|
|
// Set bash-completion.
|
|
|
|
// Each flag must first be defined before using the SetAnnotation() call.
|
2017-04-06 11:39:20 -04:00
|
|
|
_ = 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"})
|
2016-02-05 12:41:40 -05:00
|
|
|
}
|
|
|
|
|
2016-02-05 15:30:48 -05:00
|
|
|
func initBenchmarkBuildingFlags(cmd *cobra.Command) {
|
|
|
|
cmd.Flags().BoolVar(&renderToMemory, "renderToMemory", false, "render to memory (only useful for benchmark testing)")
|
|
|
|
}
|
|
|
|
|
2015-12-02 13:56:36 -05:00
|
|
|
// init initializes flags.
|
2013-09-29 02:09:03 -04:00
|
|
|
func init() {
|
2016-02-06 06:40:16 -05:00
|
|
|
HugoCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
|
2017-04-26 15:58:10 -04:00
|
|
|
HugoCmd.PersistentFlags().BoolVar(&logging, "log", false, "enable Logging")
|
|
|
|
HugoCmd.PersistentFlags().StringVar(&logFile, "logFile", "", "log File path (if set, logging enabled automatically)")
|
2016-02-06 06:40:16 -05:00
|
|
|
HugoCmd.PersistentFlags().BoolVar(&verboseLog, "verboseLog", false, "verbose logging")
|
2015-12-02 14:00:47 -05:00
|
|
|
|
2016-07-07 12:06:48 -04:00
|
|
|
initRootPersistentFlags()
|
2016-02-06 06:40:16 -05:00
|
|
|
initHugoBuilderFlags(HugoCmd)
|
|
|
|
initBenchmarkBuildingFlags(HugoCmd)
|
2015-05-31 14:30:53 -04:00
|
|
|
|
2016-02-06 06:40:16 -05:00
|
|
|
HugoCmd.Flags().BoolVarP(&buildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
|
|
|
|
hugoCmdV = HugoCmd
|
2015-04-07 07:01:04 -04:00
|
|
|
|
2016-02-06 10:31:20 -05:00
|
|
|
// Set bash-completion
|
2017-04-06 11:39:20 -04:00
|
|
|
_ = HugoCmd.PersistentFlags().SetAnnotation("logFile", cobra.BashCompFilenameExt, []string{})
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
|
|
|
|
2015-05-20 18:50:32 -04:00
|
|
|
// InitializeConfig initializes a config file with sensible default configuration flags.
|
2017-02-04 22:20:06 -05:00
|
|
|
func InitializeConfig(subCmdVs ...*cobra.Command) (*deps.DepsCfg, error) {
|
2017-01-03 11:28:51 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
var cfg *deps.DepsCfg = &deps.DepsCfg{}
|
2017-01-03 11:28:51 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
// Init file systems. This may be changed at a later point.
|
|
|
|
osFs := hugofs.Os
|
|
|
|
|
|
|
|
config, err := hugolib.LoadConfig(osFs, source, cfgFile)
|
|
|
|
if err != nil {
|
2017-01-03 11:28:51 -05:00
|
|
|
return cfg, err
|
2016-08-09 09:31:16 -04:00
|
|
|
}
|
2014-02-17 05:54:15 -05:00
|
|
|
|
2017-03-25 14:48:28 -04:00
|
|
|
// Init file systems. This may be changed at a later point.
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg.Cfg = config
|
|
|
|
|
2017-03-25 09:37:04 -04:00
|
|
|
c, err := newCommandeer(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2015-12-02 14:00:47 -05:00
|
|
|
for _, cmdV := range append([]*cobra.Command{hugoCmdV}, subCmdVs...) {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.initializeFlags(cmdV)
|
|
|
|
}
|
|
|
|
|
2017-02-18 04:02:12 -05:00
|
|
|
if len(disableKinds) > 0 {
|
|
|
|
c.Set("disableKinds", disableKinds)
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
logger, err := createLogger(cfg.Cfg)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, err
|
2015-12-01 11:48:52 -05:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg.Logger = logger
|
|
|
|
|
|
|
|
config.Set("logI18nWarnings", logI18nWarnings)
|
|
|
|
|
2016-02-05 16:58:17 -05:00
|
|
|
if baseURL != "" {
|
2017-02-04 22:20:06 -05:00
|
|
|
config.Set("baseURL", baseURL)
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
2014-04-10 08:10:12 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
if !config.GetBool("relativeURLs") && config.GetString("baseURL") == "" {
|
|
|
|
cfg.Logger.ERROR.Println("No 'baseURL' set in configuration or as a flag. Features like page menus will not work without one.")
|
2015-02-06 04:39:54 -05:00
|
|
|
}
|
|
|
|
|
2016-02-05 16:58:17 -05:00
|
|
|
if theme != "" {
|
2017-02-04 22:20:06 -05:00
|
|
|
config.Set("theme", theme)
|
2014-04-10 08:10:12 -04:00
|
|
|
}
|
|
|
|
|
2016-12-22 02:39:58 -05:00
|
|
|
if themesDir != "" {
|
2017-02-04 22:20:06 -05:00
|
|
|
config.Set("themesDir", themesDir)
|
2016-12-22 02:39:58 -05:00
|
|
|
}
|
|
|
|
|
2016-02-05 16:58:17 -05:00
|
|
|
if destination != "" {
|
2017-02-04 22:20:06 -05:00
|
|
|
config.Set("publishDir", destination)
|
2014-04-05 01:26:43 -04:00
|
|
|
}
|
|
|
|
|
2016-10-24 14:56:00 -04:00
|
|
|
var dir string
|
2016-02-05 16:58:17 -05:00
|
|
|
if source != "" {
|
2016-10-24 14:56:00 -04:00
|
|
|
dir, _ = filepath.Abs(source)
|
2014-04-05 01:26:43 -04:00
|
|
|
} else {
|
2016-10-24 14:56:00 -04:00
|
|
|
dir, _ = os.Getwd()
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
config.Set("workingDir", dir)
|
|
|
|
|
2017-03-25 14:48:28 -04:00
|
|
|
fs := hugofs.NewFrom(osFs, config)
|
2017-02-04 22:20:06 -05:00
|
|
|
|
|
|
|
// Hugo writes the output to memory instead of the disk.
|
|
|
|
// This is only used for benchmark testing. Cause the content is only visible
|
|
|
|
// in memory.
|
|
|
|
if renderToMemory {
|
2017-03-25 14:48:28 -04:00
|
|
|
fs.Destination = new(afero.MemMapFs)
|
2017-02-04 22:20:06 -05:00
|
|
|
// Rendering to memoryFS, publish to Root regardless of publishDir.
|
|
|
|
c.Set("publishDir", "/")
|
|
|
|
}
|
2014-03-31 13:23:34 -04:00
|
|
|
|
2015-12-09 01:45:24 -05:00
|
|
|
if contentDir != "" {
|
2017-02-04 22:20:06 -05:00
|
|
|
config.Set("contentDir", contentDir)
|
2015-12-09 01:45:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if layoutDir != "" {
|
2017-02-04 22:20:06 -05:00
|
|
|
config.Set("layoutDir", layoutDir)
|
2015-12-09 01:45:24 -05:00
|
|
|
}
|
|
|
|
|
2016-10-09 05:00:36 -04:00
|
|
|
if cacheDir != "" {
|
2017-02-04 22:20:06 -05:00
|
|
|
config.Set("cacheDir", cacheDir)
|
2016-10-09 05:00:36 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
cacheDir = config.GetString("cacheDir")
|
2016-02-05 16:58:17 -05:00
|
|
|
if cacheDir != "" {
|
|
|
|
if helpers.FilePathSeparator != cacheDir[len(cacheDir)-1:] {
|
|
|
|
cacheDir = cacheDir + helpers.FilePathSeparator
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
2017-03-25 14:48:28 -04:00
|
|
|
isDir, err := helpers.DirExists(cacheDir, fs.Source)
|
2017-02-21 03:41:56 -05:00
|
|
|
utils.CheckErr(cfg.Logger, err)
|
2016-11-23 12:55:51 -05:00
|
|
|
if !isDir {
|
2016-02-05 16:58:17 -05:00
|
|
|
mkdir(cacheDir)
|
2015-02-02 04:14:59 -05:00
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
config.Set("cacheDir", cacheDir)
|
2014-12-26 22:40:10 -05:00
|
|
|
} else {
|
2017-03-25 14:48:28 -04:00
|
|
|
config.Set("cacheDir", helpers.GetTempDir("hugo_cache", fs.Source))
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
|
|
|
|
2017-04-06 11:39:20 -04:00
|
|
|
if err := c.initFs(fs); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-25 14:48:28 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg.Logger.INFO.Println("Using config file:", viper.ConfigFileUsed())
|
2015-04-28 14:39:11 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
themeDir := c.PathSpec().GetThemeDir()
|
2015-07-12 13:25:43 -04:00
|
|
|
if themeDir != "" {
|
2017-01-10 04:55:03 -05:00
|
|
|
if _, err := cfg.Fs.Source.Stat(themeDir); os.IsNotExist(err) {
|
2017-01-03 11:28:51 -05:00
|
|
|
return cfg, newSystemError("Unable to find theme Directory:", themeDir)
|
2015-07-12 13:25:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
themeVersionMismatch, minVersion := c.isThemeVsHugoVersionMismatch()
|
2015-07-12 13:25:43 -04:00
|
|
|
|
2015-04-28 14:39:11 -04:00
|
|
|
if themeVersionMismatch {
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg.Logger.ERROR.Printf("Current theme does not support Hugo version %s. Minimum version required is %s\n",
|
2017-04-13 10:59:05 -04:00
|
|
|
helpers.CurrentHugoVersion.ReleaseVersion(), minVersion)
|
2015-04-28 14:39:11 -04:00
|
|
|
}
|
2015-12-02 05:42:53 -05:00
|
|
|
|
2017-01-03 11:28:51 -05:00
|
|
|
return cfg, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func createLogger(cfg config.Provider) (*jww.Notepad, error) {
|
2017-01-03 11:28:51 -05:00
|
|
|
var (
|
|
|
|
logHandle = ioutil.Discard
|
2017-02-20 03:53:11 -05:00
|
|
|
logThreshold = jww.LevelWarn
|
|
|
|
logFile = cfg.GetString("logFile")
|
2017-01-03 11:28:51 -05:00
|
|
|
outHandle = os.Stdout
|
|
|
|
stdoutThreshold = jww.LevelError
|
|
|
|
)
|
|
|
|
|
2017-02-20 03:53:11 -05:00
|
|
|
if verboseLog || logging || (logFile != "") {
|
2017-01-03 11:28:51 -05:00
|
|
|
var err error
|
2017-02-20 03:53:11 -05:00
|
|
|
if logFile != "" {
|
|
|
|
logHandle, err = os.OpenFile(logFile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
|
2017-01-03 11:28:51 -05:00
|
|
|
if err != nil {
|
2017-02-20 03:53:11 -05:00
|
|
|
return nil, newSystemError("Failed to open log file:", logFile, err)
|
2017-01-03 11:28:51 -05:00
|
|
|
}
|
|
|
|
} else {
|
2017-02-20 03:53:11 -05:00
|
|
|
logHandle, err = ioutil.TempFile("", "hugo")
|
2017-01-03 11:28:51 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, newSystemError(err)
|
|
|
|
}
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
} else if !quiet && cfg.GetBool("verbose") {
|
2017-01-03 11:28:51 -05:00
|
|
|
stdoutThreshold = jww.LevelInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
if verboseLog {
|
|
|
|
logThreshold = jww.LevelInfo
|
|
|
|
}
|
2016-05-14 00:35:16 -04:00
|
|
|
|
2017-04-04 05:02:12 -04:00
|
|
|
// The global logger is used in some few cases.
|
|
|
|
jww.SetLogOutput(logHandle)
|
|
|
|
jww.SetLogThreshold(logThreshold)
|
|
|
|
jww.SetStdoutThreshold(stdoutThreshold)
|
|
|
|
helpers.InitLoggers()
|
|
|
|
|
2017-01-03 11:28:51 -05:00
|
|
|
return jww.NewNotepad(stdoutThreshold, logThreshold, outHandle, logHandle, "", log.Ldate|log.Ltime), nil
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func (c *commandeer) initializeFlags(cmd *cobra.Command) {
|
2016-11-03 06:33:53 -04:00
|
|
|
persFlagKeys := []string{"verbose", "logFile"}
|
|
|
|
flagKeys := []string{
|
|
|
|
"cleanDestinationDir",
|
|
|
|
"buildDrafts",
|
|
|
|
"buildFuture",
|
|
|
|
"buildExpired",
|
|
|
|
"uglyURLs",
|
|
|
|
"canonifyURLs",
|
|
|
|
"disable404",
|
|
|
|
"disableRSS",
|
|
|
|
"disableSitemap",
|
|
|
|
"enableRobotsTXT",
|
|
|
|
"enableGitInfo",
|
|
|
|
"pluralizeListTitles",
|
|
|
|
"preserveTaxonomyNames",
|
|
|
|
"ignoreCache",
|
|
|
|
"forceSyncStatic",
|
|
|
|
"noTimes",
|
2016-11-29 23:52:52 -05:00
|
|
|
"noChmod",
|
2016-11-03 06:33:53 -04:00
|
|
|
}
|
|
|
|
|
2017-05-10 14:00:08 -04:00
|
|
|
// Remove these in Hugo 0.23.
|
2017-05-24 04:51:48 -04:00
|
|
|
if cmd.Flags().Changed("disable404") {
|
2017-05-10 14:00:08 -04:00
|
|
|
helpers.Deprecated("command line", "--disable404", "Use --disableKinds=404", false)
|
|
|
|
}
|
|
|
|
|
2017-05-24 04:51:48 -04:00
|
|
|
if cmd.Flags().Changed("disableRSS") {
|
2017-05-10 14:00:08 -04:00
|
|
|
helpers.Deprecated("command line", "--disableRSS", "Use --disableKinds=RSS", false)
|
|
|
|
}
|
|
|
|
|
2017-05-24 04:51:48 -04:00
|
|
|
if cmd.Flags().Changed("disableSitemap") {
|
2017-05-10 14:00:08 -04:00
|
|
|
helpers.Deprecated("command line", "--disableSitemap", "Use --disableKinds=sitemap", false)
|
|
|
|
}
|
|
|
|
|
2016-11-03 06:33:53 -04:00
|
|
|
for _, key := range persFlagKeys {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.setValueFromFlag(cmd.PersistentFlags(), key)
|
2016-11-03 06:33:53 -04:00
|
|
|
}
|
|
|
|
for _, key := range flagKeys {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.setValueFromFlag(cmd.Flags(), key)
|
2016-11-03 06:33:53 -04:00
|
|
|
}
|
2017-02-18 04:02:12 -05:00
|
|
|
|
2016-11-03 06:33:53 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func (c *commandeer) setValueFromFlag(flags *flag.FlagSet, key string) {
|
2017-05-24 04:51:48 -04:00
|
|
|
if flags.Changed(key) {
|
2016-11-03 06:33:53 -04:00
|
|
|
f := flags.Lookup(key)
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Set(key, f.Value.String())
|
2016-11-03 06:33:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func (c *commandeer) watchConfig() {
|
|
|
|
v := c.Cfg.(*viper.Viper)
|
|
|
|
v.WatchConfig()
|
|
|
|
v.OnConfigChange(func(e fsnotify.Event) {
|
|
|
|
c.Logger.FEEDBACK.Println("Config file changed:", e.Name)
|
2016-02-14 09:16:55 -05:00
|
|
|
// Force a full rebuild
|
2017-02-21 03:41:56 -05:00
|
|
|
utils.CheckErr(c.Logger, c.recreateAndBuildSites(true))
|
2017-02-04 22:20:06 -05:00
|
|
|
if !c.Cfg.GetBool("disableLiveReload") {
|
2016-01-13 01:34:29 -05:00
|
|
|
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized
|
2015-11-09 23:31:52 -05:00
|
|
|
livereload.ForceRefresh()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func (c *commandeer) build(watches ...bool) error {
|
2017-01-10 04:55:03 -05:00
|
|
|
if err := c.copyStatic(); err != nil {
|
2017-02-04 22:20:06 -05:00
|
|
|
return fmt.Errorf("Error copying static files to %s: %s", c.PathSpec().AbsPathify(c.Cfg.GetString("publishDir")), err)
|
2015-11-16 21:52:37 -05:00
|
|
|
}
|
2014-02-17 05:54:15 -05:00
|
|
|
watch := false
|
|
|
|
if len(watches) > 0 && watches[0] {
|
|
|
|
watch = true
|
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
if err := c.buildSites(buildWatch || watch); err != nil {
|
2015-12-02 05:42:53 -05:00
|
|
|
return fmt.Errorf("Error building site: %s", err)
|
|
|
|
}
|
2014-02-17 05:54:15 -05:00
|
|
|
|
2016-02-05 12:41:40 -05:00
|
|
|
if buildWatch {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.FEEDBACK.Println("Watching for changes in", c.PathSpec().AbsPathify(c.Cfg.GetString("contentDir")))
|
|
|
|
c.Logger.FEEDBACK.Println("Press Ctrl+C to stop")
|
2017-02-21 03:41:56 -05:00
|
|
|
utils.CheckErr(c.Logger, c.newWatcher(0))
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
2015-12-02 05:42:53 -05:00
|
|
|
|
|
|
|
return nil
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func (c *commandeer) getStaticSourceFs() afero.Fs {
|
2017-01-10 04:55:03 -05:00
|
|
|
source := c.Fs.Source
|
2017-02-04 22:20:06 -05:00
|
|
|
themeDir, err := c.PathSpec().GetThemeStaticDirPath()
|
|
|
|
staticDir := c.PathSpec().GetStaticDirPath() + helpers.FilePathSeparator
|
2016-01-13 11:42:43 -05:00
|
|
|
useTheme := true
|
|
|
|
useStatic := true
|
|
|
|
|
|
|
|
if err != nil {
|
2016-12-22 23:44:35 -05:00
|
|
|
if err != helpers.ErrThemeUndefined {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.WARN.Println(err)
|
2016-12-22 23:44:35 -05:00
|
|
|
}
|
2016-01-13 11:42:43 -05:00
|
|
|
useTheme = false
|
|
|
|
} else {
|
|
|
|
if _, err := source.Stat(themeDir); os.IsNotExist(err) {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.WARN.Println("Unable to find Theme Static Directory:", themeDir)
|
2016-01-13 11:42:43 -05:00
|
|
|
useTheme = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := source.Stat(staticDir); os.IsNotExist(err) {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.WARN.Println("Unable to find Static Directory:", staticDir)
|
2016-01-13 11:42:43 -05:00
|
|
|
useStatic = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !useStatic && !useTheme {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !useStatic {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.INFO.Println(themeDir, "is the only static directory available to sync from")
|
2016-01-13 11:42:43 -05:00
|
|
|
return afero.NewReadOnlyFs(afero.NewBasePathFs(source, themeDir))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !useTheme {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.INFO.Println(staticDir, "is the only static directory available to sync from")
|
2016-01-13 11:42:43 -05:00
|
|
|
return afero.NewReadOnlyFs(afero.NewBasePathFs(source, staticDir))
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.INFO.Println("using a UnionFS for static directory comprised of:")
|
|
|
|
c.Logger.INFO.Println("Base:", themeDir)
|
|
|
|
c.Logger.INFO.Println("Overlay:", staticDir)
|
2017-01-10 04:55:03 -05:00
|
|
|
base := afero.NewReadOnlyFs(afero.NewBasePathFs(source, themeDir))
|
|
|
|
overlay := afero.NewReadOnlyFs(afero.NewBasePathFs(source, staticDir))
|
2016-01-13 11:42:43 -05:00
|
|
|
return afero.NewCopyOnWriteFs(base, overlay)
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func (c *commandeer) copyStatic() error {
|
|
|
|
publishDir := c.PathSpec().AbsPathify(c.Cfg.GetString("publishDir")) + helpers.FilePathSeparator
|
2014-04-10 08:10:12 -04:00
|
|
|
|
2015-11-16 21:53:05 -05:00
|
|
|
// If root, remove the second '/'
|
|
|
|
if publishDir == "//" {
|
2015-12-04 08:17:48 -05:00
|
|
|
publishDir = helpers.FilePathSeparator
|
2015-11-16 21:53:05 -05:00
|
|
|
}
|
|
|
|
|
2016-01-25 14:40:44 -05:00
|
|
|
// Includes both theme/static & /static
|
2017-01-10 04:55:03 -05:00
|
|
|
staticSourceFs := c.getStaticSourceFs()
|
2014-11-01 11:57:29 -04:00
|
|
|
|
2016-01-13 11:42:43 -05:00
|
|
|
if staticSourceFs == nil {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.WARN.Println("No static directories found to sync")
|
2016-01-13 11:42:43 -05:00
|
|
|
return nil
|
2015-02-11 14:24:56 -05:00
|
|
|
}
|
2014-04-10 08:10:12 -04:00
|
|
|
|
2016-01-13 11:42:43 -05:00
|
|
|
syncer := fsync.NewSyncer()
|
2017-02-04 22:20:06 -05:00
|
|
|
syncer.NoTimes = c.Cfg.GetBool("noTimes")
|
|
|
|
syncer.NoChmod = c.Cfg.GetBool("noChmod")
|
2016-01-13 11:42:43 -05:00
|
|
|
syncer.SrcFs = staticSourceFs
|
2017-01-10 04:55:03 -05:00
|
|
|
syncer.DestFs = c.Fs.Destination
|
2016-01-13 11:42:43 -05:00
|
|
|
// Now that we are using a unionFs for the static directories
|
|
|
|
// We can effectively clean the publishDir on initial sync
|
2017-02-04 22:20:06 -05:00
|
|
|
syncer.Delete = c.Cfg.GetBool("cleanDestinationDir")
|
2017-03-21 08:10:31 -04:00
|
|
|
|
2016-01-29 16:54:33 -05:00
|
|
|
if syncer.Delete {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.INFO.Println("removing all files from destination that don't exist in static dirs")
|
2017-03-21 08:10:31 -04:00
|
|
|
|
|
|
|
syncer.DeleteFilter = func(f os.FileInfo) bool {
|
|
|
|
return f.IsDir() && strings.HasPrefix(f.Name(), ".")
|
|
|
|
}
|
2016-01-29 16:54:33 -05:00
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.INFO.Println("syncing static files to", publishDir)
|
2014-04-10 08:10:12 -04:00
|
|
|
|
2016-01-25 14:40:44 -05:00
|
|
|
// because we are using a baseFs (to get the union right).
|
|
|
|
// set sync src to root
|
2016-08-30 13:20:39 -04:00
|
|
|
return syncer.Sync(publishDir, helpers.FilePathSeparator)
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
|
|
|
|
2015-03-10 18:55:23 -04:00
|
|
|
// getDirList provides NewWatcher() with a list of directories to watch for changes.
|
2017-02-04 22:20:06 -05:00
|
|
|
func (c *commandeer) getDirList() []string {
|
2014-02-17 05:54:15 -05:00
|
|
|
var a []string
|
2017-02-04 22:20:06 -05:00
|
|
|
dataDir := c.PathSpec().AbsPathify(c.Cfg.GetString("dataDir"))
|
|
|
|
i18nDir := c.PathSpec().AbsPathify(c.Cfg.GetString("i18nDir"))
|
2017-05-08 11:55:48 -04:00
|
|
|
layoutDir := c.PathSpec().GetLayoutDirPath()
|
|
|
|
staticDir := c.PathSpec().GetStaticDirPath()
|
2016-10-19 13:59:15 -04:00
|
|
|
|
2014-02-17 05:54:15 -05:00
|
|
|
walker := func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
2015-03-12 16:44:36 -04:00
|
|
|
if path == dataDir && os.IsNotExist(err) {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.WARN.Println("Skip dataDir:", err)
|
2015-03-12 16:44:36 -04:00
|
|
|
return nil
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
2015-03-12 16:44:36 -04:00
|
|
|
|
2016-05-14 00:35:16 -04:00
|
|
|
if path == i18nDir && os.IsNotExist(err) {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.WARN.Println("Skip i18nDir:", err)
|
2016-05-14 00:35:16 -04:00
|
|
|
return nil
|
2015-07-18 09:14:39 -04:00
|
|
|
}
|
2016-05-14 00:35:16 -04:00
|
|
|
|
2015-07-18 09:14:39 -04:00
|
|
|
if path == layoutDir && os.IsNotExist(err) {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.WARN.Println("Skip layoutDir:", err)
|
2015-07-18 09:14:39 -04:00
|
|
|
return nil
|
2016-08-13 18:35:43 -04:00
|
|
|
}
|
2015-07-18 09:14:39 -04:00
|
|
|
|
2016-08-13 18:35:43 -04:00
|
|
|
if path == staticDir && os.IsNotExist(err) {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.WARN.Println("Skip staticDir:", err)
|
2016-08-13 18:35:43 -04:00
|
|
|
return nil
|
2015-03-12 16:44:36 -04:00
|
|
|
}
|
2016-08-13 18:35:43 -04:00
|
|
|
|
2016-10-19 14:08:57 -04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Ignore.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.ERROR.Println("Walker: ", err)
|
2014-02-17 05:54:15 -05:00
|
|
|
return nil
|
2017-05-15 06:58:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip .git directories.
|
2017-06-13 12:47:17 -04:00
|
|
|
// Related to https://github.com/gohugoio/hugo/issues/3468.
|
2017-05-15 06:58:02 -04:00
|
|
|
if fi.Name() == ".git" {
|
|
|
|
return nil
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
|
|
|
|
2014-12-10 10:48:51 -05:00
|
|
|
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
|
2015-02-17 16:21:37 -05:00
|
|
|
link, err := filepath.EvalSymlinks(path)
|
|
|
|
if err != nil {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", path, err)
|
2015-02-17 16:21:37 -05:00
|
|
|
return nil
|
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
linkfi, err := c.Fs.Source.Stat(link)
|
2015-02-17 16:21:37 -05:00
|
|
|
if err != nil {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.ERROR.Printf("Cannot stat '%s', error was: %s", link, err)
|
2015-02-17 16:21:37 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !linkfi.Mode().IsRegular() {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", path)
|
2015-02-17 16:21:37 -05:00
|
|
|
}
|
2014-12-10 10:48:51 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-17 05:54:15 -05:00
|
|
|
if fi.IsDir() {
|
2015-03-10 18:55:23 -04:00
|
|
|
if fi.Name() == ".git" ||
|
|
|
|
fi.Name() == "node_modules" || fi.Name() == "bower_components" {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
2014-02-17 05:54:15 -05:00
|
|
|
a = append(a, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-06 11:39:20 -04:00
|
|
|
// SymbolicWalk will log anny ERRORs
|
|
|
|
_ = helpers.SymbolicWalk(c.Fs.Source, dataDir, walker)
|
|
|
|
_ = helpers.SymbolicWalk(c.Fs.Source, c.PathSpec().AbsPathify(c.Cfg.GetString("contentDir")), walker)
|
|
|
|
_ = helpers.SymbolicWalk(c.Fs.Source, i18nDir, walker)
|
2017-05-08 11:55:48 -04:00
|
|
|
_ = helpers.SymbolicWalk(c.Fs.Source, layoutDir, walker)
|
2017-04-06 11:39:20 -04:00
|
|
|
_ = helpers.SymbolicWalk(c.Fs.Source, staticDir, walker)
|
2017-05-08 11:55:48 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
if c.PathSpec().ThemeSet() {
|
2017-05-08 11:55:48 -04:00
|
|
|
themesDir := c.PathSpec().GetThemeDir()
|
2017-04-06 11:39:20 -04:00
|
|
|
_ = helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "layouts"), walker)
|
|
|
|
_ = helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "static"), walker)
|
|
|
|
_ = helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "i18n"), walker)
|
|
|
|
_ = helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "data"), walker)
|
2014-04-10 08:10:12 -04:00
|
|
|
}
|
2014-02-17 05:54:15 -05:00
|
|
|
|
|
|
|
return a
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func (c *commandeer) recreateAndBuildSites(watching bool) (err error) {
|
2017-01-10 04:55:03 -05:00
|
|
|
if err := c.initSites(); err != nil {
|
2016-08-07 18:12:06 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-10-08 12:26:16 -04:00
|
|
|
if !quiet {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.FEEDBACK.Println("Started building sites ...")
|
2016-10-08 12:26:16 -04:00
|
|
|
}
|
|
|
|
return Hugo.Build(hugolib.BuildCfg{CreateSitesFromConfig: true, Watching: watching, PrintStats: !quiet})
|
2016-08-06 08:51:50 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func (c *commandeer) resetAndBuildSites(watching bool) (err error) {
|
2017-01-10 04:55:03 -05:00
|
|
|
if err = c.initSites(); err != nil {
|
|
|
|
return
|
2016-08-07 18:12:06 -04:00
|
|
|
}
|
2016-10-08 12:26:16 -04:00
|
|
|
if !quiet {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.FEEDBACK.Println("Started building sites ...")
|
2016-10-08 12:26:16 -04:00
|
|
|
}
|
|
|
|
return Hugo.Build(hugolib.BuildCfg{ResetState: true, Watching: watching, PrintStats: !quiet})
|
2016-08-06 08:51:50 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func (c *commandeer) initSites() error {
|
2016-08-06 08:51:50 -04:00
|
|
|
if Hugo != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
h, err := hugolib.NewHugoSites(*c.DepsCfg)
|
2016-08-06 08:51:50 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
Hugo = h
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func (c *commandeer) buildSites(watching bool) (err error) {
|
2017-01-10 04:55:03 -05:00
|
|
|
if err := c.initSites(); err != nil {
|
2016-08-07 18:12:06 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-10-08 12:26:16 -04:00
|
|
|
if !quiet {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.FEEDBACK.Println("Started building sites ...")
|
2016-10-08 12:26:16 -04:00
|
|
|
}
|
|
|
|
return Hugo.Build(hugolib.BuildCfg{Watching: watching, PrintStats: !quiet})
|
2015-12-22 00:10:01 -05:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
|
2017-01-10 04:55:03 -05:00
|
|
|
if err := c.initSites(); err != nil {
|
2016-08-07 18:12:06 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-11-10 14:55:52 -05:00
|
|
|
return Hugo.Build(hugolib.BuildCfg{PrintStats: !quiet, Watching: true}, events...)
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
2013-09-30 22:38:32 -04:00
|
|
|
|
2017-01-03 11:28:51 -05:00
|
|
|
// newWatcher creates a new watcher to watch filesystem events.
|
2017-02-04 22:20:06 -05:00
|
|
|
func (c *commandeer) newWatcher(port int) error {
|
2017-06-12 14:29:47 -04:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
tweakLimit()
|
|
|
|
}
|
|
|
|
|
2014-02-17 05:54:15 -05:00
|
|
|
watcher, err := watcher.New(1 * time.Second)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
for _, d := range c.getDirList() {
|
2014-02-17 05:54:15 -05:00
|
|
|
if d != "" {
|
2015-03-10 11:59:55 -04:00
|
|
|
_ = watcher.Add(d)
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2015-03-10 11:59:55 -04:00
|
|
|
case evs := <-watcher.Events:
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.INFO.Println("Received System Events:", evs)
|
2014-02-17 05:54:15 -05:00
|
|
|
|
2016-01-26 14:12:18 -05:00
|
|
|
staticEvents := []fsnotify.Event{}
|
|
|
|
dynamicEvents := []fsnotify.Event{}
|
2014-02-17 05:54:15 -05:00
|
|
|
|
|
|
|
for _, ev := range evs {
|
|
|
|
ext := filepath.Ext(ev.Name)
|
2016-07-10 20:01:45 -04:00
|
|
|
baseName := filepath.Base(ev.Name)
|
2016-02-12 05:54:37 -05:00
|
|
|
istemp := strings.HasSuffix(ext, "~") ||
|
2016-06-02 15:25:19 -04:00
|
|
|
(ext == ".swp") || // vim
|
|
|
|
(ext == ".swx") || // vim
|
|
|
|
(ext == ".tmp") || // generic temp file
|
|
|
|
(ext == ".DS_Store") || // OSX Thumbnail
|
2016-07-10 20:01:45 -04:00
|
|
|
baseName == "4913" || // vim
|
2016-06-02 15:25:19 -04:00
|
|
|
strings.HasPrefix(ext, ".goutputstream") || // gnome
|
|
|
|
strings.HasSuffix(ext, "jb_old___") || // intelliJ
|
2016-06-02 15:29:52 -04:00
|
|
|
strings.HasSuffix(ext, "jb_tmp___") || // intelliJ
|
2016-06-02 15:25:19 -04:00
|
|
|
strings.HasSuffix(ext, "jb_bak___") || // intelliJ
|
2016-07-10 20:01:45 -04:00
|
|
|
strings.HasPrefix(ext, ".sb-") || // byword
|
|
|
|
strings.HasPrefix(baseName, ".#") || // emacs
|
|
|
|
strings.HasPrefix(baseName, "#") // emacs
|
2014-02-17 05:54:15 -05:00
|
|
|
if istemp {
|
|
|
|
continue
|
|
|
|
}
|
2016-01-13 11:42:43 -05:00
|
|
|
// Sometimes during rm -rf operations a '"": REMOVE' is triggered. Just ignore these
|
|
|
|
if ev.Name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-11-18 16:59:32 -05:00
|
|
|
// Write and rename operations are often followed by CHMOD.
|
|
|
|
// There may be valid use cases for rebuilding the site on CHMOD,
|
|
|
|
// but that will require more complex logic than this simple conditional.
|
|
|
|
// On OS X this seems to be related to Spotlight, see:
|
|
|
|
// https://github.com/go-fsnotify/fsnotify/issues/15
|
|
|
|
// A workaround is to put your site(s) on the Spotlight exception list,
|
|
|
|
// but that may be a little mysterious for most end users.
|
|
|
|
// So, for now, we skip reload on CHMOD.
|
2016-07-26 16:18:15 -04:00
|
|
|
// We do have to check for WRITE though. On slower laptops a Chmod
|
|
|
|
// could be aggregated with other important events, and we still want
|
|
|
|
// to rebuild on those
|
|
|
|
if ev.Op&(fsnotify.Chmod|fsnotify.Write|fsnotify.Create) == fsnotify.Chmod {
|
2015-11-18 16:59:32 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-28 09:31:25 -05:00
|
|
|
walkAdder := func(path string, f os.FileInfo, err error) error {
|
2016-01-13 11:42:43 -05:00
|
|
|
if f.IsDir() {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.FEEDBACK.Println("adding created directory to watchlist", path)
|
2017-04-06 11:39:20 -04:00
|
|
|
if err := watcher.Add(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-08 14:00:05 -04:00
|
|
|
} else if !c.isStatic(path) {
|
|
|
|
// Hugo's rebuilding logic is entirely file based. When you drop a new folder into
|
|
|
|
// /content on OSX, the above logic will handle future watching of those files,
|
|
|
|
// but the initial CREATE is lost.
|
|
|
|
dynamicEvents = append(dynamicEvents, fsnotify.Event{Name: path, Op: fsnotify.Create})
|
2016-01-13 11:42:43 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// recursively add new directories to watch list
|
|
|
|
// When mkdir -p is used, only the top directory triggers an event (at least on OSX)
|
|
|
|
if ev.Op&fsnotify.Create == fsnotify.Create {
|
2017-01-10 04:55:03 -05:00
|
|
|
if s, err := c.Fs.Source.Stat(ev.Name); err == nil && s.Mode().IsDir() {
|
2017-04-06 11:39:20 -04:00
|
|
|
_ = helpers.SymbolicWalk(c.Fs.Source, ev.Name, walkAdder)
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
|
|
|
}
|
2016-01-11 12:06:52 -05:00
|
|
|
|
2017-06-08 14:00:05 -04:00
|
|
|
if c.isStatic(ev.Name) {
|
2016-01-11 12:06:52 -05:00
|
|
|
staticEvents = append(staticEvents, ev)
|
|
|
|
} else {
|
|
|
|
dynamicEvents = append(dynamicEvents, ev)
|
|
|
|
}
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
|
|
|
|
2016-01-11 12:06:52 -05:00
|
|
|
if len(staticEvents) > 0 {
|
2017-02-04 22:20:06 -05:00
|
|
|
publishDir := c.PathSpec().AbsPathify(c.Cfg.GetString("publishDir")) + helpers.FilePathSeparator
|
2016-01-13 11:42:43 -05:00
|
|
|
|
|
|
|
// If root, remove the second '/'
|
|
|
|
if publishDir == "//" {
|
|
|
|
publishDir = helpers.FilePathSeparator
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.FEEDBACK.Println("\nStatic file changes detected")
|
2016-01-13 11:42:43 -05:00
|
|
|
const layout = "2006-01-02 15:04 -0700"
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.FEEDBACK.Println(time.Now().Format(layout))
|
2016-01-13 11:42:43 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
if c.Cfg.GetBool("forceSyncStatic") {
|
|
|
|
c.Logger.FEEDBACK.Printf("Syncing all static files\n")
|
2017-01-10 04:55:03 -05:00
|
|
|
err := c.copyStatic()
|
2015-12-04 08:17:48 -05:00
|
|
|
if err != nil {
|
2017-02-21 03:41:56 -05:00
|
|
|
utils.StopOnErr(c.Logger, err, fmt.Sprintf("Error copying static files to %s", publishDir))
|
2015-12-04 08:17:48 -05:00
|
|
|
}
|
|
|
|
} else {
|
2017-01-10 04:55:03 -05:00
|
|
|
staticSourceFs := c.getStaticSourceFs()
|
2015-12-04 08:17:48 -05:00
|
|
|
|
2016-01-13 11:42:43 -05:00
|
|
|
if staticSourceFs == nil {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.WARN.Println("No static directories found to sync")
|
2016-01-13 11:42:43 -05:00
|
|
|
return
|
2015-12-04 08:17:48 -05:00
|
|
|
}
|
|
|
|
|
2016-01-13 11:42:43 -05:00
|
|
|
syncer := fsync.NewSyncer()
|
2017-02-04 22:20:06 -05:00
|
|
|
syncer.NoTimes = c.Cfg.GetBool("noTimes")
|
|
|
|
syncer.NoChmod = c.Cfg.GetBool("noChmod")
|
2016-01-13 11:42:43 -05:00
|
|
|
syncer.SrcFs = staticSourceFs
|
2017-01-10 04:55:03 -05:00
|
|
|
syncer.DestFs = c.Fs.Destination
|
2016-01-02 11:44:57 -05:00
|
|
|
|
2016-01-28 09:31:25 -05:00
|
|
|
// prevent spamming the log on changes
|
|
|
|
logger := helpers.NewDistinctFeedbackLogger()
|
2016-01-02 11:44:57 -05:00
|
|
|
|
2016-01-11 12:06:52 -05:00
|
|
|
for _, ev := range staticEvents {
|
2016-01-13 11:42:43 -05:00
|
|
|
// Due to our approach of layering both directories and the content's rendered output
|
|
|
|
// into one we can't accurately remove a file not in one of the source directories.
|
|
|
|
// If a file is in the local static dir and also in the theme static dir and we remove
|
|
|
|
// it from one of those locations we expect it to still exist in the destination
|
|
|
|
//
|
2016-01-26 14:12:18 -05:00
|
|
|
// If Hugo generates a file (from the content dir) over a static file
|
|
|
|
// the content generated file should take precedence.
|
|
|
|
//
|
|
|
|
// Because we are now watching and handling individual events it is possible that a static
|
|
|
|
// event that occupies the same path as a content generated file will take precedence
|
|
|
|
// until a regeneration of the content takes places.
|
|
|
|
//
|
|
|
|
// Hugo assumes that these cases are very rare and will permit this bad behavior
|
|
|
|
// The alternative is to track every single file and which pipeline rendered it
|
|
|
|
// and then to handle conflict resolution on every event.
|
2016-01-13 11:42:43 -05:00
|
|
|
|
2016-01-11 12:06:52 -05:00
|
|
|
fromPath := ev.Name
|
2016-01-02 11:44:57 -05:00
|
|
|
|
2016-01-11 12:06:52 -05:00
|
|
|
// If we are here we already know the event took place in a static dir
|
2017-02-04 22:20:06 -05:00
|
|
|
relPath, err := c.PathSpec().MakeStaticPathRelative(fromPath)
|
2016-01-11 12:06:52 -05:00
|
|
|
if err != nil {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.ERROR.Println(err)
|
2016-01-11 12:06:52 -05:00
|
|
|
continue
|
2016-01-02 11:44:57 -05:00
|
|
|
}
|
2016-01-11 12:06:52 -05:00
|
|
|
|
2016-01-26 14:12:18 -05:00
|
|
|
// Remove || rename is harder and will require an assumption.
|
|
|
|
// Hugo takes the following approach:
|
|
|
|
// If the static file exists in any of the static source directories after this event
|
|
|
|
// Hugo will re-sync it.
|
|
|
|
// If it does not exist in all of the static directories Hugo will remove it.
|
|
|
|
//
|
|
|
|
// This assumes that Hugo has not generated content on top of a static file and then removed
|
|
|
|
// the source of that static file. In this case Hugo will incorrectly remove that file
|
|
|
|
// from the published directory.
|
2016-01-11 12:06:52 -05:00
|
|
|
if ev.Op&fsnotify.Rename == fsnotify.Rename || ev.Op&fsnotify.Remove == fsnotify.Remove {
|
2016-01-13 11:42:43 -05:00
|
|
|
if _, err := staticSourceFs.Stat(relPath); os.IsNotExist(err) {
|
|
|
|
// If file doesn't exist in any static dir, remove it
|
2016-01-28 09:31:25 -05:00
|
|
|
toRemove := filepath.Join(publishDir, relPath)
|
|
|
|
logger.Println("File no longer exists in static dir, removing", toRemove)
|
2017-04-06 11:39:20 -04:00
|
|
|
_ = c.Fs.Destination.RemoveAll(toRemove)
|
2016-01-13 11:42:43 -05:00
|
|
|
} else if err == nil {
|
|
|
|
// If file still exists, sync it
|
2016-01-28 09:31:25 -05:00
|
|
|
logger.Println("Syncing", relPath, "to", publishDir)
|
2016-03-01 10:38:56 -05:00
|
|
|
if err := syncer.Sync(filepath.Join(publishDir, relPath), relPath); err != nil {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.ERROR.Println(err)
|
2016-03-01 10:38:56 -05:00
|
|
|
}
|
2016-01-13 11:42:43 -05:00
|
|
|
} else {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.ERROR.Println(err)
|
2016-01-13 11:42:43 -05:00
|
|
|
}
|
|
|
|
|
2016-01-11 12:06:52 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-26 14:12:18 -05:00
|
|
|
// For all other event operations Hugo will sync static.
|
2016-01-28 09:31:25 -05:00
|
|
|
logger.Println("Syncing", relPath, "to", publishDir)
|
2016-03-01 10:38:56 -05:00
|
|
|
if err := syncer.Sync(filepath.Join(publishDir, relPath), relPath); err != nil {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.ERROR.Println(err)
|
2016-03-01 10:38:56 -05:00
|
|
|
}
|
2015-12-04 08:17:48 -05:00
|
|
|
}
|
2015-11-16 21:52:37 -05:00
|
|
|
}
|
2014-05-16 11:48:59 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
if !buildWatch && !c.Cfg.GetBool("disableLiveReload") {
|
2016-01-13 01:34:29 -05:00
|
|
|
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized
|
2014-09-10 06:21:22 -04:00
|
|
|
|
|
|
|
// force refresh when more than one file
|
2016-01-13 11:42:43 -05:00
|
|
|
if len(staticEvents) > 0 {
|
2016-01-11 12:06:52 -05:00
|
|
|
for _, ev := range staticEvents {
|
2017-02-04 22:20:06 -05:00
|
|
|
path, _ := c.PathSpec().MakeStaticPathRelative(ev.Name)
|
2014-09-10 06:21:22 -04:00
|
|
|
livereload.RefreshPath(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
livereload.ForceRefresh()
|
|
|
|
}
|
2014-08-25 13:49:53 -04:00
|
|
|
}
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
|
|
|
|
2016-01-13 11:42:43 -05:00
|
|
|
if len(dynamicEvents) > 0 {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.FEEDBACK.Println("\nChange detected, rebuilding site")
|
2014-10-27 17:23:54 -04:00
|
|
|
const layout = "2006-01-02 15:04 -0700"
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.FEEDBACK.Println(time.Now().Format(layout))
|
2015-12-22 00:10:01 -05:00
|
|
|
|
2017-02-21 03:46:56 -05:00
|
|
|
if err := c.rebuildSites(dynamicEvents); err != nil {
|
|
|
|
c.Logger.ERROR.Println("Failed to rebuild site:", err)
|
|
|
|
}
|
2014-05-16 11:48:59 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
if !buildWatch && !c.Cfg.GetBool("disableLiveReload") {
|
2017-06-26 15:34:16 -04:00
|
|
|
|
|
|
|
navigate := c.Cfg.GetBool("navigateToChanged")
|
|
|
|
|
|
|
|
var p *hugolib.Page
|
|
|
|
|
|
|
|
if navigate {
|
|
|
|
|
|
|
|
// It is probably more confusing than useful
|
|
|
|
// to navigate to a new URL on RENAME etc.
|
|
|
|
// so for now we use the WRITE event only.
|
|
|
|
name := pickOneWritePath(dynamicEvents)
|
|
|
|
|
|
|
|
if name != "" {
|
|
|
|
p = Hugo.GetContentPage(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if p != nil {
|
|
|
|
livereload.NavigateToPath(p.RelPermalink())
|
|
|
|
} else {
|
|
|
|
livereload.ForceRefresh()
|
|
|
|
}
|
2014-08-25 13:49:53 -04:00
|
|
|
}
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
2015-03-10 11:59:55 -04:00
|
|
|
case err := <-watcher.Errors:
|
2014-02-17 05:54:15 -05:00
|
|
|
if err != nil {
|
2017-02-04 22:20:06 -05:00
|
|
|
c.Logger.ERROR.Println(err)
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if port > 0 {
|
2017-02-04 22:20:06 -05:00
|
|
|
if !c.Cfg.GetBool("disableLiveReload") {
|
2014-05-16 17:49:27 -04:00
|
|
|
livereload.Initialize()
|
|
|
|
http.HandleFunc("/livereload.js", livereload.ServeJS)
|
|
|
|
http.HandleFunc("/livereload", livereload.Handler)
|
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
go c.serve(port)
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
return nil
|
2013-09-30 22:38:32 -04:00
|
|
|
}
|
2015-09-14 11:31:39 -04:00
|
|
|
|
2017-06-26 15:34:16 -04:00
|
|
|
func pickOneWritePath(events []fsnotify.Event) string {
|
2017-06-27 05:17:43 -04:00
|
|
|
name := ""
|
|
|
|
|
2017-06-26 15:34:16 -04:00
|
|
|
for _, ev := range events {
|
2017-06-27 05:17:43 -04:00
|
|
|
if ev.Op&fsnotify.Write == fsnotify.Write && len(ev.Name) > len(name) {
|
|
|
|
name = ev.Name
|
2017-06-26 15:34:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 05:17:43 -04:00
|
|
|
return name
|
2017-06-26 15:34:16 -04:00
|
|
|
}
|
|
|
|
|
2017-06-08 14:00:05 -04:00
|
|
|
func (c *commandeer) isStatic(path string) bool {
|
|
|
|
return strings.HasPrefix(path, c.PathSpec().GetStaticDirPath()) || (len(c.PathSpec().GetThemesDirPath()) > 0 && strings.HasPrefix(path, c.PathSpec().GetThemesDirPath()))
|
|
|
|
}
|
|
|
|
|
2015-12-02 13:56:36 -05:00
|
|
|
// isThemeVsHugoVersionMismatch returns whether the current Hugo version is
|
|
|
|
// less than the theme's min_version.
|
2017-02-04 22:20:06 -05:00
|
|
|
func (c *commandeer) isThemeVsHugoVersionMismatch() (mismatch bool, requiredMinVersion string) {
|
|
|
|
if !c.PathSpec().ThemeSet() {
|
2015-09-14 11:31:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
themeDir := c.PathSpec().GetThemeDir()
|
2015-09-14 11:31:39 -04:00
|
|
|
|
|
|
|
path := filepath.Join(themeDir, "theme.toml")
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
exists, err := helpers.Exists(path, c.Fs.Source)
|
2015-09-14 11:31:39 -04:00
|
|
|
|
|
|
|
if err != nil || !exists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
b, err := afero.ReadFile(c.Fs.Source, path)
|
2015-09-14 11:31:39 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
tomlMeta, err := parser.HandleTOMLMetaData(b)
|
2015-09-14 11:31:39 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
config := tomlMeta.(map[string]interface{})
|
2015-09-14 11:31:39 -04:00
|
|
|
|
|
|
|
if minVersion, ok := config["min_version"]; ok {
|
2017-03-01 09:03:28 -05:00
|
|
|
return helpers.CompareVersion(minVersion) > 0, fmt.Sprint(minVersion)
|
2015-09-14 11:31:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|