Skip to content

Commit

Permalink
some cleanup on astgen
Browse files Browse the repository at this point in the history
  • Loading branch information
maddalax committed Sep 23, 2024
1 parent 2bc62b8 commit 4e563ce
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions cli/htmgo/tasks/astgen/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ type Partial struct {
Import string
}

const GeneratedDirName = "__htmgo"
const EchoModuleName = "github.com/labstack/echo/v4"
const ModuleName = "github.com/maddalax/htmgo/framework/h"

var PackageName = fmt.Sprintf("package %s", GeneratedDirName)
var GeneratedFileLine = fmt.Sprintf("// Package %s THIS FILE IS GENERATED. DO NOT EDIT.", GeneratedDirName)

func sliceCommonPrefix(dir1, dir2 string) string {
// Use filepath.Clean to normalize the paths
dir1 = filepath.Clean(dir1)
Expand Down Expand Up @@ -257,10 +264,10 @@ func writePartialsFile() {
}

builder := NewCodeBuilder(nil)
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")
builder.AppendLine(GeneratedFileLine)
builder.AppendLine(PackageName)
builder.AddImport(ModuleName)
builder.AddImport(EchoModuleName)

moduleName := GetModuleName()
for _, partial := range partials {
Expand All @@ -269,7 +276,7 @@ func writePartialsFile() {

buildGetPartialFromContext(builder, partials)

WriteFile(filepath.Join("__htmgo", "partials-generated.go"), func(content *ast.File) string {
WriteFile(filepath.Join(GeneratedDirName, "partials-generated.go"), func(content *ast.File) string {
return builder.String()
})
}
Expand All @@ -296,14 +303,14 @@ func formatRoute(path string) string {
func writePagesFile() {

builder := NewCodeBuilder(nil)
builder.AppendLine(`// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT.`)
builder.AppendLine("package __htmgo")
builder.AddImport("github.com/labstack/echo/v4")
builder.AppendLine(GeneratedFileLine)
builder.AppendLine(PackageName)
builder.AddImport(EchoModuleName)

pages, _ := findPublicFuncsReturningHPage("pages")

if len(pages) > 0 {
builder.AddImport("github.com/maddalax/htmgo/framework/h")
builder.AddImport(ModuleName)
}

for _, page := range pages {
Expand Down Expand Up @@ -340,7 +347,7 @@ func writePagesFile() {

builder.Append(builder.BuildFunction(f))

WriteFile("__htmgo/pages-generated.go", func(content *ast.File) string {
WriteFile(filepath.Join(GeneratedDirName, "pages-generated.go"), func(content *ast.File) string {
return builder.String()
})
}
Expand Down

0 comments on commit 4e563ce

Please sign in to comment.