-
Notifications
You must be signed in to change notification settings - Fork 171
/
goby.go
175 lines (144 loc) · 3.76 KB
/
goby.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/goby-lang/goby/compiler"
"github.com/goby-lang/goby/compiler/parser"
"github.com/goby-lang/goby/igb"
_ "github.com/goby-lang/goby/native/db"
_ "github.com/goby-lang/goby/native/plugin"
_ "github.com/goby-lang/goby/native/result"
_ "github.com/goby-lang/goby/native/ripper"
"github.com/goby-lang/goby/vm"
"github.com/pkg/profile"
)
const version string = vm.Version
func main() {
profileCPUOptionPtr := flag.Bool("profile-cpu", false, "Profile cpu usage")
profileMemOptionPtr := flag.Bool("profile-mem", false, "Profile memory allocation")
versionOptionPtr := flag.Bool("v", false, "Show current Goby version")
interactiveOptionPtr := flag.Bool("i", false, "Run interactive goby")
issueOptionPtr := flag.Bool("e", false, "Generate reporting format")
flag.Parse()
if *interactiveOptionPtr {
igb.StartIgb(version)
os.Exit(0)
}
if *profileCPUOptionPtr {
defer profile.Start().Stop()
}
if *profileMemOptionPtr {
defer profile.Start(profile.MemProfile).Stop()
}
if *versionOptionPtr {
fmt.Println(version)
os.Exit(0)
}
var fp string
switch flag.Arg(0) {
case "":
flag.Usage()
os.Exit(0)
case "test":
args := flag.Args()[1:]
filePath := flag.Arg(1)
fileInfo, err := os.Stat(filePath)
reportErrorAndExit(err)
dir := extractDirFromFilePath(filePath, fileInfo)
v, err := vm.New(dir, args)
if err != nil {
reportErrorAndExit(err)
}
if fileInfo.Mode().IsDir() {
fileInfos, err := ioutil.ReadDir(filePath)
reportErrorAndExit(err)
for _, fileInfo := range fileInfos {
fp := filepath.Join(dir, fileInfo.Name())
reportErrorAndExit(err)
err := runSpecFile(v, fp)
reportErrorAndExit(err)
}
} else {
err := runSpecFile(v, filePath)
reportErrorAndExit(err)
}
instructionSets, err := compiler.CompileToInstructions("Spec.run", parser.NormalMode)
if err != nil {
reportErrorAndExit(err)
}
v.ExecInstructions(instructionSets, filePath)
return
default:
fp = flag.Arg(0)
if !strings.Contains(fp, ".") {
flag.Usage()
os.Exit(0)
}
}
// Execute files normally
dir, _, fileExt := extractFileInfo(fp)
file := readFile(fp)
switch fileExt {
case "gb", "rb":
args := flag.Args()[1:]
instructionSets, err := compiler.CompileToInstructions(string(file), parser.NormalMode)
reportErrorAndExit(err)
var v *vm.VM
if *issueOptionPtr {
fmt.Println("Will generate issue report on error...")
v, err = vm.InitIssueReportVM(dir, args)
defer vm.PrintError(v)
} else {
v, err = vm.New(dir, args)
}
reportErrorAndExit(err)
fp, err := filepath.Abs(fp)
reportErrorAndExit(err)
v.ExecInstructions(instructionSets, fp)
default:
fmt.Printf("Unknown file extension: %s", fileExt)
}
}
func extractFileInfo(fp string) (dir, filename, fileExt string) {
dir, filename = filepath.Split(fp)
dir, _ = filepath.Abs(dir)
fileExt = filepath.Ext(fp)
splited := strings.Split(filename, ".")
filename, fileExt = splited[0], splited[1]
return
}
func extractDirFromFilePath(filePath string, fileInfo os.FileInfo) string {
if fileInfo.Mode().IsDir() {
dir, err := filepath.Abs(filePath)
reportErrorAndExit(err)
return dir
}
filePath, err := filepath.Abs(filePath)
reportErrorAndExit(err)
dir, _, _ := extractFileInfo(filePath)
return dir
}
func readFile(filepath string) (file []byte) {
file, err := ioutil.ReadFile(filepath)
reportErrorAndExit(err)
return
}
func runSpecFile(v *vm.VM, fp string) (err error) {
file := readFile(fp)
instructionSets, err := compiler.CompileToInstructions(string(file), parser.NormalMode)
if err != nil {
return
}
v.ExecInstructions(instructionSets, fp)
return
}
func reportErrorAndExit(err error) {
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}