all repos — vite @ 1ba292b8a8589cd78155ddf1cdbde2f2edc87596

a fast (this time, actually) and minimal static site generator

Add pre-build and post-build commands
Anirudh Oppiliappan x@icyphox.sh
Sat, 13 Jul 2024 00:26:52 +0300
commit

1ba292b8a8589cd78155ddf1cdbde2f2edc87596

parent

bce0b549b8c10271ee7df30d6d7c78af2675cf6c

4 files changed, 88 insertions(+), 7 deletions(-)

jump to
M commands/build.gocommands/build.go

@@ -229,6 +229,9 @@

// Core builder function. Converts markdown to html, // copies over non .md files, etc. func Build() error { + if err := preBuild(); err != nil { + return err + } fmt.Print("vite: building... ") pages := Pages{} if err := pages.initPages(); err != nil {

@@ -286,7 +289,30 @@ os.Mkdir(buildStatic, 0755)

if err := util.CopyDir(StaticDir, buildStatic); err != nil { return err } + fmt.Print("done\n") - fmt.Print("done\n") + if err := postBuild(); err != nil { + return err + } + return nil +} + +func postBuild() error { + for _, cmd := range config.Config.PostBuild { + fmt.Println("vite: running post-build command:", cmd) + if err := util.RunCmd(cmd); err != nil { + return err + } + } + return nil +} + +func preBuild() error { + for _, cmd := range config.Config.PreBuild { + fmt.Println("vite: running pre-build command:", cmd) + if err := util.RunCmd(cmd); err != nil { + return err + } + } return nil }
M config/config.goconfig/config.go

@@ -20,14 +20,44 @@

type ConfigYaml struct { Title string `yaml:"title"` Desc string `yaml:"description"` - DefaultTemplate string `yaml:"default-template"` + DefaultTemplate string `yaml:"-"` Author struct { Name string `yaml:"name"` Email string `yaml:"email"` } `yaml:"author"` - URL string `yaml:"url"` - // Prebuild []string `yaml:"prebuild"` - // Postbuild []string `yaml:"postbuild"` + URL string `yaml:"url"` + PreBuild []string `yaml:"preBuild"` + PostBuild []string `yaml:"postBuild"` +} + +// For backward compat with `default-template` +func (c *ConfigYaml) UnmarshalYAML(value *yaml.Node) error { + type Alias ConfigYaml // Create an alias to avoid recursion + + var aux Alias + + if err := value.Decode(&aux); err != nil { + return err + } + + // Handle the DefaultTemplate field + var temp struct { + DefaultTemplate1 string `yaml:"default-template"` + DefaultTemplate2 string `yaml:"defaultTemplate"` + } + if err := value.Decode(&temp); err != nil { + return err + } + + if temp.DefaultTemplate1 != "" { + aux.DefaultTemplate = temp.DefaultTemplate1 + } else { + aux.DefaultTemplate = temp.DefaultTemplate2 + } + + *c = ConfigYaml(aux) // Assign the unmarshalled values back to the original struct + + return nil } func (c *ConfigYaml) parseConfig() error {
M readmereadme

@@ -29,12 +29,14 @@

type ConfigYaml struct { Title string `yaml:"title"` Desc string `yaml:"description"` - DefaultTemplate string `yaml:"default-template"` + DefaultTemplate string `yaml:"-"` Author struct { Name string `yaml:"name"` Email string `yaml:"email"` } `yaml:"author"` - URL string `yaml:"url"` + URL string `yaml:"url"` + PreBuild []string `yaml:"preBuild"` + PostBuild []string `yaml:"postBuild"` } Example config: https://git.icyphox.sh/site/tree/config.yaml
A util/run.go

@@ -0,0 +1,23 @@

+package util + +import ( + "fmt" + "os/exec" + "strings" +) + +func RunCmd(cmd string, args ...string) error { + // Split the command into the executable and its arguments + parts := strings.Fields(cmd) + if len(parts) == 0 { + return fmt.Errorf("error: is there an empty command?") + } + + execCmd := exec.Command(parts[0], parts[1:]...) + + output, err := execCmd.CombinedOutput() + if err != nil { + return fmt.Errorf("error: command %q failed with %v: %s", cmd, err, output) + } + return nil +}