-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
116 lines (109 loc) · 2.45 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
//go:generate qexp -outdir pkg github.com/goplus/spx
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"github.com/goplus/igop"
"github.com/goplus/igop/gopbuild"
"github.com/goplus/ispx/github"
spxfs "github.com/goplus/spx/fs"
_ "github.com/goplus/igop/pkg/fmt"
_ "github.com/goplus/igop/pkg/math"
_ "github.com/goplus/ispx/pkg/github.com/goplus/spx"
_ "github.com/goplus/reflectx/icall/icall8192"
)
var (
flagDumpSrc bool
flagTrace bool
flagDumpSSA bool
flagDumpPKG bool
flagGithubToken string
flagVerbose bool
)
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "ispc [flags] dir\n")
flag.PrintDefaults()
}
flag.BoolVar(&flagDumpSrc, "dumpsrc", false, "print source code")
flag.BoolVar(&flagDumpSSA, "dumpssa", false, "print ssa code information")
flag.BoolVar(&flagDumpPKG, "dumppkg", false, "print import pkgs")
flag.BoolVar(&flagTrace, "trace", false, "trace")
flag.BoolVar(&flagVerbose, "v", false, "print verbose information")
flag.StringVar(&flagGithubToken, "ghtoken", "", "set github.com api token")
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) != 1 {
flag.Usage()
return
}
path := args[0]
var mode igop.Mode
if flagDumpSSA {
mode |= igop.EnableDumpInstr
}
if flagTrace {
mode |= igop.EnableTracing
}
if flagDumpPKG {
mode |= igop.EnableDumpImports
}
ctx := igop.NewContext(mode)
var (
data []byte
err error
)
if root, ok := github.IsSupport(path); ok {
if flagVerbose {
github.Verbose = true
}
client := github.NewClient(flagGithubToken)
fs, err := github.NewFileSystem(client, root)
if err != nil {
log.Fatalln("error", err)
}
if flagVerbose {
log.Println("BuildDir", root)
}
data, err = gopbuild.BuildFSDir(ctx, fs, root)
if err != nil {
log.Panicln(err)
}
spxfs.RegisterSchema("", func(file string) (spxfs.Dir, error) {
assert := root + "/" + file
fs, err := github.NewDir(client, assert)
if err != nil {
log.Panicln("load fs error", err)
}
return fs, nil
})
} else {
if flagVerbose {
log.Println("BuildDir", path)
}
data, err = gopbuild.BuildDir(ctx, path)
if err != nil {
log.Panicln(err)
}
if !filepath.IsAbs(path) {
dir, _ := os.Getwd()
path = filepath.Join(dir, path)
}
os.Chdir(path)
if flagVerbose {
log.Println("Chdir", path)
}
}
if flagDumpSrc {
fmt.Println(string(data))
}
_, err = ctx.RunFile("main.go", data, nil)
if err != nil {
log.Panicln(err)
}
}