-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprovider.go
More file actions
56 lines (44 loc) · 1.2 KB
/
provider.go
File metadata and controls
56 lines (44 loc) · 1.2 KB
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
package generators
import (
"fmt"
"os"
"strings"
)
const ModuleImport = "@modules:import"
const ModuleRegister = "@modules:register"
type Provider struct {
}
func (p *Provider) Generate(template Template, modulePath string, driver string) {
workDir, _ := os.Getwd()
var path strings.Builder
path.WriteString(workDir)
path.WriteString("/configs/provider.go")
file, err := os.ReadFile(path.String())
if err != nil {
panic(err)
}
contents := strings.Split(string(file), "\n")
importIdx := 0
moduleIdx := 0
skipImport := true
for k, v := range contents {
if strings.Contains(v, ModuleImport) {
importIdx = k
skipImport = false
continue
}
if strings.Contains(v, ModuleRegister) {
moduleIdx = k
break
}
}
if !skipImport {
contents[importIdx] = fmt.Sprintf(` //%s
%s %q`, ModuleImport, template.ModuleLowercase, fmt.Sprintf("%s/%s", template.PackageName, template.ModulePluralLowercase))
}
contents[moduleIdx] = fmt.Sprintf(`
/*@module:%s*/if err := p.AddDefSlice(%s.Dic); err != nil {return err}
//%s`, template.ModuleLowercase, template.ModuleLowercase, ModuleRegister)
body := strings.Join(contents, "\n")
os.WriteFile(path.String(), []byte(body), 0644)
}