tangled
alpha
login
or
join now
anirudh.fi
/
vite
5
fork
atom
fast and minimal static site generator
ssg
5
fork
atom
overview
issues
1
pulls
pipelines
Add pre-build and post-build commands
anirudh.fi
2 years ago
1ba292b8
bce0b549
+88
-7
4 changed files
expand all
collapse all
unified
split
commands
build.go
config
config.go
readme
util
run.go
+27
-1
commands/build.go
···
229
229
// Core builder function. Converts markdown to html,
230
230
// copies over non .md files, etc.
231
231
func Build() error {
232
232
+
if err := preBuild(); err != nil {
233
233
+
return err
234
234
+
}
232
235
fmt.Print("vite: building... ")
233
236
pages := Pages{}
234
237
if err := pages.initPages(); err != nil {
···
286
289
if err := util.CopyDir(StaticDir, buildStatic); err != nil {
287
290
return err
288
291
}
292
292
+
fmt.Print("done\n")
289
293
290
290
-
fmt.Print("done\n")
294
294
+
if err := postBuild(); err != nil {
295
295
+
return err
296
296
+
}
297
297
+
return nil
298
298
+
}
299
299
+
300
300
+
func postBuild() error {
301
301
+
for _, cmd := range config.Config.PostBuild {
302
302
+
fmt.Println("vite: running post-build command:", cmd)
303
303
+
if err := util.RunCmd(cmd); err != nil {
304
304
+
return err
305
305
+
}
306
306
+
}
307
307
+
return nil
308
308
+
}
309
309
+
310
310
+
func preBuild() error {
311
311
+
for _, cmd := range config.Config.PreBuild {
312
312
+
fmt.Println("vite: running pre-build command:", cmd)
313
313
+
if err := util.RunCmd(cmd); err != nil {
314
314
+
return err
315
315
+
}
316
316
+
}
291
317
return nil
292
318
}
+34
-4
config/config.go
···
20
20
type ConfigYaml struct {
21
21
Title string `yaml:"title"`
22
22
Desc string `yaml:"description"`
23
23
-
DefaultTemplate string `yaml:"default-template"`
23
23
+
DefaultTemplate string `yaml:"-"`
24
24
Author struct {
25
25
Name string `yaml:"name"`
26
26
Email string `yaml:"email"`
27
27
} `yaml:"author"`
28
28
-
URL string `yaml:"url"`
29
29
-
// Prebuild []string `yaml:"prebuild"`
30
30
-
// Postbuild []string `yaml:"postbuild"`
28
28
+
URL string `yaml:"url"`
29
29
+
PreBuild []string `yaml:"preBuild"`
30
30
+
PostBuild []string `yaml:"postBuild"`
31
31
+
}
32
32
+
33
33
+
// For backward compat with `default-template`
34
34
+
func (c *ConfigYaml) UnmarshalYAML(value *yaml.Node) error {
35
35
+
type Alias ConfigYaml // Create an alias to avoid recursion
36
36
+
37
37
+
var aux Alias
38
38
+
39
39
+
if err := value.Decode(&aux); err != nil {
40
40
+
return err
41
41
+
}
42
42
+
43
43
+
// Handle the DefaultTemplate field
44
44
+
var temp struct {
45
45
+
DefaultTemplate1 string `yaml:"default-template"`
46
46
+
DefaultTemplate2 string `yaml:"defaultTemplate"`
47
47
+
}
48
48
+
if err := value.Decode(&temp); err != nil {
49
49
+
return err
50
50
+
}
51
51
+
52
52
+
if temp.DefaultTemplate1 != "" {
53
53
+
aux.DefaultTemplate = temp.DefaultTemplate1
54
54
+
} else {
55
55
+
aux.DefaultTemplate = temp.DefaultTemplate2
56
56
+
}
57
57
+
58
58
+
*c = ConfigYaml(aux) // Assign the unmarshalled values back to the original struct
59
59
+
60
60
+
return nil
31
61
}
32
62
33
63
func (c *ConfigYaml) parseConfig() error {
+4
-2
readme
···
29
29
type ConfigYaml struct {
30
30
Title string `yaml:"title"`
31
31
Desc string `yaml:"description"`
32
32
-
DefaultTemplate string `yaml:"default-template"`
32
32
+
DefaultTemplate string `yaml:"-"`
33
33
Author struct {
34
34
Name string `yaml:"name"`
35
35
Email string `yaml:"email"`
36
36
} `yaml:"author"`
37
37
-
URL string `yaml:"url"`
37
37
+
URL string `yaml:"url"`
38
38
+
PreBuild []string `yaml:"preBuild"`
39
39
+
PostBuild []string `yaml:"postBuild"`
38
40
}
39
41
40
42
Example config: https://git.icyphox.sh/site/tree/config.yaml
+23
util/run.go
···
1
1
+
package util
2
2
+
3
3
+
import (
4
4
+
"fmt"
5
5
+
"os/exec"
6
6
+
"strings"
7
7
+
)
8
8
+
9
9
+
func RunCmd(cmd string, args ...string) error {
10
10
+
// Split the command into the executable and its arguments
11
11
+
parts := strings.Fields(cmd)
12
12
+
if len(parts) == 0 {
13
13
+
return fmt.Errorf("error: is there an empty command?")
14
14
+
}
15
15
+
16
16
+
execCmd := exec.Command(parts[0], parts[1:]...)
17
17
+
18
18
+
output, err := execCmd.CombinedOutput()
19
19
+
if err != nil {
20
20
+
return fmt.Errorf("error: command %q failed with %v: %s", cmd, err, output)
21
21
+
}
22
22
+
return nil
23
23
+
}