|
|
@ -2,13 +2,21 @@ package caddyhugo |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"io/ioutil" |
|
|
|
"time" |
|
|
|
"os" |
|
|
|
|
|
|
|
"os/exec" |
|
|
|
"github.com/gohugoio/hugo/deps" |
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/hugofs" |
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/hugolib" |
|
|
|
|
|
|
|
"github.com/spf13/afero" |
|
|
|
|
|
|
|
|
|
|
|
"git.stephensearles.com/stephen/idleshut" |
|
|
|
"git.stephensearles.com/stephen/idleshut" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
|
|
|
|
IdleWebsocketTimeout = 10 * time.Minute |
|
|
|
|
|
|
|
WebsocketFileTicker = 1 * time.Second |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type HugoInteractor interface { |
|
|
|
type HugoInteractor interface { |
|
|
|
Render(srcdir, workdir string) HugoRenderer |
|
|
|
Render(srcdir, workdir string) HugoRenderer |
|
|
|
} |
|
|
|
} |
|
|
@ -19,48 +27,52 @@ type HugoRenderer interface { |
|
|
|
Stop() error |
|
|
|
Stop() error |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) idleshut.Config { |
|
|
|
func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) { |
|
|
|
cmd := exec.Command("hugo", "--watch", "-D", "-d", es.tmpdir) |
|
|
|
|
|
|
|
cmd.Dir = es.tmpdir |
|
|
|
printTree(es.tmpfs) |
|
|
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
hugoCfg := &deps.DepsCfg{Fs: hugofs.NewFrom(es.tmpfs, ch.HugoCfg.Cfg)} |
|
|
|
|
|
|
|
hugoCfg.Cfg, err = hugolib.LoadConfig(es.tmpfs, "/", "config.toml") |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return idleshut.Config{}, fmt.Errorf("caddy-hugo: loading site configuration: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hugoCfg.Cfg.Set("workingdir", "/") |
|
|
|
|
|
|
|
hugoSites, err := hugolib.NewHugoSites(*hugoCfg) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return idleshut.Config{}, fmt.Errorf("caddy-hugo: initializing site: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = hugoSites.Build(hugolib.BuildCfg{ResetState: true}) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
// TODO better
|
|
|
|
|
|
|
|
return idleshut.Config{}, fmt.Errorf("caddy-hugo: building site: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return idleshut.Config{ |
|
|
|
return idleshut.Config{ |
|
|
|
TickDuration: WebsocketFileTicker, |
|
|
|
TickDuration: WebsocketFileTicker, |
|
|
|
MaxIdleTicks: uint(IdleWebsocketTimeout/WebsocketFileTicker) + 1, |
|
|
|
MaxIdleTicks: uint(IdleWebsocketTimeout/WebsocketFileTicker) + 1, |
|
|
|
Start: func() error { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := cmd.Start() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("error starting hugo: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
Stop: func() error { |
|
|
|
Stop: func() error { |
|
|
|
if cmd == nil { |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := cmd.Process.Signal(os.Interrupt) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("error signalling hugo to stop: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
err = cmd.Wait() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("error waiting for hugo to stop: %v", err) |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
es.doc.Close() |
|
|
|
es.doc.Close() |
|
|
|
os.RemoveAll(es.tmpdir) |
|
|
|
|
|
|
|
delete(ch.docs, es.filename) |
|
|
|
delete(ch.docs, es.filename) |
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
return nil |
|
|
|
}, |
|
|
|
}, |
|
|
|
TickError: func(err error) { |
|
|
|
TickError: func(err error) { |
|
|
|
fmt.Println("error processing draft:", err) |
|
|
|
fmt.Println("error processing draft:", err) |
|
|
|
}, |
|
|
|
}, |
|
|
|
Tick: func() error { |
|
|
|
Tick: func() error { |
|
|
|
return ioutil.WriteFile(es.filename, []byte(es.doc.Contents()), 0644) |
|
|
|
fmt.Println("TICK") |
|
|
|
|
|
|
|
err := afero.WriteFile(es.tmpfs, es.filename, []byte(es.doc.Contents()), 0644) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = hugoSites.Build(hugolib.BuildCfg{ResetState: true}) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
}, |
|
|
|
}, |
|
|
|
} |
|
|
|
}, nil |
|
|
|
} |
|
|
|
} |
|
|
|