Skip to content

Commit

Permalink
Added common_import_root & common_import_root_alias options to handle…
Browse files Browse the repository at this point in the history
… proto imports
  • Loading branch information
lyonlai committed Oct 26, 2020
1 parent 1de35eb commit 08c6dff
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 8 deletions.
11 changes: 8 additions & 3 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ type TypeScriptGRPCGatewayGenerator struct {
}

// New returns an initialised generator
func New() *TypeScriptGRPCGatewayGenerator {
return &TypeScriptGRPCGatewayGenerator{
Registry: registry.NewRegistry(),
func New(paramsMap map[string]string) (*TypeScriptGRPCGatewayGenerator, error) {
registry, err := registry.NewRegistry(paramsMap)
if err != nil {
return nil, errors.Wrap(err, "error instantiating a new registry")
}

return &TypeScriptGRPCGatewayGenerator{
Registry: registry,
}, nil
}

// Generate take a code generator request and returns a response. it analyse request with registry and use the generated data to render ts files
Expand Down
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ func encodeResponse(resp proto.Message) {

func main() {
req := decodeReq()
g := generator.New()
paramsMap := getParamsMap(req)
g, err := generator.New(paramsMap)
if err != nil {
panic(err)
}

err := configureLogging(paramsMap)
err = configureLogging(paramsMap)
if err != nil {
panic(err)
}
Expand Down
80 changes: 77 additions & 3 deletions registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ import (
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
log "github.com/sirupsen/logrus" // nolint: depguard
"google.golang.org/protobuf/types/descriptorpb"
"path"
"path/filepath"
"strings"
)

const (
// TSImportRootParamsKey contains the key for common_import_root in parameters
TSImportRootParamsKey = "ts_import_root"
// TSImportRootAliasParamsKey contains the key for common_import_root_alias in parameters
TSImportRootAliasParamsKey = "ts_import_root_alias"
)

// Registry analyse generation request, spits out the data the the rendering process
// it also holds the information about all the types
type Registry struct {
Expand All @@ -18,13 +26,51 @@ type Registry struct {

// FilesToGenerate contains a list of actual file to generate, different from all the files from the request, some of which are import files
FilesToGenerate map[string]bool

// TSImportRoot represents the ts import root for the generator to figure out required import path, will default to cwd
TSImportRoot string

// TSImportRootAlias if not empty will substitutes the common import root when writing the import into the js file
TSImportRootAlias string
}

// NewRegistry initialise the registry and return the instance
func NewRegistry() *Registry {
func NewRegistry(paramsMap map[string]string) (*Registry, error) {
tsImportRoot, tsImportRootAlias, err := getTSImportRootInformation(paramsMap)
if err != nil {
return nil, errors.Wrap(err, "error getting common import root information")
}
return &Registry{
Types: make(map[string]*TypeInformation),
Types: make(map[string]*TypeInformation),
TSImportRoot: tsImportRoot,
TSImportRootAlias: tsImportRootAlias,
}, nil
}

func getTSImportRootInformation(paramsMap map[string]string) (string, string, error) {
tsImportRoot, ok := paramsMap[TSImportRootParamsKey]

if !ok {
tsImportRoot = "."
}

if !path.IsAbs(tsImportRoot) {
absPath, err := filepath.Abs(tsImportRoot)
if err != nil {
return "", "", errors.Wrapf(err, "error turning path %s into absolute path", tsImportRoot)
}

tsImportRoot = absPath
}

tsImportRootAlias, ok := paramsMap[TSImportRootAliasParamsKey]

if !ok {
tsImportRootAlias = ""
}

return tsImportRoot, tsImportRootAlias, nil

}

// TypeInformation store the information about a given type
Expand Down Expand Up @@ -100,6 +146,7 @@ func (r *Registry) isExternalDependenciesOutsidePackage(fqTypeName, packageName

func (r *Registry) collectExternalDependenciesFromData(filesData map[string]*data.File) error {
for _, fileData := range filesData {
log.Debugf("collecting dependencies information for %s", fileData.TSFileName)
// dependency group up the dependency by package+file
dependencies := make(map[string]*data.Dependency)
for _, typeName := range fileData.ExternalDependingTypes {
Expand All @@ -120,7 +167,34 @@ func (r *Registry) collectExternalDependenciesFromData(filesData map[string]*dat
sourceFile := ""
var err error
if !r.IsFileToGenerate(typeInfo.File) {
sourceFile = "gap/protos/" + target
// try to find the actual file path using glob
matches, err := filepath.Glob(path.Join(r.TSImportRoot, "**", typeInfo.File))
if err != nil {
return errors.Wrapf(err, "error looking up real path for proto file %s", typeInfo.File)
}
if len(matches) > 1 {
log.Warnf("more than one proto file found for %s, taking the first one", typeInfo.File)
}

absoluteTsFileName := data.GetTSFileName(matches[0])
log.Debugf("absolute path for match found is: %s", absoluteTsFileName)
if r.TSImportRootAlias != "" { // if an alias has been provided
sourceFile = strings.ReplaceAll(absoluteTsFileName, r.TSImportRoot, r.TSImportRootAlias)
log.Debugf("replacing root alias %s for %s, result: %s", r.TSImportRootAlias, absoluteTsFileName, sourceFile)
} else {
log.Debugf("no root alias found, trying to get the relative path for %s", absoluteTsFileName)
absBase, err := filepath.Abs(base)
if err != nil {
return errors.Wrapf(err, "error looking up absolute directory with base dir: %s", base)
}

sourceFile, err = filepath.Rel(filepath.Dir(absBase), absoluteTsFileName)
if err != nil {
return errors.Wrapf(err, "error looking up relative path for source file %s", absoluteTsFileName)
}

log.Debugf("no root alias found, trying to get the relative path for %s, result: %s", absoluteTsFileName, sourceFile)
}

} else {
sourceFile, err = filepath.Rel(filepath.Dir(base), target)
Expand Down

0 comments on commit 08c6dff

Please sign in to comment.