|
|
@ -2,13 +2,22 @@ package caddyhugo |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"io/ioutil" |
|
|
|
"path" |
|
|
|
"os" |
|
|
|
"time" |
|
|
|
"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,39 +28,37 @@ type HugoRenderer interface { |
|
|
|
Stop() error |
|
|
|
Stop() error |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) idleshut.Config { |
|
|
|
func HugoInternalProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) { |
|
|
|
cmd := exec.Command("hugo", "--watch", "-D", "-d", es.tmpdir) |
|
|
|
|
|
|
|
cmd.Dir = es.tmpdir |
|
|
|
var err error |
|
|
|
|
|
|
|
hugoCfg := &deps.DepsCfg{Fs: hugofs.NewFrom(es.tmpfs, ch.HugoCfg.Cfg)} |
|
|
|
|
|
|
|
fmt.Println(ch.Dir) |
|
|
|
|
|
|
|
hugoCfg.Cfg, err = hugolib.LoadConfig(es.tmpfs, "", path.Join(ch.Dir, "config.toml")) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return idleshut.Config{}, fmt.Errorf("caddy-hugo: loading site configuration: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hugoCfg.Cfg.Set("workingDir", ch.Dir) |
|
|
|
|
|
|
|
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 { |
|
|
|
|
|
|
|
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 { |
|
|
|
ch.persistEditsForSession(es) |
|
|
|
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) |
|
|
|
|
|
|
|
|
|
|
|
ch.mtx.Lock() |
|
|
|
|
|
|
|
defer ch.mtx.Unlock() |
|
|
|
delete(ch.docs, es.filename) |
|
|
|
delete(ch.docs, es.filename) |
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
return nil |
|
|
@ -60,7 +67,17 @@ func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) idlesh |
|
|
|
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) |
|
|
|
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 |
|
|
|
} |
|
|
|
} |
|
|
|