hugo/commands/hugo.go

433 lines
14 KiB
Go
Raw Normal View History

// Copyright © 2013 Steve Francia <spf@spf13.com>.
//
// Licensed under the Simple Public 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://opensource.org/licenses/Simple-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.
2014-12-11 18:05:02 +00:00
//Package commands defines and implements command-line commands and flags used by Hugo. Commands and flags are implemented using
//cobra.
package commands
import (
2014-02-17 10:54:15 +00:00
"fmt"
"net/http"
2014-02-17 10:54:15 +00:00
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/spf13/cobra"
"github.com/spf13/fsync"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/hugolib"
"github.com/spf13/hugo/livereload"
"github.com/spf13/hugo/utils"
"github.com/spf13/hugo/watcher"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/nitro"
"github.com/spf13/viper"
)
2014-12-11 18:05:02 +00:00
//HugoCmd is Hugo's root command. Every other command attached to HugoCmd is a child command to it.
var HugoCmd = &cobra.Command{
2014-02-17 10:54:15 +00:00
Use: "hugo",
Short: "Hugo is a very fast static site generator",
Long: `A Fast and Flexible Static Site Generator built with
love by spf13 and friends in Go.
2014-09-27 07:35:42 +00:00
Complete documentation is available at http://gohugo.io`,
2014-02-17 10:54:15 +00:00
Run: func(cmd *cobra.Command, args []string) {
InitializeConfig()
build()
},
}
var hugoCmdV *cobra.Command
2014-12-11 18:05:02 +00:00
//Flags that are to be added to commands.
2014-11-06 02:13:22 +00:00
var BuildWatch, Draft, Future, UglyUrls, Verbose, Logging, VerboseLog, DisableRSS, DisableSitemap, PluralizeListTitles, NoTimes bool
var Source, Destination, Theme, BaseUrl, CfgFile, LogFile, Editor string
2014-12-11 18:05:02 +00:00
//Execute adds all child commands to the root command HugoCmd and sets flags appropriately.
func Execute() {
2014-02-17 10:54:15 +00:00
AddCommands()
utils.StopOnErr(HugoCmd.Execute())
}
2014-12-11 18:05:02 +00:00
//AddCommands adds child commands to the root command HugoCmd.
func AddCommands() {
2014-02-17 10:54:15 +00:00
HugoCmd.AddCommand(serverCmd)
HugoCmd.AddCommand(version)
HugoCmd.AddCommand(config)
2014-02-17 10:54:15 +00:00
HugoCmd.AddCommand(check)
HugoCmd.AddCommand(benchmark)
HugoCmd.AddCommand(convertCmd)
HugoCmd.AddCommand(newCmd)
HugoCmd.AddCommand(listCmd)
}
2014-12-11 18:05:02 +00:00
//Initializes flags
func init() {
HugoCmd.PersistentFlags().BoolVarP(&Draft, "buildDrafts", "D", false, "include content marked as draft")
HugoCmd.PersistentFlags().BoolVarP(&Future, "buildFuture", "F", false, "include content with datePublished in the future")
HugoCmd.PersistentFlags().BoolVar(&DisableRSS, "disableRSS", false, "Do not build RSS files")
2014-05-06 10:50:23 +00:00
HugoCmd.PersistentFlags().BoolVar(&DisableSitemap, "disableSitemap", false, "Do not build Sitemap file")
2014-02-17 10:54:15 +00:00
HugoCmd.PersistentFlags().StringVarP(&Source, "source", "s", "", "filesystem path to read files relative from")
HugoCmd.PersistentFlags().StringVarP(&Destination, "destination", "d", "", "filesystem path to write files to")
2014-04-10 12:10:12 +00:00
HugoCmd.PersistentFlags().StringVarP(&Theme, "theme", "t", "", "theme to use (located in /themes/THEMENAME/)")
2014-02-17 10:54:15 +00:00
HugoCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
HugoCmd.PersistentFlags().BoolVar(&UglyUrls, "uglyUrls", false, "if true, use /filename.html instead of /filename/")
HugoCmd.PersistentFlags().StringVarP(&BaseUrl, "baseUrl", "b", "", "hostname (and path) to the root eg. http://spf13.com/")
2014-02-17 10:54:15 +00:00
HugoCmd.PersistentFlags().StringVar(&CfgFile, "config", "", "config file (default is path/config.yaml|json|toml)")
HugoCmd.PersistentFlags().StringVar(&Editor, "editor", "", "edit new content with this editor, if provided")
HugoCmd.PersistentFlags().BoolVar(&Logging, "log", false, "Enable Logging")
HugoCmd.PersistentFlags().StringVar(&LogFile, "logFile", "", "Log File path (if set, logging enabled automatically)")
HugoCmd.PersistentFlags().BoolVar(&VerboseLog, "verboseLog", false, "verbose logging")
2014-02-17 10:54:15 +00:00
HugoCmd.PersistentFlags().BoolVar(&nitro.AnalysisOn, "stepAnalysis", false, "display memory and timing of different steps of the program")
HugoCmd.PersistentFlags().BoolVar(&PluralizeListTitles, "pluralizeListTitles", true, "Pluralize titles in lists using inflect")
2014-02-17 10:54:15 +00:00
HugoCmd.Flags().BoolVarP(&BuildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
2014-11-06 02:13:22 +00:00
HugoCmd.Flags().BoolVarP(&NoTimes, "noTimes", "", false, "Don't sync modification time of files")
2014-02-17 10:54:15 +00:00
hugoCmdV = HugoCmd
}
2014-12-11 18:05:02 +00:00
// InitializeConfig initializes a config file with sensible default configuration flags.
func InitializeConfig() {
viper.SetConfigFile(CfgFile)
viper.AddConfigPath(Source)
2014-04-10 18:24:18 +00:00
err := viper.ReadInConfig()
if err != nil {
jww.ERROR.Println("Unable to locate Config file. Perhaps you need to create a new site. Run `hugo help new` for details")
2014-04-10 18:24:18 +00:00
}
viper.RegisterAlias("indexes", "taxonomies")
viper.SetDefault("Watch", false)
viper.SetDefault("MetaDataFormat", "toml")
viper.SetDefault("DisableRSS", false)
2014-05-06 10:50:23 +00:00
viper.SetDefault("DisableSitemap", false)
viper.SetDefault("ContentDir", "content")
viper.SetDefault("LayoutDir", "layouts")
viper.SetDefault("StaticDir", "static")
viper.SetDefault("ArchetypeDir", "archetypes")
viper.SetDefault("PublishDir", "public")
viper.SetDefault("DataDir", "data")
viper.SetDefault("DefaultLayout", "post")
viper.SetDefault("BuildDrafts", false)
viper.SetDefault("BuildFuture", false)
viper.SetDefault("UglyUrls", false)
viper.SetDefault("Verbose", false)
viper.SetDefault("CanonifyUrls", false)
viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "categories"})
viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0))
2014-05-07 06:56:57 +00:00
viper.SetDefault("Sitemap", hugolib.Sitemap{Priority: -1})
viper.SetDefault("PygmentsStyle", "monokai")
viper.SetDefault("DefaultExtension", "html")
viper.SetDefault("PygmentsUseClasses", false)
viper.SetDefault("DisableLiveReload", false)
viper.SetDefault("PluralizeListTitles", true)
viper.SetDefault("FootnoteAnchorPrefix", "")
viper.SetDefault("FootnoteReturnLinkContents", "")
viper.SetDefault("NewContentEditor", "")
viper.SetDefault("Paginate", 10)
viper.SetDefault("PaginatePath", "page")
2015-01-31 17:24:00 +00:00
viper.SetDefault("Blackfriday", helpers.NewBlackfriday())
2014-02-17 10:54:15 +00:00
if hugoCmdV.PersistentFlags().Lookup("buildDrafts").Changed {
viper.Set("BuildDrafts", Draft)
2014-02-17 10:54:15 +00:00
}
if hugoCmdV.PersistentFlags().Lookup("buildFuture").Changed {
viper.Set("BuildFuture", Future)
}
if hugoCmdV.PersistentFlags().Lookup("uglyUrls").Changed {
viper.Set("UglyUrls", UglyUrls)
2014-02-17 10:54:15 +00:00
}
if hugoCmdV.PersistentFlags().Lookup("disableRSS").Changed {
viper.Set("DisableRSS", DisableRSS)
}
2014-05-06 10:50:23 +00:00
if hugoCmdV.PersistentFlags().Lookup("disableSitemap").Changed {
viper.Set("DisableSitemap", DisableSitemap)
}
2014-02-17 10:54:15 +00:00
if hugoCmdV.PersistentFlags().Lookup("verbose").Changed {
viper.Set("Verbose", Verbose)
2014-02-17 10:54:15 +00:00
}
if hugoCmdV.PersistentFlags().Lookup("pluralizeListTitles").Changed {
viper.Set("PluralizeListTitles", PluralizeListTitles)
}
if hugoCmdV.PersistentFlags().Lookup("editor").Changed {
viper.Set("NewContentEditor", Editor)
}
if hugoCmdV.PersistentFlags().Lookup("logFile").Changed {
viper.Set("LogFile", LogFile)
}
2014-02-17 10:54:15 +00:00
if BaseUrl != "" {
if !strings.HasSuffix(BaseUrl, "/") {
BaseUrl = BaseUrl + "/"
}
viper.Set("BaseUrl", BaseUrl)
2014-02-17 10:54:15 +00:00
}
2014-04-10 12:10:12 +00:00
if viper.GetString("BaseUrl") == "" {
jww.ERROR.Println("No 'baseurl' set in configuration or as a flag. Features like page menus will not work without one.")
}
2014-04-10 12:10:12 +00:00
if Theme != "" {
viper.Set("theme", Theme)
}
2014-02-17 10:54:15 +00:00
if Destination != "" {
viper.Set("PublishDir", Destination)
}
if Source != "" {
viper.Set("WorkingDir", Source)
} else {
dir, _ := os.Getwd()
viper.Set("WorkingDir", dir)
2014-02-17 10:54:15 +00:00
}
if VerboseLog || Logging || (viper.IsSet("LogFile") && viper.GetString("LogFile") != "") {
if viper.IsSet("LogFile") && viper.GetString("LogFile") != "" {
jww.SetLogFile(viper.GetString("LogFile"))
} else {
jww.UseTempLogFile("hugo")
}
} else {
jww.DiscardLogging()
}
if viper.GetBool("verbose") {
2014-05-27 22:29:55 +00:00
jww.SetStdoutThreshold(jww.LevelInfo)
}
if VerboseLog {
2014-05-27 22:29:55 +00:00
jww.SetLogThreshold(jww.LevelInfo)
}
jww.INFO.Println("Using config file:", viper.ConfigFileUsed())
}
func build(watches ...bool) {
utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", helpers.AbsPathify(viper.GetString("PublishDir"))))
2014-02-17 10:54:15 +00:00
watch := false
if len(watches) > 0 && watches[0] {
watch = true
}
utils.StopOnErr(buildSite(BuildWatch || watch))
if BuildWatch {
jww.FEEDBACK.Println("Watching for changes in", helpers.AbsPathify(viper.GetString("ContentDir")))
2015-01-29 21:19:12 +00:00
jww.FEEDBACK.Println("Press Ctrl+C to stop")
2014-02-17 10:54:15 +00:00
utils.CheckErr(NewWatcher(0))
}
}
func copyStatic() error {
staticDir := helpers.AbsPathify(viper.GetString("StaticDir")) + "/"
2014-02-17 10:54:15 +00:00
if _, err := os.Stat(staticDir); os.IsNotExist(err) {
jww.ERROR.Println("Unable to find Static Directory:", staticDir)
2014-02-17 10:54:15 +00:00
return nil
}
publishDir := helpers.AbsPathify(viper.GetString("PublishDir")) + "/"
2014-04-10 12:10:12 +00:00
syncer := fsync.NewSyncer()
2014-11-06 02:13:22 +00:00
syncer.NoTimes = viper.GetBool("notimes")
syncer.SrcFs = hugofs.SourceFs
syncer.DestFs = hugofs.DestinationFS
themeDir, err := helpers.GetThemeStaticDirPath()
if err != nil {
jww.ERROR.Println(err)
return nil
}
2014-04-10 12:10:12 +00:00
2015-02-14 23:30:15 +00:00
if themeDir != "" {
// Copy Static to Destination
jww.INFO.Println("syncing from", themeDir, "to", publishDir)
utils.CheckErr(syncer.Sync(publishDir, themeDir), fmt.Sprintf("Error copying static files of theme to %s", publishDir))
}
2014-04-10 12:10:12 +00:00
2014-02-17 10:54:15 +00:00
// Copy Static to Destination
jww.INFO.Println("syncing from", staticDir, "to", publishDir)
return syncer.Sync(publishDir, staticDir)
}
func getDirList() []string {
2014-02-17 10:54:15 +00:00
var a []string
walker := func(path string, fi os.FileInfo, err error) error {
if err != nil {
jww.ERROR.Println("Walker: ", err)
2014-02-17 10:54:15 +00:00
return nil
}
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
jww.ERROR.Printf("Symbolic links not supported, skipping '%s'", path)
return nil
}
2014-02-17 10:54:15 +00:00
if fi.IsDir() {
a = append(a, path)
}
return nil
}
filepath.Walk(helpers.AbsPathify(viper.GetString("DataDir")), walker)
filepath.Walk(helpers.AbsPathify(viper.GetString("ContentDir")), walker)
filepath.Walk(helpers.AbsPathify(viper.GetString("LayoutDir")), walker)
filepath.Walk(helpers.AbsPathify(viper.GetString("StaticDir")), walker)
if helpers.ThemeSet() {
2014-04-10 12:10:12 +00:00
filepath.Walk(helpers.AbsPathify("themes/"+viper.GetString("theme")), walker)
}
2014-02-17 10:54:15 +00:00
return a
}
func buildSite(watching ...bool) (err error) {
2014-02-17 10:54:15 +00:00
startTime := time.Now()
site := &hugolib.Site{}
2014-02-17 10:54:15 +00:00
if len(watching) > 0 && watching[0] {
site.RunMode.Watching = true
}
err = site.Build()
if err != nil {
2014-08-10 16:58:10 +00:00
return err
2014-02-17 10:54:15 +00:00
}
site.Stats()
jww.FEEDBACK.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds()))
2014-02-17 10:54:15 +00:00
return nil
}
2014-12-11 18:05:02 +00:00
//NewWatcher creates a new watcher to watch filesystem events.
func NewWatcher(port int) error {
2014-02-17 10:54:15 +00:00
if runtime.GOOS == "darwin" {
tweakLimit()
}
watcher, err := watcher.New(1 * time.Second)
var wg sync.WaitGroup
if err != nil {
fmt.Println(err)
return err
}
defer watcher.Close()
wg.Add(1)
for _, d := range getDirList() {
if d != "" {
_ = watcher.Watch(d)
}
}
go func() {
for {
select {
case evs := <-watcher.Event:
jww.INFO.Println("File System Event:", evs)
2014-02-17 10:54:15 +00:00
static_changed := false
dynamic_changed := false
static_files_changed := make(map[string]bool)
2014-02-17 10:54:15 +00:00
for _, ev := range evs {
ext := filepath.Ext(ev.Name)
istemp := strings.HasSuffix(ext, "~") || (ext == ".swp") || (ext == ".swx") || (ext == ".tmp") || (strings.HasPrefix(ext, ".goutputstream"))
2014-02-17 10:54:15 +00:00
if istemp {
continue
}
// renames are always followed with Create/Modify
if ev.IsRename() {
continue
}
isstatic := strings.HasPrefix(ev.Name, helpers.GetStaticDirPath()) || strings.HasPrefix(ev.Name, helpers.GetThemesDirPath())
2014-02-17 10:54:15 +00:00
static_changed = static_changed || isstatic
dynamic_changed = dynamic_changed || !isstatic
if isstatic {
if staticPath, err := helpers.MakeStaticPathRelative(ev.Name); err == nil {
static_files_changed[staticPath] = true
}
}
2014-02-17 10:54:15 +00:00
// add new directory to watch list
if s, err := os.Stat(ev.Name); err == nil && s.Mode().IsDir() {
if ev.IsCreate() {
watcher.Watch(ev.Name)
}
}
}
if static_changed {
2014-10-31 06:13:44 +00:00
jww.FEEDBACK.Println("Static file changed, syncing\n")
utils.StopOnErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", helpers.AbsPathify(viper.GetString("PublishDir"))))
2014-10-31 06:13:44 +00:00
if !BuildWatch && !viper.GetBool("DisableLiveReload") {
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initalized
// force refresh when more than one file
if len(static_files_changed) == 1 {
for path := range static_files_changed {
livereload.RefreshPath(path)
}
} else {
livereload.ForceRefresh()
}
}
2014-02-17 10:54:15 +00:00
}
if dynamic_changed {
fmt.Print("\nChange detected, rebuilding site\n")
const layout = "2006-01-02 15:04 -0700"
fmt.Println(time.Now().Format(layout))
utils.CheckErr(buildSite(true))
2014-10-31 06:13:44 +00:00
if !BuildWatch && !viper.GetBool("DisableLiveReload") {
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initalized
livereload.ForceRefresh()
}
2014-02-17 10:54:15 +00:00
}
case err := <-watcher.Error:
if err != nil {
fmt.Println("error:", err)
}
}
}
}()
if port > 0 {
if !viper.GetBool("DisableLiveReload") {
livereload.Initialize()
http.HandleFunc("/livereload.js", livereload.ServeJS)
http.HandleFunc("/livereload", livereload.Handler)
}
2014-02-17 10:54:15 +00:00
go serve(port)
}
wg.Wait()
return nil
}