Fix build

This commit is contained in:
mattn 2014-02-17 19:54:15 +09:00 committed by spf13
parent 1bead0ed7a
commit 8ebb85f1f7
4 changed files with 253 additions and 218 deletions

View file

@ -14,52 +14,18 @@
package commands package commands
import ( import (
"fmt" "github.com/spf13/cobra"
"github.com/spf13/cobra" "github.com/spf13/hugo/hugolib"
"github.com/spf13/hugo/hugolib"
"syscall"
) )
func init() {
check.AddCommand(limit)
}
var check = &cobra.Command{ var check = &cobra.Command{
Use: "check", Use: "check",
Short: "Check content in the source directory", Short: "Check content in the source directory",
Long: `Hugo will perform some basic analysis on the Long: `Hugo will perform some basic analysis on the
content provided and will give feedback.`, content provided and will give feedback.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
InitializeConfig() InitializeConfig()
site := hugolib.Site{Config: *Config} site := hugolib.Site{Config: *Config}
site.Analyze() site.Analyze()
}, },
}
var limit = &cobra.Command{
Use: "ulimit",
Short: "Check system ulimit settings",
Long: `Hugo will inspect the current ulimit settings on the system.
This is primarily to ensure that Hugo can watch enough files on some OSs`,
Run: func(cmd *cobra.Command, args []string) {
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Getting Rlimit ", err)
}
fmt.Println("Current rLimit:", rLimit)
fmt.Println("Attempting to increase limit")
rLimit.Max = 999999
rLimit.Cur = 999999
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Setting rLimit ", err)
}
err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Getting rLimit ", err)
}
fmt.Println("rLimit after change:", rLimit)
},
} }

View file

@ -14,34 +14,33 @@
package commands package commands
import ( import (
"fmt" "fmt"
"github.com/mostafah/fsync" "github.com/mostafah/fsync"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/hugo/hugolib" "github.com/spf13/hugo/hugolib"
"github.com/spf13/hugo/utils" "github.com/spf13/hugo/utils"
"github.com/spf13/hugo/watcher" "github.com/spf13/hugo/watcher"
"github.com/spf13/nitro" "github.com/spf13/nitro"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"sync" "sync"
"syscall" "time"
"time"
) )
var Config *hugolib.Config var Config *hugolib.Config
var HugoCmd = &cobra.Command{ var HugoCmd = &cobra.Command{
Use: "hugo", Use: "hugo",
Short: "Hugo is a very fast static site generator", Short: "Hugo is a very fast static site generator",
Long: `A Fast and Flexible Static Site Generator built with Long: `A Fast and Flexible Static Site Generator built with
love by spf13 and friends in Go. love by spf13 and friends in Go.
Complete documentation is available at http://hugo.spf13.com`, Complete documentation is available at http://hugo.spf13.com`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
InitializeConfig() InitializeConfig()
build() build()
}, },
} }
var hugoCmdV *cobra.Command var hugoCmdV *cobra.Command
@ -49,203 +48,191 @@ var BuildWatch, Draft, UglyUrls, Verbose bool
var Source, Destination, BaseUrl, CfgFile string var Source, Destination, BaseUrl, CfgFile string
func Execute() { func Execute() {
AddCommands() AddCommands()
utils.StopOnErr(HugoCmd.Execute()) utils.StopOnErr(HugoCmd.Execute())
} }
func AddCommands() { func AddCommands() {
HugoCmd.AddCommand(serverCmd) HugoCmd.AddCommand(serverCmd)
HugoCmd.AddCommand(version) HugoCmd.AddCommand(version)
HugoCmd.AddCommand(check) HugoCmd.AddCommand(check)
HugoCmd.AddCommand(benchmark) HugoCmd.AddCommand(benchmark)
} }
func init() { func init() {
HugoCmd.PersistentFlags().BoolVarP(&Draft, "build-drafts", "D", false, "include content marked as draft") HugoCmd.PersistentFlags().BoolVarP(&Draft, "build-drafts", "D", false, "include content marked as draft")
HugoCmd.PersistentFlags().StringVarP(&Source, "source", "s", "", "filesystem path to read files relative from") 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") HugoCmd.PersistentFlags().StringVarP(&Destination, "destination", "d", "", "filesystem path to write files to")
HugoCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output") 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().BoolVar(&UglyUrls, "uglyurls", false, "if true, use /filename.html instead of /filename/")
HugoCmd.PersistentFlags().StringVarP(&BaseUrl, "base-url", "b", "", "hostname (and path) to the root eg. http://spf13.com/") HugoCmd.PersistentFlags().StringVarP(&BaseUrl, "base-url", "b", "", "hostname (and path) to the root eg. http://spf13.com/")
HugoCmd.PersistentFlags().StringVar(&CfgFile, "config", "", "config file (default is path/config.yaml|json|toml)") HugoCmd.PersistentFlags().StringVar(&CfgFile, "config", "", "config file (default is path/config.yaml|json|toml)")
HugoCmd.PersistentFlags().BoolVar(&nitro.AnalysisOn, "stepAnalysis", false, "display memory and timing of different steps of the program") HugoCmd.PersistentFlags().BoolVar(&nitro.AnalysisOn, "stepAnalysis", false, "display memory and timing of different steps of the program")
HugoCmd.Flags().BoolVarP(&BuildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed") HugoCmd.Flags().BoolVarP(&BuildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
hugoCmdV = HugoCmd hugoCmdV = HugoCmd
} }
func InitializeConfig() { func InitializeConfig() {
Config = hugolib.SetupConfig(&CfgFile, &Source) Config = hugolib.SetupConfig(&CfgFile, &Source)
if hugoCmdV.PersistentFlags().Lookup("build-drafts").Changed { if hugoCmdV.PersistentFlags().Lookup("build-drafts").Changed {
Config.BuildDrafts = Draft Config.BuildDrafts = Draft
} }
if hugoCmdV.PersistentFlags().Lookup("uglyurls").Changed { if hugoCmdV.PersistentFlags().Lookup("uglyurls").Changed {
Config.UglyUrls = UglyUrls Config.UglyUrls = UglyUrls
} }
if hugoCmdV.PersistentFlags().Lookup("verbose").Changed { if hugoCmdV.PersistentFlags().Lookup("verbose").Changed {
Config.Verbose = Verbose Config.Verbose = Verbose
} }
if BaseUrl != "" { if BaseUrl != "" {
Config.BaseUrl = BaseUrl Config.BaseUrl = BaseUrl
} }
if Destination != "" { if Destination != "" {
Config.PublishDir = Destination Config.PublishDir = Destination
} }
} }
func build(watches ...bool) { func build(watches ...bool) {
utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir))) utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir)))
watch := false watch := false
if len(watches) > 0 && watches[0] { if len(watches) > 0 && watches[0] {
watch = true watch = true
} }
utils.StopOnErr(buildSite(BuildWatch || watch)) utils.StopOnErr(buildSite(BuildWatch || watch))
if BuildWatch { if BuildWatch {
fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir)) fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))
fmt.Println("Press ctrl+c to stop") fmt.Println("Press ctrl+c to stop")
utils.CheckErr(NewWatcher(0)) utils.CheckErr(NewWatcher(0))
} }
} }
func copyStatic() error { func copyStatic() error {
staticDir := Config.GetAbsPath(Config.StaticDir + "/") staticDir := Config.GetAbsPath(Config.StaticDir + "/")
if _, err := os.Stat(staticDir); os.IsNotExist(err) { if _, err := os.Stat(staticDir); os.IsNotExist(err) {
return nil return nil
} }
// Copy Static to Destination // Copy Static to Destination
return fsync.Sync(Config.GetAbsPath(Config.PublishDir+"/"), Config.GetAbsPath(Config.StaticDir+"/")) return fsync.Sync(Config.GetAbsPath(Config.PublishDir+"/"), Config.GetAbsPath(Config.StaticDir+"/"))
} }
func getDirList() []string { func getDirList() []string {
var a []string var a []string
walker := func(path string, fi os.FileInfo, err error) error { walker := func(path string, fi os.FileInfo, err error) error {
if err != nil { if err != nil {
fmt.Println("Walker: ", err) fmt.Println("Walker: ", err)
return nil return nil
} }
if fi.IsDir() { if fi.IsDir() {
a = append(a, path) a = append(a, path)
} }
return nil return nil
} }
filepath.Walk(Config.GetAbsPath(Config.ContentDir), walker) filepath.Walk(Config.GetAbsPath(Config.ContentDir), walker)
filepath.Walk(Config.GetAbsPath(Config.LayoutDir), walker) filepath.Walk(Config.GetAbsPath(Config.LayoutDir), walker)
filepath.Walk(Config.GetAbsPath(Config.StaticDir), walker) filepath.Walk(Config.GetAbsPath(Config.StaticDir), walker)
return a return a
} }
func buildSite(watching ...bool) (err error) { func buildSite(watching ...bool) (err error) {
startTime := time.Now() startTime := time.Now()
site := &hugolib.Site{Config: *Config} site := &hugolib.Site{Config: *Config}
if len(watching) > 0 && watching[0] { if len(watching) > 0 && watching[0] {
site.RunMode.Watching = true site.RunMode.Watching = true
} }
err = site.Build() err = site.Build()
if err != nil { if err != nil {
return return
} }
site.Stats() site.Stats()
fmt.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds())) fmt.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds()))
return nil return nil
} }
func NewWatcher(port int) error { func NewWatcher(port int) error {
if runtime.GOOS == "darwin" { if runtime.GOOS == "darwin" {
var rLimit syscall.Rlimit tweakLimit()
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) }
if err != nil {
fmt.Println("Unable to obtain rLimit", err)
}
if rLimit.Cur < rLimit.Max {
rLimit.Max = 999999
rLimit.Cur = 999999
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Unable to increase number of open files limit", err)
}
}
}
watcher, err := watcher.New(1 * time.Second) watcher, err := watcher.New(1 * time.Second)
var wg sync.WaitGroup var wg sync.WaitGroup
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return err return err
} }
defer watcher.Close() defer watcher.Close()
wg.Add(1) wg.Add(1)
for _, d := range getDirList() { for _, d := range getDirList() {
if d != "" { if d != "" {
_ = watcher.Watch(d) _ = watcher.Watch(d)
} }
} }
go func() { go func() {
for { for {
select { select {
case evs := <-watcher.Event: case evs := <-watcher.Event:
if Verbose { if Verbose {
fmt.Println(evs) fmt.Println(evs)
} }
static_changed := false static_changed := false
dynamic_changed := false dynamic_changed := false
for _, ev := range evs { for _, ev := range evs {
ext := filepath.Ext(ev.Name) ext := filepath.Ext(ev.Name)
istemp := strings.HasSuffix(ext, "~") || (ext == ".swp") || (ext == ".tmp") istemp := strings.HasSuffix(ext, "~") || (ext == ".swp") || (ext == ".tmp")
if istemp { if istemp {
continue continue
} }
// renames are always followed with Create/Modify // renames are always followed with Create/Modify
if ev.IsRename() { if ev.IsRename() {
continue continue
} }
isstatic := strings.HasPrefix(ev.Name, Config.GetAbsPath(Config.StaticDir)) isstatic := strings.HasPrefix(ev.Name, Config.GetAbsPath(Config.StaticDir))
static_changed = static_changed || isstatic static_changed = static_changed || isstatic
dynamic_changed = dynamic_changed || !isstatic dynamic_changed = dynamic_changed || !isstatic
// add new directory to watch list // add new directory to watch list
if s, err := os.Stat(ev.Name); err == nil && s.Mode().IsDir() { if s, err := os.Stat(ev.Name); err == nil && s.Mode().IsDir() {
if ev.IsCreate() { if ev.IsCreate() {
watcher.Watch(ev.Name) watcher.Watch(ev.Name)
} }
} }
} }
if static_changed { if static_changed {
fmt.Println("Static file changed, syncing\n") fmt.Println("Static file changed, syncing\n")
utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir))) utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir)))
} }
if dynamic_changed { if dynamic_changed {
fmt.Println("Change detected, rebuilding site\n") fmt.Println("Change detected, rebuilding site\n")
utils.StopOnErr(buildSite(true)) utils.StopOnErr(buildSite(true))
} }
case err := <-watcher.Error: case err := <-watcher.Error:
if err != nil { if err != nil {
fmt.Println("error:", err) fmt.Println("error:", err)
} }
} }
} }
}() }()
if port > 0 { if port > 0 {
go serve(port) go serve(port)
} }
wg.Wait() wg.Wait()
return nil return nil
} }

63
commands/limit_darwin.go Normal file
View file

@ -0,0 +1,63 @@
// +build darwin
// 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
func init() {
check.AddCommand(limit)
}
var limit = &cobra.Command{
Use: "ulimit",
Short: "Check system ulimit settings",
Long: `Hugo will inspect the current ulimit settings on the system.
This is primarily to ensure that Hugo can watch enough files on some OSs`,
Run: func(cmd *cobra.Command, args []string) {
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Getting Rlimit ", err)
}
fmt.Println("Current rLimit:", rLimit)
fmt.Println("Attempting to increase limit")
rLimit.Max = 999999
rLimit.Cur = 999999
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Setting rLimit ", err)
}
err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Getting rLimit ", err)
}
fmt.Println("rLimit after change:", rLimit)
},
}
func tweakLimit() {
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Unable to obtain rLimit", err)
}
if rLimit.Cur < rLimit.Max {
rLimit.Max = 999999
rLimit.Cur = 999999
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Unable to increase number of open files limit", err)
}
}
}

19
commands/limit_others.go Normal file
View file

@ -0,0 +1,19 @@
// +build !darwin
// 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
func tweakLimit() {
// nothing to do
}