-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.go
81 lines (69 loc) · 2.02 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package generate
import (
"embed"
"errors"
"fmt"
"go/parser"
"go/token"
"path/filepath"
"github.com/huandu/xstrings"
filesystem "github.com/kilianpaquier/filesystem/pkg"
)
//go:embed all:templates
var tmpl embed.FS
// Run runs the builder generation with input options.
func Run(pwd string, options CLIOptions) error {
// force first rune to lowercase in case of unexported types
// it will be titled in gen template in case the type is exported
options.Prefix = xstrings.FirstRuneToLower(options.Prefix)
// retrieve destination full path
destdir, err := filepath.Abs(options.Destdir)
if err != nil {
return fmt.Errorf("absolute '%s' path: %w", options.Destdir, err)
}
src, err := parseSrc(pwd, options.File)
if err != nil {
return err // error wrapping is handled in parseSrc function
}
srcdir := filepath.Dir(src)
// parse source file as ast to retrieve golang code
file, err := parser.ParseFile(token.NewFileSet(), src, nil, parser.SkipObjectResolution)
if err != nil {
return fmt.Errorf("file %s parsing: %w", src, err)
}
// retrieve file imports to reuse them in template
imports, err := parseImports(file, srcdir, destdir)
if err != nil {
return fmt.Errorf("find %s imports: %w", srcdir, err)
}
sourcePackage, destPackage := func() (string, string) {
if destdir == srcdir {
return "", file.Name.String()
}
return file.Name.String(), filepath.Base(destdir)
}()
var errs []error
// generate all builders for input structs
pkg := packageData{
DestDir: destdir,
DestName: destPackage,
Imports: imports,
SourceName: sourcePackage,
}
builders, err := generateBuilders(file, pkg, options)
if err != nil {
errs = append(errs, err)
}
// generate implementation file$
dest := filepath.Join(destdir, "builders_impl.go")
if len(builders) > 0 && !filesystem.Exists(dest) {
impl := &implData{
Builders: builders,
DestPackage: filepath.Base(destdir),
}
if err := generateAny(ImplTemplate, dest, impl); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}