11package main
22
33import (
4- "bytes"
54 "flag"
65 "fmt"
7- "go/ast"
8- "go/format"
9- "go/parser"
10- "go/token"
6+ "github.com/donutloop/toolkit/internal/ast"
117 "io/ioutil"
128 "log"
139 "os"
14- "strings"
1510 "text/tabwriter"
16- "github.com/fatih/astrewrite"
1711)
1812
1913func main () {
20- log .SetFlags (log . Ldate | log . Lshortfile | log . Ltime )
14+ log .SetFlags (0 )
2115
2216 fs := flag .NewFlagSet ("xcode" , flag .ExitOnError )
2317 var (
24- in = fs .String ("in" , "" , "input file" )
25- out = fs .String ("out" , "" , "output file" )
26- pkg = fs .String ("pkg" , "" , "package name" )
27- typ = fs .String ("type" , "" , "type" )
28- mode = fs .String ("mode" , "" , "activate mode" )
18+ in = fs .String ("in" , "" , "input file" )
19+ out = fs .String ("out" , "" , "output file" )
20+ pkg = fs .String ("pkg" , "" , "package name" )
21+ typ = fs .String ("type" , "" , "type" )
22+ mode = fs .String ("mode" , "" , "activate mode" )
2923 )
3024 fs .Usage = usageFor (fs , "xcode [flags]" )
3125 fs .Parse (os .Args [1 :])
@@ -51,10 +45,10 @@ func main() {
5145 log .Fatalf ("could not read file (%v)" , err )
5246 }
5347
54- rnFunc := RenamePackage (* pkg )
55- ctFunc := ChangeType ("GenericType" , * typ , * mode )
48+ rnFunc := ast . RenamePackage (* pkg )
49+ ctFunc := ast . ChangeType ("GenericType" , * typ , * mode )
5650
57- modifiedFile , err := modifyAst (inputFile , rnFunc , ctFunc )
51+ modifiedFile , err := ast . ModifyAst (inputFile , rnFunc , ctFunc )
5852 if err != nil {
5953 log .Fatalf ("could not modify ast of file (%v)" , err )
6054 }
@@ -64,73 +58,6 @@ func main() {
6458 }
6559}
6660
67- const (
68- DebugMode string = "DEV"
69- )
70-
71- func RenamePackage (packageName string ) func (file * ast.File ) * ast.File {
72- return func (file * ast.File ) * ast.File {
73- file .Name = & ast.Ident {Name : packageName }
74- return file
75- }
76- }
77-
78- func ChangeType (typeName string , newType string , debugMode string ) func (file * ast.File ) * ast.File {
79- return func (file * ast.File ) * ast.File {
80- rewriteFunc := func (n ast.Node ) (ast.Node , bool ) {
81- switch x := n .(type ) {
82- case * ast.Ident :
83- if typeName == x .Name {
84- x = & ast.Ident {Name : newType }
85- }
86- return x , true
87- case * ast.CallExpr :
88- for i := 0 ; i < len (x .Args ); i ++ {
89- v , ok := x .Args [i ].(* ast.Ident )
90- if ok {
91- if strings .ToLower (typeName ) == strings .ToLower (v .Name ) {
92- x .Args [i ] = & ast.Ident {Name : fmt .Sprintf ("%s.(%s)" , v .Name , newType )}
93- }
94- }
95- }
96- return x , true
97- default :
98- if debugMode == DebugMode {
99- log .SetFlags (0 )
100- log .Println ("ast node:" )
101- log .Println (fmt .Sprintf ("verbose value: %#v" , x ))
102- log .Println (fmt .Sprintf ("type: %T" , x ))
103- log .Println (fmt .Sprintf ("value: %v" , x ))
104- }
105- }
106- return n , true
107- }
108-
109- astrewrite .Walk (file , rewriteFunc )
110-
111- return file
112- }
113- }
114-
115- func modifyAst (dest []byte , fns ... func (* ast.File ) * ast.File ) ([]byte , error ) {
116- destFset := token .NewFileSet ()
117- destF , err := parser .ParseFile (destFset , "" , dest , 0 )
118- if err != nil {
119- return nil , err
120- }
121-
122- for _ , fn := range fns {
123- destF = fn (destF )
124- }
125-
126- var buf bytes.Buffer
127- if err := format .Node (& buf , destFset , destF ); err != nil {
128- return nil , fmt .Errorf ("couldn't format package code (%v)" , err )
129- }
130-
131- return buf .Bytes (), nil
132- }
133-
13461func usageFor (fs * flag.FlagSet , short string ) func () {
13562 return func () {
13663 fmt .Fprintf (os .Stdout , "USAGE\n " )
@@ -139,7 +66,7 @@ func usageFor(fs *flag.FlagSet, short string) func() {
13966 fmt .Fprintf (os .Stdout , "FLAGS\n " )
14067 tw := tabwriter .NewWriter (os .Stdout , 0 , 2 , 2 , ' ' , 0 )
14168 fs .VisitAll (func (f * flag.Flag ) {
142- if f .Name == "mode " {
69+ if f .Name == "debug " {
14370 return
14471 }
14572 def := f .DefValue
@@ -151,4 +78,3 @@ func usageFor(fs *flag.FlagSet, short string) func() {
15178 tw .Flush ()
15279 }
15380}
154-
0 commit comments