Skip to content

Commit

Permalink
chore(generator): added file generator
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeCaruso committed Aug 23, 2022
1 parent 280705d commit 4e1c87b
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 44 deletions.
2 changes: 2 additions & 0 deletions examples/interfaces/nested.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package interfaces

import (
"context"
"time"

generated_interface "github.com/GuilhermeCaruso/mooncake/examples/generated"
Expand All @@ -11,6 +12,7 @@ type Other interface {
}

type InternalInterface interface {
ReturnContext(context.Context) context.Context
InternalMethod() (string, generated_interface.NewMockMyNested)
NewMethod(string) (string, int, time.Ticker)
}
Expand Down
6 changes: 6 additions & 0 deletions examples/mocks/generated_nested.go
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
6 changes: 6 additions & 0 deletions examples/mocks/generated_reference.go
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
6 changes: 6 additions & 0 deletions examples/mocks/generated_simple.go
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
12 changes: 8 additions & 4 deletions examples/mooncake.yaml
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
31 changes: 31 additions & 0 deletions generator/builder/builder.go
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))
}
}
57 changes: 57 additions & 0 deletions generator/config/mooncake.go
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)),
})
}
}
42 changes: 23 additions & 19 deletions generator/generator.go
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)
}
21 changes: 0 additions & 21 deletions generator/models/generator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package models

import "log"

type Generator struct {
Files []File
}
Expand All @@ -11,24 +9,5 @@ func (g *Generator) RegisterFile(f File) {
}

func (g *Generator) PrepareNested() {
mim := make(map[string][]Method)
for _, f := range g.Files {
for _, i := range f.Implementations {
mim[i.Name] = i.Methods
}
}

for idxf, f := range g.Files {
for idxi, i := range f.Implementations {
for _, r := range i.References {
imp, has := mim[r]
if !has {
log.Fatalf("Unknown reference: %s", r)
}
g.Files[idxf].Implementations[idxi].Methods = append(
g.Files[idxf].Implementations[idxi].Methods,
imp...)
}
}
}
}
27 changes: 27 additions & 0 deletions generator/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go/token"
"log"

"github.com/GuilhermeCaruso/mooncake/generator/builder"
"github.com/GuilhermeCaruso/mooncake/generator/models"
)

Expand All @@ -29,6 +30,32 @@ func (p *Parser) Parse(fp string) models.File {
return p.parse(file)
}

func (p Parser) PrepareNested(f []builder.BuilderRef) []builder.BuilderRef {
files := f
mim := make(map[string][]models.Method)
for _, f := range files {

for _, i := range f.File.Implementations {
mim[i.Name] = i.Methods
}
}

for idxf, f := range files {
for idxi, i := range f.File.Implementations {
for _, r := range i.References {
imp, has := mim[r]
if !has {
log.Fatalf("Unknown reference: %s", r)
}
files[idxf].File.Implementations[idxi].Methods = append(
files[idxf].File.Implementations[idxi].Methods,
imp...)
}
}
}
return files
}

func (p Parser) parse(f *ast.File) models.File {
newFile := new(models.File)
for _, decl := range f.Decls {
Expand Down
25 changes: 25 additions & 0 deletions generator/template/template.go
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
}`
2 changes: 2 additions & 0 deletions go.mod
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
4 changes: 4 additions & 0 deletions go.sum
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=

0 comments on commit 4e1c87b

Please sign in to comment.