fix module name on ast gen tool

This commit is contained in:
maddalax 2024-09-13 13:29:02 -05:00
parent 2020e47053
commit 723438a677
9 changed files with 93 additions and 39 deletions

3
.gitignore vendored
View file

@ -4,3 +4,6 @@ node_modules/
dist/ dist/
js/dist js/dist
js/node_modules js/node_modules
go.work
go.work.sum
.idea

3
cli/go.mod Normal file
View file

@ -0,0 +1,3 @@
module github.com/maddalax/mhtml/cli
go 1.23.0

23
cli/mhtml/Taskfile.yml Normal file
View file

@ -0,0 +1,23 @@
version: '3'
interval: 500ms
tasks:
ast:
dir: '{{.USER_WORKING_DIR}}'
desc: Generate AST from source code
generates:
- '**/generated.go'
cmds:
- go run github.com/maddalax/mhtml/framework/tooling/astgen
ast-watch:
dir: '{{.USER_WORKING_DIR}}'
desc: Generate AST from source code and watch for changes
watch: true
generates:
- '**/generated.go'
sources:
- '**/*.go'
cmds:
- go run github.com/maddalax/mhtml/framework/tooling/astgen

5
cli/mhtml/main.go Normal file
View file

@ -0,0 +1,5 @@
package main
func main() {
println("Hesssllo, ssHTML!")
}

View file

@ -21,5 +21,6 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect github.com/valyala/fasthttp v1.51.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/sys v0.25.0 // indirect golang.org/x/sys v0.25.0 // indirect
) )

View file

