|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//go:build go1.18 |
| 16 | +// +build go1.18 |
| 17 | + |
| 18 | +package main |
| 19 | + |
| 20 | +import ( |
| 21 | + "bytes" |
| 22 | + "flag" |
| 23 | + "go/ast" |
| 24 | + "go/format" |
| 25 | + "go/parser" |
| 26 | + "go/token" |
| 27 | + "io" |
| 28 | + "io/fs" |
| 29 | + "log" |
| 30 | + "os" |
| 31 | + "path/filepath" |
| 32 | + "strconv" |
| 33 | + "strings" |
| 34 | + |
| 35 | + "golang.org/x/tools/imports" |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + fset = token.NewFileSet() |
| 40 | +) |
| 41 | + |
| 42 | +func main() { |
| 43 | + flag.Parse() |
| 44 | + path := flag.Arg(0) |
| 45 | + if path == "" { |
| 46 | + log.Fatalf("expected one argument -- path to the directory needing updates") |
| 47 | + } |
| 48 | + if err := processPath(path); err != nil { |
| 49 | + log.Fatal(err) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func processPath(path string) error { |
| 54 | + dir, err := os.Stat(path) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + if dir.IsDir() { |
| 59 | + filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error { |
| 60 | + if err == nil && !d.IsDir() && strings.HasSuffix(d.Name(), ".go") { |
| 61 | + err = processFile(path, nil) |
| 62 | + } |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + return nil |
| 67 | + }) |
| 68 | + } else { |
| 69 | + if err := processFile(path, nil); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + } |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +// processFile checks to see if the given file needs any imports rewritten and |
| 77 | +// does so if needed. Note an io.Writer is injected here for testability. |
| 78 | +func processFile(name string, w io.Writer) error { |
| 79 | + if w == nil { |
| 80 | + file, err := os.Open(name) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + defer file.Close() |
| 85 | + w = file |
| 86 | + } |
| 87 | + f, err := parser.ParseFile(fset, name, nil, parser.ParseComments) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + var modified bool |
| 92 | + for _, imp := range f.Imports { |
| 93 | + importPath, err := strconv.Unquote(imp.Path.Value) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + if pkg, ok := m[importPath]; ok && pkg.migrated { |
| 98 | + oldNamespace := importPath[strings.LastIndex(importPath, "/")+1:] |
| 99 | + newNamespace := pkg.importPath[strings.LastIndex(pkg.importPath, "/")+1:] |
| 100 | + if imp.Name == nil && oldNamespace != newNamespace { |
| 101 | + // use old namespace for fewer diffs |
| 102 | + imp.Name = ast.NewIdent(oldNamespace) |
| 103 | + } else if imp.Name != nil && imp.Name.Name == newNamespace { |
| 104 | + // no longer need named import if matching named import |
| 105 | + imp.Name = nil |
| 106 | + } |
| 107 | + imp.EndPos = imp.End() |
| 108 | + imp.Path.Value = strconv.Quote(pkg.importPath) |
| 109 | + modified = true |
| 110 | + } |
| 111 | + } |
| 112 | + if modified { |
| 113 | + var buf bytes.Buffer |
| 114 | + if err := format.Node(&buf, fset, f); err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + b, err := imports.Process(name, buf.Bytes(), nil) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + if _, err := w.Write(b); err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + } |
| 125 | + return nil |
| 126 | +} |
0 commit comments