read assets from memory directly

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-02-02 07:44:06 +03:00
parent 57187e75dd
commit 6883237766
2 changed files with 82 additions and 75 deletions

View File

@ -2,6 +2,8 @@ package main
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil"
"log" "log"
"net/url" "net/url"
"os" "os"
@ -12,11 +14,13 @@ import (
"github.com/golang/protobuf/protoc-gen-go/descriptor" "github.com/golang/protobuf/protoc-gen-go/descriptor"
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin" plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/unistack-org/protoc-gen-micro/assets"
pgghelpers "github.com/unistack-org/protoc-gen-micro/helpers" pgghelpers "github.com/unistack-org/protoc-gen-micro/helpers"
) )
type GenericTemplateBasedEncoder struct { type GenericTemplateBasedEncoder struct {
templateDir string templateDir string
assetsDir string
service *descriptor.ServiceDescriptorProto service *descriptor.ServiceDescriptorProto
file *descriptor.FileDescriptorProto file *descriptor.FileDescriptorProto
enum []*descriptor.EnumDescriptorProto enum []*descriptor.EnumDescriptorProto
@ -77,6 +81,51 @@ func NewGenericTemplateBasedEncoder(templateDir string, file *descriptor.FileDes
func (e *GenericTemplateBasedEncoder) templates() ([]string, error) { func (e *GenericTemplateBasedEncoder) templates() ([]string, error) {
filenames := []string{} filenames := []string{}
if e.templateDir == "" {
dir, err := assets.Assets.Open("/")
if err != nil {
return nil, fmt.Errorf("failed to open assets dir")
}
fi, err := dir.Readdir(-1)
if err != nil {
return nil, fmt.Errorf("failed to get assets files")
}
if debug {
log.Printf("components to generate: %v", components)
}
for _, f := range fi {
skip := true
for _, component := range components {
if component == "all" || strings.Contains(f.Name(), "_"+component+".pb.go") {
skip = false
}
}
if skip {
if debug {
log.Printf("skip template %s", f.Name())
}
continue
}
if f.IsDir() {
continue
}
if filepath.Ext(f.Name()) != ".tmpl" {
continue
}
if e.debug {
log.Printf("new template: %q", f.Name())
}
filenames = append(filenames, f.Name())
}
return filenames, nil
}
err := filepath.Walk(e.templateDir, func(path string, info os.FileInfo, err error) error { err := filepath.Walk(e.templateDir, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
@ -156,10 +205,27 @@ func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, err
} }
func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, string, error) { func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, string, error) {
var tmpl *template.Template
var err error
if e.templateDir == "" {
fs, err := assets.Assets.Open("/" + templateFilename)
if err != nil {
return "", "", err
}
buf, err := ioutil.ReadAll(fs)
if err != nil {
return "", "", err
}
if err = fs.Close(); err == nil {
tmpl, err = template.New("/" + templateFilename).Funcs(pgghelpers.ProtoHelpersFuncMap).Parse(string(buf))
}
} else {
// initialize template engine // initialize template engine
fullPath := filepath.Join(e.templateDir, templateFilename) fullPath := filepath.Join(e.templateDir, templateFilename)
templateName := filepath.Base(fullPath) templateName := filepath.Base(fullPath)
tmpl, err := template.New(templateName).Funcs(pgghelpers.ProtoHelpersFuncMap).ParseFiles(fullPath) tmpl, err = template.New(templateName).Funcs(pgghelpers.ProtoHelpersFuncMap).ParseFiles(fullPath)
}
if err != nil { if err != nil {
return "", "", err return "", "", err
} }

83
main.go
View File

@ -3,18 +3,15 @@ package main
import ( import (
"fmt" "fmt"
"go/format" "go/format"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"net/url" "net/url"
"os" "os"
"path/filepath"
"strings" "strings"
"github.com/golang/protobuf/protoc-gen-go/generator" "github.com/golang/protobuf/protoc-gen-go/generator"
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin" plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin"
ggdescriptor "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor" ggdescriptor "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
"github.com/unistack-org/protoc-gen-micro/assets"
pgghelpers "github.com/unistack-org/protoc-gen-micro/helpers" pgghelpers "github.com/unistack-org/protoc-gen-micro/helpers"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
) )
@ -28,6 +25,17 @@ const (
boolFalse = "false" boolFalse = "false"
) )
var (
templateDir = ""
templateRepo = ""
destinationDir = "."
debug = false
all = false
singlePackageMode = false
fileMode = false
components = []string{"micro", "grpc"}
)
func main() { func main() {
g := generator.New() g := generator.New()
@ -47,16 +55,6 @@ func main() {
g.CommandLineParameters(g.Request.GetParameter()) g.CommandLineParameters(g.Request.GetParameter())
// Parse parameters // Parse parameters
var (
templateDir = ""
templateRepo = ""
destinationDir = "."
debug = false
all = false
singlePackageMode = false
fileMode = false
components = []string{"micro", "grpc"}
)
if parameter := g.Request.GetParameter(); parameter != "" { if parameter := g.Request.GetParameter(); parameter != "" {
for _, param := range strings.Split(parameter, ",") { for _, param := range strings.Split(parameter, ",") {
parts := strings.Split(param, "=") parts := strings.Split(param, "=")
@ -135,7 +133,7 @@ func main() {
} }
} }
if templateDir == "" || templateRepo != "" { if templateDir == "" && templateRepo != "" {
if templateDir, err = ioutil.TempDir("", "gen-*"); err != nil { if templateDir, err = ioutil.TempDir("", "gen-*"); err != nil {
g.Error(err, "failed to create tmp dir") g.Error(err, "failed to create tmp dir")
} }
@ -149,63 +147,6 @@ func main() {
if err = clone(templateRepo, templateDir); err != nil { if err = clone(templateRepo, templateDir); err != nil {
g.Error(err, "failed to clone repo") g.Error(err, "failed to clone repo")
} }
} else {
dir, err := assets.Assets.Open("/")
if err != nil {
g.Error(err, "failed to open assets dir")
}
fi, err := dir.Readdir(-1)
if err != nil {
g.Error(err, "failed to get assets files")
}
if debug {
log.Printf("components to generate: %v", components)
}
for _, f := range fi {
skip := true
for _, component := range components {
if component == "all" || strings.Contains(f.Name(), "_"+component+".pb.go") {
skip = false
}
}
if skip {
if debug {
log.Printf("skip template %s", f.Name())
}
continue
}
if debug {
log.Printf("copy template %s", f.Name())
}
fpath := filepath.Join(templateDir, f.Name())
if err = os.MkdirAll(filepath.Dir(fpath), os.FileMode(0755)); err != nil {
g.Error(err, "failed to create nested dir")
}
if f.IsDir() {
continue
}
fd, err := os.OpenFile(fpath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, f.Mode())
if err != nil {
g.Error(err, "failed to create template file")
}
fs, err := assets.Assets.Open(f.Name())
if err != nil {
g.Error(err, "failed to open template file")
}
if _, err = io.Copy(fd, fs); err != nil {
fd.Close()
fs.Close()
g.Error(err, "failed to copy template file")
}
if err = fd.Close(); err != nil {
g.Error(err, "failed to flush template file")
}
if err = fs.Close(); err != nil {
g.Error(err, "failed to flush template file")
}
}
} }
} }