move astgen files into a __htmgo folder

This commit is contained in:
maddalax 2024-09-22 21:15:53 -05:00
parent a58cb051e8
commit 2bc62b8ce6
9 changed files with 71 additions and 49 deletions

View file

@ -257,22 +257,19 @@ func writePartialsFile() {
}
builder := NewCodeBuilder(nil)
builder.AppendLine(`// Package partials THIS FILE IS GENERATED. DO NOT EDIT.`)
builder.AppendLine("package load")
builder.AppendLine(`// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT.`)
builder.AppendLine("package __htmgo")
builder.AddImport("github.com/maddalax/htmgo/framework/h")
builder.AddImport("github.com/labstack/echo/v4")
moduleName := GetModuleName()
for _, partial := range partials {
if partial.Import == "partials/load" {
continue
}
builder.AddImport(fmt.Sprintf(`%s/%s`, moduleName, partial.Import))
}
buildGetPartialFromContext(builder, partials)
WriteFile(filepath.Join("partials", "load", "generated.go"), func(content *ast.File) string {
WriteFile(filepath.Join("__htmgo", "partials-generated.go"), func(content *ast.File) string {
return builder.String()
})
}
@ -299,8 +296,8 @@ func formatRoute(path string) string {
func writePagesFile() {
builder := NewCodeBuilder(nil)
builder.AppendLine(`// Package pages THIS FILE IS GENERATED. DO NOT EDIT.`)
builder.AppendLine("package pages")
builder.AppendLine(`// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT.`)
builder.AppendLine("package __htmgo")
builder.AddImport("github.com/labstack/echo/v4")
pages, _ := findPublicFuncsReturningHPage("pages")
@ -310,8 +307,11 @@ func writePagesFile() {
}
for _, page := range pages {
if page.Import != "" && page.Package != "pages" {
builder.AddImport(page.Import)
if page.Import != "" {
moduleName := GetModuleName()
builder.AddImport(
fmt.Sprintf(`%s/%s`, moduleName, page.Import),
)
}
}
@ -321,9 +321,6 @@ func writePagesFile() {
for _, page := range pages {
call := fmt.Sprintf("%s.%s", page.Package, page.FuncName)
if page.Package == "pages" {
call = page.FuncName
}
body += fmt.Sprintf(`
f.GET("%s", func(ctx echo.Context) error {
@ -343,7 +340,7 @@ func writePagesFile() {
builder.Append(builder.BuildFunction(f))
WriteFile("pages/generated.go", func(content *ast.File) string {
WriteFile("__htmgo/pages-generated.go", func(content *ast.File) string {
return builder.String()
})
}
@ -369,5 +366,22 @@ func GenAst(flags ...process.RunFlag) error {
}
writePartialsFile()
writePagesFile()
WriteFile("__htmgo/setup-generated.go", func(content *ast.File) string {
return `
// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT.
package __htmgo
import (
"github.com/labstack/echo/v4"
)
func Register(e *echo.Echo) {
RegisterPartials(e)
RegisterPages(e)
}
`
})
return nil
}

View file

@ -1,9 +1,22 @@
// Package partials THIS FILE IS GENERATED. DO NOT EDIT.
package load
package __htmgo
import (
"github.com/labstack/echo/v4"
"htmgo-site/pages"
"htmgo-site/partials"
)
import "github.com/maddalax/htmgo/framework/h"
import "github.com/labstack/echo/v4"
import "htmgo-site/partials"
func RegisterPages(f *echo.Echo) {
f.GET("/docs", func(ctx echo.Context) error {
cc := ctx.(*h.RequestContext)
return h.HtmlView(ctx, pages.DocsPage(cc))
})
f.GET("/", func(ctx echo.Context) error {
cc := ctx.(*h.RequestContext)
return h.HtmlView(ctx, pages.IndexPage(cc))
})
}
func GetPartialFromContext(ctx echo.Context) *h.Partial {
path := ctx.Request().URL.Path

View file

@ -6,9 +6,9 @@ import (
"github.com/maddalax/htmgo/framework/h"
"github.com/maddalax/htmgo/framework/service"
_ "github.com/mattn/go-sqlite3"
"htmgo-site/__htmgo"
"htmgo-site/internal/markdown"
"htmgo-site/pages"
"htmgo-site/partials/load"
"io/fs"
)
@ -42,8 +42,9 @@ func main() {
}
})
load.RegisterPartials(e)
pages.RegisterPages(e)
__htmgo.RegisterPartials(e)
__htmgo.RegisterPages(e)
pages.RegisterMarkdown(e, "md", MarkdownAssets, func(ctx echo.Context, path string) error {
return pages.MarkdownHandler(ctx.(*h.RequestContext), path)
})

View file

@ -1,16 +0,0 @@
// Package pages THIS FILE IS GENERATED. DO NOT EDIT.
package pages
import "github.com/labstack/echo/v4"
import "github.com/maddalax/htmgo/framework/h"
func RegisterPages(f *echo.Echo) {
f.GET("/docs", func(ctx echo.Context) error {
cc := ctx.(*h.RequestContext)
return h.HtmlView(ctx, DocsPage(cc))
})
f.GET("/", func(ctx echo.Context) error {
cc := ctx.(*h.RequestContext)
return h.HtmlView(ctx, IndexPage(cc))
})
}

View file

@ -3,14 +3,14 @@ version: '3'
tasks:
run:
cmds:
- go run github.com/maddalax/htmgo/cli@latest run
- go run github.com/maddalax/htmgo/cli/htmgo@latest run
silent: true
build:
cmds:
- go run github.com/maddalax/htmgo/cli@latest build
- go run github.com/maddalax/htmgo/cli/htmgo@latest build
watch:
cmds:
- go run github.com/maddalax/htmgo/cli@latest watch
- go run github.com/maddalax/htmgo/cli/htmgo@latest watch
silent: true

View file

@ -1,12 +1,13 @@
// Package pages THIS FILE IS GENERATED. DO NOT EDIT.
package pages
// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT.
package __htmgo
import "github.com/labstack/echo/v4"
import "github.com/maddalax/htmgo/framework/h"
import "todolist/pages"
func RegisterPages(f *echo.Echo) {
f.GET("/", func(ctx echo.Context) error {
cc := ctx.(*h.RequestContext)
return h.HtmlView(ctx, TaskListPage(cc))
return h.HtmlView(ctx, pages.TaskListPage(cc))
})
}

View file

@ -1,5 +1,5 @@
// Package partials THIS FILE IS GENERATED. DO NOT EDIT.
package load
// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT.
package __htmgo
import "github.com/maddalax/htmgo/framework/h"
import "github.com/labstack/echo/v4"

View file

@ -0,0 +1,11 @@
// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT.
package __htmgo
import (
"github.com/labstack/echo/v4"
)
func Register(e *echo.Echo) {
RegisterPartials(e)
RegisterPages(e)
}

View file

@ -5,10 +5,9 @@ import (
"github.com/maddalax/htmgo/framework/h"
"github.com/maddalax/htmgo/framework/service"
_ "github.com/mattn/go-sqlite3"
"todolist/__htmgo"
"todolist/ent"
"todolist/infrastructure/db"
"todolist/pages"
"todolist/partials/load"
)
func main() {
@ -23,8 +22,7 @@ func main() {
LiveReload: true,
Register: func(e *echo.Echo) {
e.Static("/public", "./assets/dist")
load.RegisterPartials(e)
pages.RegisterPages(e)
__htmgo.Register(e)
},
})
}