fixed template interpreter after AI flub
This commit is contained in:
@ -2,8 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/urfave/cli/v2"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -15,6 +16,7 @@ func main() {
|
|||||||
setupCmd(),
|
setupCmd(),
|
||||||
vueGenCmd(),
|
vueGenCmd(),
|
||||||
serveCmd(), // New command for server interpreter
|
serveCmd(), // New command for server interpreter
|
||||||
|
templateCmd(), // New command for template interpreter
|
||||||
}
|
}
|
||||||
|
|
||||||
app := &cli.App{
|
app := &cli.App{
|
||||||
|
@ -579,62 +579,87 @@ func templateCmd() *cli.Command {
|
|||||||
Name: "template",
|
Name: "template",
|
||||||
Aliases: []string{"tmpl"},
|
Aliases: []string{"tmpl"},
|
||||||
Usage: "Generate code from templates using Masonry DSL",
|
Usage: "Generate code from templates using Masonry DSL",
|
||||||
|
Description: "This command parses a Masonry file and applies template files to generate code.",
|
||||||
|
Category: "generator",
|
||||||
Flags: []cli.Flag{
|
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{
|
&cli.StringFlag{
|
||||||
Name: "input",
|
Name: "input",
|
||||||
Usage: "Input Masonry file path",
|
Usage: "Path to the Masonry input file",
|
||||||
Required: true,
|
Required: true,
|
||||||
Aliases: []string{"i"},
|
Aliases: []string{"i"},
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "template",
|
||||||
|
Usage: "Path to a single template file",
|
||||||
|
Aliases: []string{"t"},
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "template-dir",
|
||||||
|
Usage: "Path to a directory containing template files",
|
||||||
|
Aliases: []string{"d"},
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "root-template",
|
||||||
|
Usage: "Name of the root template file when using template-dir (defaults to main.tmpl)",
|
||||||
|
Value: "main.tmpl",
|
||||||
|
Aliases: []string{"r"},
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "output",
|
||||||
|
Usage: "Output file path (if not specified, prints to stdout)",
|
||||||
|
Aliases: []string{"o"},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
templateDir := c.String("templates")
|
|
||||||
outputDir := c.String("output")
|
|
||||||
inputFile := c.String("input")
|
inputFile := c.String("input")
|
||||||
|
templateFile := c.String("template")
|
||||||
|
templateDir := c.String("template-dir")
|
||||||
|
rootTemplate := c.String("root-template")
|
||||||
|
outputFile := c.String("output")
|
||||||
|
|
||||||
fmt.Printf("Processing templates from: %s\n", templateDir)
|
// Validate input parameters
|
||||||
fmt.Printf("Input file: %s\n", inputFile)
|
if templateFile == "" && templateDir == "" {
|
||||||
fmt.Printf("Output directory: %s\n", outputDir)
|
return fmt.Errorf("either --template or --template-dir must be specified")
|
||||||
|
}
|
||||||
// Read the Masonry file
|
if templateFile != "" && templateDir != "" {
|
||||||
content, err := os.ReadFile(inputFile)
|
return fmt.Errorf("cannot specify both --template and --template-dir")
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error reading Masonry file: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the Masonry file
|
// Set default template directory and root template if none specified
|
||||||
parser, err := participle.Build[lang.AST](
|
if templateFile == "" && templateDir == "" {
|
||||||
participle.Unquote("String"),
|
templateDir = "./lang_templates"
|
||||||
)
|
rootTemplate = "basic_go_server.tmpl"
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error building parser: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ast, err := parser.ParseString("", string(content))
|
// Create template templateInterpreter
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error parsing Masonry file: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create template interpreter and process templates
|
|
||||||
templateInterpreter := interpreter.NewTemplateInterpreter()
|
templateInterpreter := interpreter.NewTemplateInterpreter()
|
||||||
err = templateInterpreter.ProcessTemplates(*ast, templateDir, outputDir)
|
|
||||||
if err != nil {
|
var result string
|
||||||
return fmt.Errorf("error processing templates: %w", err)
|
var err error
|
||||||
|
|
||||||
|
if templateFile != "" {
|
||||||
|
// Use single template file
|
||||||
|
result, err = templateInterpreter.InterpretFromFile(inputFile, templateFile)
|
||||||
|
} else {
|
||||||
|
// Use template directory
|
||||||
|
result, err = templateInterpreter.InterpretFromDirectory(inputFile, templateDir, rootTemplate)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error generating code: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output result
|
||||||
|
if outputFile != "" {
|
||||||
|
err = os.WriteFile(outputFile, []byte(result), 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error writing output file: %w", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("Generated code written to: %s\n", outputFile)
|
||||||
|
} else {
|
||||||
|
fmt.Print(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Template processing completed successfully!")
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user