2013-09-29 02:09:03 -04:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2014-02-17 05:54:15 -05:00
|
|
|
"fmt"
|
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
|
|
|
|
|
|
|
"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"
|
2013-09-29 02:09:03 -04:00
|
|
|
)
|
|
|
|
|
2014-04-05 01:26:43 -04:00
|
|
|
//var Config *hugolib.Config
|
2013-09-29 02:09:03 -04:00
|
|
|
var HugoCmd = &cobra.Command{
|
2014-02-17 05:54:15 -05:00
|
|
|
Use: "hugo",
|
|
|
|
Short: "Hugo is a very fast static site generator",
|
|
|
|
Long: `A Fast and Flexible Static Site Generator built with
|
2013-09-29 02:09:03 -04:00
|
|
|
love by spf13 and friends in Go.
|
|
|
|
|
2014-09-27 03:35:42 -04:00
|
|
|
Complete documentation is available at http://gohugo.io`,
|
2014-02-17 05:54:15 -05:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
InitializeConfig()
|
|
|
|
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-06-24 17:44:16 -04:00
|
|
|
var BuildWatch, Draft, Future, UglyUrls, Verbose, Logging, VerboseLog, DisableRSS, DisableSitemap, PluralizeListTitles bool
|
2014-04-10 08:10:12 -04:00
|
|
|
var Source, Destination, Theme, BaseUrl, CfgFile, LogFile string
|
2013-09-29 02:09:03 -04:00
|
|
|
|
|
|
|
func Execute() {
|
2014-02-17 05:54:15 -05:00
|
|
|
AddCommands()
|
|
|
|
utils.StopOnErr(HugoCmd.Execute())
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func AddCommands() {
|
2014-02-17 05:54:15 -05:00
|
|
|
HugoCmd.AddCommand(serverCmd)
|
|
|
|
HugoCmd.AddCommand(version)
|
|
|
|
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)
|
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")
|
2014-05-29 00:48:40 -04:00
|
|
|
HugoCmd.PersistentFlags().BoolVarP(&Future, "buildFuture", "F", false, "include content with datePublished 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")
|
|
|
|
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")
|
2014-05-16 17:49:27 -04:00
|
|
|
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 05:54:15 -05:00
|
|
|
HugoCmd.PersistentFlags().StringVar(&CfgFile, "config", "", "config file (default is path/config.yaml|json|toml)")
|
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")
|
2014-02-17 05:54:15 -05:00
|
|
|
HugoCmd.Flags().BoolVarP(&BuildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
|
|
|
|
hugoCmdV = HugoCmd
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func InitializeConfig() {
|
2014-07-01 10:59:58 -04:00
|
|
|
viper.SetConfigFile(CfgFile)
|
2014-04-05 01:26:43 -04:00
|
|
|
viper.AddConfigPath(Source)
|
2014-04-10 14:24:18 -04:00
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
2014-10-16 20:20:39 -04:00
|
|
|
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 14:24:18 -04:00
|
|
|
}
|
2014-04-05 01:26:43 -04:00
|
|
|
|
2014-04-08 23:15:57 -04:00
|
|
|
viper.RegisterAlias("taxonomies", "indexes")
|
|
|
|
|
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")
|
|
|
|
viper.SetDefault("DefaultLayout", "post")
|
|
|
|
viper.SetDefault("BuildDrafts", false)
|
2014-05-29 00:48:40 -04:00
|
|
|
viper.SetDefault("BuildFuture", false)
|
2014-04-05 01:26:43 -04:00
|
|
|
viper.SetDefault("UglyUrls", false)
|
|
|
|
viper.SetDefault("Verbose", false)
|
|
|
|
viper.SetDefault("CanonifyUrls", false)
|
|
|
|
viper.SetDefault("Indexes", map[string]string{"tag": "tags", "category": "categories"})
|
|
|
|
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)
|
2014-05-16 17:49:27 -04:00
|
|
|
viper.SetDefault("DisableLiveReload", false)
|
2014-06-24 17:44:16 -04:00
|
|
|
viper.SetDefault("PluralizeListTitles", true)
|
2014-09-26 23:44:09 -04:00
|
|
|
viper.SetDefault("FootnoteAnchorPrefix", "")
|
|
|
|
viper.SetDefault("FootnoteReturnLinkContents", "")
|
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)
|
|
|
|
}
|
|
|
|
|
2014-05-16 17:49:27 -04:00
|
|
|
if hugoCmdV.PersistentFlags().Lookup("uglyUrls").Changed {
|
2014-04-05 01:26:43 -04:00
|
|
|
viper.Set("UglyUrls", UglyUrls)
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2014-02-17 05:54:15 -05:00
|
|
|
if BaseUrl != "" {
|
2014-04-05 01:26:43 -04:00
|
|
|
if !strings.HasSuffix(BaseUrl, "/") {
|
|
|
|
BaseUrl = BaseUrl + "/"
|
|
|
|
}
|
|
|
|
viper.Set("BaseUrl", BaseUrl)
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
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 {
|
|
|
|
dir, _ := helpers.FindCWD()
|
|
|
|
viper.Set("WorkingDir", dir)
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
2014-03-31 13:23:34 -04:00
|
|
|
|
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())
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
|
|
|
|
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")))
|
2014-03-31 13:23:34 -04: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
|
|
|
staticDir := helpers.AbsPathify(viper.GetString("StaticDir")) + "/"
|
2014-02-17 05:54:15 -05:00
|
|
|
if _, err := os.Stat(staticDir); os.IsNotExist(err) {
|
2014-10-05 03:41:54 -04:00
|
|
|
jww.ERROR.Println("Unable to find Static Directory:", staticDir)
|
2014-02-17 05:54:15 -05:00
|
|
|
return nil
|
|
|
|
}
|
2013-10-31 18:46:55 -04:00
|
|
|
|
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()
|
|
|
|
syncer.SrcFs = hugofs.SourceFs
|
|
|
|
syncer.DestFs = hugofs.DestinationFS
|
|
|
|
|
2014-04-10 08:10:12 -04:00
|
|
|
if themeSet() {
|
|
|
|
themeDir := helpers.AbsPathify("themes/"+viper.GetString("theme")) + "/static/"
|
|
|
|
if _, err := os.Stat(themeDir); os.IsNotExist(err) {
|
2014-10-05 03:41:54 -04:00
|
|
|
jww.ERROR.Println("Unable to find static directory for theme:", viper.GetString("theme"), "in", themeDir)
|
2014-04-10 08:10:12 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy Static to Destination
|
|
|
|
jww.INFO.Println("syncing from", themeDir, "to", publishDir)
|
2014-11-01 11:57:29 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-17 05:54:15 -05:00
|
|
|
// Copy Static to Destination
|
2014-04-05 01:26:43 -04:00
|
|
|
jww.INFO.Println("syncing from", staticDir, "to", publishDir)
|
2014-11-01 11:57:29 -04:00
|
|
|
return syncer.Sync(publishDir, staticDir)
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func getDirList() []string {
|
2014-02-17 05:54:15 -05:00
|
|
|
var a []string
|
|
|
|
walker := func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
2014-03-31 13:23:34 -04:00
|
|
|
jww.ERROR.Println("Walker: ", err)
|
2014-02-17 05:54:15 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
a = append(a, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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)
|
2014-04-10 08:10:12 -04:00
|
|
|
if themeSet() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-04-10 08:10:12 -04:00
|
|
|
func themeSet() bool {
|
|
|
|
return viper.GetString("theme") != ""
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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 != "" {
|
|
|
|
_ = watcher.Watch(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case evs := <-watcher.Event:
|
2014-08-25 13:49:53 -04:00
|
|
|
jww.INFO.Println("File System Event:", evs)
|
2014-02-17 05:54:15 -05:00
|
|
|
|
|
|
|
static_changed := false
|
|
|
|
dynamic_changed := false
|
2014-09-10 06:21:22 -04:00
|
|
|
static_files_changed := make(map[string]bool)
|
2014-02-17 05:54:15 -05:00
|
|
|
|
|
|
|
for _, ev := range evs {
|
|
|
|
ext := filepath.Ext(ev.Name)
|
2014-09-10 06:21:22 -04:00
|
|
|
istemp := strings.HasSuffix(ext, "~") || (ext == ".swp") || (ext == ".tmp") || (strings.HasPrefix(ext, ".goutputstream"))
|
2014-02-17 05:54:15 -05:00
|
|
|
if istemp {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// renames are always followed with Create/Modify
|
|
|
|
if ev.IsRename() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-05-27 18:29:55 -04:00
|
|
|
isstatic := strings.HasPrefix(ev.Name, helpers.AbsPathify(viper.GetString("StaticDir"))) || strings.HasPrefix(ev.Name, helpers.AbsPathify("themes/"+viper.GetString("theme"))+"/static/")
|
2014-02-17 05:54:15 -05:00
|
|
|
static_changed = static_changed || isstatic
|
|
|
|
dynamic_changed = dynamic_changed || !isstatic
|
|
|
|
|
2014-09-10 06:21:22 -04:00
|
|
|
if isstatic {
|
|
|
|
if staticPath, err := helpers.MakeStaticPathRelative(ev.Name); err == nil {
|
|
|
|
static_files_changed[staticPath] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
if ev.IsCreate() {
|
|
|
|
watcher.Watch(ev.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if static_changed {
|
2014-10-31 02:13:44 -04:00
|
|
|
jww.FEEDBACK.Println("Static file changed, syncing\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
|
|
|
|
if len(static_files_changed) == 1 {
|
|
|
|
for path := range static_files_changed {
|
|
|
|
livereload.RefreshPath(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
livereload.ForceRefresh()
|
|
|
|
}
|
2014-08-25 13:49:53 -04:00
|
|
|
}
|
2014-02-17 05:54:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if dynamic_changed {
|
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))
|
2014-02-17 05:54:15 -05:00
|
|
|
utils.StopOnErr(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
|
|
|
}
|
|
|
|
case err := <-watcher.Error:
|
|
|
|
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
|
|
|
}
|