| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"go/ast" |
| 5 |
"go/build" |
| 6 |
"go/importer" |
| 7 |
"go/parser" |
| 8 |
"go/printer" |
| 9 |
"go/token" |
| 10 |
"go/types" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
|
| 14 |
"github.com/neelance/astrewrite" |
| 15 |
) |
| 16 |
|
| 17 |
func main() { |
| 18 |
importPath := os.Args[1] |
| 19 |
|
| 20 |
pkg, err := build.Import(importPath, "", 0) |
| 21 |
if err != nil { |
| 22 |
panic(err) |
| 23 |
} |
| 24 |
|
| 25 |
fset := token.NewFileSet() |
| 26 |
files := make([]*ast.File, len(pkg.GoFiles)) |
| 27 |
for i, name := range pkg.GoFiles { |
| 28 |
file, err := parser.ParseFile(fset, filepath.Join(pkg.Dir, name), nil, parser.ParseComments) |
| 29 |
if err != nil { |
| 30 |
panic(err) |
| 31 |
} |
| 32 |
files[i] = file |
| 33 |
} |
| 34 |
|
| 35 |
typesInfo := &types.Info{ |
| 36 |
Types: make(map[ast.Expr]types.TypeAndValue), |
| 37 |
Defs: make(map[*ast.Ident]types.Object), |
| 38 |
Uses: make(map[*ast.Ident]types.Object), |
| 39 |
Scopes: make(map[ast.Node]*types.Scope), |
| 40 |
} |
| 41 |
config := &types.Config{ |
| 42 |
Importer: importer.Default(), |
| 43 |
} |
| 44 |
if _, err := config.Check(importPath, fset, files, typesInfo); err != nil { |
| 45 |
panic(err) |
| 46 |
} |
| 47 |
|
| 48 |
for i, file := range files { |
| 49 |
simplifiedFile := astrewrite.Simplify(file, typesInfo, false) |
| 50 |
out, err := os.Create(filepath.Join("goroot", "src", importPath, pkg.GoFiles[i])) |
| 51 |
if err != nil { |
| 52 |
panic(err) |
| 53 |
} |
| 54 |
if err := printer.Fprint(out, fset, simplifiedFile); err != nil { |
| 55 |
panic(err) |
| 56 |
} |
| 57 |
out.Close() |
| 58 |
} |
| 59 |
} |