|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "go/ast" |
| 8 | + "go/parser" |
| 9 | + "go/token" |
| 10 | + "reflect" |
| 11 | + "sort" |
| 12 | + "strings" |
| 13 | + "text/template" |
| 14 | + |
| 15 | + "github.com/bitfield/script" |
| 16 | +) |
| 17 | + |
| 18 | +func pipeFromBitfield() map[string]reflect.Method { |
| 19 | + p := script.NewPipe() |
| 20 | + pipeType := reflect.ValueOf(p).Type() |
| 21 | + methods := make(map[string]reflect.Method) |
| 22 | + for i := 0; i < pipeType.NumMethod(); i++ { |
| 23 | + method := pipeType.Method(i) |
| 24 | + // take method returning *Pipe |
| 25 | + if method.Func.Type().NumOut() != 1 { |
| 26 | + continue |
| 27 | + } |
| 28 | + if method.Func.Type().Out(0) != reflect.TypeOf(p) { |
| 29 | + continue |
| 30 | + } |
| 31 | + methods[method.Name] = method |
| 32 | + } |
| 33 | + return methods |
| 34 | +} |
| 35 | + |
| 36 | +func pipeFromSourceFile(file string) (map[string]struct{}, error) { |
| 37 | + // Create a new file set |
| 38 | + fset := token.NewFileSet() |
| 39 | + |
| 40 | + // Parse the Go source file |
| 41 | + node, err := parser.ParseFile(fset, file, nil, parser.AllErrors) |
| 42 | + if err != nil { |
| 43 | + return nil, fmt.Errorf("error parsing file: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + rv := make(map[string]struct{}) |
| 47 | + |
| 48 | + for _, decl := range node.Decls { |
| 49 | + fn, ok := decl.(*ast.FuncDecl) |
| 50 | + if !ok || fn.Recv == nil { |
| 51 | + continue |
| 52 | + } |
| 53 | + // check if the receiver is *Pipe |
| 54 | + if len(fn.Recv.List) != 1 { |
| 55 | + continue |
| 56 | + } |
| 57 | + if _, ok := fn.Recv.List[0].Type.(*ast.StarExpr); !ok { |
| 58 | + continue |
| 59 | + } |
| 60 | + if sel, ok := fn.Recv.List[0].Type.(*ast.StarExpr).X.(*ast.SelectorExpr); ok { |
| 61 | + if sel.Sel.Name != "Pipe" { |
| 62 | + continue |
| 63 | + } |
| 64 | + } |
| 65 | + if !isPublic(fn.Name.Name) { |
| 66 | + continue |
| 67 | + } |
| 68 | + rv[fn.Name.Name] = struct{}{} |
| 69 | + } |
| 70 | + |
| 71 | + return rv, nil |
| 72 | +} |
| 73 | + |
| 74 | +func isPublic(name string) bool { |
| 75 | + return strings.ToUpper(name[:1]) == name[:1] |
| 76 | +} |
| 77 | + |
| 78 | +var pipeMethodTemplate = template.Must( |
| 79 | + template.New("pipeMethodTemplate"). |
| 80 | + Parse(` |
| 81 | +func (p *Pipe) {{.Name}}({{.Params}}) *Pipe { |
| 82 | + p.Pipe = p.Pipe.{{.Name}}({{.Args}}) |
| 83 | + return p |
| 84 | +} |
| 85 | +`)) |
| 86 | + |
| 87 | +var generatedTemplate = template.Must( |
| 88 | + template.New("generatedTemplate"). |
| 89 | + Parse(`// Code generated by script-contextual/generator DO NOT EDIT. |
| 90 | +package script |
| 91 | +
|
| 92 | +import ( |
| 93 | + "io" |
| 94 | + "strings" |
| 95 | + "regexp" |
| 96 | + "net/http" |
| 97 | +) |
| 98 | +
|
| 99 | +{{.Methods}} |
| 100 | +`)) |
| 101 | + |
| 102 | +var flagSourceFile string |
| 103 | + |
| 104 | +func init() { |
| 105 | + flag.StringVar(&flagSourceFile, "source", "", "source file to generate from") |
| 106 | +} |
| 107 | + |
| 108 | +func main() { |
| 109 | + flag.Parse() |
| 110 | + if flagSourceFile == "" { |
| 111 | + panic("source file is required") |
| 112 | + } |
| 113 | + |
| 114 | + pipeMethodsFromBitfield := pipeFromBitfield() |
| 115 | + pipeMethodsFromSource, err := pipeFromSourceFile(flagSourceFile) |
| 116 | + if err != nil { |
| 117 | + panic(err) |
| 118 | + } |
| 119 | + |
| 120 | + methodNamesSorted := make([]string, 0, len(pipeMethodsFromBitfield)) |
| 121 | + for methodName := range pipeMethodsFromBitfield { |
| 122 | + methodNamesSorted = append(methodNamesSorted, methodName) |
| 123 | + } |
| 124 | + sort.Strings(methodNamesSorted) |
| 125 | + |
| 126 | + var methodsCode []string |
| 127 | + for _, methodName := range methodNamesSorted { |
| 128 | + if _, ok := pipeMethodsFromSource[methodName]; ok { |
| 129 | + continue |
| 130 | + } |
| 131 | + |
| 132 | + method := pipeMethodsFromBitfield[methodName] |
| 133 | + methodFuncType := method.Func.Type() |
| 134 | + var params []string |
| 135 | + var args []string |
| 136 | + // 0 is the method receiver |
| 137 | + for i := 1; i < methodFuncType.NumIn(); i++ { |
| 138 | + arg := methodFuncType.In(i) |
| 139 | + argName := fmt.Sprintf("a%d", i) |
| 140 | + if methodFuncType.IsVariadic() && i == methodFuncType.NumIn()-1 { |
| 141 | + params = append(params, fmt.Sprintf("%s ...%s", argName, arg.Elem().String())) |
| 142 | + args = append(args, fmt.Sprintf("%s...", argName)) |
| 143 | + } else { |
| 144 | + params = append(params, fmt.Sprintf("%s %s", argName, arg.String())) |
| 145 | + args = append(args, argName) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // convert method to template payload: |
| 150 | + // - Name: method name |
| 151 | + // - Params: method typed parameters |
| 152 | + // - Args: method parameters |
| 153 | + payload := map[string]string{ |
| 154 | + "Name": methodName, |
| 155 | + "Params": strings.Join(params, ", "), |
| 156 | + "Args": strings.Join(args, ", "), |
| 157 | + } |
| 158 | + b := new(bytes.Buffer) |
| 159 | + if err := pipeMethodTemplate.Execute(b, payload); err != nil { |
| 160 | + panic(err) |
| 161 | + } |
| 162 | + |
| 163 | + methodsCode = append(methodsCode, b.String()) |
| 164 | + } |
| 165 | + |
| 166 | + payload := map[string]string{ |
| 167 | + "Methods": strings.Join(methodsCode, "\n"), |
| 168 | + } |
| 169 | + generatedCode := new(bytes.Buffer) |
| 170 | + if err := generatedTemplate.Execute(generatedCode, payload); err != nil { |
| 171 | + panic(err) |
| 172 | + } |
| 173 | + |
| 174 | + generatedCodeFormatted, err := script. |
| 175 | + Echo(generatedCode.String()). |
| 176 | + Exec("gofmt"). |
| 177 | + // Tee(os.Stderr). |
| 178 | + String() |
| 179 | + if err != nil { |
| 180 | + panic(err) |
| 181 | + } |
| 182 | + |
| 183 | + fmt.Println(generatedCodeFormatted) |
| 184 | +} |
0 commit comments