func build(watches ...bool) { utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", helpers.AbsPathify(viper.GetString("PublishDir")))) 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"))) jww.FEEDBACK.Println("Press Ctrl+C to stop") utils.CheckErr(NewWatcher(0)) } }
func copyStatic() error { staticDir := helpers.AbsPathify(viper.GetString("StaticDir")) + "/" if _, err := os.Stat(staticDir); os.IsNotExist(err) { jww.ERROR.Println("Unable to find Static Directory:", staticDir) return nil } publishDir := helpers.AbsPathify(viper.GetString("PublishDir")) + "/" syncer := fsync.NewSyncer() 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 } 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)) } // Copy Static to Destination jww.INFO.Println("syncing from", staticDir, "to", publishDir) return syncer.Sync(publishDir, staticDir) }
// NewWatcher creates a new watcher to watch filesystem events. func NewWatcher(port int) error { 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.Add(d) } } go func() { for { select { case evs := <-watcher.Events: jww.INFO.Println("File System Event:", evs) staticChanged := false dynamicChanged := false staticFilesChanged := make(map[string]bool) for _, ev := range evs { ext := filepath.Ext(ev.Name) istemp := strings.HasSuffix(ext, "~") || (ext == ".swp") || (ext == ".swx") || (ext == ".tmp") || strings.HasPrefix(ext, ".goutputstream") if istemp { continue } // renames are always followed with Create/Modify if ev.Op&fsnotify.Rename == fsnotify.Rename { continue } isstatic := strings.HasPrefix(ev.Name, helpers.GetStaticDirPath()) || (len(helpers.GetThemesDirPath()) > 0 && strings.HasPrefix(ev.Name, helpers.GetThemesDirPath())) staticChanged = staticChanged || isstatic dynamicChanged = dynamicChanged || !isstatic if isstatic { if staticPath, err := helpers.MakeStaticPathRelative(ev.Name); err == nil { staticFilesChanged[staticPath] = true } } // add new directory to watch list if s, err := os.Stat(ev.Name); err == nil && s.Mode().IsDir() { if ev.Op&fsnotify.Create == fsnotify.Create { watcher.Add(ev.Name) } } } if staticChanged { jww.FEEDBACK.Printf("Static file changed, syncing\n\n") utils.StopOnErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", helpers.AbsPathify(viper.GetString("PublishDir")))) 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(staticFilesChanged) == 1 { for path := range staticFilesChanged { livereload.RefreshPath(path) } } else { livereload.ForceRefresh() } } } if dynamicChanged { 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)) 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() } } case err := <-watcher.Errors: 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) } go serve(port) } wg.Wait() return nil }
// InitializeConfig initializes a config file with sensible default configuration flags. func InitializeConfig() { viper.SetConfigFile(CfgFile) viper.AddConfigPath(Source) 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") } viper.RegisterAlias("indexes", "taxonomies") LoadDefaultSettings() if hugoCmdV.PersistentFlags().Lookup("buildDrafts").Changed { viper.Set("BuildDrafts", Draft) } if hugoCmdV.PersistentFlags().Lookup("buildFuture").Changed { viper.Set("BuildFuture", Future) } if hugoCmdV.PersistentFlags().Lookup("uglyUrls").Changed { viper.Set("UglyURLs", UglyURLs) } if hugoCmdV.PersistentFlags().Lookup("disableRSS").Changed { viper.Set("DisableRSS", DisableRSS) } if hugoCmdV.PersistentFlags().Lookup("disableSitemap").Changed { viper.Set("DisableSitemap", DisableSitemap) } if hugoCmdV.PersistentFlags().Lookup("verbose").Changed { viper.Set("Verbose", Verbose) } if hugoCmdV.PersistentFlags().Lookup("pluralizeListTitles").Changed { viper.Set("PluralizeListTitles", PluralizeListTitles) } if hugoCmdV.PersistentFlags().Lookup("preserveTaxonomyNames").Changed { viper.Set("PreserveTaxonomyNames", PreserveTaxonomyNames) } if hugoCmdV.PersistentFlags().Lookup("editor").Changed { viper.Set("NewContentEditor", Editor) } if hugoCmdV.PersistentFlags().Lookup("logFile").Changed { viper.Set("LogFile", LogFile) } if BaseURL != "" { if !strings.HasSuffix(BaseURL, "/") { BaseURL = BaseURL + "/" } viper.Set("BaseURL", BaseURL) } if !viper.GetBool("RelativeURLs") && viper.GetString("BaseURL") == "" { jww.ERROR.Println("No 'baseurl' set in configuration or as a flag. Features like page menus will not work without one.") } if Theme != "" { viper.Set("theme", Theme) } if Destination != "" { viper.Set("PublishDir", Destination) } if Source != "" { viper.Set("WorkingDir", Source) } else { dir, _ := os.Getwd() viper.Set("WorkingDir", dir) } if hugoCmdV.PersistentFlags().Lookup("ignoreCache").Changed { viper.Set("IgnoreCache", IgnoreCache) } if CacheDir != "" { if helpers.FilePathSeparator != CacheDir[len(CacheDir)-1:] { CacheDir = CacheDir + helpers.FilePathSeparator } isDir, err := helpers.DirExists(CacheDir, hugofs.SourceFs) utils.CheckErr(err) if isDir == false { mkdir(CacheDir) } viper.Set("CacheDir", CacheDir) } else { viper.Set("CacheDir", helpers.GetTempDir("hugo_cache", hugofs.SourceFs)) } 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") { jww.SetStdoutThreshold(jww.LevelInfo) } if VerboseLog { jww.SetLogThreshold(jww.LevelInfo) } jww.INFO.Println("Using config file:", viper.ConfigFileUsed()) themeDir := helpers.GetThemeDir() if themeDir != "" { if _, err := os.Stat(themeDir); os.IsNotExist(err) { jww.FATAL.Fatalln("Unable to find theme Directory:", themeDir) } } themeVersionMismatch, minVersion := helpers.IsThemeVsHugoVersionMismatch() if themeVersionMismatch { jww.ERROR.Printf("Current theme does not support Hugo version %s. Minimum version required is %s\n", helpers.HugoReleaseVersion(), minVersion) } }