⚡ generate tmpl in parallel
This commit is contained in:
parent
1afdd936da
commit
aa9a474262
43
encoder.go
43
encoder.go
@ -133,30 +133,39 @@ func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (str
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File {
|
func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File {
|
||||||
files := []*plugin_go.CodeGeneratorResponse_File{}
|
|
||||||
|
|
||||||
templates, err := e.templates()
|
templates, err := e.templates()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("cannot get templates from %q: %v", e.templateDir, err)
|
log.Fatalf("cannot get templates from %q: %v", e.templateDir, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
length := len(templates)
|
||||||
|
files := make([]*plugin_go.CodeGeneratorResponse_File, 0, length)
|
||||||
|
errChan := make(chan error, length)
|
||||||
|
resultChan := make(chan *plugin_go.CodeGeneratorResponse_File, length)
|
||||||
for _, templateFilename := range templates {
|
for _, templateFilename := range templates {
|
||||||
content, err := e.buildContent(templateFilename)
|
go func(tmpl string) {
|
||||||
if err != nil {
|
content, translatedFilename, err := e.buildContent(tmpl)
|
||||||
panic(err)
|
if err != nil {
|
||||||
}
|
errChan <- err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
filename := translatedFilename[:len(translatedFilename)-len(".tmpl")]
|
||||||
|
|
||||||
translatedFilename, err := e.translateString(templateFilename, templateFilename)
|
resultChan <- &plugin_go.CodeGeneratorResponse_File{
|
||||||
if err != nil {
|
Content: &content,
|
||||||
panic(err)
|
Name: &filename,
|
||||||
}
|
}
|
||||||
filename := translatedFilename[0 : len(translatedFilename)-len(".tmpl")]
|
}(templateFilename)
|
||||||
|
}
|
||||||
files = append(files, &plugin_go.CodeGeneratorResponse_File{
|
for i := 0; i < length; i++ {
|
||||||
Content: &content,
|
select {
|
||||||
Name: &filename,
|
case f := <-resultChan:
|
||||||
})
|
files = append(files, f)
|
||||||
|
case err = <-errChan:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user