forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
212 lines (185 loc) · 4.2 KB
/
run.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/test"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/std"
)
type runCfg struct {
verbose bool
rootDir string
expr string
debug bool
debugAddr string
}
func newRunCmd(io commands.IO) *commands.Command {
cfg := &runCfg{}
return commands.NewCommand(
commands.Metadata{
Name: "run",
ShortUsage: "run [flags] <file> [<file>...]",
ShortHelp: "run gno packages",
},
cfg,
func(_ context.Context, args []string) error {
return execRun(cfg, args, io)
},
)
}
func (c *runCfg) RegisterFlags(fs *flag.FlagSet) {
fs.BoolVar(
&c.verbose,
"v",
false,
"verbose output when running",
)
fs.StringVar(
&c.rootDir,
"root-dir",
"",
"clone location of github.com/gnolang/gno (gno binary tries to guess it)",
)
fs.StringVar(
&c.expr,
"expr",
"main()",
"value of expression to evaluate. Defaults to executing function main() with no args",
)
fs.BoolVar(
&c.debug,
"debug",
false,
"enable interactive debugger using stdin and stdout",
)
fs.StringVar(
&c.debugAddr,
"debug-addr",
"",
"enable interactive debugger using tcp address in the form [host]:port",
)
}
func execRun(cfg *runCfg, args []string, io commands.IO) error {
if len(args) == 0 {
return flag.ErrHelp
}
if cfg.rootDir == "" {
cfg.rootDir = gnoenv.RootDir()
}
stdin := io.In()
stdout := io.Out()
stderr := io.Err()
// init store and machine
_, testStore := test.Store(
cfg.rootDir,
stdin, stdout, stderr)
if cfg.verbose {
testStore.SetLogStoreOps(true)
}
if len(args) == 0 {
args = []string{"."}
}
// read files
files, err := parseFiles(args, stderr)
if err != nil {
return err
}
if len(files) == 0 {
return errors.New("no files to run")
}
var send std.Coins
pkgPath := string(files[0].PkgName)
ctx := test.Context(pkgPath, send)
m := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: pkgPath,
Output: stdout,
Input: stdin,
Store: testStore,
Context: ctx,
Debug: cfg.debug || cfg.debugAddr != "",
})
defer m.Release()
// If the debug address is set, the debugger waits for a remote client to connect to it.
if cfg.debugAddr != "" {
if err := m.Debugger.Serve(cfg.debugAddr); err != nil {
return err
}
}
// run files
m.RunFiles(files...)
runExpr(m, cfg.expr)
return nil
}
func parseFiles(fnames []string, stderr io.WriteCloser) ([]*gno.FileNode, error) {
files := make([]*gno.FileNode, 0, len(fnames))
var hasError bool
for _, fname := range fnames {
if s, err := os.Stat(fname); err == nil && s.IsDir() {
subFns, err := listNonTestFiles(fname)
if err != nil {
return nil, err
}
subFiles, err := parseFiles(subFns, stderr)
if err != nil {
return nil, err
}
files = append(files, subFiles...)
continue
} else if err != nil {
// either not found or some other kind of error --
// in either case not a file we can parse.
return nil, err
}
hasError = catchRuntimeError(fname, stderr, func() {
files = append(files, gno.MustReadFile(fname))
})
}
if hasError {
return nil, commands.ExitCodeError(1)
}
return files, nil
}
func listNonTestFiles(dir string) ([]string, error) {
fs, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
fn := make([]string, 0, len(fs))
for _, f := range fs {
n := f.Name()
if isGnoFile(f) &&
!strings.HasSuffix(n, "_test.gno") &&
!strings.HasSuffix(n, "_filetest.gno") {
fn = append(fn, filepath.Join(dir, n))
}
}
return fn, nil
}
func runExpr(m *gno.Machine, expr string) {
defer func() {
if r := recover(); r != nil {
switch r := r.(type) {
case gno.UnhandledPanicError:
fmt.Printf("panic running expression %s: %v\nStacktrace: %s\n",
expr, r.Error(), m.ExceptionsStacktrace())
default:
fmt.Printf("panic running expression %s: %v\nMachine State:%s\nStacktrace: %s\n",
expr, r, m.String(), m.Stacktrace().String())
}
panic(r)
}
}()
ex, err := gno.ParseExpr(expr)
if err != nil {
panic(fmt.Errorf("could not parse: %w", err))
}
m.Eval(ex)
}