Skip to content

Commit

Permalink
fix(import): fixed imports from generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeCaruso committed Sep 9, 2022
1 parent 82bdf1f commit 37f288c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ module github.com/GuilhermeCaruso/mooncake

go 1.18

require gopkg.in/yaml.v3 v3.0.1
require (
golang.org/x/tools v0.1.12
gopkg.in/yaml.v3 v3.0.1
)

require (
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
49 changes: 36 additions & 13 deletions moongen/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,31 @@ import (

"github.com/GuilhermeCaruso/mooncake/moongen/models"
"github.com/GuilhermeCaruso/mooncake/moongen/template"
"golang.org/x/tools/imports"
)

type Builder struct {
pkg string
}

type BuilderRef struct {
b *bytes.Buffer
f *os.File
c []byte
OriginalPath string
NewPath string
File models.File
}

func (br *BuilderRef) build(pkg string) {
br.reset()
br.prepareBuffer()
br.writeHeader()
br.writePkg(pkg)
br.writeImports()
br.writeMockBase()
br.f.Close()
br.formatContent()
br.createFile()
br.writeFile()
}

func (br *BuilderRef) writeMockStructure(template string, i models.Implementation) {
Expand All @@ -39,8 +44,8 @@ func (br *BuilderRef) writeMockStructure(template string, i models.Implementatio
replacedStructure = strings.ReplaceAll(replacedStructure, "%i", pWith)
replacedStructure = strings.ReplaceAll(replacedStructure, "%k", pWithout)

br.f.WriteString(replacedStructure)
br.f.WriteString("\n")
br.b.WriteString(replacedStructure)
br.b.WriteString("\n")
}

func (br *BuilderRef) writeMethods(template string, i models.Implementation, m models.Method) {
Expand All @@ -52,8 +57,8 @@ func (br *BuilderRef) writeMethods(template string, i models.Implementation, m m
replacedMethod = strings.ReplaceAll(replacedMethod, "%u", models.GetArgGenericListString(m.Params))
replacedMethod = strings.ReplaceAll(replacedMethod, "%r", models.GetArgListString(m.Results))
replacedMethod = strings.ReplaceAll(replacedMethod, "%a", models.GetResultListString(m.Results))
br.f.WriteString(replacedMethod)
br.f.WriteString("\n")
br.b.WriteString(replacedMethod)
br.b.WriteString("\n")
}

func (br *BuilderRef) writeMockBase() {
Expand All @@ -71,29 +76,47 @@ func (br *BuilderRef) writeMockBase() {
}
}

func (br *BuilderRef) create() {
func (br *BuilderRef) formatContent() {
formattedContent, err := imports.Process(br.NewPath, br.b.Bytes(), &imports.Options{})
if err != nil {
log.Fatalf("something went wrong when trying to formmat file. err=%q", err.Error())
}
br.c = formattedContent
}

func (br *BuilderRef) createFile() {
f, err := os.Create(br.NewPath)
if err != nil {
log.Fatalf("something went wrong when trying to write file. err=%q", err.Error())
}
br.f = f
}

func (br *BuilderRef) reset() {
func (br *BuilderRef) writeFile() {
br.f.Write(br.c)
br.f.Close()
}

func (br *BuilderRef) prepareBuffer() {
var f bytes.Buffer
br.b = &f
}

func (br *BuilderRef) reset() {
if _, err := os.Stat(br.NewPath); errors.Is(err, os.ErrNotExist) {
br.create()
br.createFile()
return
}

err := os.Remove(br.NewPath)
if err != nil {
log.Fatalf("something went wrong when trying to remove file. err=%q", err.Error())
}
br.create()
br.createFile()
}

func (br *BuilderRef) writeHeader() {
br.f.WriteString(fmt.Sprintf(template.FILE_HEADER,
br.b.WriteString(fmt.Sprintf(template.FILE_HEADER,
time.Now().Format("2006-01-02 15:04:05"), br.OriginalPath))
}

Expand Down Expand Up @@ -130,11 +153,11 @@ func (br *BuilderRef) writeImports() {
buffer.WriteString(")")
}

br.f.WriteString(buffer.String())
br.b.WriteString(buffer.String())
}
}
func (br BuilderRef) writePkg(pkg string) {
br.f.WriteString(fmt.Sprintf("package %s\n", pkg))
br.b.WriteString(fmt.Sprintf("package %s\n", pkg))
}

func NewBuilder(pkg string) *Builder {
Expand Down

0 comments on commit 37f288c

Please sign in to comment.