-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(generator): added file generator
- Loading branch information
1 parent
280705d
commit 4e1c87b
Showing
13 changed files
with
197 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// ############################ | ||
// Generated by Mooncake | ||
// Date: 2022-01-03 | ||
// Source: xpto | ||
// ############################ | ||
package mocks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// ############################ | ||
// Generated by Mooncake | ||
// Date: 2022-01-03 | ||
// Source: xpto | ||
// ############################ | ||
package mocks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// ############################ | ||
// Generated by Mooncake | ||
// Date: 2022-01-03 | ||
// Source: xpto | ||
// ############################ | ||
package mocks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
path: examples/interfaces | ||
files: | ||
- nested.go | ||
- simple.go | ||
mocks: | ||
package: mocks | ||
path: examples/interfaces | ||
files: | ||
- nested.go | ||
- simple.go | ||
- reference.go | ||
output: examples/mocks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package builder | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/GuilhermeCaruso/mooncake/generator/models" | ||
"github.com/GuilhermeCaruso/mooncake/generator/template" | ||
) | ||
|
||
type Builder struct { | ||
pkg string | ||
} | ||
|
||
type BuilderRef struct { | ||
OriginalPath string | ||
NewPath string | ||
File models.File | ||
} | ||
|
||
func NewBuilder(pkg string) *Builder { | ||
return &Builder{ | ||
pkg: pkg, | ||
} | ||
} | ||
|
||
func (b Builder) BuildFiles(refs []BuilderRef) { | ||
for _, r := range refs { | ||
fmt.Println(ioutil.WriteFile(r.NewPath, []byte(template.FILE_TMP), 0755)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"path/filepath" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type MooncakeFile struct { | ||
Mocks Mock `yaml:"mocks"` | ||
} | ||
|
||
type Mock struct { | ||
Package string `yaml:"package"` | ||
Path string `yaml:"path"` | ||
Files []string `yaml:"files"` | ||
Output string `yaml:"output"` | ||
} | ||
|
||
type Config struct { | ||
Package string | ||
Files []ConfigFile | ||
} | ||
|
||
type ConfigFile struct { | ||
Original string | ||
New string | ||
} | ||
|
||
func NewConfig(p string) Config { | ||
b, err := ioutil.ReadFile(p) | ||
if err != nil { | ||
log.Fatalf("Something went wrong: %s", err.Error()) | ||
} | ||
|
||
var mf MooncakeFile | ||
|
||
if err := yaml.Unmarshal(b, &mf); err != nil { | ||
log.Fatalf("Fail to parse file content: %s", err.Error()) | ||
} | ||
|
||
config := new(Config) | ||
config.setFiles(mf.Mocks.Path, mf.Mocks.Output, mf.Mocks.Files) | ||
return *config | ||
} | ||
|
||
func (c *Config) setFiles(path string, newPath string, files []string) { | ||
for _, f := range files { | ||
c.Files = append(c.Files, ConfigFile{ | ||
Original: filepath.Join(path, f), | ||
New: filepath.Join(newPath, fmt.Sprintf("generated_%s", f)), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/GuilhermeCaruso/mooncake/generator/models" | ||
"github.com/GuilhermeCaruso/mooncake/generator/builder" | ||
"github.com/GuilhermeCaruso/mooncake/generator/config" | ||
"github.com/GuilhermeCaruso/mooncake/generator/parser" | ||
) | ||
|
||
func main() { | ||
generator := new(models.Generator) | ||
parseFiles(generator) | ||
generator.PrepareNested() | ||
mf := config.NewConfig("./examples/mooncake.yaml") | ||
parsedFiles := parseFiles(mf.Files) | ||
|
||
builder.NewBuilder(mf.Package).BuildFiles(parsedFiles) | ||
|
||
b, _ := json.Marshal(generator.Files) | ||
fmt.Println(string(b)) | ||
// b, _ := json.Marshal(parsedFiles) | ||
// fmt.Println(string(b)) | ||
} | ||
|
||
func parseFiles(generator *models.Generator) { | ||
filesToGenerate := []string{ | ||
"./examples/interfaces/nested.go", | ||
"./examples/interfaces/simple.go", | ||
"./examples/interfaces/reference.go", | ||
} | ||
func parseFiles(f []config.ConfigFile) []builder.BuilderRef { | ||
filesToGenerate := f | ||
|
||
queue := make(chan models.File) | ||
queue := make(chan builder.BuilderRef) | ||
defer close(queue) | ||
|
||
files := make([]builder.BuilderRef, 0) | ||
|
||
p := parser.NewParser() | ||
for _, ftg := range filesToGenerate { | ||
go func(fp string) { | ||
queue <- p.Parse(fp) | ||
go func(fp config.ConfigFile) { | ||
result := p.Parse(fp.Original) | ||
queue <- builder.BuilderRef{ | ||
OriginalPath: fp.Original, | ||
NewPath: fp.New, | ||
File: result, | ||
} | ||
}(ftg) | ||
} | ||
for range filesToGenerate { | ||
generator.RegisterFile(<-queue) | ||
files = append(files, <-queue) | ||
} | ||
|
||
return p.PrepareNested(files) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package template | ||
|
||
// import "github.com/GuilhermeCaruso/mooncake/generator/models" | ||
|
||
// type Template struct { | ||
// f models.File | ||
// } | ||
|
||
// func NewTemplate(f models.File) *Template { | ||
// return &Template{ | ||
// f: f, | ||
// } | ||
// } | ||
|
||
const FILE_TMP = `// ############################ | ||
// Generated by Mooncake | ||
// Date: 2022-01-03 | ||
// Source: xpto | ||
// ############################ | ||
package mocks | ||
` | ||
|
||
const METHOD_SIGN = `func (%s *%s) %s %s (%s) %s { | ||
%s | ||
}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/GuilhermeCaruso/mooncake | ||
|
||
go 1.18 | ||
|
||
require gopkg.in/yaml.v3 v3.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
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= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |