84 lines
2.1 KiB
Go
84 lines
2.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
_ "embed"
|
||
|
"flag"
|
||
|
"log"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"text/template"
|
||
|
)
|
||
|
|
||
|
//go:embed internal/lib.html.gotmpl
|
||
|
var libContentTemplate string
|
||
|
|
||
|
//go:embed internal/index.html.gotmpl
|
||
|
var indexContentTemplate string
|
||
|
|
||
|
type Generate struct {
|
||
|
Path string
|
||
|
Package string
|
||
|
Repository string
|
||
|
VCS string
|
||
|
}
|
||
|
|
||
|
func renderFile(filename string, template *template.Template, data any) error {
|
||
|
dir := filepath.Dir(filename)
|
||
|
_ = os.MkdirAll(dir, 0755)
|
||
|
|
||
|
file, err := os.Create(filename)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
defer file.Close()
|
||
|
|
||
|
return template.Execute(file, data)
|
||
|
}
|
||
|
|
||
|
func must[T any](val T, err error) T {
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
return val
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
libTemplate := must(template.New("lib").Parse(libContentTemplate))
|
||
|
indexTemplate := must(template.New("index").Parse(indexContentTemplate))
|
||
|
wd := must(os.Getwd())
|
||
|
|
||
|
cli := flag.NewFlagSet("go-repo", flag.ExitOnError)
|
||
|
|
||
|
baseDir := cli.String("base_dir", filepath.Join(wd, "site"), "configure the directory where this script outputs data")
|
||
|
|
||
|
// ignore due to ExitOnError
|
||
|
_ = cli.Parse(os.Args[1:])
|
||
|
|
||
|
generates := []Generate{
|
||
|
{"em", "go.pitz.tech/em", "https://github.com/mjpitz/em", "git"},
|
||
|
{"lib", "go.pitz.tech/lib", "https://github.com/mjpitz/myago", "git"},
|
||
|
{"okit", "go.pitz.tech/okit", "https://code.pitz.tech/mya/okit", "git"},
|
||
|
{"units", "go.pitz.tech/units", "https://github.com/mjpitz/units", "git"},
|
||
|
{filepath.Join("gorm", "encryption"), "go.pitz.tech/gorm/encryption", "https://github.com/mjpitz/gorm-encryption", "git"},
|
||
|
{filepath.Join("gorm", "crud"), "go.pitz.tech/gorm/crud", "https://code.pitz.tech/mya/gorm-crud", "git"},
|
||
|
{"spdx-fmt", "go.pitz.tech/spdx-fmt", "https://github.com/mjpitz/spdx-fmt", "git"},
|
||
|
}
|
||
|
|
||
|
for _, generate := range generates {
|
||
|
log.Println("rendering", generate.Path)
|
||
|
|
||
|
err := renderFile(filepath.Join(*baseDir, generate.Path, "index.html"), libTemplate, generate)
|
||
|
if err != nil {
|
||
|
log.Fatalln(err.Error())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
log.Println("rendering", "index.html")
|
||
|
err := renderFile(filepath.Join(*baseDir, "index.html"), indexTemplate, map[string]any{"Generates": generates})
|
||
|
if err != nil {
|
||
|
log.Fatalln(err.Error())
|
||
|
}
|
||
|
}
|