fix module name on ast gen tool
This commit is contained in:
parent
2020e47053
commit
723438a677
9 changed files with 93 additions and 39 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -4,3 +4,6 @@ node_modules/
|
|||
dist/
|
||||
js/dist
|
||||
js/node_modules
|
||||
go.work
|
||||
go.work.sum
|
||||
.idea
|
||||
3
cli/go.mod
Normal file
3
cli/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module github.com/maddalax/mhtml/cli
|
||||
|
||||
go 1.23.0
|
||||
23
cli/mhtml/Taskfile.yml
Normal file
23
cli/mhtml/Taskfile.yml
Normal 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
5
cli/mhtml/main.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package main
|
||||
|
||||
func main() {
|
||||
println("Hesssllo, ssHTML!")
|
||||
}
|
||||
|
|
@ -21,5 +21,6 @@ require (
|
|||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasthttp v1.51.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
|
||||
)
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"golang.org/x/mod/modfile"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
|
@ -188,12 +189,13 @@ func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) {
|
|||
path := ctx.Path()
|
||||
`
|
||||
|
||||
moduleName := GetModuleName()
|
||||
for _, f := range partials {
|
||||
if f.FuncName == fName {
|
||||
continue
|
||||
}
|
||||
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(`
|
||||
if path == "%s" || path == "%s" {
|
||||
|
|
@ -216,6 +218,20 @@ func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) {
|
|||
}
|
||||
|
||||
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() {
|
||||
|
|
@ -230,15 +246,16 @@ func writePartialsFile() {
|
|||
builder := NewCodeBuilder(nil)
|
||||
builder.AppendLine(`// Package partials THIS FILE IS GENERATED. DO NOT EDIT.`)
|
||||
builder.AppendLine("package load")
|
||||
builder.AddImport("mhtml/h")
|
||||
builder.AddImport("github.com/maddalax/mhtml/framework/h")
|
||||
builder.AddImport("github.com/gofiber/fiber/v2")
|
||||
|
||||
moduleName := GetModuleName()
|
||||
for _, partial := range partials {
|
||||
if partial.Import == "partials/load" {
|
||||
continue
|
||||
}
|
||||
fmt.Println(partial.Import)
|
||||
builder.AddImport(fmt.Sprintf(`mhtml/%s`, partial.Import))
|
||||
builder.AddImport(fmt.Sprintf(`%s/%s`, moduleName, partial.Import))
|
||||
}
|
||||
|
||||
buildGetPartialFromContext(builder, partials)
|
||||
|
|
@ -272,7 +289,7 @@ func writePagesFile() {
|
|||
builder.AppendLine(`// Package pages THIS FILE IS GENERATED. DO NOT EDIT.`)
|
||||
builder.AppendLine("package pages")
|
||||
builder.AddImport("github.com/gofiber/fiber/v2")
|
||||
builder.AddImport("mhtml/h")
|
||||
builder.AddImport("github.com/maddalax/mhtml/framework/h")
|
||||
|
||||
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() {
|
||||
writePartialsFile()
|
||||
writePagesFile()
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
// Package pages THIS FILE IS GENERATED. DO NOT EDIT.
|
||||
package pages
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/maddalax/mhtml/framework/h"
|
||||
)
|
||||
import "github.com/gofiber/fiber/v2"
|
||||
import "github.com/maddalax/mhtml/framework/h"
|
||||
|
||||
func RegisterPages(f *fiber.App) {
|
||||
f.Get("/", func(ctx *fiber.Ctx) error {
|
||||
|
|
|
|||
|
|
@ -1,36 +1,44 @@
|
|||
// Package partials THIS FILE IS GENERATED. DO NOT EDIT.
|
||||
package load
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/maddalax/mhtml/framework/h"
|
||||
"github.com/maddalax/mhtml/starter-template/partials"
|
||||
"github.com/maddalax/mhtml/starter-template/partials/patient"
|
||||
"github.com/maddalax/mhtml/starter-template/partials/sheet"
|
||||
)
|
||||
import "github.com/maddalax/mhtml/framework/h"
|
||||
import "github.com/gofiber/fiber/v2"
|
||||
import "github.com/maddalax/mhtml/starter-template/partials"
|
||||
import "github.com/maddalax/mhtml/starter-template/partials/patient"
|
||||
import "github.com/maddalax/mhtml/starter-template/partials/sheet"
|
||||
|
||||
func GetPartialFromContext(ctx *fiber.Ctx) *h.Partial {
|
||||
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)
|
||||
}
|
||||
if path == "NewsSheetOpenCount" || path == "/mhtml/partials.NewsSheetOpenCount" {
|
||||
if path == "NewsSheetOpenCount" || path == "/github.com/maddalax/mhtml/starter-template/partials.NewsSheetOpenCount" {
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
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 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)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
Loading…
Reference in a new issue