diff --git a/cmd/demo/config.yml b/cmd/demo/config.yml index cc59a99..bdd8aef 100644 --- a/cmd/demo/config.yml +++ b/cmd/demo/config.yml @@ -1,6 +1,8 @@ --- targets: demo: + services: + - Service package: github.com/foomo/gotsrpc/demo out: /tmp/test.ts mappings: diff --git a/cmd/gotsrpc/gotsrpc.go b/cmd/gotsrpc/gotsrpc.go index 3ec5f41..0355cad 100644 --- a/cmd/gotsrpc/gotsrpc.go +++ b/cmd/gotsrpc/gotsrpc.go @@ -4,7 +4,11 @@ import ( "encoding/json" "flag" "fmt" + "go/format" + "io/ioutil" "os" + "path" + "strings" "github.com/foomo/gotsrpc" "github.com/foomo/gotsrpc/config" @@ -60,8 +64,9 @@ func main() { } fmt.Println(os.Stderr, buildTargets) - /* - longPackageName := args[0] + for name, target := range buildTargets { + fmt.Println(os.Stderr, "building target", name) + longPackageName := target.Package longPackageNameParts := strings.Split(longPackageName, "/") goFilename := path.Join(goPath, "src", longPackageName, "gotsrpc.go") @@ -72,20 +77,19 @@ func main() { } packageName := longPackageNameParts[len(longPackageNameParts)-1] - services, structs, err := gotsrpc.Read(goPath, longPackageName, args[1:]) + services, structs, err := gotsrpc.Read(goPath, longPackageName, target.Services) if err != nil { fmt.Fprintln(os.Stderr, "an error occured while trying to understand your code", err) os.Exit(2) } - jsonDump(structs) - ts, err := gotsrpc.RenderTypeScript(services, structs, conf) + ts, err := gotsrpc.RenderTypeScript(services, structs, target.TypeScriptModule) if err != nil { fmt.Fprintln(os.Stderr, "could not generate ts code", err) os.Exit(3) } - fmt.Println(ts) + fmt.Println(os.Stdout, ts) gocode, goerr := gotsrpc.RenderGo(services, packageName) if goerr != nil { @@ -105,7 +109,6 @@ func main() { fmt.Fprintln(os.Stderr, "could not write go source to file", writeErr) os.Exit(5) } - //fmt.Println(goFilename, gocode) - //gotsrpc.ReadFile("/Users/jan/go/src/github.com/foomo/gotsrpc/demo/demo.go", []string{"Service"}) - */ + } + } diff --git a/config/config.go b/config/config.go index 0c44858..f1475a3 100644 --- a/config/config.go +++ b/config/config.go @@ -7,9 +7,10 @@ import ( ) type Target struct { - Name string - Package string - Services []string + Package string + Services []string + TypeScriptModule string `yaml:"module"` + Out string } type Mapping struct { diff --git a/config/config_test.go b/config/config_test.go index 1586c6f..c07d58e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -5,8 +5,11 @@ import "testing" const sampleConf = `--- targets: demo: + services: + - Service package: github.com/foomo/gotsrpc/demo - out: /tmp/test.ts + module: My.Service + out: /tmp/my-service.ts mappings: foo/bar: module: Sample.Module @@ -34,4 +37,26 @@ func TestLoadConfig(t *testing.T) { if foo.Out != "path/to/ts" || foo.TypeScriptModule != "Sample.Module" { t.Fatal("unexpected data", foo) } + + // looking at the targets + + demoTarget, ok := c.Targets["demo"] + if !ok { + t.Fatal("demo target not found") + } + if demoTarget.Out != "/tmp/my-service.ts" { + t.Fatal("demo target out is wrong") + } + if demoTarget.Package != "github.com/foomo/gotsrpc/demo" { + t.Fatal("wrong target package") + } + if demoTarget.TypeScriptModule != "My.Service" { + t.Fatal("wromg ts module") + } + if len(demoTarget.Services) != 1 { + t.Fatal("wrong number of services") + } + if demoTarget.Services[0] != "Service" { + t.Fatal("first serive is wrong") + } }