github.com/frostornge/solgen@v0.1.3/proto/proto.go (about) 1 package proto 2 3 import ( 4 "errors" 5 "os" 6 "path/filepath" 7 8 "github.com/frostornge/solgen/deployment" 9 ) 10 11 type option map[string]string 12 type Options map[string]option 13 14 type binder struct { 15 deployments deployment.Deployments 16 typeOptions Options 17 contracts []contract 18 } 19 20 func (bind *binder) parseContracts(deployments deployment.Deployments) { 21 for name, deployment := range deployments { 22 contract := &contract{typeOptions: bind.typeOptions, contractName: name} 23 contract.parseContract(deployment.ABI) 24 bind.contracts = append(bind.contracts, *contract) 25 } 26 } 27 28 func GenerateBind(path string, deployments deployment.Deployments, typeOptions Options) error { 29 stat, err := os.Stat(path) 30 if os.IsNotExist(err) { 31 if err = os.MkdirAll(path, os.ModePerm); err != nil { 32 return err 33 } 34 } else { 35 if !stat.IsDir() { 36 return errors.New("is not directory") 37 } 38 } 39 40 bind := &binder{ 41 deployments: deployments, 42 typeOptions: typeOptions, 43 } 44 bind.parseContracts(deployments) 45 46 for _, contract := range bind.contracts { 47 file := filepath.Join(path, contract.PackageName+".proto") 48 if err := RenderFile(file, contract); err != nil { 49 return err 50 } 51 } 52 return nil 53 }