working interpreter for template files

This commit is contained in:
2025-09-01 13:57:09 -06:00
parent 23e84c263d
commit 382129d2bb
6 changed files with 522 additions and 46 deletions

View File

@ -5,9 +5,6 @@ import (
"embed"
_ "embed"
"fmt"
"github.com/urfave/cli/v2"
"golang.org/x/text/cases"
"golang.org/x/text/language"
vue_gen "masonry/vue-gen"
"os"
"os/exec"
@ -16,6 +13,10 @@ import (
"strings"
"text/template"
"github.com/urfave/cli/v2"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"github.com/alecthomas/participle/v2"
"masonry/interpreter"
@ -572,3 +573,69 @@ func serveCmd() *cli.Command {
},
}
}
func templateCmd() *cli.Command {
return &cli.Command{
Name: "template",
Aliases: []string{"tmpl"},
Usage: "Generate code from templates using Masonry DSL",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "templates",
Usage: "Path to template directory",
Value: "./lang_templates",
Aliases: []string{"t"},
},
&cli.StringFlag{
Name: "output",
Usage: "Output destination directory",
Value: "./output",
Aliases: []string{"o"},
},
&cli.StringFlag{
Name: "input",
Usage: "Input Masonry file path",
Required: true,
Aliases: []string{"i"},
},
},
Action: func(c *cli.Context) error {
templateDir := c.String("templates")
outputDir := c.String("output")
inputFile := c.String("input")
fmt.Printf("Processing templates from: %s\n", templateDir)
fmt.Printf("Input file: %s\n", inputFile)
fmt.Printf("Output directory: %s\n", outputDir)
// Read the Masonry file
content, err := os.ReadFile(inputFile)
if err != nil {
return fmt.Errorf("error reading Masonry file: %w", err)
}
// Parse the Masonry file
parser, err := participle.Build[lang.AST](
participle.Unquote("String"),
)
if err != nil {
return fmt.Errorf("error building parser: %w", err)
}
ast, err := parser.ParseString("", string(content))
if err != nil {
return fmt.Errorf("error parsing Masonry file: %w", err)
}
// Create template interpreter and process templates
templateInterpreter := interpreter.NewTemplateInterpreter()
err = templateInterpreter.ProcessTemplates(*ast, templateDir, outputDir)
if err != nil {
return fmt.Errorf("error processing templates: %w", err)
}
fmt.Println("Template processing completed successfully!")
return nil
},
}
}