⚡ generate tmpl in parallel
This commit is contained in:
parent
1afdd936da
commit
aa9a474262
35
encoder.go
35
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) {
|
||||||
|
content, translatedFilename, err := e.buildContent(tmpl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
errChan <- err
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
filename := translatedFilename[:len(translatedFilename)-len(".tmpl")]
|
||||||
|
|
||||||
translatedFilename, err := e.translateString(templateFilename, templateFilename)
|
resultChan <- &plugin_go.CodeGeneratorResponse_File{
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
filename := translatedFilename[0 : len(translatedFilename)-len(".tmpl")]
|
|
||||||
|
|
||||||
files = append(files, &plugin_go.CodeGeneratorResponse_File{
|
|
||||||
Content: &content,
|
Content: &content,
|
||||||
Name: &filename,
|
Name: &filename,
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
}(templateFilename)
|
||||||
|
}
|
||||||
|
for i := 0; i < length; i++ {
|
||||||
|
select {
|
||||||
|
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