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