refactor: build command

This commit is contained in:
Gustavo "Guz" L. de Mello
2024-05-21 18:37:49 -03:00
parent f1847f214f
commit 3bceac455a
5 changed files with 152 additions and 115 deletions

View File

@@ -1,115 +0,0 @@
package main
import (
"context"
"flag"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"github.com/a-h/templ"
"guz.one/pages"
)
const PERMISSIONS = 0755
type Page struct {
path string
component templ.Component
}
type Writer struct {
root *string
pages []Page
context context.Context
}
func (w Writer) writeFile(path string, writer func(ctx context.Context, w io.Writer) error) {
directory := filepath.Dir(path)
err := os.MkdirAll(directory, PERMISSIONS)
if err != nil {
log.Fatal(err)
}
f, err := os.Create(path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
err = writer(w.context, f)
if err != nil {
log.Fatal(err)
}
}
func (w Writer) WriteAll() {
for _, page := range w.pages {
p := filepath.Join(*w.root, page.path)
log.Printf("Writing page %s", p)
w.writeFile(p, page.component.Render)
}
}
func main() {
dir := flag.String("d", "./dist", "the directory to write the files")
staticDir := flag.String("s", "./static", "the directory to copy static files from")
pages := []Page{
{"index.html", pages.Homepage()},
}
w := Writer{dir, pages, context.Background()}
w.WriteAll()
a, err := filepath.Abs(*staticDir)
if err != nil {
log.Fatal(err)
}
copyFile := func(root string, dest *string) func(string, fs.DirEntry, error) error {
return func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
} else if path == root {
return nil
}
c, err := os.ReadFile(path)
if err != nil {
return err
}
p := strings.TrimPrefix(path, root)
log.Printf("Copying static file %s to %s directory", p, *dest)
p = filepath.Join(*dest, p)
err = os.MkdirAll(filepath.Dir(p), PERMISSIONS)
if err != nil {
return err
}
f, err := os.Create(p)
if err != nil {
return err
}
defer f.Close()
b, err := f.Write(c)
if err != nil {
return err
}
log.Printf("Wrote %v bytes in %s", b, p)
return nil
}
}
err = filepath.WalkDir(a, copyFile(a, dir))
if err != nil {
log.Fatal(err)
}
}

27
cmd/build/main.go Normal file
View File

@@ -0,0 +1,27 @@
package main
import (
"context"
"flag"
"log"
"guz.one/internals"
)
func main() {
dir := flag.String("d", "./dist", "the directory to write the files")
staticDir := flag.String("s", "./static", "the directory to copy static files from")
w := internals.StaticWriter{
DistDir: dir,
StaticDir: staticDir,
Pages: internals.ROUTES,
Context: context.Background(),
Logger: *log.Default(),
}
err := w.WriteAll()
if err != nil {
log.Fatal(err)
}
}

17
config/routes.go Normal file
View File

@@ -0,0 +1,17 @@
package config
import (
"net/http"
"guz.one/api"
"guz.one/internals"
"guz.one/pages"
)
var ROUTES = []internals.Page{
{Path: "index.html", Component: pages.Homepage()},
}
func APIROUTES(mux *http.ServeMux) {
mux.HandleFunc("/api/hello", api.Hello)
}

107
internals/static_writer.go Normal file
View File

@@ -0,0 +1,107 @@
package internals
import (
"context"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"github.com/a-h/templ"
)
const PERMISSIONS = 0755
type Page struct {
Path string
Component templ.Component
}
type StaticWriter struct {
DistDir *string
StaticDir *string
Pages []Page
Context context.Context
Logger log.Logger
}
func (w *StaticWriter) WritePage(path string, writer func(ctx context.Context, w io.Writer) error) error {
directory := filepath.Dir(path)
err := os.MkdirAll(directory, PERMISSIONS)
if err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
err = writer(w.Context, f)
return err
}
func (w *StaticWriter) WriteAll() error {
for _, page := range w.Pages {
p := filepath.Join(*w.DistDir, page.Path)
w.Logger.Printf("Writing page %s", p)
err := w.WritePage(p, page.Component.Render)
if err != nil {
return err
}
}
err := filepath.WalkDir(*w.StaticDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
} else if d.IsDir() || path == *w.StaticDir {
return nil
}
f, err := filepath.Abs(path)
if err != nil {
return err
}
s, err := filepath.Abs(*w.StaticDir)
if err != nil {
return err
}
err = w.CopyStatic(strings.TrimPrefix(f, s))
if err != nil {
return err
}
return nil
})
return err
}
func (w *StaticWriter) CopyStatic(path string) error {
c, err := os.ReadFile(filepath.Join(*w.StaticDir, path))
if err != nil {
return err
}
p := filepath.Join(*w.DistDir, path)
err = os.MkdirAll(filepath.Dir(p), PERMISSIONS)
if err != nil {
return err
}
f, err := os.Create(p)
if err != nil {
return err
}
defer f.Close()
b, err := f.Write(c)
if err != nil {
return err
}
w.Logger.Printf("Wrote %v bytes in %s", b, p)
return nil
}

View File

@@ -2,3 +2,4 @@
"$schema": "https://openapi.vercel.sh/vercel.json",
"outputDirectory": "dist"
}