|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "go/parser" |
| 8 | + "go/token" |
| 9 | + "io" |
| 10 | + "io/fs" |
| 11 | + "log" |
| 12 | + "os" |
| 13 | + "os/exec" |
| 14 | + "path/filepath" |
| 15 | + "strings" |
| 16 | + |
| 17 | + "github.com/dave/dst" |
| 18 | + "github.com/dave/dst/decorator" |
| 19 | + "github.com/urfave/cli/v2" |
| 20 | +) |
| 21 | + |
| 22 | +func main() { |
| 23 | + app := &cli.App{ |
| 24 | + Name: "deprecator", |
| 25 | + Usage: "Adds deprecation comments to all exported types in the module, with pointers to a new location. You should run this in your module root.", |
| 26 | + Flags: []cli.Flag{ |
| 27 | + &cli.StringFlag{ |
| 28 | + Name: "path", |
| 29 | + Usage: "the new package path that the deprecated message should point users to", |
| 30 | + Required: true, |
| 31 | + }, |
| 32 | + }, |
| 33 | + Action: func(ctx *cli.Context) error { |
| 34 | + newPkgPath := ctx.String("path") |
| 35 | + wd, err := os.Getwd() |
| 36 | + if err != nil { |
| 37 | + return fmt.Errorf("getting working dir: %w", err) |
| 38 | + } |
| 39 | + fileToPackage, err := buildFileToPackage(wd) |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("building mapping of files to packages: %w", err) |
| 42 | + } |
| 43 | + |
| 44 | + modPath, err := getModulePath(wd) |
| 45 | + if err != nil { |
| 46 | + return fmt.Errorf("finding current module path: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + fset := token.NewFileSet() |
| 50 | + return filepath.Walk(wd, func(path string, info fs.FileInfo, err error) error { |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + if info.IsDir() || filepath.Ext(path) != ".go" { |
| 55 | + return nil |
| 56 | + } |
| 57 | + |
| 58 | + addComment := func(name string, decs *dst.Decorations) { |
| 59 | + oldPkg := fileToPackage[path] |
| 60 | + newPkg := strings.Replace(oldPkg, modPath, newPkgPath, 1) |
| 61 | + newSym := newPkg + "." + name |
| 62 | + comment := fmt.Sprintf("// Deprecated: use %s", newSym) |
| 63 | + if len(decs.All()) > 0 { |
| 64 | + decs.Append("//") |
| 65 | + } |
| 66 | + decs.Append(comment) |
| 67 | + } |
| 68 | + |
| 69 | + file, err := decorator.ParseFile(fset, path, nil, parser.ParseComments) |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("parsing %s: %w", path, err) |
| 72 | + } |
| 73 | + |
| 74 | + if _, ok := fileToPackage[path]; !ok { |
| 75 | + // this happens in the case of e.g. test files, which we want to skip |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + // process the AST, adding comments where necessary |
| 80 | + dst.Inspect(file, func(n dst.Node) bool { return inspectASTNode(addComment, n) }) |
| 81 | + |
| 82 | + outFile, err := os.Create(path) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("creating %s to write: %w", path, err) |
| 85 | + } |
| 86 | + defer outFile.Close() |
| 87 | + err = decorator.Fprint(outFile, file) |
| 88 | + if err != nil { |
| 89 | + return fmt.Errorf("writing %s: %w", path, err) |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | + }) |
| 94 | + }, |
| 95 | + } |
| 96 | + err := app.Run(os.Args) |
| 97 | + if err != nil { |
| 98 | + log.Fatal(err) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +type pkg struct { |
| 103 | + Dir string |
| 104 | + ImportPath string |
| 105 | + GoFiles []string |
| 106 | +} |
| 107 | + |
| 108 | +func inspectASTNode(addComment func(string, *dst.Decorations), n dst.Node) bool { |
| 109 | + switch x := n.(type) { |
| 110 | + case *dst.GenDecl: |
| 111 | + if x.Tok == token.CONST || x.Tok == token.VAR || x.Tok == token.TYPE { |
| 112 | + for _, spec := range x.Specs { |
| 113 | + switch s := spec.(type) { |
| 114 | + case *dst.ValueSpec: |
| 115 | + // if parenthesized, put a comment above each exported type in the group |
| 116 | + if x.Lparen { |
| 117 | + for _, name := range s.Names { |
| 118 | + if !name.IsExported() { |
| 119 | + continue |
| 120 | + } |
| 121 | + addComment(name.Name, &s.Decs.Start) |
| 122 | + } |
| 123 | + } else { |
| 124 | + name := s.Names[0] |
| 125 | + if !name.IsExported() { |
| 126 | + continue |
| 127 | + } |
| 128 | + addComment(name.Name, &x.Decs.Start) |
| 129 | + } |
| 130 | + case *dst.TypeSpec: |
| 131 | + name := s.Name |
| 132 | + if !name.IsExported() { |
| 133 | + continue |
| 134 | + } |
| 135 | + addComment(name.Name, &x.Decs.Start) |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + case *dst.FuncDecl: |
| 140 | + // don't add notices to methods |
| 141 | + if x.Name.IsExported() && x.Recv == nil { |
| 142 | + addComment(x.Name.Name, &x.Decs.Start) |
| 143 | + } |
| 144 | + } |
| 145 | + return true |
| 146 | + |
| 147 | +} |
| 148 | + |
| 149 | +func getModulePath(dir string) (string, error) { |
| 150 | + cmd := exec.Command("go", "list", "-m") |
| 151 | + cmd.Dir = dir |
| 152 | + stdout := &bytes.Buffer{} |
| 153 | + cmd.Stdout = stdout |
| 154 | + err := cmd.Run() |
| 155 | + if err != nil { |
| 156 | + return "", err |
| 157 | + } |
| 158 | + return strings.TrimSpace(stdout.String()), nil |
| 159 | +} |
| 160 | + |
| 161 | +func buildFileToPackage(dir string) (map[string]string, error) { |
| 162 | + cmd := exec.Command("go", "list", "-json", "./...") |
| 163 | + cmd.Dir = dir |
| 164 | + stdout := &bytes.Buffer{} |
| 165 | + stderr := &bytes.Buffer{} |
| 166 | + cmd.Stdout = stdout |
| 167 | + cmd.Stderr = stderr |
| 168 | + err := cmd.Run() |
| 169 | + if err != nil { |
| 170 | + return nil, err |
| 171 | + } |
| 172 | + dec := json.NewDecoder(stdout) |
| 173 | + fileToPackage := map[string]string{} |
| 174 | + for { |
| 175 | + var p pkg |
| 176 | + err := dec.Decode(&p) |
| 177 | + if err == io.EOF { |
| 178 | + return fileToPackage, nil |
| 179 | + } |
| 180 | + if err != nil { |
| 181 | + return nil, err |
| 182 | + } |
| 183 | + for _, f := range p.GoFiles { |
| 184 | + fileToPackage[filepath.Join(p.Dir, f)] = p.ImportPath |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments