2013-07-04 15:32:55 +00: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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/howeyc/fsnotify"
|
2013-07-30 05:26:02 +00:00
|
|
|
"github.com/mostafah/fsync"
|
2013-07-19 07:38:24 +00:00
|
|
|
flag "github.com/ogier/pflag"
|
2013-07-19 07:10:42 +00:00
|
|
|
"github.com/spf13/hugo/hugolib"
|
2013-07-30 05:26:02 +00:00
|
|
|
"log"
|
2013-07-04 15:32:55 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime/pprof"
|
|
|
|
"sync"
|
2013-07-10 13:21:41 +00:00
|
|
|
"time"
|
2013-07-04 15:32:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2013-07-26 22:06:13 +00:00
|
|
|
baseUrl = flag.StringP("base-url", "b", "", "hostname (and path) to the root eg. http://spf13.com/")
|
|
|
|
cfgfile = flag.String("config", "", "config file (default is path/config.yaml|json|toml)")
|
|
|
|
checkMode = flag.Bool("check", false, "analyze content and provide feedback")
|
|
|
|
draft = flag.BoolP("build-drafts", "D", false, "include content marked as draft")
|
|
|
|
help = flag.BoolP("help", "h", false, "show this help")
|
|
|
|
source = flag.StringP("source", "s", "", "filesystem path to read files relative from")
|
|
|
|
destination = flag.StringP("destination", "d", "", "filesystem path to write files to")
|
|
|
|
verbose = flag.BoolP("verbose", "v", false, "verbose output")
|
|
|
|
version = flag.Bool("version", false, "which version of hugo")
|
|
|
|
cpuprofile = flag.Int("profile", 0, "Number of times to create the site and profile it")
|
|
|
|
watchMode = flag.BoolP("watch", "w", false, "watch filesystem for changes and recreate as needed")
|
|
|
|
server = flag.BoolP("server", "S", false, "run a (very) simple web server")
|
|
|
|
port = flag.String("port", "1313", "port to run web server on, default :1313")
|
|
|
|
uglyUrls = flag.Bool("uglyurls", false, "use /filename.html instead of /filename/ ")
|
2013-07-04 15:32:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func usage() {
|
|
|
|
PrintErr("usage: hugo [flags]", "")
|
|
|
|
flag.PrintDefaults()
|
2013-07-19 07:38:24 +00:00
|
|
|
os.Exit(0)
|
2013-07-04 15:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
flag.Usage = usage
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *help {
|
|
|
|
usage()
|
|
|
|
}
|
|
|
|
|
2013-08-08 20:18:37 +00:00
|
|
|
if *version {
|
|
|
|
fmt.Println("Hugo Static Site Generator v0.8")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-07-26 22:06:13 +00:00
|
|
|
config := hugolib.SetupConfig(cfgfile, source)
|
2013-07-04 15:32:55 +00:00
|
|
|
config.BuildDrafts = *draft
|
2013-07-19 07:10:42 +00:00
|
|
|
config.UglyUrls = *uglyUrls
|
|
|
|
config.Verbose = *verbose
|
2013-07-04 15:32:55 +00:00
|
|
|
|
|
|
|
if *baseUrl != "" {
|
|
|
|
config.BaseUrl = *baseUrl
|
2013-07-10 04:14:02 +00:00
|
|
|
} else if *server {
|
2013-07-19 07:10:42 +00:00
|
|
|
config.BaseUrl = "http://localhost:" + *port
|
|
|
|
}
|
2013-07-04 15:32:55 +00:00
|
|
|
|
2013-07-30 05:26:02 +00:00
|
|
|
if *destination != "" {
|
|
|
|
config.PublishDir = *destination
|
|
|
|
}
|
|
|
|
|
2013-07-04 15:32:55 +00:00
|
|
|
if *cpuprofile != 0 {
|
|
|
|
f, err := os.Create("/tmp/hugo-cpuprofile")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
|
|
|
|
for i := 0; i < *cpuprofile; i++ {
|
2013-08-01 18:55:18 +00:00
|
|
|
_, _ = buildSite(config)
|
2013-07-04 15:32:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-30 05:26:02 +00:00
|
|
|
// Copy Static to Destination first
|
|
|
|
err := fsync.SyncDel(config.GetAbsPath(config.PublishDir+"/"), config.GetAbsPath(config.StaticDir+"/"))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error copying static files to %s: %v", config.GetAbsPath(config.PublishDir), err)
|
|
|
|
}
|
|
|
|
|
2013-07-04 15:32:55 +00:00
|
|
|
if *checkMode {
|
2013-08-08 00:21:22 +00:00
|
|
|
site := hugolib.Site{Config: *config}
|
2013-07-04 15:32:55 +00:00
|
|
|
site.Analyze()
|
2013-07-19 07:38:24 +00:00
|
|
|
os.Exit(0)
|
2013-07-04 15:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if *watchMode {
|
|
|
|
fmt.Println("Watching for changes. Press ctrl+c to stop")
|
2013-08-01 18:55:18 +00:00
|
|
|
_, err = buildSite(config)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2013-07-04 15:32:55 +00:00
|
|
|
err := NewWatcher(config, *port, *server)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 18:55:18 +00:00
|
|
|
if _, err = buildSite(config); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2013-07-04 15:32:55 +00:00
|
|
|
|
|
|
|
if *server {
|
|
|
|
serve(*port, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func serve(port string, config *hugolib.Config) {
|
2013-07-26 13:24:35 +00:00
|
|
|
|
|
|
|
if config.Verbose {
|
|
|
|
fmt.Println("Serving pages from " + config.GetAbsPath(config.PublishDir))
|
|
|
|
}
|
|
|
|
|
2013-07-04 15:32:55 +00:00
|
|
|
fmt.Println("Web Server is available at http://localhost:" + port)
|
|
|
|
fmt.Println("Press ctrl+c to stop")
|
2013-07-26 13:25:30 +00:00
|
|
|
panic(http.ListenAndServe(":"+port, http.FileServer(http.Dir(config.GetAbsPath(config.PublishDir)))))
|
2013-07-04 15:32:55 +00:00
|
|
|
}
|
|
|
|
|
2013-08-01 18:55:18 +00:00
|
|
|
func buildSite(config *hugolib.Config) (site *hugolib.Site, err error) {
|
2013-07-19 07:10:42 +00:00
|
|
|
startTime := time.Now()
|
2013-08-08 00:21:22 +00:00
|
|
|
site = &hugolib.Site{Config: *config}
|
2013-08-01 18:55:18 +00:00
|
|
|
err = site.Build()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-07-04 15:32:55 +00:00
|
|
|
site.Stats()
|
2013-07-19 07:10:42 +00:00
|
|
|
fmt.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds()))
|
2013-08-01 18:55:18 +00:00
|
|
|
return site, nil
|
2013-07-04 15:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func watchChange(c *hugolib.Config) {
|
|
|
|
fmt.Println("Change detected, rebuilding site\n")
|
|
|
|
buildSite(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWatcher(c *hugolib.Config, port string, server bool) error {
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev := <-watcher.Event:
|
|
|
|
var _ = ev
|
2013-07-19 07:10:42 +00:00
|
|
|
if c.Verbose {
|
|
|
|
fmt.Println(ev)
|
|
|
|
}
|
2013-07-04 15:32:55 +00:00
|
|
|
watchChange(c)
|
|
|
|
// TODO add newly created directories to the watch list
|
|
|
|
case err := <-watcher.Error:
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, d := range getDirList(c) {
|
|
|
|
if d != "" {
|
|
|
|
_ = watcher.Watch(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if server {
|
|
|
|
go serve(port, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDirList(c *hugolib.Config) []string {
|
|
|
|
var a []string
|
|
|
|
walker := func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
PrintErr("Walker: ", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
a = append(a, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-26 22:06:13 +00:00
|
|
|
filepath.Walk(c.GetAbsPath(c.ContentDir), walker)
|
2013-07-04 15:32:55 +00:00
|
|
|
filepath.Walk(c.GetAbsPath(c.LayoutDir), walker)
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintErr(str string, a ...interface{}) {
|
|
|
|
fmt.Fprintln(os.Stderr, str, a)
|
|
|
|
}
|