diff options
Diffstat (limited to 'internal/builder/builder.go')
-rw-r--r-- | internal/builder/builder.go | 120 |
1 files changed, 25 insertions, 95 deletions
diff --git a/internal/builder/builder.go b/internal/builder/builder.go index bb39c43..7c1911e 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -1,10 +1,7 @@ package builder import ( - "context" "fmt" - "io" - "os" "path" "path/filepath" "slices" @@ -12,11 +9,11 @@ import ( "go.alanpearce.eu/website/internal/config" "go.alanpearce.eu/website/internal/content" + "go.alanpearce.eu/website/internal/outputs" "go.alanpearce.eu/website/internal/sitemap" "go.alanpearce.eu/website/templates" "go.alanpearce.eu/x/log" - "github.com/a-h/templ" mapset "github.com/deckarep/golang-set/v2" "gitlab.com/tozd/go/errors" ) @@ -31,62 +28,6 @@ type Result struct { Hashes []string } -var compressFiles = false - -func mkdirp(dirs ...string) error { - err := os.MkdirAll(path.Join(dirs...), 0755) - - return errors.WithMessage(err, "could not create directory") -} - -func outputToFile(output io.Reader, pathParts ...string) error { - filename := path.Join(pathParts...) - // log.Debug("outputting file", "filename", filename) - file, err := openFileAndVariants(filename) - if err != nil { - return errors.WithMessage(err, "could not open output file") - } - defer file.Close() - - if _, err := io.Copy(file, output); err != nil { - return errors.WithMessage(err, "could not write output file") - } - - return nil -} - -func renderToFile(component templ.Component, pathParts ...string) error { - filename := path.Join(pathParts...) - // log.Debug("outputting file", "filename", filename) - file, err := openFileAndVariants(filename) - if err != nil { - return errors.WithMessage(err, "could not open output file") - } - defer file.Close() - - if err := component.Render(context.TODO(), file); err != nil { - return errors.WithMessage(err, "could not write output file") - } - - return nil -} - -func writerToFile(writer io.WriterTo, pathParts ...string) error { - filename := path.Join(pathParts...) - // log.Debug("outputting file", "filename", path.Join(filename...)) - file, err := openFileAndVariants(filename) - if err != nil { - return errors.WithMessage(err, "could not open output file") - } - defer file.Close() - - if _, err := writer.WriteTo(file); err != nil { - return errors.WithMessage(err, "could not write output file") - } - - return nil -} - func joinSourcePath(src string) func(string) string { return func(rel string) string { return filepath.Join(src, rel) @@ -94,26 +35,27 @@ func joinSourcePath(src string) func(string) string { } func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, error) { + output := outputs.NewFilesOutput(ioConfig.Destination, log, &outputs.Options{ + CompressFiles: !ioConfig.Development, + }) + outDir := ioConfig.Destination joinSource := joinSourcePath(ioConfig.Source) - log.Debug("output", "dir", outDir) + + log.Debug("output", "dir", ioConfig.Destination) r := &Result{ Hashes: make([]string, 0), } - err := copyRecursive(joinSource("static"), outDir) + err := output.CopyRecursive(joinSource("static")) if err != nil { return nil, errors.WithMessage(err, "could not copy static files") } - if err := mkdirp(outDir, "post"); err != nil { - return nil, errors.WithMessage(err, "could not create post output directory") - } log.Debug("reading posts") posts, tags, err := content.ReadPosts(&content.Config{ - Root: joinSource("content"), - InputDir: "post", - OutputDir: outDir, + Root: joinSource("content"), + InputDir: "post", }, log.Named("content")) if err != nil { return nil, err @@ -126,25 +68,17 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, } for _, post := range posts { - if err := mkdirp(outDir, "post", post.Basename); err != nil { - return nil, errors.WithMessage(err, "could not create directory for post") - } log.Debug("rendering post", "post", post.Basename) sitemap.AddPath(post.URL, post.Date) - if err := renderToFile(templates.PostPage(config, post), post.Output); err != nil { + if err := output.RenderToFile(templates.PostPage(config, post), post.Output); err != nil { return nil, err } } - if err := mkdirp(outDir, "tags"); err != nil { - return nil, errors.WithMessage(err, "could not create directory for tags") - } log.Debug("rendering tags list") - if err := renderToFile( + if err := output.RenderToFile( templates.TagsPage(config, "tags", mapset.Sorted(tags), "/tags"), - outDir, - "tags", - "index.html", + path.Join("tags", "index.html"), ); err != nil { return nil, err } @@ -157,17 +91,11 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, matchingPosts = append(matchingPosts, post) } } - if err := mkdirp(outDir, "tags", tag); err != nil { - return nil, errors.WithMessage(err, "could not create directory") - } log.Debug("rendering tags page", "tag", tag) url := "/tags/" + tag - if err := renderToFile( + if err := output.RenderToFile( templates.TagPage(config, tag, matchingPosts, url), - outDir, - "tags", - tag, - "index.html", + path.Join("tags", tag, "index.html"), ); err != nil { return nil, err } @@ -183,13 +111,16 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, if err != nil { return nil, errors.WithMessage(err, "could not render tag feed page") } - if err := writerToFile(feed, outDir, "tags", tag, "atom.xml"); err != nil { + if err := output.WriterToFile(feed, path.Join("tags", tag, "atom.xml")); err != nil { return nil, err } } log.Debug("rendering list page") - if err := renderToFile(templates.ListPage(config, posts, "/post"), outDir, "post", "index.html"); err != nil { + if err := output.RenderToFile( + templates.ListPage(config, posts, "/post"), + path.Join("post", "index.html"), + ); err != nil { return nil, err } sitemap.AddPath("/post/", lastMod) @@ -199,7 +130,7 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, if err != nil { return nil, errors.WithMessage(err, "could not render feed") } - if err := writerToFile(feed, outDir, "atom.xml"); err != nil { + if err := output.WriterToFile(feed, "atom.xml"); err != nil { return nil, err } @@ -208,7 +139,7 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, if err != nil { return nil, errors.WithMessage(err, "could not render feed styles") } - if err := outputToFile(feedStyles, outDir, "feed-styles.xsl"); err != nil { + if err := output.OutputToFile(feedStyles, "feed-styles.xsl"); err != nil { return nil, err } _, err = feedStyles.Seek(0, 0) @@ -230,7 +161,7 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, if err != nil { return nil, err } - if err := renderToFile(templates.Homepage(config, posts, content), outDir, "index.html"); err != nil { + if err := output.RenderToFile(templates.Homepage(config, posts, content), "index.html"); err != nil { return nil, err } // it would be nice to set LastMod here, but using the latest post @@ -241,7 +172,7 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, r.Hashes = append(r.Hashes, h) log.Debug("rendering sitemap") - if err := writerToFile(sitemap, outDir, "sitemap.xml"); err != nil { + if err := output.WriterToFile(sitemap, "sitemap.xml"); err != nil { return nil, err } @@ -250,7 +181,7 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, if err != nil { return nil, err } - if err := outputToFile(rob, outDir, "robots.txt"); err != nil { + if err := output.OutputToFile(rob, "robots.txt"); err != nil { return nil, err } @@ -262,7 +193,6 @@ func BuildSite(ioConfig *IOConfig, cfg *config.Config, log *log.Logger) (*Result return nil, errors.New("config is nil") } cfg.InjectLiveReload = ioConfig.Development - compressFiles = !ioConfig.Development templates.Setup() loadCSS(ioConfig.Source) |