-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite generation and expose simple types
- Loading branch information
Showing
41 changed files
with
1,658 additions
and
1,040 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"go/ast" | ||
"io" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"slices" | ||
|
||
"golang.org/x/tools/go/packages" | ||
|
||
"github.com/crhntr/muxt" | ||
"github.com/crhntr/muxt/internal/source" | ||
) | ||
|
||
const CodeGenerationComment = "// Code generated by muxt. DO NOT EDIT." | ||
|
||
type Generate struct { | ||
Package *packages.Package | ||
goPackage string | ||
goFile string | ||
goLine string | ||
|
||
templatesVariable string | ||
outputFilename string | ||
routesFunction string | ||
} | ||
|
||
func (g Generate) ImportReceiverMethods(tp, method string) (*ast.FuncType, []*ast.ImportSpec, bool) { | ||
return source.GoFiles(g.Package.Syntax).ImportReceiverMethods(tp, method) | ||
} | ||
|
||
func generateCommand(args []string, workingDirectory string, getEnv func(string) string, stdout, stderr io.Writer) error { | ||
config := Generate{ | ||
goPackage: getEnv("GOPACKAGE"), | ||
goFile: getEnv("GOFILE"), | ||
goLine: getEnv("GOLINE"), | ||
} | ||
flagSet := flag.NewFlagSet("generate", flag.ContinueOnError) | ||
flagSet.StringVar(&config.templatesVariable, "templates-variable", muxt.DefaultTemplatesVariableName, "templates variable name") | ||
flagSet.StringVar(&config.outputFilename, "output-file", "template_routes.go", "file name of generated output") | ||
flagSet.StringVar(&config.routesFunction, "routes-func", muxt.DefaultRoutesFunctionName, "file name of generated output") | ||
if err := flagSet.Parse(args); err != nil { | ||
return err | ||
} | ||
list, err := packages.Load(&packages.Config{ | ||
Mode: packages.NeedFiles | packages.NeedSyntax | packages.NeedEmbedPatterns | packages.NeedEmbedFiles, | ||
Dir: workingDirectory, | ||
Tests: false, | ||
}, workingDirectory) | ||
if err != nil { | ||
return err | ||
} | ||
if config.goPackage != "" { | ||
i := slices.IndexFunc(list, func(p *packages.Package) bool { return p.ID == config.goPackage }) | ||
if i < 0 { | ||
return fmt.Errorf("package %s not loaded", config.goPackage) | ||
} | ||
config.Package = list[i] | ||
} else if len(list) > 0 { | ||
config.Package = list[0] | ||
} | ||
ts, err := source.Templates(workingDirectory, config.templatesVariable, config.Package.Fset, config.Package.Syntax, config.Package.EmbedFiles) | ||
if err != nil { | ||
return err | ||
} | ||
patterns, err := muxt.TemplatePatterns(ts) | ||
if err != nil { | ||
return err | ||
} | ||
out := log.New(stdout, "", 0) | ||
s, err := muxt.Generate(patterns, config.goPackage, config.templatesVariable, config.routesFunction, "", config.Package.Fset, config.Package.Syntax, config.Package.Syntax, out) | ||
if err != nil { | ||
return err | ||
} | ||
return os.WriteFile(filepath.Join(workingDirectory, config.outputFilename), []byte(CodeGenerationComment+"\n\n"+s), 0644) | ||
} |
26 changes: 12 additions & 14 deletions
26
internal/generate/generate_test.go → cmd/muxt/generate_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
_ "embed" | ||
"log" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/crhntr/muxt/internal/generate" | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
log.Fatal(err) | ||
os.Exit(handleError(err)) | ||
} | ||
os.Exit(handleError(command(wd, flag.Args(), os.Getenv, os.Stdout, os.Stderr))) | ||
} | ||
|
||
func command(wd string, args []string, getEnv func(string) string, stdout, stderr io.Writer) error { | ||
if len(args) > 0 { | ||
switch args[0] { | ||
case "generate", "gen", "g": | ||
return generateCommand(args[1:], wd, getEnv, stdout, stderr) | ||
} | ||
} | ||
logger := log.New(os.Stdout, "muxt: ", 0) | ||
if err := generate.Command(os.Args[1:], wd, logger, os.LookupEnv); err != nil { | ||
log.Fatal(err) | ||
return fmt.Errorf("unknown command") | ||
} | ||
|
||
func handleError(err error) int { | ||
if err != nil { | ||
_, _ = os.Stderr.WriteString(err.Error()) | ||
return 1 | ||
} | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.