Browse Source

consolidating some hugo setup code

Stephen Searles 1 year ago
parent
commit
6190379c74
3 changed files with 41 additions and 54 deletions
  1. 1
    24
      caddyhugo.go
  2. 38
    18
      hugo.go
  3. 2
    12
      setup.go

+ 1
- 24
caddyhugo.go View File

@@ -1,10 +1,8 @@
1 1
 package caddyhugo
2 2
 
3 3
 import (
4
-	"fmt"
5 4
 	"html/template"
6 5
 	"net/http"
7
-	"os/exec"
8 6
 	"path/filepath"
9 7
 	"strings"
10 8
 	"sync"
@@ -50,12 +48,7 @@ type CaddyHugo struct {
50 48
 
51 49
 // Build rebuilds the cached state of the site. TODO: determine if this republishes
52 50
 func (ch *CaddyHugo) Build() error {
53
-	err := ch.HugoSites.Build(hugolib.BuildCfg{ResetState: true})
54
-	if err != nil {
55
-		return fmt.Errorf("error building hugo sites: %v", err)
56
-	}
57
-
58
-	return nil
51
+	return buildSite(ch.HugoSites)
59 52
 }
60 53
 
61 54
 // BasePath returns the directory that the CaddyHugo internal/author pages are under
@@ -72,22 +65,6 @@ func (ch *CaddyHugo) docFilename(orig string) string {
72 65
 	return filepath.Join(ch.Dir, docname(orig))
73 66
 }
74 67
 
75
-// Publish really renders new content into the public directory
76
-func (ch *CaddyHugo) Publish() error {
77
-	err := ch.persistAllEdits()
78
-	if err != nil {
79
-		return err
80
-	}
81
-	cmd := exec.Command("hugo")
82
-	cmd.Dir = ch.Dir
83
-	_, err = cmd.CombinedOutput()
84
-	if err != nil {
85
-		return err
86
-	}
87
-
88
-	return nil
89
-}
90
-
91 68
 // TmplData collects data for template execution
92 69
 func (ch *CaddyHugo) TmplData(r *http.Request, docref *editSession) interface{} {
93 70
 	var doc *acedoc.Document

+ 38
- 18
hugo.go View File

@@ -2,11 +2,11 @@ package caddyhugo
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"os/exec"
5 6
 	"path"
6 7
 	"time"
7 8
 
8 9
 	"github.com/gohugoio/hugo/deps"
9
-	"github.com/gohugoio/hugo/hugofs"
10 10
 	"github.com/gohugoio/hugo/hugolib"
11 11
 	"github.com/spf13/afero"
12 12
 
@@ -18,35 +18,55 @@ const (
18 18
 	WebsocketFileTicker  = 1 * time.Second
19 19
 )
20 20
 
21
-type HugoInteractor interface {
22
-	Render(srcdir, workdir string) HugoRenderer
23
-}
21
+// Publish really renders new content into the public directory
22
+func (ch *CaddyHugo) Publish() error {
23
+	err := ch.persistAllEdits()
24
+	if err != nil {
25
+		return err
26
+	}
27
+	cmd := exec.Command("hugo")
28
+	cmd.Dir = ch.Dir
29
+	_, err = cmd.CombinedOutput()
30
+	if err != nil {
31
+		return err
32
+	}
24 33
 
25
-type HugoRenderer interface {
26
-	WriteContent(contents string) error
27
-	Start() error
28
-	Stop() error
34
+	return nil
29 35
 }
30 36
 
31
-func HugoInternalProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) {
37
+func buildSite(sites *hugolib.HugoSites) error {
38
+	err := sites.Build(hugolib.BuildCfg{ResetState: true})
39
+	if err != nil {
40
+		return fmt.Errorf("caddy-hugo: building site: %v", err)
41
+	}
32 42
 
43
+	return err
44
+}
45
+
46
+func (ch *CaddyHugo) configWithFs(fs afero.Fs) (*hugolib.HugoSites, *deps.DepsCfg, error) {
33 47
 	var err error
34
-	hugoCfg := &deps.DepsCfg{Fs: hugofs.NewFrom(es.tmpfs, ch.HugoCfg.Cfg)}
35
-	fmt.Println(ch.Dir)
36
-	hugoCfg.Cfg, err = hugolib.LoadConfig(es.tmpfs, "", path.Join(ch.Dir, "config.toml"))
48
+	cfg := &deps.DepsCfg{}
49
+	cfgPath := path.Join(ch.Dir, "config.toml")
50
+	cfg.Cfg, err = hugolib.LoadConfig(fs, "", cfgPath)
37 51
 	if err != nil {
38
-		return idleshut.Config{}, fmt.Errorf("caddy-hugo: loading site configuration: %v", err)
52
+		return nil, cfg, fmt.Errorf("caddy-hugo: loading site configuration: %v", err)
39 53
 	}
54
+	cfg.Cfg.Set("workingDir", ch.Dir)
40 55
 
41
-	hugoCfg.Cfg.Set("workingDir", ch.Dir)
42
-	hugoSites, err := hugolib.NewHugoSites(*hugoCfg)
56
+	sites, err := hugolib.NewHugoSites(*cfg)
43 57
 	if err != nil {
44
-		return idleshut.Config{}, fmt.Errorf("caddy-hugo: initializing site: %v", err)
58
+		return nil, cfg, fmt.Errorf("caddy-hugo: initializing site: %v", err)
45 59
 	}
46 60
 
47
-	err = hugoSites.Build(hugolib.BuildCfg{ResetState: true})
61
+	err = buildSite(sites)
62
+
63
+	return sites, cfg, err
64
+}
65
+
66
+func HugoInternalProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) {
67
+	hugoSites, _, err := ch.configWithFs(es.tmpfs)
48 68
 	if err != nil {
49
-		return idleshut.Config{}, fmt.Errorf("caddy-hugo: building site: %v", err)
69
+		return idleshut.Config{}, fmt.Errorf("caddy-hugo: loading site configuration: %v", err)
50 70
 	}
51 71
 
52 72
 	return idleshut.Config{

+ 2
- 12
setup.go View File

@@ -9,9 +9,7 @@ import (
9 9
 	"git.stephensearles.com/stephen/caddy-hugo2/comments"
10 10
 	"git.stephensearles.com/stephen/caddy-hugo2/media"
11 11
 
12
-	"github.com/gohugoio/hugo/deps"
13 12
 	"github.com/gohugoio/hugo/hugofs"
14
-	"github.com/gohugoio/hugo/hugolib"
15 13
 	"github.com/mholt/caddy"
16 14
 	"github.com/mholt/caddy/caddyhttp/httpserver"
17 15
 )
@@ -56,17 +54,9 @@ func (ch *CaddyHugo) Setup(dir string) error {
56 54
 	ch.docs = make(map[string]*editSession)
57 55
 	ch.confirmingToClient = make(map[uint64]struct{})
58 56
 
59
-	ch.HugoCfg = &deps.DepsCfg{}
60
-
61
-	ch.HugoCfg.Cfg, err = hugolib.LoadConfig(hugofs.Os, dir, "")
62
-	if err != nil {
63
-		return fmt.Errorf("error loading hugo config: %v", err)
64
-	}
65
-
66
-	ch.HugoCfg.Cfg.Set("workingdir", dir)
67
-	ch.HugoSites, err = hugolib.NewHugoSites(*ch.HugoCfg)
57
+	ch.HugoSites, ch.HugoCfg, err = ch.configWithFs(hugofs.Os)
68 58
 	if err != nil {
69
-		return fmt.Errorf("error intializing hugo: %v", err)
59
+		return fmt.Errorf("error setting up hugo : %v", err)
70 60
 	}
71 61
 
72 62
 	err = ch.Build()

Loading…
Cancel
Save