@ -5,6 +5,7 @@ import (
"go/ast" "go/ast"
"go/parser" "go/parser"
"go/token" "go/token"
"golang.org/x/mod/modfile"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -188,12 +189,13 @@ func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) {
path := ctx.Path() path := ctx.Path()
` `
moduleName := GetModuleName()
for _, f := range partials { for _, f := range partials {
if f.FuncName == fName { if f.FuncName == fName {
continue continue
} }
caller := fmt.Sprintf("%s.%s", f.Package, f.FuncName) caller := fmt.Sprintf("%s.%s", f.Package, f.FuncName)
path := fmt.Sprintf("/mhtml/%s.%s", f.Import, f.FuncName) path := fmt.Sprintf("/%s/%s.%s", moduleName, f.Import, f.FuncName)
body += fmt.Sprintf(` body += fmt.Sprintf(`
if path == "%s" || path == "%s" { if path == "%s" || path == "%s" {
@ -216,6 +218,20 @@ func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) {
} }
builder.Append(builder.BuildFunction(f)) builder.Append(builder.BuildFunction(f))
registerFunction := fmt.Sprintf(`
func RegisterPartials(f *fiber.App) {
f.All("%s/partials*", func(ctx *fiber.Ctx) error {
partial := GetPartialFromContext(ctx)
if partial == nil {
return ctx.SendStatus(404)
}
return h.PartialView(ctx, partial)
})
}
`, moduleName)
builder.AppendLine(registerFunction)
} }
func writePartialsFile() { func writePartialsFile() {
@ -230,15 +246,16 @@ func writePartialsFile() {
builder := NewCodeBuilder(nil) builder := NewCodeBuilder(nil)
builder.AppendLine(`// Package partials THIS FILE IS GENERATED. DO NOT EDIT.`) builder.AppendLine(`// Package partials THIS FILE IS GENERATED. DO NOT EDIT.`)
builder.AppendLine("package load") builder.AppendLine("package load")
builder.AddImport("mhtml/h") builder.AddImport("github.com/maddalax/mhtml/framework/h")
builder.AddImport("github.com/gofiber/fiber/v2") builder.AddImport("github.com/gofiber/fiber/v2")
moduleName := GetModuleName()
for _, partial := range partials { for _, partial := range partials {
if partial.Import == "partials/load" { if partial.Import == "partials/load" {
continue continue
} }
fmt.Println(partial.Import) fmt.Println(partial.Import)
builder.AddImport(fmt.Sprintf(`mhtml/%s`, partial.Import)) builder.AddImport(fmt.Sprintf(`%s/%s`, moduleName, partial.Import))
} }
buildGetPartialFromContext(builder, partials) buildGetPartialFromContext(builder, partials)
@ -272,7 +289,7 @@ func writePagesFile() {
builder.AppendLine(`// Package pages THIS FILE IS GENERATED. DO NOT EDIT.`) builder.AppendLine(`// Package pages THIS FILE IS GENERATED. DO NOT EDIT.`)
builder.AppendLine("package pages") builder.AppendLine("package pages")
builder.AddImport("github.com/gofiber/fiber/v2") builder.AddImport("github.com/gofiber/fiber/v2")
builder.AddImport("mhtml/h") builder.AddImport("github.com/maddalax/mhtml/framework/h")
pages, _ := findPublicFuncsReturningHPage("pages") pages, _ := findPublicFuncsReturningHPage("pages")
@ -314,6 +331,18 @@ func writePagesFile() {
}) })
} }
func GetModuleName() string {
wd, _ := os.Getwd()
modPath := filepath.Join(wd, "go.mod")
goModBytes, err := os.ReadFile(modPath)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading go.mod: %v\n", err)
os.Exit(1)
}
modName := modfile.ModulePath(goModBytes)
return modName
}
func main() { func main() {
writePartialsFile() writePartialsFile()
writePagesFile() writePagesFile()

View file

@ -1,10 +1,8 @@
// Package pages THIS FILE IS GENERATED. DO NOT EDIT. // Package pages THIS FILE IS GENERATED. DO NOT EDIT.
package pages package pages
import ( import "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2" import "github.com/maddalax/mhtml/framework/h"
"github.com/maddalax/mhtml/framework/h"
)
func RegisterPages(f *fiber.App) { func RegisterPages(f *fiber.App) {
f.Get("/", func(ctx *fiber.Ctx) error { f.Get("/", func(ctx *fiber.Ctx) error {

View file

@ -1,36 +1,44 @@
// Package partials THIS FILE IS GENERATED. DO NOT EDIT. // Package partials THIS FILE IS GENERATED. DO NOT EDIT.
package load package load
import ( import "github.com/maddalax/mhtml/framework/h"
"github.com/gofiber/fiber/v2" import "github.com/gofiber/fiber/v2"
"github.com/maddalax/mhtml/framework/h" import "github.com/maddalax/mhtml/starter-template/partials"
"github.com/maddalax/mhtml/starter-template/partials" import "github.com/maddalax/mhtml/starter-template/partials/patient"
"github.com/maddalax/mhtml/starter-template/partials/patient" import "github.com/maddalax/mhtml/starter-template/partials/sheet"
"github.com/maddalax/mhtml/starter-template/partials/sheet"
)
func GetPartialFromContext(ctx *fiber.Ctx) *h.Partial { func GetPartialFromContext(ctx *fiber.Ctx) *h.Partial {
path := ctx.Path() path := ctx.Path()
if path == "NewsSheet" || path == "/mhtml/partials.NewsSheet" { if path == "NewsSheet" || path == "/github.com/maddalax/mhtml/starter-template/partials.NewsSheet" {
return partials.NewsSheet(ctx) return partials.NewsSheet(ctx)
} }
if path == "NewsSheetOpenCount" || path == "/mhtml/partials.NewsSheetOpenCount" { if path == "NewsSheetOpenCount" || path == "/github.com/maddalax/mhtml/starter-template/partials.NewsSheetOpenCount" {
return partials.NewsSheetOpenCount(ctx) return partials.NewsSheetOpenCount(ctx)
} }
if path == "Create" || path == "/mhtml/partials/patient.Create" { if path == "Create" || path == "/github.com/maddalax/mhtml/starter-template/partials/patient.Create" {
return patient.Create(ctx) return patient.Create(ctx)
} }
if path == "List" || path == "/mhtml/partials/patient.List" { if path == "List" || path == "/github.com/maddalax/mhtml/starter-template/partials/patient.List" {
return patient.List(ctx) return patient.List(ctx)
} }
if path == "AddPatientSheetPartial" || path == "/mhtml/partials/patient.AddPatientSheetPartial" { if path == "AddPatientSheetPartial" || path == "/github.com/maddalax/mhtml/starter-template/partials/patient.AddPatientSheetPartial" {
return patient.AddPatientSheetPartial(ctx) return patient.AddPatientSheetPartial(ctx)
} }
if path == "ValidateForm" || path == "/mhtml/partials/patient.ValidateForm" { if path == "ValidateForm" || path == "/github.com/maddalax/mhtml/starter-template/partials/patient.ValidateForm" {
return patient.ValidateForm(ctx) return patient.ValidateForm(ctx)
} }
if path == "Close" || path == "/mhtml/partials/sheet.Close" { if path == "Close" || path == "/github.com/maddalax/mhtml/starter-template/partials/sheet.Close" {
return sheet.Close(ctx) return sheet.Close(ctx)
} }
return nil return nil
} }
func RegisterPartials(f *fiber.App) {
f.All("github.com/maddalax/mhtml/starter-template/partials*", func(ctx *fiber.Ctx) error {
partial := GetPartialFromContext(ctx)
if partial == nil {
return ctx.SendStatus(404)
}
return h.PartialView(ctx, partial)
})
}

View file

@ -1,16 +0,0 @@
package load
import (
"github.com/gofiber/fiber/v2"
"github.com/maddalax/mhtml/framework/h"
)
func RegisterPartials(f *fiber.App) {
f.All("/mhtml/partials*", func(ctx *fiber.Ctx) error {
partial := GetPartialFromContext(ctx)
if partial == nil {
return ctx.SendStatus(404)
}
return h.PartialView(ctx, partial)
})
}