From 2bc62b8ce69125250c8839bf1ecc21a5a4b0009c Mon Sep 17 00:00:00 2001 From: maddalax Date: Sun, 22 Sep 2024 21:15:53 -0500 Subject: [PATCH] move astgen files into a __htmgo folder --- cli/htmgo/tasks/astgen/entry.go | 42 ++++++++++++------- .../load/generated.go => __htmgo/app.go} | 21 ++++++++-- htmgo-site/main.go | 7 ++-- htmgo-site/pages/generated.go | 16 ------- todo-list/Taskfile.yml | 6 +-- .../pages-generated.go} | 7 ++-- .../partials-generated.go} | 4 +- todo-list/__htmgo/setup-generated.go | 11 +++++ todo-list/main.go | 6 +-- 9 files changed, 71 insertions(+), 49 deletions(-) rename htmgo-site/{partials/load/generated.go => __htmgo/app.go} (57%) delete mode 100644 htmgo-site/pages/generated.go rename todo-list/{pages/generated.go => __htmgo/pages-generated.go} (58%) rename todo-list/{partials/load/generated.go => __htmgo/partials-generated.go} (96%) create mode 100644 todo-list/__htmgo/setup-generated.go diff --git a/cli/htmgo/tasks/astgen/entry.go b/cli/htmgo/tasks/astgen/entry.go index 97d12c2..56bbd8b 100644 --- a/cli/htmgo/tasks/astgen/entry.go +++ b/cli/htmgo/tasks/astgen/entry.go @@ -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 } diff --git a/htmgo-site/partials/load/generated.go b/htmgo-site/__htmgo/app.go similarity index 57% rename from htmgo-site/partials/load/generated.go rename to htmgo-site/__htmgo/app.go index e63b0bf..4e7f980 100644 --- a/htmgo-site/partials/load/generated.go +++ b/htmgo-site/__htmgo/app.go @@ -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 diff --git a/htmgo-site/main.go b/htmgo-site/main.go index c9d3a6c..4cf1a82 100644 --- a/htmgo-site/main.go +++ b/htmgo-site/main.go @@ -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) }) diff --git a/htmgo-site/pages/generated.go b/htmgo-site/pages/generated.go deleted file mode 100644 index 2e0e656..0000000 --- a/htmgo-site/pages/generated.go +++ /dev/null @@ -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)) - }) -} diff --git a/todo-list/Taskfile.yml b/todo-list/Taskfile.yml index 8e9b9c3..8ed7363 100644 --- a/todo-list/Taskfile.yml +++ b/todo-list/Taskfile.yml @@ -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 \ No newline at end of file diff --git a/todo-list/pages/generated.go b/todo-list/__htmgo/pages-generated.go similarity index 58% rename from todo-list/pages/generated.go rename to todo-list/__htmgo/pages-generated.go index 968a9bb..9123bd7 100644 --- a/todo-list/pages/generated.go +++ b/todo-list/__htmgo/pages-generated.go @@ -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)) }) } diff --git a/todo-list/partials/load/generated.go b/todo-list/__htmgo/partials-generated.go similarity index 96% rename from todo-list/partials/load/generated.go rename to todo-list/__htmgo/partials-generated.go index b7d9e68..b7f8546 100644 --- a/todo-list/partials/load/generated.go +++ b/todo-list/__htmgo/partials-generated.go @@ -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" diff --git a/todo-list/__htmgo/setup-generated.go b/todo-list/__htmgo/setup-generated.go new file mode 100644 index 0000000..1bf4b9c --- /dev/null +++ b/todo-list/__htmgo/setup-generated.go @@ -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) +} diff --git a/todo-list/main.go b/todo-list/main.go index 0188341..f37ebe2 100644 --- a/todo-list/main.go +++ b/todo-list/main.go @@ -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) }, }) }