-
Notifications
You must be signed in to change notification settings - Fork 13
/
plugin_convert.go
executable file
·176 lines (148 loc) · 4.77 KB
/
plugin_convert.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package gbgen
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"runtime"
"github.com/web-ridge/gqlgen-sqlboiler/v3/structs"
"github.com/web-ridge/gqlgen-sqlboiler/v3/cache"
"github.com/web-ridge/gqlgen-sqlboiler/v3/customization"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/web-ridge/gqlgen-sqlboiler/v3/templates"
)
var pathRegex *regexp.Regexp //nolint:gochecknoglobals
func init() { //nolint:gochecknoinits
pathRegex = regexp.MustCompile(`src/(.*)`)
// Default level for this example is info, unless debug flag is present
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
type Import struct {
Alias string
ImportPath string
}
type ConvertTemplateData struct {
Backend structs.Config
Frontend structs.Config
PluginConfig ConvertPluginConfig
PackageName string
Interfaces []*structs.Interface
Models []*structs.Model
Enums []*structs.Enum
Scalars []string
}
func (t ConvertTemplateData) Imports() []Import {
return []Import{
{
Alias: t.Frontend.PackageName,
ImportPath: t.Frontend.Directory,
},
{
Alias: t.Backend.PackageName,
ImportPath: t.Backend.Directory,
},
}
}
func NewConvertPlugin(modelCache *cache.ModelCache, pluginConfig ConvertPluginConfig) *ConvertPlugin {
return &ConvertPlugin{
ModelCache: modelCache,
PluginConfig: pluginConfig,
rootImportPath: getRootImportPath(),
}
}
type ConvertPlugin struct {
BoilerCache *cache.BoilerCache
ModelCache *cache.ModelCache
PluginConfig ConvertPluginConfig
rootImportPath string
}
// DatabaseDriver defines which data syntax to use for some of the converts
type DatabaseDriver string
const (
// MySQL is the default
MySQL DatabaseDriver = "mysql"
// PostgreSQL is the default
PostgreSQL DatabaseDriver = "postgres"
)
type ConvertPluginConfig struct {
DatabaseDriver DatabaseDriver
}
func (m *ConvertPlugin) GenerateCode() error {
data := &ConvertTemplateData{
PackageName: m.ModelCache.Output.PackageName,
Backend: structs.Config{
Directory: path.Join(m.rootImportPath, m.ModelCache.Backend.Directory),
PackageName: m.ModelCache.Backend.PackageName,
},
Frontend: structs.Config{
Directory: path.Join(m.rootImportPath, m.ModelCache.Frontend.Directory),
PackageName: m.ModelCache.Frontend.PackageName,
},
PluginConfig: m.PluginConfig,
Interfaces: m.ModelCache.Interfaces,
Models: m.ModelCache.Models,
Enums: m.ModelCache.Enums,
Scalars: m.ModelCache.Scalars,
}
if err := os.MkdirAll(m.ModelCache.Output.Directory, os.ModePerm); err != nil {
log.Error().Err(err).Str("directory", m.ModelCache.Output.Directory).Msg("could not create directories")
}
if m.PluginConfig.DatabaseDriver == "" {
fmt.Println("Please specify database driver, see README on github")
}
if len(m.ModelCache.Models) == 0 {
log.Warn().Msg("no structs found in graphql so skipping generation")
return nil
}
filesToGenerate := []string{
"generated_convert.go",
"generated_convert_batch.go",
"generated_convert_input.go",
"generated_filter.go",
"generated_preload.go",
"generated_sort.go",
}
// We get all function names from helper repository to check if any customizations are available
// we ignore the files we generated by this plugin
userDefinedFunctions, err := customization.GetFunctionNamesFromDir(m.ModelCache.Output.PackageName, filesToGenerate)
if err != nil {
log.Err(err).Msg("could not parse user defined functions")
}
for _, fn := range filesToGenerate {
m.generateFile(data, fn, userDefinedFunctions)
}
return nil
}
func (m *ConvertPlugin) generateFile(data *ConvertTemplateData, fileName string, userDefinedFunctions []string) {
templateName := fileName + "tpl"
// log.Debug().Msg("[convert] render " + templateName)
templateContent, err := getTemplateContent(templateName)
if err != nil {
log.Err(err).Msg("error when reading " + templateName)
}
if renderError := templates.WriteTemplateFile(
m.ModelCache.Output.Directory+"/"+fileName,
templates.Options{
Template: templateContent,
PackageName: m.ModelCache.Output.PackageName,
Data: data,
UserDefinedFunctions: userDefinedFunctions,
}); renderError != nil {
log.Err(renderError).Msg("error while rendering " + templateName)
}
log.Debug().Msg("[convert] generated " + templateName)
}
func getTemplateContent(filename string) (string, error) {
// load path relative to calling source file
_, callerFile, _, _ := runtime.Caller(1) //nolint:dogsled
rootDir := filepath.Dir(callerFile)
content, err := ioutil.ReadFile(path.Join(rootDir, "template_files", filename))
if err != nil {
return "", fmt.Errorf("could not read template file: %v", err)
}
return string(content), nil
}