draft overhaul #8
+4
-6
@@ -75,15 +75,13 @@ func (ch *CaddyHugo) docFilename(orig string) string {
|
|||||||
|
|
||||||
// Publish really renders new content into the public directory
|
// Publish really renders new content into the public directory
|
||||||
func (ch *CaddyHugo) Publish() error {
|
func (ch *CaddyHugo) Publish() error {
|
||||||
ch.mtx.Lock()
|
err := ch.persistAllEdits()
|
||||||
for _, es := range ch.docs {
|
if err != nil {
|
||||||
ch.persistEdits(es)
|
return err
|
||||||
}
|
}
|
||||||
ch.mtx.Unlock()
|
|
||||||
|
|
||||||
cmd := exec.Command("hugo")
|
cmd := exec.Command("hugo")
|
||||||
cmd.Dir = ch.Dir
|
cmd.Dir = ch.Dir
|
||||||
_, err := cmd.CombinedOutput()
|
_, err = cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,6 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) {
|
|||||||
docname: docName,
|
docname: docName,
|
||||||
filename: filename,
|
filename: filename,
|
||||||
doc: acedoc.NewString(string(contents)),
|
doc: acedoc.NewString(string(contents)),
|
||||||
// tmpfs: afero.NewCopyOnWriteFs(afero.NewBasePathFs(afero.NewOsFs(), ch.Dir+"/"), afero.NewMemMapFs()),
|
|
||||||
tmpfs: afero.NewCopyOnWriteFs(afero.NewOsFs(), afero.NewMemMapFs()),
|
tmpfs: afero.NewCopyOnWriteFs(afero.NewOsFs(), afero.NewMemMapFs()),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +70,7 @@ func (ch *CaddyHugo) renderDraft(es *editSession) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg, err := HugoCmdProcessConfig(ch, es, f)
|
cfg, err := HugoInternalProcessConfig(ch, es, f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("rendering draft: %v", err)
|
return fmt.Errorf("rendering draft: %v", err)
|
||||||
}
|
}
|
||||||
@@ -80,7 +79,19 @@ func (ch *CaddyHugo) renderDraft(es *editSession) error {
|
|||||||
return proc.Start()
|
return proc.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch *CaddyHugo) persistEdits(es *editSession) error {
|
func (ch *CaddyHugo) persistAllEdits() error {
|
||||||
|
ch.mtx.Lock()
|
||||||
|
defer ch.mtx.Unlock()
|
||||||
|
for _, es := range ch.docs {
|
||||||
|
err := ch.persistEditsForSession(es)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ch *CaddyHugo) persistEditsForSession(es *editSession) error {
|
||||||
err := afero.WriteFile(afero.NewOsFs(), es.filename, []byte(es.doc.Contents()), 0644)
|
err := afero.WriteFile(afero.NewOsFs(), es.filename, []byte(es.doc.Contents()), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -215,35 +215,6 @@ func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, er
|
|||||||
return 200, nil
|
return 200, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func printTree(fs afero.Fs) {
|
|
||||||
const tab = " "
|
|
||||||
|
|
||||||
afero.Walk(fs, "/", filepath.WalkFunc(func(path string, info os.FileInfo, err error) error {
|
|
||||||
if filepath.Base(path) == ".git" {
|
|
||||||
return filepath.SkipDir
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil && !os.IsNotExist(err) {
|
|
||||||
fmt.Println(err)
|
|
||||||
return err
|
|
||||||
} else if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
nIndent := len(strings.Split(path, string(filepath.Separator))) - 1
|
|
||||||
indent := strings.Repeat(tab, nIndent)
|
|
||||||
|
|
||||||
if info.IsDir() {
|
|
||||||
fmt.Printf("%s├ %s\n", indent, info.Name())
|
|
||||||
nIndent++
|
|
||||||
} else {
|
|
||||||
fmt.Printf("%s| %s\t%d\n", indent, info.Name(), info.Size())
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
type aferoHTTP struct {
|
type aferoHTTP struct {
|
||||||
afero.Fs
|
afero.Fs
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ type HugoRenderer interface {
|
|||||||
Stop() error
|
Stop() error
|
||||||
}
|
}
|
||||||
|
|
||||||
func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) {
|
func HugoInternalProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
hugoCfg := &deps.DepsCfg{Fs: hugofs.NewFrom(es.tmpfs, ch.HugoCfg.Cfg)}
|
hugoCfg := &deps.DepsCfg{Fs: hugofs.NewFrom(es.tmpfs, ch.HugoCfg.Cfg)}
|
||||||
@@ -46,7 +46,6 @@ func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idles
|
|||||||
|
|
||||||
err = hugoSites.Build(hugolib.BuildCfg{ResetState: true})
|
err = hugoSites.Build(hugolib.BuildCfg{ResetState: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO better
|
|
||||||
return idleshut.Config{}, fmt.Errorf("caddy-hugo: building site: %v", err)
|
return idleshut.Config{}, fmt.Errorf("caddy-hugo: building site: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,7 +53,7 @@ func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idles
|
|||||||
TickDuration: WebsocketFileTicker,
|
TickDuration: WebsocketFileTicker,
|
||||||
MaxIdleTicks: uint(IdleWebsocketTimeout/WebsocketFileTicker) + 1,
|
MaxIdleTicks: uint(IdleWebsocketTimeout/WebsocketFileTicker) + 1,
|
||||||
Stop: func() error {
|
Stop: func() error {
|
||||||
ch.persistEdits(es)
|
ch.persistEditsForSession(es)
|
||||||
|
|
||||||
es.doc.Close()
|
es.doc.Close()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user