Skip to content

Commit

Permalink
Embed templates in go binary
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmcnamee committed Jul 16, 2023
1 parent 2451ee1 commit 326b75a
Showing 1 changed file with 9 additions and 27 deletions.
36 changes: 9 additions & 27 deletions generator/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package generator

import (
"bytes"
"embed"
"fmt"
"go/build"
"go/format"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"text/template"
Expand Down Expand Up @@ -64,46 +63,29 @@ func Run(input *Root) error {
return nil
}

//go:embed templates/*.gotpl templates/actions/*.gotpl
var templateFS embed.FS

func generateClient(input *Root) error {
var buf bytes.Buffer

ctx := build.Default
pkg, err := ctx.Import("github.com/steebchen/prisma-client-go", ".", build.FindOnly)
if err != nil {
return fmt.Errorf("could not get main template asset: %w", err)
}

var templates []*template.Template

templateDir := pkg.Dir + "/generator/templates"
err = filepath.Walk(templateDir, func(path string, info os.FileInfo, err error) error {
if strings.Contains(path, ".gotpl") {
tpl, err := template.ParseFiles(path)
if err != nil {
return err
}
templates = append(templates, tpl.Templates()...)
}

return err
})

tpl, err := template.ParseFS(templateFS, "templates/*.gotpl", "templates/actions/*.gotpl")
if err != nil {
return fmt.Errorf("could not walk dir %s: %w", templateDir, err)
return fmt.Errorf("could not parse template fs: %w", err)
}

// Run header template first
header, err := template.ParseFiles(templateDir + "/_header.gotpl")
header, err := template.ParseFS(templateFS, "templates/_header.gotpl")
if err != nil {
return fmt.Errorf("could not find header template %s: %w", templateDir, err)
return fmt.Errorf("could not find header template: %w", err)
}

if err := header.Execute(&buf, input); err != nil {
return fmt.Errorf("could not write header template: %w", err)
}

// Then process all remaining templates
for _, tpl := range templates {
for _, tpl := range tpl.Templates() {
if strings.Contains(tpl.Name(), "_") {
continue
}
Expand Down

0 comments on commit 326b75a

Please sign in to comment.