-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
runner.go
79 lines (68 loc) · 2.11 KB
/
runner.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
package goverter
import (
"os"
"path/filepath"
"github.com/jmattheis/goverter/comments"
"github.com/jmattheis/goverter/config"
"github.com/jmattheis/goverter/enum"
"github.com/jmattheis/goverter/generator"
)
// GenerateConfig the config for generating a converter.
type GenerateConfig struct {
// PackagePatterns are golang package patterns to scan, required.
PackagePatterns []string
// WorkingDir is the working directory (usually the location of go.mod file), can be empty.
WorkingDir string
// Global are the global config commands that will be applied to all converters
Global config.RawLines
// BuildTags is a comma separated list passed to -tags when scanning for conversion interfaces.
BuildTags string
// OutputBuildConstraint will be added as go:build constraints to all files.
OutputBuildConstraint string
// EnumTransformers describes additional enum transformers usable in the enum:transform setting.
EnumTransformers map[string]enum.Transformer
}
// GenerateConverters generates converters.
func GenerateConverters(c *GenerateConfig) error {
files, err := generateConvertersRaw(c)
if err != nil {
return err
}
return writeFiles(files)
}
func generateConvertersRaw(c *GenerateConfig) (map[string][]byte, error) {
rawConverters, err := comments.ParseDocs(comments.ParseDocsConfig{
BuildTags: c.BuildTags,
PackagePattern: c.PackagePatterns,
WorkingDir: c.WorkingDir,
})
if err != nil {
return nil, err
}
converters, err := config.Parse(&config.Raw{
BuildTags: c.BuildTags,
WorkDir: c.WorkingDir,
Converters: rawConverters,
Global: c.Global,
OuputBuildConstraint: c.OutputBuildConstraint,
EnumTransformers: c.EnumTransformers,
})
if err != nil {
return nil, err
}
return generator.Generate(converters, generator.Config{
WorkingDir: c.WorkingDir,
BuildConstraint: c.OutputBuildConstraint,
})
}
func writeFiles(files map[string][]byte) error {
for path, content := range files {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
if err := os.WriteFile(path, content, 0o644); err != nil {
return err
}
}
return nil
}