func Generate(g *gen.Graph) error {
	entProtoDir := path.Join(g.Config.Target, "proto")
	adapter, err := LoadAdapter(g)
	if err != nil {
		return fmt.Errorf("entproto: failed parsing ent graph: %w", err)
	}
	var errs error
	for _, schema := range g.Schemas {
		name := schema.Name
		_, err := adapter.GetFileDescriptor(name)
		if err != nil && !errors.Is(err, ErrSchemaSkipped) {
			errs = multierr.Append(errs, err)
		}
	}
	if errs != nil {
		return fmt.Errorf("entproto: failed parsing some schemas: %w", errs)
	}
	allDescriptors := make([]*desc.FileDescriptor, 0, len(adapter.AllFileDescriptors()))
	for _, filedesc := range adapter.AllFileDescriptors() {
		allDescriptors = append(allDescriptors, filedesc)
	}
	var printer protoprint.Printer
	if err = printer.PrintProtosToFileSystem(allDescriptors, entProtoDir); err != nil {
		return fmt.Errorf("entproto: failed writing .proto files: %w", err)
	}
	for _, fd := range allDescriptors {
		protoFilePath := filepath.Join(entProtoDir, fd.GetName())
		dir := filepath.Dir(protoFilePath)
		genGoPath := filepath.Join(dir, "generate.go")
		if !fileExists(genGoPath) {
			contents := protocGenerateGo(fd)
			if err := ioutil.WriteFile(genGoPath, []byte(contents), 0600); err != nil {
				return fmt.Errorf("entproto: failed generating generate.go file for %q: %w", protoFilePath, err)
			}
		}
	}
	return nil
}
Generate takes a *gen.Graph and creates .proto files. Next to each .proto file, Generate creates a generate.go
file containing a //go:generate directive to invoke protoc and compile Go code from the protobuf definitions.
If generate.go already exists next to the .proto file, this step is skipped.
package entproto
import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"strings"
	"entgo.io/ent/entc/gen"
	"github.com/jhump/protoreflect/desc"
	"github.com/jhump/protoreflect/desc/protoprint"
	"go.uber.org/multierr"
)
func Hook() gen.Hook {
	return func(next gen.Generator) gen.Generator {
		return gen.GenerateFunc(func(g *gen.Graph) error {
			err := next.Generate(g)
			if err != nil {
				return err
			}
			return Generate(g)
		})
	}
}