Now have a ./bin/fgen command that can generate the stubs of a new feature.

master
Zed A. Shaw 4 weeks ago
parent 20fc1c3960
commit 4ddf65e6d1
  1. 5
      Makefile
  2. 8
      features/init.go
  3. 8
      static/style.css
  4. 78
      tools/cmd/fgen/main.go
  5. 15
      tools/cmd/fgen/templates/feature/api.go
  6. 9
      tools/cmd/fgen/templates/feature/db.go
  7. 10
      tools/cmd/fgen/templates/feature/init.go
  8. 11
      tools/cmd/fgen/templates/feature/views.go
  9. 3
      tools/cmd/fgen/templates/views/index.html

@ -5,9 +5,12 @@ else
curl http://127.0.0.1:9999/webapp || true
endif
build:
build: cmds
go build -o bin/webapp .
cmds:
go build -o bin/fgen ./tools/cmd/fgen
site:
go tool ssgod

@ -0,0 +1,8 @@
package features
import (
"github.com/gofiber/fiber/v2"
)
func Setup(app *fiber.App) {
}

@ -307,6 +307,9 @@
.min-w-md {
min-width: var(--container-md);
}
.grow {
flex-grow: 1;
}
.transform {
transform: var(--tw-rotate-x,) var(--tw-rotate-y,) var(--tw-rotate-z,) var(--tw-skew-x,) var(--tw-skew-y,);
}
@ -395,6 +398,11 @@
}
}
}
.debug {
border-style: var(--tw-border-style) !important;
border-width: 1px !important;
border-color: var(--color-red-900) !important;
}
.border-1 {
border-style: var(--tw-border-style);
border-width: 1px;

@ -0,0 +1,78 @@
package main
import (
"embed"
"fmt"
"io/fs"
"flag"
"path/filepath"
"text/template"
"strings"
"os"
)
//go:embed templates
var templates embed.FS
type Config struct {
Name string
}
func WriteTemplate(config Config, from string, to string) error {
source, err := templates.ReadFile(from)
if err != nil { return err }
t := template.Must(template.New(from).Parse(string(source)))
out, err := os.Create(to)
if err != nil { return err }
err = t.Execute(out, config)
if err != nil { panic(err) }
return nil
}
func main() {
var config Config
flag.StringVar(&config.Name, "name", "", "name of the feature to generate")
flag.Parse()
if config.Name == "" {
fmt.Println("USAGE: fgen -name name")
return
}
feature_dir := filepath.Join("features", config.Name)
view_dir := filepath.Join("views", config.Name)
err := os.MkdirAll(feature_dir, 0755)
if err != nil { panic(err) }
err = os.MkdirAll(view_dir, 0755)
if err != nil { panic(err) }
err = fs.WalkDir(templates, "templates",
func(path string, d fs.DirEntry, err error) error {
if err != nil { return err }
target := filepath.Base(path)
if !d.IsDir() {
switch {
case strings.HasPrefix(path, "templates/views"):
out_path := filepath.Join("views", config.Name, target)
err := WriteTemplate(config, path, out_path)
if err != nil { panic(err) }
case strings.HasPrefix(path, "templates/feature"):
out_path := filepath.Join("features", config.Name, target)
err := WriteTemplate(config, path, out_path)
if err != nil { panic(err) }
}
}
return nil
})
if err != nil { panic(err) }
}

@ -0,0 +1,15 @@
package features_{{ .Name }}
import (
"github.com/gofiber/fiber/v2"
)
func GetApiExample(c *fiber.Ctx) error {
tables := []string{"test", "that"}
return c.JSON(tables)
}
func SetupApi(app *fiber.App) {
app.Get("/api/{{ .Name }}/example", GetApiExample)
}

@ -0,0 +1,9 @@
package features_{{ .Name }}
import (
// "MY/webapp/data"
// _ "github.com/mattn/go-sqlite3"
// sq "github.com/Masterminds/squirrel"
)

@ -0,0 +1,10 @@
package features_{{ .Name }}
import (
"github.com/gofiber/fiber/v2"
)
func Setup(app *fiber.App) {
SetupApi(app)
SetupViews(app)
}

@ -0,0 +1,11 @@
package features_{{ .Name }}
import (
"github.com/gofiber/fiber/v2"
. "MY/webapp/common"
)
func SetupViews(app *fiber.App) {
err := ConfigViews(app, "views/{{ .Name }}")
if err != nil { panic(err) }
}

@ -0,0 +1,3 @@
<h1>{{ .Name }}</h1>
<p>Replace me.</p>
Loading…
Cancel
Save