Permalink
Browse files

keep on working on refactor

  • Loading branch information...
peter-edge committed Nov 29, 2015
1 parent faf40e4 commit 4f5c8e4c1ddfb263f28b83c3076c9883abff5305
Showing with 407 additions and 731 deletions.
  1. +1 −1 Makefile
  2. +22 −43 api_server.go
  3. +9 −18 client_compiler.go
  4. +59 −41 cmd/protoeasy/main.go
  5. +40 −182 plugins.go
  6. +4 −2 protoeasy.go
  7. +104 −334 protoeasy.pb.go
  8. +2 −0 protoeasy.proto
  9. +37 −65 server_compiler.go
  10. +4 −45 util.go
  11. +125 −0 var.go
View
@@ -2,7 +2,7 @@ PKGS := $(shell go list ./... | grep -v 'go.pedge.io/protoeasy/vendor')
export GO15VENDOREXPERIMENT=1
all: build docker-build docker-launch install installplugins proto example
all: build docker-build docker-launch install installplugins proto example-complete
deps:
GO15VENDOREXPERIMENT=0 go get -d -v $(PKGS)
View
@@ -1,34 +1,36 @@
package protoeasy
import (
"fmt"
"io/ioutil"
"os"
"time"
"go.pedge.io/protolog"
"go.pedge.io/proto/rpclog"
"golang.org/x/net/context"
)
type apiServer struct {
protorpclog.Logger
compiler Compiler
}
func newAPIServer(compiler Compiler) *apiServer {
return &apiServer{compiler}
return &apiServer{protorpclog.NewLogger("protoeasy.API"), compiler}
}
func (a *apiServer) Compile(ctx context.Context, request *CompileRequest) (response *CompileResponse, retErr error) {
defer func(start time.Time) { logCompile(request, response, retErr, time.Since(start)) }(time.Now())
if request == nil {
return nil, newErrNil("request")
}
if request.Archive == nil {
return nil, newErrNil("request.Archive")
}
if request.Directives == nil {
return nil, newErrNil("request.Directives")
}
defer func(start time.Time) {
var compileOptions *CompileOptions
if request != nil {
compileOptions = request.CompileOptions
}
var compileInfo *CompileInfo
if response != nil {
compileInfo = response.CompileInfo
}
a.Log(compileOptions, compileInfo, retErr, time.Since(start))
}(time.Now())
dirPath, err := ioutil.TempDir("", "protoeasy-input")
if err != nil {
return nil, err
@@ -47,44 +49,21 @@ func (a *apiServer) Compile(ctx context.Context, request *CompileRequest) (respo
retErr = err
}
}()
if err := untar(request.Archive, dirPath); err != nil {
if err := untar(request.Tar, dirPath); err != nil {
return nil, err
}
commands, err := a.compiler.Compile(dirPath, outDirPath, request.Directives)
commands, err := a.compiler.Compile(dirPath, outDirPath, request.CompileOptions)
if err != nil {
return nil, err
}
archive, err := tar(outDirPath, []string{"."})
tar, err := tar(outDirPath, []string{"."})
if err != nil {
return nil, err
}
return &CompileResponse{
Command: commands,
Archive: archive,
Tar: tar,
CompileInfo: &CompileInfo{
Command: commands,
},
}, nil
}
func logCompile(request *CompileRequest, response *CompileResponse, err error, duration time.Duration) {
// TODO(pedge): this whole thing needs work, this is just to get it logging as of now
got := "<nil>"
with := 0
ran := 0
returned := 0
errString := ""
if request != nil {
got = fmt.Sprintf("%v", request.Directives)
if request.Archive != nil {
with = len(request.Archive.Value)
}
}
if response != nil {
ran = len(response.Command)
if response.Archive != nil {
returned = len(response.Archive.Value)
}
}
if err != nil {
errString = fmt.Sprintf(", had error %s", err.Error())
}
protolog.Infof("protoeasy.API#Compile: took %v, got %v with %d bytes, ran %d commands, returned %d bytes%s\n", duration, got, with, ran, returned, errString)
}
View
@@ -11,39 +11,30 @@ func newClientCompiler(apiClient APIClient, options CompilerOptions) *clientComp
return &clientCompiler{apiClient, options}
}
func (c *clientCompiler) Compile(dirPath string, outDirPath string, directives *Directives) (retVal []*Command, retErr error) {
if directives == nil {
return nil, newErrNil("directives")
}
func (c *clientCompiler) Compile(dirPath string, outDirPath string, compileOptions *CompileOptions) (retVal []*Command, retErr error) {
relFilePaths, err := getAllRelProtoFilePaths(dirPath)
if err != nil {
return nil, err
}
archive, err := tar(dirPath, relFilePaths)
tar, err := tar(dirPath, relFilePaths)
if err != nil {
return nil, err
}
compileResponse, err := c.apiClient.Compile(
context.Background(),
&CompileRequest{
Archive: archive,
Directives: directives,
Tar: tar,
CompileOptions: compileOptions,
},
)
if err != nil {
return nil, err
}
if compileResponse == nil {
return nil, newErrNil("compileResponse")
}
if compileResponse.Archive == nil {
return nil, newErrNil("compileResponse.Archive")
}
for _, command := range compileResponse.Command {
logCommand(command)
}
if err := untar(compileResponse.Archive, outDirPath); err != nil {
if err := untar(compileResponse.Tar, outDirPath); err != nil {
return nil, err
}
return compileResponse.Command, nil
if compileResponse.CompileInfo != nil {
return compileResponse.CompileInfo.Command, nil
}
return nil, nil
}
View
@@ -3,24 +3,26 @@ package main
import (
"fmt"
"os"
"strings"
"google.golang.org/grpc"
"go.pedge.io/env"
"go.pedge.io/protoeasy"
"go.pedge.io/protolog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type appEnv struct {
Address string `env:"PROTOEASY_ADDRESS"`
}
type options struct {
GoModifiers []string
GoProtocPlugin string
Grpc bool
OutDirPath string
GoModifiers []string
GoPluginType string
OutDirPath string
}
func main() {
@@ -29,7 +31,7 @@ func main() {
func do(appEnvObj interface{}) error {
appEnv := appEnvObj.(*appEnv)
directives := &protoeasy.Directives{}
compileOptions := &protoeasy.CompileOptions{}
options := &options{}
rootCmd := &cobra.Command{
@@ -38,9 +40,9 @@ func do(appEnvObj interface{}) error {
if len(args) != 1 {
return fmt.Errorf("must pass one argument, the directory, but passed %d arguments", len(args))
}
//if err := optionsToDirectives(options, directives); err != nil {
//return err
//}
if err := optionsToCompileOptions(options, compileOptions); err != nil {
return err
}
dirPath := args[0]
outDirPath := args[0]
if options.OutDirPath != "" {
@@ -61,54 +63,63 @@ func do(appEnvObj interface{}) error {
)
}
_, err := compiler.Compile(dirPath, outDirPath, directives)
return err
commands, err := compiler.Compile(dirPath, outDirPath, compileOptions)
if err != nil {
return err
}
logCommands(commands)
return nil
},
}
//bindDirectives(rootCmd.Flags(), directives)
//bindOptions(rootCmd.Flags(), options)
bindCompileOptions(rootCmd.Flags(), compileOptions)
bindOptions(rootCmd.Flags(), options)
return rootCmd.Execute()
}
/*
func bindDirectives(flagSet *pflag.FlagSet, directives *protoeasy.Directives) {
flagSet.StringSliceVar(&directives.ExcludePattern, "exclude", []string{}, "Exclude file patterns.")
flagSet.BoolVar(&directives.Cpp, "cpp", false, "Output cpp files.")
flagSet.StringVar(&directives.CppRelOutDirPath, "cpp-rel-out", "", "The directory, relative to the output directory, to output cpp files.")
flagSet.BoolVar(&directives.Csharp, "csharp", false, "Output csharp files.")
flagSet.StringVar(&directives.CsharpRelOutDirPath, "csharp-rel-out", "", "The directory, relative to the output directory, to output csharp files.")
flagSet.BoolVar(&directives.Objc, "objc", false, "Output objc files.")
flagSet.StringVar(&directives.ObjcRelOutDirPath, "objc-rel-out", "", "The directory, relative to the output directory, to output objc files.")
flagSet.BoolVar(&directives.Python, "python", false, "Output python files.")
flagSet.StringVar(&directives.PythonRelOutDirPath, "python-rel-out", "", "The directory, relative to the output directory, to output python files.")
flagSet.BoolVar(&directives.Ruby, "ruby", false, "Output ruby files.")
flagSet.StringVar(&directives.RubyRelOutDirPath, "ruby-rel-out", "", "The directory, relative to the output directory, to output ruby files.")
flagSet.BoolVar(&directives.Go, "go", false, "Output go files.")
flagSet.StringVar(&directives.GoRelOutDirPath, "go-rel-out", "", "The directory, relative to the output directory, to output go files.")
flagSet.StringVar(&directives.GoImportPath, "go-import-path", "", "Go package.")
flagSet.BoolVar(&directives.GoNoDefaultModifiers, "go-no-default-modifiers", false, "Do not set the default Mfile=package modifiers for --go_out.")
flagSet.BoolVar(&directives.GrpcGateway, "grpc-gateway", false, "Output grpc-gateway .gw.go files.")
func bindCompileOptions(flagSet *pflag.FlagSet, compileOptions *protoeasy.CompileOptions) {
flagSet.BoolVar(&compileOptions.Grpc, "grpc", false, "Output grpc files.")
flagSet.BoolVar(&compileOptions.GrpcGateway, "grpc-gateway", false, "Output grpc-gateway .gw.go files.")
flagSet.StringSliceVar(&compileOptions.ExcludePattern, "exclude", []string{}, "Exclude file patterns.")
flagSet.BoolVar(&compileOptions.Cpp, "cpp", false, "Output cpp files.")
flagSet.StringVar(&compileOptions.CppRelOut, "cpp-rel-out", "", "The directory, relative to the output directory, to output cpp files.")
flagSet.BoolVar(&compileOptions.Csharp, "csharp", false, "Output csharp files.")
flagSet.StringVar(&compileOptions.CsharpRelOut, "csharp-rel-out", "", "The directory, relative to the output directory, to output csharp files.")
flagSet.BoolVar(&compileOptions.Go, "go", false, "Output go files.")
flagSet.StringVar(&compileOptions.GoRelOut, "go-rel-out", "", "The directory, relative to the output directory, to output go files.")
flagSet.StringVar(&compileOptions.GoImportPath, "go-import-path", "", "Go package.")
flagSet.BoolVar(&compileOptions.GoNoDefaultModifiers, "go-no-default-modifiers", false, "Do not set the default Mfile=package modifiers for --go_out.")
flagSet.BoolVar(&compileOptions.Objc, "objc", false, "Output objc files.")
flagSet.StringVar(&compileOptions.ObjcRelOut, "objc-rel-out", "", "The directory, relative to the output directory, to output objc files.")
flagSet.BoolVar(&compileOptions.Python, "python", false, "Output python files.")
flagSet.StringVar(&compileOptions.PythonRelOut, "python-rel-out", "", "The directory, relative to the output directory, to output python files.")
flagSet.BoolVar(&compileOptions.Ruby, "ruby", false, "Output ruby files.")
flagSet.StringVar(&compileOptions.RubyRelOut, "ruby-rel-out", "", "The directory, relative to the output directory, to output ruby files.")
}
func bindOptions(flagSet *pflag.FlagSet, options *options) {
flagSet.StringSliceVar(&options.GoModifiers, "go-modifier", []string{}, "Extra Mfile=package modifiers for --go_out, specify just as file=package to this flag.")
flagSet.StringVar(&options.GoProtocPlugin, "go-protoc-plugin", "go", fmt.Sprintf("The go protoc plugin to use, allowed values are %s, if not go, --go-no-default-modifiers is implied.", strings.Join(protoeasy.AllGoProtocPluginSimpleStrings(), ",")))
flagSet.BoolVar(&options.Grpc, "grpc", false, "Output grpc files.")
flagSet.StringVar(&options.GoPluginType, "go-protoc-plugin", "go", fmt.Sprintf("The go protoc plugin to use, allowed values are %s, if not go, --go-no-default-modifiers is implied.", strings.Join(protoeasy.AllGoPluginTypeSimpleStrings(), ",")))
flagSet.StringVar(&options.OutDirPath, "out", "", "Customize out directory path.")
}
func optionsToDirectives(options *options, directives *protoeasy.Directives) error {
if strings.ToLower(options.GoProtocPlugin) == "none" {
return fmt.Errorf("invalid value for --go-protoc-plugin: %s", options.GoProtocPlugin)
func optionsToCompileOptions(options *options, compileOptions *protoeasy.CompileOptions) error {
if strings.ToLower(options.GoPluginType) == "none" {
return fmt.Errorf("invalid value for --go-protoc-plugin: %s", options.GoPluginType)
}
goProtocPlugin, err := protoeasy.GoProtocPluginSimpleValueOf(options.GoProtocPlugin)
goPluginType, err := protoeasy.GoPluginTypeSimpleValueOf(options.GoPluginType)
if err != nil {
return err
}
directives.GoProtocPlugin = goProtocPlugin
if goProtocPlugin != protoeasy.GoProtocPlugin_GO_PROTOC_PLUGIN_GO {
directives.GoNoDefaultModifiers = true
compileOptions.GoPluginType = goPluginType
if goPluginType != protoeasy.GoPluginType_GO_PLUGIN_TYPE_GO {
compileOptions.GoNoDefaultModifiers = true
}
modifiers := make(map[string]string)
for _, modifierString := range options.GoModifiers {
@@ -118,7 +129,14 @@ func optionsToDirectives(options *options, directives *protoeasy.Directives) err
}
modifiers[split[0]] = split[1]
}
directives.GoModifier = modifiers
compileOptions.GoModifiers = modifiers
return nil
}
*/
func logCommands(commands []*protoeasy.Command) {
for _, command := range commands {
if len(command.Arg) > 0 {
protolog.Infof("\n%s\n", strings.Join(command.Arg, " \\\n\t"))
}
}
}
Oops, something went wrong.

0 comments on commit 4f5c8e4

Please sign in to comment.