| 
 | 1 | +package main  | 
 | 2 | + | 
 | 3 | +import (  | 
 | 4 | +	"fmt"  | 
 | 5 | +	"io/fs"  | 
 | 6 | +	"os"  | 
 | 7 | +	"path"  | 
 | 8 | +	"strings"  | 
 | 9 | + | 
 | 10 | +	"github.com/spf13/cobra"  | 
 | 11 | +	"gitub.com/strangelove-ventures/spawn/spawn"  | 
 | 12 | +)  | 
 | 13 | + | 
 | 14 | +func ProtoServiceGenerate() *cobra.Command {  | 
 | 15 | +	cmd := &cobra.Command{  | 
 | 16 | +		// TODO: this name is ew  | 
 | 17 | +		// TODO: Put this in the make file on proto-gen? (after)  | 
 | 18 | +		Use:     "service-generate [module]",  | 
 | 19 | +		Short:   "Auto generate the MsgService stubs from proto -> Cosmos-SDK",  | 
 | 20 | +		Example: `spawn service-generate mymodule`,  | 
 | 21 | +		Args:    cobra.MaximumNArgs(1),  | 
 | 22 | +		Aliases: []string{"sg"},  | 
 | 23 | +		Run: func(cmd *cobra.Command, args []string) {  | 
 | 24 | +			fmt.Println("service-generate called")  | 
 | 25 | + | 
 | 26 | +			cwd, err := os.Getwd()  | 
 | 27 | +			if err != nil {  | 
 | 28 | +				fmt.Println("Error: ", err)  | 
 | 29 | +			}  | 
 | 30 | + | 
 | 31 | +			protoPath := path.Join(cwd, "proto")  | 
 | 32 | + | 
 | 33 | +			dirs, err := os.ReadDir(protoPath)  | 
 | 34 | +			if err != nil {  | 
 | 35 | +				fmt.Println("Error: ", err)  | 
 | 36 | +			}  | 
 | 37 | + | 
 | 38 | +			absDirs := make([]string, 0)  | 
 | 39 | +			for _, dir := range dirs {  | 
 | 40 | +				if !dir.IsDir() {  | 
 | 41 | +					continue  | 
 | 42 | +				}  | 
 | 43 | + | 
 | 44 | +				if len(args) > 0 && dir.Name() != args[0] {  | 
 | 45 | +					continue  | 
 | 46 | +				}  | 
 | 47 | + | 
 | 48 | +				absDirs = append(absDirs, path.Join(protoPath, dir.Name()))  | 
 | 49 | +			}  | 
 | 50 | + | 
 | 51 | +			fmt.Println("Found dirs: ", absDirs)  | 
 | 52 | + | 
 | 53 | +			// walk it  | 
 | 54 | + | 
 | 55 | +			modules := make(map[string]map[spawn.FileType][]spawn.ProtoService)  | 
 | 56 | + | 
 | 57 | +			fs.WalkDir(os.DirFS(protoPath), ".", func(relPath string, d fs.DirEntry, e error) error {  | 
 | 58 | +				if !strings.HasSuffix(relPath, ".proto") {  | 
 | 59 | +					return nil  | 
 | 60 | +				}  | 
 | 61 | + | 
 | 62 | +				// read file content  | 
 | 63 | +				content, err := os.ReadFile(path.Join(protoPath, relPath))  | 
 | 64 | +				if err != nil {  | 
 | 65 | +					fmt.Println("Error: ", err)  | 
 | 66 | +				}  | 
 | 67 | + | 
 | 68 | +				fileType := spawn.SortContentToFileType(content)  | 
 | 69 | + | 
 | 70 | +				parent := path.Dir(relPath)  | 
 | 71 | +				parent = strings.Split(parent, "/")[0]  | 
 | 72 | + | 
 | 73 | +				// add/append to modules  | 
 | 74 | +				if _, ok := modules[parent]; !ok {  | 
 | 75 | +					modules[parent] = make(map[spawn.FileType][]spawn.ProtoService)  | 
 | 76 | +				}  | 
 | 77 | + | 
 | 78 | +				fmt.Println("debugging... : ", parent, relPath, fileType)  | 
 | 79 | + | 
 | 80 | +				switch fileType {  | 
 | 81 | +				case spawn.Tx:  | 
 | 82 | +					fmt.Println("File is a transaction")  | 
 | 83 | +					tx := spawn.ProtoServiceParser(content)  | 
 | 84 | +					modules[parent][spawn.Tx] = append(modules[parent][spawn.Tx], tx...)  | 
 | 85 | + | 
 | 86 | +				case spawn.Query:  | 
 | 87 | +					fmt.Println("File is a query")  | 
 | 88 | +					query := spawn.ProtoServiceParser(content)  | 
 | 89 | +					modules[parent][spawn.Query] = append(modules[parent][spawn.Query], query...)  | 
 | 90 | +				case spawn.None:  | 
 | 91 | +					fmt.Println("File is neither a transaction nor a query")  | 
 | 92 | +				}  | 
 | 93 | + | 
 | 94 | +				return nil  | 
 | 95 | +			})  | 
 | 96 | + | 
 | 97 | +			fmt.Printf("Modules: %+v\n", modules)  | 
 | 98 | + | 
 | 99 | +		},  | 
 | 100 | +	}  | 
 | 101 | + | 
 | 102 | +	return cmd  | 
 | 103 | +}  | 
0 commit comments