PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/script/regis-controller

http://github.com/sut-go/gon
#! | 71 lines | 62 code | 9 blank | 0 comment | 0 complexity | c50cc5cafcae1b8220cc950ad4f0b3ac MD5 | raw file
 1package main
 2
 3import (
 4    // "os"
 5    "fmt"
 6    "flag"
 7    "go/ast"
 8    "go/parser"
 9    "go/token"
10    //"go/printer"
11    //"container/vector"
12)
13
14var c = map[string]string{}
15var a = map[string]string{}
16
17func parseFile(filename string) {
18    var fset = token.NewFileSet()
19    astf, error := parser.ParseFile(fset, filename, nil, 0)
20    if error != nil  {
21        return
22    }
23    pname := astf.Name
24    c[filename[0:len(filename)-3]] = pname.String()
25    //fmt.Printf("%s\n", pname)
26
27    for _, d:= range astf.Decls {
28        if fd, ok:= d.(*ast.FuncDecl); ok {
29            name := fd.Name.String()
30            action := fmt.Sprintf("%s/%c%s", pname, 'a'+(name[0]-'A'),name[1:len(name)])
31            a[action] = pname.String() + "." + name
32        }
33    }
34}
35
36func genInit() {
37    fmt.Printf("package controller\n\n")
38    fmt.Printf("import \"reflect\"\n")
39    for k,_ := range c {
40        fmt.Printf("import \"%s\"\n", k)
41    }
42    fmt.Printf(`
43var Controllers = map[string]*reflect.FuncValue{}
44
45func register(s string, c interface{}) {
46    v := reflect.NewValue(c).(*reflect.FuncValue)
47    Controllers[s] = v
48}
49
50func init() {
51`)
52    for k,v := range a {
53        fmt.Printf("    register(\"%s\", %s)\n", k, v)
54    }
55    fmt.Printf("}\n",)
56}
57
58func main() {
59    flag.Parse()
60    narg := flag.NArg()
61    if narg != 1 {
62        fmt.Printf("-\n")
63        return
64    }
65    filename := flag.Arg(0)
66    parseFile(filename)
67    // fmt.Printf("%s\n", c)
68    // fmt.Printf("%s\n", a)
69    genInit()
70}
71