2015-03-10 11:59:55 -04:00
// Copyright © 2013-2015 Steve Francia <spf@spf13.com>.
2013-09-29 02:09:03 -04:00
//
// 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 13:05:02 -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"
2015-09-14 11:31:39 -04:00
"io/ioutil"
2014-05-16 17:49:27 -04:00
"net/http"
2014-02-17 05:54:15 -05:00
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
2014-03-31 13:23:34 -04:00
2015-11-09 23:31:52 -05:00
"github.com/spf13/hugo/parser"
2014-03-31 13:23:34 -04:00
"github.com/spf13/cobra"
2014-11-01 11:57:29 -04:00
"github.com/spf13/fsync"
2014-04-05 01:26:43 -04:00
"github.com/spf13/hugo/helpers"
2014-11-01 11:57:29 -04:00
"github.com/spf13/hugo/hugofs"
2014-03-31 13:23:34 -04:00
"github.com/spf13/hugo/hugolib"
2014-05-16 17:49:27 -04:00
"github.com/spf13/hugo/livereload"
2014-03-31 13:23:34 -04:00
"github.com/spf13/hugo/utils"
"github.com/spf13/hugo/watcher"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/nitro"
2014-04-05 01:26:43 -04:00
"github.com/spf13/viper"
2015-03-10 11:59:55 -04:00
"gopkg.in/fsnotify.v1"
2013-09-29 02:09:03 -04:00
)
2014-12-11 13:05:02 -05:00
//HugoCmd is Hugo's root command. Every other command attached to HugoCmd is a child command to it.
2013-09-29 02:09:03 -04: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/.`,
2014-02-17 05:54:15 -05:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
InitializeConfig ( )
2015-11-09 23:31:52 -05:00
watchConfig ( )
2014-02-17 05:54:15 -05:00
build ( )
} ,
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
2014-12-11 13:05:02 -05:00
//Flags that are to be added to commands.
2015-10-10 04:50:55 -04:00
var BuildWatch , IgnoreCache , Draft , Future , UglyURLs , CanonifyURLs , Verbose , Logging , VerboseLog , DisableRSS , DisableSitemap , PluralizeListTitles , PreserveTaxonomyNames , NoTimes bool
2015-03-11 13:34:57 -04:00
var Source , CacheDir , Destination , Theme , BaseURL , CfgFile , LogFile , Editor string
2013-09-29 02:09:03 -04:00
2014-12-11 13:05:02 -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 ( ) {
2015-10-02 00:47:34 -04:00
HugoCmd . SetGlobalNormalizationFunc ( helpers . NormalizeHugoFlags )
2014-02-17 05:54:15 -05:00
AddCommands ( )
2015-10-05 14:26:49 -04:00
if err := HugoCmd . Execute ( ) ; err != nil {
// the err is already logged by Cobra
os . Exit ( - 1 )
}
2013-09-29 02:09:03 -04:00
}
2014-12-11 13:05:02 -05:00
//AddCommands adds child commands to the root command HugoCmd.
2013-09-29 02:09:03 -04:00
func AddCommands ( ) {
2014-02-17 05:54:15 -05:00
HugoCmd . AddCommand ( serverCmd )
HugoCmd . AddCommand ( version )
2015-02-01 16:05:47 -05:00
HugoCmd . AddCommand ( config )
2014-02-17 05:54:15 -05:00
HugoCmd . AddCommand ( check )
HugoCmd . AddCommand ( benchmark )
2014-05-01 13:23:32 -04:00
HugoCmd . AddCommand ( convertCmd )
2014-05-02 01:06:01 -04:00
HugoCmd . AddCommand ( newCmd )
2014-11-19 16:24:30 -05:00
HugoCmd . AddCommand ( listCmd )
2015-03-15 18:32:41 -04:00
HugoCmd . AddCommand ( undraftCmd )
2015-05-16 12:04:56 -04:00
HugoCmd . AddCommand ( genautocompleteCmd )
2015-05-13 09:31:30 -04:00
HugoCmd . AddCommand ( gendocCmd )
2015-09-30 21:04:30 -04:00
HugoCmd . AddCommand ( importCmd )
2013-09-29 02:09:03 -04:00
}
2014-12-11 13:05:02 -05:00
//Initializes flags
2013-09-29 02:09:03 -04:00
func init ( ) {
2014-05-16 17:49:27 -04:00
HugoCmd . PersistentFlags ( ) . BoolVarP ( & Draft , "buildDrafts" , "D" , false , "include content marked as draft" )
2015-05-10 14:38:05 -04:00
HugoCmd . PersistentFlags ( ) . BoolVarP ( & Future , "buildFuture" , "F" , false , "include content with publishdate in the future" )
2014-04-09 17:45:34 -04:00
HugoCmd . PersistentFlags ( ) . BoolVar ( & DisableRSS , "disableRSS" , false , "Do not build RSS files" )
2014-05-06 06:50:23 -04:00
HugoCmd . PersistentFlags ( ) . BoolVar ( & DisableSitemap , "disableSitemap" , false , "Do not build Sitemap file" )
2014-02-17 05:54:15 -05:00
HugoCmd . PersistentFlags ( ) . StringVarP ( & Source , "source" , "s" , "" , "filesystem path to read files relative from" )
2015-02-08 17:22:50 -05:00
HugoCmd . PersistentFlags ( ) . StringVarP ( & CacheDir , "cacheDir" , "" , "" , "filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/" )
2015-02-02 04:14:59 -05:00
HugoCmd . PersistentFlags ( ) . BoolVarP ( & IgnoreCache , "ignoreCache" , "" , false , "Ignores the cache directory for reading but still writes to it" )
2014-02-17 05:54:15 -05:00
HugoCmd . PersistentFlags ( ) . StringVarP ( & Destination , "destination" , "d" , "" , "filesystem path to write files to" )
2014-04-10 08:10:12 -04:00
HugoCmd . PersistentFlags ( ) . StringVarP ( & Theme , "theme" , "t" , "" , "theme to use (located in /themes/THEMENAME/)" )
2014-02-17 05:54:15 -05:00
HugoCmd . PersistentFlags ( ) . BoolVarP ( & Verbose , "verbose" , "v" , false , "verbose output" )
2015-09-09 01:05:11 -04:00
HugoCmd . PersistentFlags ( ) . BoolVar ( & UglyURLs , "uglyURLs" , false , "if true, use /filename.html instead of /filename/" )
2015-10-10 04:50:55 -04:00
HugoCmd . PersistentFlags ( ) . BoolVar ( & CanonifyURLs , "canonifyURLs" , false , "if true, all relative URLs will be canonicalized using baseURL" )
2015-09-09 01:05:11 -04:00
HugoCmd . PersistentFlags ( ) . StringVarP ( & BaseURL , "baseURL" , "b" , "" , "hostname (and path) to the root, e.g. http://spf13.com/" )
2014-02-17 05:54:15 -05:00
HugoCmd . PersistentFlags ( ) . StringVar ( & CfgFile , "config" , "" , "config file (default is path/config.yaml|json|toml)" )
2014-10-14 22:48:55 -04:00
HugoCmd . PersistentFlags ( ) . StringVar ( & Editor , "editor" , "" , "edit new content with this editor, if provided" )
2014-03-31 13:23:34 -04:00
HugoCmd . PersistentFlags ( ) . BoolVar ( & Logging , "log" , false , "Enable Logging" )
2014-05-16 17:49:27 -04:00
HugoCmd . PersistentFlags ( ) . StringVar ( & LogFile , "logFile" , "" , "Log File path (if set, logging enabled automatically)" )
HugoCmd . PersistentFlags ( ) . BoolVar ( & VerboseLog , "verboseLog" , false , "verbose logging" )
2014-02-17 05:54:15 -05:00
HugoCmd . PersistentFlags ( ) . BoolVar ( & nitro . AnalysisOn , "stepAnalysis" , false , "display memory and timing of different steps of the program" )
2014-06-24 17:44:16 -04:00
HugoCmd . PersistentFlags ( ) . BoolVar ( & PluralizeListTitles , "pluralizeListTitles" , true , "Pluralize titles in lists using inflect" )
2015-05-31 14:30:53 -04:00
HugoCmd . PersistentFlags ( ) . BoolVar ( & PreserveTaxonomyNames , "preserveTaxonomyNames" , false , ` Preserve taxonomy names as written ("Gérard Depardieu" vs "gerard-depardieu") ` )
2014-02-17 05:54:15 -05:00
HugoCmd . Flags ( ) . BoolVarP ( & BuildWatch , "watch" , "w" , false , "watch filesystem for changes and recreate as needed" )
2014-11-05 21:13:22 -05:00
HugoCmd . Flags ( ) . BoolVarP ( & NoTimes , "noTimes" , "" , false , "Don't sync modification time of files" )
2014-02-17 05:54:15 -05:00
hugoCmdV = HugoCmd
2015-04-07 07:01:04 -04:00
2015-05-16 12:04:56 -04:00
// for Bash autocomplete
validConfigFilenames := [ ] string { "json" , "js" , "yaml" , "yml" , "toml" , "tml" }
2015-08-08 00:50:36 -04:00
HugoCmd . PersistentFlags ( ) . SetAnnotation ( "config" , cobra . BashCompFilenameExt , validConfigFilenames )
2015-08-09 20:49:54 -04:00
HugoCmd . PersistentFlags ( ) . SetAnnotation ( "theme" , cobra . BashCompSubdirsInDir , [ ] string { "themes" } )
2015-05-16 12:04:56 -04:00
2015-04-07 09:17:24 -04:00
// This message will be shown to Windows users if Hugo is opened from explorer.exe
2015-04-07 07:01:04 -04:00
cobra . MousetrapHelpText = `
2015-01-06 12:11:06 -05:00
2015-04-07 07:01:04 -04:00
Hugo is a command line tool
You need to open cmd . exe and run it from there . `
2013-09-29 02:09:03 -04:00
}
2015-05-20 18:50:32 -04:00
func LoadDefaultSettings ( ) {
2014-05-16 17:49:27 -04:00
viper . SetDefault ( "Watch" , false )
2014-05-29 18:40:16 -04:00
viper . SetDefault ( "MetaDataFormat" , "toml" )
2014-04-09 17:45:34 -04:00
viper . SetDefault ( "DisableRSS" , false )
2014-05-06 06:50:23 -04:00
viper . SetDefault ( "DisableSitemap" , false )
2014-04-05 01:26:43 -04:00
viper . SetDefault ( "ContentDir" , "content" )
viper . SetDefault ( "LayoutDir" , "layouts" )
viper . SetDefault ( "StaticDir" , "static" )
2014-05-02 01:06:01 -04:00
viper . SetDefault ( "ArchetypeDir" , "archetypes" )
2014-04-05 01:26:43 -04:00
viper . SetDefault ( "PublishDir" , "public" )
2015-01-20 17:08:01 -05:00
viper . SetDefault ( "DataDir" , "data" )
2014-04-05 01:26:43 -04:00
viper . SetDefault ( "DefaultLayout" , "post" )
viper . SetDefault ( "BuildDrafts" , false )
2014-05-29 00:48:40 -04:00
viper . SetDefault ( "BuildFuture" , false )
2015-03-11 13:34:57 -04:00
viper . SetDefault ( "UglyURLs" , false )
2014-04-05 01:26:43 -04:00
viper . SetDefault ( "Verbose" , false )
2015-02-02 04:14:59 -05:00
viper . SetDefault ( "IgnoreCache" , false )
2015-03-11 13:34:57 -04:00
viper . SetDefault ( "CanonifyURLs" , false )
2015-05-15 18:11:39 -04:00
viper . SetDefault ( "RelativeURLs" , false )
2015-06-16 13:25:48 -04:00
viper . SetDefault ( "RemovePathAccents" , false )
2015-02-16 15:14:47 -05:00
viper . SetDefault ( "Taxonomies" , map [ string ] string { "tag" : "tags" , "category" : "categories" } )
2014-04-05 01:26:43 -04:00
viper . SetDefault ( "Permalinks" , make ( hugolib . PermalinkOverrides , 0 ) )
2014-05-07 02:56:57 -04:00
viper . SetDefault ( "Sitemap" , hugolib . Sitemap { Priority : - 1 } )
2014-05-07 12:38:14 -04:00
viper . SetDefault ( "PygmentsStyle" , "monokai" )
2014-10-16 20:20:09 -04:00
viper . SetDefault ( "DefaultExtension" , "html" )
2014-05-07 12:38:14 -04:00
viper . SetDefault ( "PygmentsUseClasses" , false )
2015-07-03 17:53:50 -04:00
viper . SetDefault ( "PygmentsCodeFences" , false )
2014-05-16 17:49:27 -04:00
viper . SetDefault ( "DisableLiveReload" , false )
2014-06-24 17:44:16 -04:00
viper . SetDefault ( "PluralizeListTitles" , true )
2015-05-31 14:30:53 -04:00
viper . SetDefault ( "PreserveTaxonomyNames" , false )
2014-09-26 23:44:09 -04:00
viper . SetDefault ( "FootnoteAnchorPrefix" , "" )
viper . SetDefault ( "FootnoteReturnLinkContents" , "" )
2014-10-14 22:48:55 -04:00
viper . SetDefault ( "NewContentEditor" , "" )
2015-01-26 09:26:19 -05:00
viper . SetDefault ( "Paginate" , 10 )
2014-12-27 08:11:19 -05:00
viper . SetDefault ( "PaginatePath" , "page" )
2015-01-31 12:24:00 -05:00
viper . SetDefault ( "Blackfriday" , helpers . NewBlackfriday ( ) )
2015-04-24 14:25:09 -04:00
viper . SetDefault ( "RSSUri" , "index.xml" )
2015-01-06 12:11:06 -05:00
viper . SetDefault ( "SectionPagesMenu" , "" )
2015-09-01 08:53:25 -04:00
viper . SetDefault ( "DisablePathToLower" , false )
2015-09-03 06:22:20 -04:00
viper . SetDefault ( "HasCJKLanguage" , false )
2015-05-20 18:50:32 -04:00
}
// InitializeConfig initializes a config file with sensible default configuration flags.
func InitializeConfig ( ) {
viper . SetConfigFile ( CfgFile )
2015-08-19 02:36:22 -04:00
// See https://github.com/spf13/viper/issues/73#issuecomment-126970794
if Source == "" {
viper . AddConfigPath ( "." )
} else {
viper . AddConfigPath ( Source )
}
2015-05-20 18:50:32 -04:00
err := viper . ReadInConfig ( )
if err != nil {
2015-11-11 16:47:09 -05:00
if _ , ok := err . ( viper . ConfigParseError ) ; ok {
jww . ERROR . Println ( err )
} else {
jww . ERROR . Println ( "Unable to locate Config file. Perhaps you need to create a new site. Run `hugo help new` for details" , err )
}
2015-05-20 18:50:32 -04:00
}
viper . RegisterAlias ( "indexes" , "taxonomies" )
LoadDefaultSettings ( )
2014-02-17 05:54:15 -05:00
2014-05-16 17:49:27 -04:00
if hugoCmdV . PersistentFlags ( ) . Lookup ( "buildDrafts" ) . Changed {
2014-04-05 01:26:43 -04:00
viper . Set ( "BuildDrafts" , Draft )
2014-02-17 05:54:15 -05:00
}
2014-05-29 00:48:40 -04:00
if hugoCmdV . PersistentFlags ( ) . Lookup ( "buildFuture" ) . Changed {
viper . Set ( "BuildFuture" , Future )
}
2015-09-09 01:05:11 -04:00
if hugoCmdV . PersistentFlags ( ) . Lookup ( "uglyURLs" ) . Changed {
2015-03-11 13:34:57 -04:00
viper . Set ( "UglyURLs" , UglyURLs )
2014-02-17 05:54:15 -05:00
}
2015-10-10 04:50:55 -04:00
if hugoCmdV . PersistentFlags ( ) . Lookup ( "canonifyURLs" ) . Changed {
viper . Set ( "CanonifyURLs" , CanonifyURLs )
}
2014-04-09 17:45:34 -04:00
if hugoCmdV . PersistentFlags ( ) . Lookup ( "disableRSS" ) . Changed {
viper . Set ( "DisableRSS" , DisableRSS )
}
2014-05-06 06:50:23 -04:00
if hugoCmdV . PersistentFlags ( ) . Lookup ( "disableSitemap" ) . Changed {
viper . Set ( "DisableSitemap" , DisableSitemap )
}
2015-06-16 13:25:48 -04:00
2014-02-17 05:54:15 -05:00
if hugoCmdV . PersistentFlags ( ) . Lookup ( "verbose" ) . Changed {
2014-04-05 01:26:43 -04:00
viper . Set ( "Verbose" , Verbose )
2014-02-17 05:54:15 -05:00
}
2014-03-31 13:23:34 -04:00
2014-06-24 17:44:16 -04:00
if hugoCmdV . PersistentFlags ( ) . Lookup ( "pluralizeListTitles" ) . Changed {
viper . Set ( "PluralizeListTitles" , PluralizeListTitles )
}
2015-05-31 14:30:53 -04:00
if hugoCmdV . PersistentFlags ( ) . Lookup ( "preserveTaxonomyNames" ) . Changed {
viper . Set ( "PreserveTaxonomyNames" , PreserveTaxonomyNames )
}
2014-10-14 22:48:55 -04:00
if hugoCmdV . PersistentFlags ( ) . Lookup ( "editor" ) . Changed {
viper . Set ( "NewContentEditor" , Editor )
}
2014-05-16 17:49:27 -04:00
if hugoCmdV . PersistentFlags ( ) . Lookup ( "logFile" ) . Changed {
2014-04-05 01:26:43 -04:00
viper . Set ( "LogFile" , LogFile )
2014-03-31 13:23:34 -04:00
}
2015-03-11 13:34:57 -04:00
if BaseURL != "" {
if ! strings . HasSuffix ( BaseURL , "/" ) {
BaseURL = BaseURL + "/"
2014-04-05 01:26:43 -04:00
}
2015-03-11 13:34:57 -04:00
viper . Set ( "BaseURL" , BaseURL )
2014-02-17 05:54:15 -05:00
}
2014-04-10 08:10:12 -04:00
2015-05-16 09:42:10 -04:00
if ! viper . GetBool ( "RelativeURLs" ) && viper . GetString ( "BaseURL" ) == "" {
2015-02-06 04:39:54 -05:00
jww . ERROR . Println ( "No 'baseurl' set in configuration or as a flag. Features like page menus will not work without one." )
}
2014-04-10 08:10:12 -04:00
if Theme != "" {
viper . Set ( "theme" , Theme )
}
2014-02-17 05:54:15 -05:00
if Destination != "" {
2014-04-05 01:26:43 -04:00
viper . Set ( "PublishDir" , Destination )
}
if Source != "" {
viper . Set ( "WorkingDir" , Source )
} else {
2014-11-20 19:40:45 -05:00
dir , _ := os . Getwd ( )
2014-04-05 01:26:43 -04:00
viper . Set ( "WorkingDir" , dir )
2014-02-17 05:54:15 -05:00
}
2014-03-31 13:23:34 -04:00
2015-02-02 04:14:59 -05:00
if hugoCmdV . PersistentFlags ( ) . Lookup ( "ignoreCache" ) . Changed {
viper . Set ( "IgnoreCache" , IgnoreCache )
}
2014-12-26 22:40:10 -05:00
if CacheDir != "" {
if helpers . FilePathSeparator != CacheDir [ len ( CacheDir ) - 1 : ] {
CacheDir = CacheDir + helpers . FilePathSeparator
}
2015-02-02 04:14:59 -05:00
isDir , err := helpers . DirExists ( CacheDir , hugofs . SourceFs )
utils . CheckErr ( err )
if isDir == false {
mkdir ( CacheDir )
}
2014-12-26 22:40:10 -05:00
viper . Set ( "CacheDir" , CacheDir )
} else {
viper . Set ( "CacheDir" , helpers . GetTempDir ( "hugo_cache" , hugofs . SourceFs ) )
}
2014-04-05 01:26:43 -04:00
if VerboseLog || Logging || ( viper . IsSet ( "LogFile" ) && viper . GetString ( "LogFile" ) != "" ) {
if viper . IsSet ( "LogFile" ) && viper . GetString ( "LogFile" ) != "" {
jww . SetLogFile ( viper . GetString ( "LogFile" ) )
2014-03-31 13:23:34 -04:00
} else {
jww . UseTempLogFile ( "hugo" )
}
} else {
jww . DiscardLogging ( )
}
2014-04-05 01:26:43 -04:00
if viper . GetBool ( "verbose" ) {
2014-05-27 18:29:55 -04:00
jww . SetStdoutThreshold ( jww . LevelInfo )
2014-03-31 13:23:34 -04:00
}
if VerboseLog {
2014-05-27 18:29:55 -04:00
jww . SetLogThreshold ( jww . LevelInfo )
2014-03-31 13:23:34 -04:00
}
2014-04-07 23:29:35 -04:00
jww . INFO . Println ( "Using config file:" , viper . ConfigFileUsed ( ) )
2015-04-28 14:39:11 -04:00
2015-07-12 13:25:43 -04:00
themeDir := helpers . GetThemeDir ( )
if themeDir != "" {
if _ , err := os . Stat ( themeDir ) ; os . IsNotExist ( err ) {
2015-07-13 04:38:28 -04:00
jww . FATAL . Fatalln ( "Unable to find theme Directory:" , themeDir )
2015-07-12 13:25:43 -04:00
}
}
2015-09-14 11:31:39 -04:00
themeVersionMismatch , minVersion := isThemeVsHugoVersionMismatch ( )
2015-07-12 13:25:43 -04:00
2015-04-28 14:39:11 -04:00
if themeVersionMismatch {
jww . ERROR . Printf ( "Current theme does not support Hugo version %s. Minimum version required is %s\n" ,
helpers . HugoReleaseVersion ( ) , minVersion )
}
2013-09-29 02:09:03 -04:00
}
2015-11-09 23:31:52 -05:00
func watchConfig ( ) {
viper . WatchConfig ( )
viper . OnConfigChange ( func ( e fsnotify . Event ) {
fmt . Println ( "Config file changed:" , e . Name )
utils . CheckErr ( buildSite ( true ) )
if ! viper . GetBool ( "DisableLiveReload" ) {
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initalized
livereload . ForceRefresh ( )
}
} )
}
2013-10-25 18:03:14 -04:00
func build ( watches ... bool ) {
2014-04-05 01:26:43 -04:00
utils . CheckErr ( copyStatic ( ) , fmt . Sprintf ( "Error copying static files to %s" , helpers . AbsPathify ( viper . GetString ( "PublishDir" ) ) ) )
2014-02-17 05:54:15 -05:00
watch := false
if len ( watches ) > 0 && watches [ 0 ] {
watch = true
}
utils . StopOnErr ( buildSite ( BuildWatch || watch ) )
if BuildWatch {
2014-04-05 01:26:43 -04:00
jww . FEEDBACK . Println ( "Watching for changes in" , helpers . AbsPathify ( viper . GetString ( "ContentDir" ) ) )
2015-01-29 16:19:12 -05:00
jww . FEEDBACK . Println ( "Press Ctrl+C to stop" )
2014-02-17 05:54:15 -05:00
utils . CheckErr ( NewWatcher ( 0 ) )
}
2013-09-29 02:09:03 -04:00
}
func copyStatic ( ) error {
2014-04-05 01:26:43 -04:00
publishDir := helpers . AbsPathify ( viper . GetString ( "PublishDir" ) ) + "/"
2014-04-10 08:10:12 -04:00
2014-11-01 11:57:29 -04:00
syncer := fsync . NewSyncer ( )
2014-11-05 21:13:22 -05:00
syncer . NoTimes = viper . GetBool ( "notimes" )
2014-11-01 11:57:29 -04:00
syncer . SrcFs = hugofs . SourceFs
syncer . DestFs = hugofs . DestinationFS
2015-02-11 14:24:56 -05:00
themeDir , err := helpers . GetThemeStaticDirPath ( )
if err != nil {
jww . ERROR . Println ( err )
return nil
}
2014-04-10 08:10:12 -04:00
2015-10-15 03:36:27 -04:00
// Copy the theme's static directory
2015-02-14 18:30:15 -05:00
if themeDir != "" {
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 08:10:12 -04:00
2015-10-15 03:36:27 -04:00
// Copy the site's own static directory
staticDir := helpers . AbsPathify ( viper . GetString ( "StaticDir" ) ) + "/"
if _ , err := os . Stat ( staticDir ) ; err == nil {
jww . INFO . Println ( "syncing from" , staticDir , "to" , publishDir )
return syncer . Sync ( publishDir , staticDir )
} else if os . IsNotExist ( err ) {
jww . WARN . Println ( "Unable to find Static Directory:" , staticDir )
}
return nil
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.
2013-09-29 02:09:03 -04:00
func getDirList ( ) [ ] string {
2014-02-17 05:54:15 -05:00
var a [ ] string
2015-03-12 16:44:36 -04:00
dataDir := helpers . AbsPathify ( viper . GetString ( "DataDir" ) )
2015-07-18 09:14:39 -04:00
layoutDir := helpers . AbsPathify ( viper . GetString ( "LayoutDir" ) )
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 ) {
jww . WARN . Println ( "Skip DataDir:" , err )
return nil
2015-07-18 09:14:39 -04:00
}
if path == layoutDir && os . IsNotExist ( err ) {
jww . WARN . Println ( "Skip LayoutDir:" , err )
return nil
2015-03-12 16:44:36 -04:00
}
2014-03-31 13:23:34 -04:00
jww . ERROR . Println ( "Walker: " , err )
2014-02-17 05:54:15 -05:00
return nil
}
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 {
jww . ERROR . Printf ( "Cannot read symbolic link '%s', error was: %s" , path , err )
return nil
}
linkfi , err := os . Stat ( link )
if err != nil {
jww . ERROR . Printf ( "Cannot stat '%s', error was: %s" , link , err )
return nil
}
if ! linkfi . Mode ( ) . IsRegular ( ) {
jww . ERROR . Printf ( "Symbolic links for directories not supported, skipping '%s'" , path )
}
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
}
2015-03-12 16:44:36 -04:00
filepath . Walk ( dataDir , walker )
2014-04-05 01:26:43 -04:00
filepath . Walk ( helpers . AbsPathify ( viper . GetString ( "ContentDir" ) ) , walker )
filepath . Walk ( helpers . AbsPathify ( viper . GetString ( "LayoutDir" ) ) , walker )
filepath . Walk ( helpers . AbsPathify ( viper . GetString ( "StaticDir" ) ) , walker )
2015-02-11 14:24:56 -05:00
if helpers . ThemeSet ( ) {
2014-04-10 08:10:12 -04:00
filepath . Walk ( helpers . AbsPathify ( "themes/" + viper . GetString ( "theme" ) ) , walker )
}
2014-02-17 05:54:15 -05:00
return a
2013-09-29 02:09:03 -04:00
}
2013-10-25 18:03:14 -04:00
func buildSite ( watching ... bool ) ( err error ) {
2014-02-17 05:54:15 -05:00
startTime := time . Now ( )
2014-04-05 01:26:43 -04:00
site := & hugolib . Site { }
2014-02-17 05:54:15 -05:00
if len ( watching ) > 0 && watching [ 0 ] {
site . RunMode . Watching = true
}
err = site . Build ( )
if err != nil {
2014-08-10 12:58:10 -04:00
return err
2014-02-17 05:54:15 -05:00
}
site . Stats ( )
2014-03-31 13:23:34 -04:00
jww . FEEDBACK . Printf ( "in %v ms\n" , int ( 1000 * time . Since ( startTime ) . Seconds ( ) ) )
2014-05-16 11:48:59 -04:00
2014-02-17 05:54:15 -05:00
return nil
2013-09-29 02:09:03 -04:00
}
2013-09-30 22:38:32 -04:00
2015-03-10 11:59:55 -04:00
// NewWatcher creates a new watcher to watch filesystem events.
2013-09-30 22:38:32 -04:00
func NewWatcher ( port int ) error {
2014-02-17 05:54:15 -05: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 != "" {
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 :
2014-08-25 13:49:53 -04:00
jww . INFO . Println ( "File System Event:" , evs )
2014-02-17 05:54:15 -05:00
2015-03-06 12:07:50 -05:00
staticChanged := false
dynamicChanged := false
staticFilesChanged := make ( map [ string ] bool )
2014-02-17 05:54:15 -05:00
for _ , ev := range evs {
ext := filepath . Ext ( ev . Name )
2015-03-10 11:59:55 -04:00
istemp := strings . HasSuffix ( ext , "~" ) || ( ext == ".swp" ) || ( ext == ".swx" ) || ( ext == ".tmp" ) || strings . HasPrefix ( ext , ".goutputstream" )
2014-02-17 05:54:15 -05:00
if istemp {
continue
}
// renames are always followed with Create/Modify
2015-03-10 11:59:55 -04:00
if ev . Op & fsnotify . Rename == fsnotify . Rename {
2014-02-17 05:54:15 -05:00
continue
}
2015-06-28 13:27:28 -04:00
isstatic := strings . HasPrefix ( ev . Name , helpers . GetStaticDirPath ( ) ) || ( len ( helpers . GetThemesDirPath ( ) ) > 0 && strings . HasPrefix ( ev . Name , helpers . GetThemesDirPath ( ) ) )
2015-03-06 12:07:50 -05:00
staticChanged = staticChanged || isstatic
dynamicChanged = dynamicChanged || ! isstatic
2014-02-17 05:54:15 -05:00
2014-09-10 06:21:22 -04:00
if isstatic {
if staticPath , err := helpers . MakeStaticPathRelative ( ev . Name ) ; err == nil {
2015-03-06 12:07:50 -05:00
staticFilesChanged [ staticPath ] = true
2014-09-10 06:21:22 -04:00
}
}
2014-02-17 05:54:15 -05:00
// add new directory to watch list
if s , err := os . Stat ( ev . Name ) ; err == nil && s . Mode ( ) . IsDir ( ) {
2015-03-10 11:59:55 -04:00
if ev . Op & fsnotify . Create == fsnotify . Create {
watcher . Add ( ev . Name )
2014-02-17 05:54:15 -05:00
}
}
}
2015-03-06 12:07:50 -05:00
if staticChanged {
2015-04-03 16:18:16 -04:00
jww . FEEDBACK . Printf ( "Static file changed, syncing\n\n" )
2014-04-05 01:26:43 -04:00
utils . StopOnErr ( copyStatic ( ) , fmt . Sprintf ( "Error copying static files to %s" , helpers . AbsPathify ( viper . GetString ( "PublishDir" ) ) ) )
2014-05-16 11:48:59 -04:00
2014-10-31 02:13:44 -04:00
if ! BuildWatch && ! viper . GetBool ( "DisableLiveReload" ) {
2014-08-25 13:49:53 -04:00
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initalized
2014-09-10 06:21:22 -04:00
// force refresh when more than one file
2015-03-06 12:07:50 -05:00
if len ( staticFilesChanged ) == 1 {
for path := range staticFilesChanged {
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
}
2015-03-06 12:07:50 -05:00
if dynamicChanged {
2014-10-27 17:23:54 -04:00
fmt . Print ( "\nChange detected, rebuilding site\n" )
const layout = "2006-01-02 15:04 -0700"
fmt . Println ( time . Now ( ) . Format ( layout ) )
2015-01-09 04:37:21 -05:00
utils . CheckErr ( buildSite ( true ) )
2014-05-16 11:48:59 -04:00
2014-10-31 02:13:44 -04:00
if ! BuildWatch && ! viper . GetBool ( "DisableLiveReload" ) {
2014-08-25 13:49:53 -04:00
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initalized
livereload . ForceRefresh ( )
}
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 {
fmt . Println ( "error:" , err )
}
}
}
} ( )
if port > 0 {
2014-05-16 17:49:27 -04:00
if ! viper . GetBool ( "DisableLiveReload" ) {
livereload . Initialize ( )
http . HandleFunc ( "/livereload.js" , livereload . ServeJS )
http . HandleFunc ( "/livereload" , livereload . Handler )
}
2014-02-17 05:54:15 -05:00
go serve ( port )
}
wg . Wait ( )
return nil
2013-09-30 22:38:32 -04:00
}
2015-09-14 11:31:39 -04:00
// isThemeVsHugoVersionMismatch returns whether the current Hugo version is < theme's min_version
func isThemeVsHugoVersionMismatch ( ) ( mismatch bool , requiredMinVersion string ) {
if ! helpers . ThemeSet ( ) {
return
}
themeDir := helpers . GetThemeDir ( )
fs := hugofs . SourceFs
path := filepath . Join ( themeDir , "theme.toml" )
exists , err := helpers . Exists ( path , fs )
if err != nil || ! exists {
return
}
f , err := fs . Open ( path )
if err != nil {
return
}
defer f . Close ( )
b , err := ioutil . ReadAll ( f )
if err != nil {
return
}
c , err := parser . HandleTOMLMetaData ( b )
if err != nil {
return
}
config := c . ( map [ string ] interface { } )
if minVersion , ok := config [ "min_version" ] ; ok {
switch minVersion . ( type ) {
case float32 :
return helpers . HugoVersionNumber < minVersion . ( float32 ) , fmt . Sprint ( minVersion )
case float64 :
return helpers . HugoVersionNumber < minVersion . ( float64 ) , fmt . Sprint ( minVersion )
default :
return
}
}
return
}