Skip to content

Commit

Permalink
Add output base path on config and arg
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvillacortac committed Mar 11, 2022
1 parent 7b913fe commit d762282
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 77 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Rosetta ✨

[![GoDoc](https://godoc.org/github.com/juanvillacortac/rosetta?status.svg)](https://godoc.org/github.com/juanvillacortac/rosetta)

An extensible CLI tool for generic code generation based in Protobuff and YAML source files

## Why?
Expand Down
9 changes: 8 additions & 1 deletion cmd/rosetta.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var (
commit = "XXX"

config = flag.String("c", "config.yml", "path to the config file (json or yaml)")
schema = flag.String("s", "schema.yml", "path to the schema file (overrides defined in json config file)")
output = flag.String("o", "", "path to the output base path")
schema = flag.String("s", "", "path to the schema file (overrides defined in json config file) (default \"schema.yml\" if not defined in config)")
showVersion = flag.Bool("v", false, "show version")
verbose = flag.Bool("V", false, "Verbose output")
)
Expand Down Expand Up @@ -52,6 +53,12 @@ func run() int {

if *schema != "" {
p.SchemaFile = *schema
} else if p.SchemaFile == "" {
p.SchemaFile = "schema.yml"
}

if *output != "" {
p.OutputBasePath = *output
}

if err := p.Parse(); err != nil {
Expand Down
31 changes: 13 additions & 18 deletions examples/angular/source.json → examples/angular/config.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"schema": "schema.yml",
"generators": [
{
"name": "TS Models",
"template": "templates/model.ts.go.tpl",
"output": "generated/models/[model-].ts",
"output": "generated",
"definitions": {
"ts": {
"types": {
"int32": "number",
"int64": "number",
Expand All @@ -16,22 +13,20 @@
"helpers": {
"\"now()\"": "new Date()"
}
}
},
"generators": [
{
"name": "TS Models",
"template": "templates/model.ts.go.tpl",
"output": "models/[model-].ts",
"from": "ts"
},
{
"name": "TS Service",
"template": "templates/service.ts.go.tpl",
"output": "generated/services/[model-].service.ts",
"types": {
"int32": "number",
"int64": "number",
"double": "number",
"boolean": "boolean",
"string": "string",
"date": "Date"
},
"helpers": {
"\"now()\"": "new Date()"
}
"output": "services/[model-].service.ts",
"from": "ts"
}
]
}
6 changes: 4 additions & 2 deletions examples/angular/source.yml → examples/angular/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
output: generated

definitions:
ts:
types:
Expand All @@ -13,10 +15,10 @@ definitions:
generators:
- name: TS Models
template: templates/model.ts.go.tpl
output: generated/models/[model-].ts
output: models/[model-].ts
from: ts

- name: TS Service
template: templates/service.ts.go.tpl
output: generated/services/[model-].service.ts
output: services/[model-].service.ts
from: ts
2 changes: 1 addition & 1 deletion examples/angular/generate.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
go run ../.. -c source.yml
go run ../..
15 changes: 3 additions & 12 deletions pkg/parser/proto/parser.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package proto

import (
"encoding/json"
"fmt"
"io"
"os"
"strings"

"github.com/juanvillacortac/rosetta/pkg/ast"
Expand All @@ -15,19 +13,12 @@ import (
protoparser "github.com/yoheimuta/go-protoparser/v4/parser"
)

func GetRootNodeFromProto(reader io.Reader, config *ParseConfig) (*ast.RootNode, error) {
func GetRootNodeFromProto(reader io.Reader) (*ast.RootNode, error) {
got, err := p_.Parse(
reader,
p_.WithDebug(config.Debug),
p_.WithPermissive(config.Debug),
p_.WithDebug(false),
p_.WithPermissive(true),
)
if config.Debug {
gotJSON, err := json.MarshalIndent(got, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to marshal, err %v\n", err)
}
os.WriteFile("./.rosetta_proto_ast.json", gotJSON, os.ModePerm)
}
if err != nil {
return nil, fmt.Errorf("[Proto parsing error]: %v", err)
}
Expand Down
24 changes: 0 additions & 24 deletions pkg/program/config.go

This file was deleted.

33 changes: 14 additions & 19 deletions pkg/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ import (
y "gopkg.in/yaml.v2"
)

type Program struct {
Definitions generators.Definitions `json:"definitions" yaml:"definitions"`
SchemaFile string `json:"schema" yaml:"schema"`
Generators []generators.GenerateConfig `json:"generators"`
type ProgramConfig struct {
Definitions generators.Definitions `json:"definitions" yaml:"definitions"`
SchemaFile string `json:"schema" yaml:"schema"`
OutputBasePath string `json:"output" yaml:"output"`
Generators []generators.GenerateConfig `json:"generators"`

root *ast.RootNode
}

func NewProgramFromConfigFile(reader *os.File) (*Program, error) {
func NewProgramFromConfigFile(reader *os.File) (*ProgramConfig, error) {
buffer := bytes.Buffer{}
buffer.ReadFrom(reader)
if _, err := buffer.ReadFrom(reader); err != nil {
return nil, err
}
p := &Program{}
p := &ProgramConfig{}
ext := path.Ext(reader.Name())
switch ext {
case ".yml", ".yaml":
Expand All @@ -47,14 +48,7 @@ func NewProgramFromConfigFile(reader *os.File) (*Program, error) {
return p, nil
}

func (p *Program) Parse(options ...Option) error {
config := &ParseConfig{
permissive: true,
}
for _, opt := range options {
opt(config)
}

func (p *ProgramConfig) Parse() error {
reader, err := os.Open(p.SchemaFile)
if err != nil {
err = fmt.Errorf("failed to open %s, err %v", p.SchemaFile, err)
Expand All @@ -74,10 +68,7 @@ func (p *Program) Parse(options ...Option) error {
return fmt.Errorf("[Models parsing error]: %v", err)
}
case ".proto":
root, err = proto.GetRootNodeFromProto(reader, &proto.ParseConfig{
Debug: config.debug,
Permissive: config.permissive,
})
root, err = proto.GetRootNodeFromProto(reader)
default:
return fmt.Errorf("schema file extension not allowed")
}
Expand All @@ -89,7 +80,7 @@ func (p *Program) Parse(options ...Option) error {
return nil
}

func (p *Program) Generate(verbose bool) ([]generators.OutputFile, error) {
func (p *ProgramConfig) Generate(verbose bool) ([]generators.OutputFile, error) {
if p.root == nil {
return nil, fmt.Errorf("schema not loaded")
}
Expand All @@ -102,6 +93,10 @@ func (p *Program) Generate(verbose bool) ([]generators.OutputFile, error) {
if err != nil {
return nil, err
}
for i := range fs {
f := &fs[i]
f.Filename = path.Join(p.OutputBasePath, f.Filename)
}
files = append(files, fs...)
}
return files, nil
Expand Down

0 comments on commit d762282

Please sign in to comment.