in codegen/module.go [1439:1523]
func (system *ModuleSystem) Build(packageRoot string, baseDirectory string, physicalGenDir string,
instance *ModuleInstance, options Options) error {
classGenerators := system.classes[instance.ClassName]
generator := classGenerators.types[instance.ClassType]
if generator == nil || instance.PackageInfo.CustomInitialisation {
fmt.Fprintf(
os.Stderr,
"Skipping generation of %q %q class of type %q "+
"as generator is not defined\n",
instance.InstanceName,
instance.ClassName,
instance.ClassType,
)
return nil
}
buildResult, err := generator.Generate(instance)
if err != nil {
fmt.Fprintf(
os.Stderr,
"Error generating %q %q of type %q:\n%s\n",
instance.InstanceName,
instance.ClassName,
instance.ClassType,
err.Error(),
)
return err
}
if buildResult == nil {
return nil
}
instance.mu.Lock()
instance.genSpec = buildResult.Spec
instance.mu.Unlock()
if !options.CommitChange {
return nil
}
runner := parallelize.NewUnboundedRunner(len(buildResult.Files))
for filePath, content := range buildResult.Files {
f := func(filePathInf interface{}, contentInf interface{}) (interface{}, error) {
filePath := filePathInf.(string)
content := contentInf.([]byte)
filePath = filepath.Clean(filePath)
resolvedPath := filepath.Join(
physicalGenDir,
filePath,
)
if err := writeFile(resolvedPath, content); err != nil {
return nil, errors.Wrapf(
err,
"Error writing to file %q",
resolvedPath,
)
}
// HACK: The module system writer shouldn't
// assume that we want to format the files in
// this way, but we don't have these formatters
// as a library or a custom post build script
// for the generators yet.
if filepath.Ext(filePath) == ".go" {
if err := FormatGoFile(resolvedPath); err != nil {
return nil, err
}
}
return nil, nil
}
wrk := ¶llelize.TwoParamWork{Data1: filePath, Data2: content, Func: f}
runner.SubmitWork(wrk)
}
_, err = runner.GetResult()
if err != nil {
return err
}
return nil
}