-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli.go
207 lines (196 loc) · 4.47 KB
/
cli.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
package cli
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
cli "github.com/urfave/cli/v2"
"github.com/nevalang/neva/internal/builder"
"github.com/nevalang/neva/internal/compiler"
"github.com/nevalang/neva/internal/interpreter"
"github.com/nevalang/neva/pkg"
)
func NewApp( //nolint:funlen
workdir string,
bldr builder.Builder,
goc compiler.Compiler,
nativec compiler.Compiler,
wasmc compiler.Compiler,
jsonc compiler.Compiler,
dotc compiler.Compiler,
) *cli.App {
var (
target string
debug bool
)
return &cli.App{
Name: "neva",
Usage: "Flow-based programming language",
Commands: []*cli.Command{
{
Name: "version",
Usage: "Get current Nevalang version",
Action: func(_ *cli.Context) error {
fmt.Println(pkg.Version)
return nil
},
},
{
Name: "new",
Usage: "Create new Nevalang project",
Args: true,
Action: func(cCtx *cli.Context) error {
if path := cCtx.Args().First(); path != "" {
if err := os.Mkdir(path, 0755); err != nil {
return err
}
return createNevaMod(path)
}
return createNevaMod(workdir)
},
},
{
Name: "get",
Usage: "Add dependency to current module",
Args: true,
ArgsUsage: "Provide path to the module",
Action: func(cCtx *cli.Context) error {
installedPath, err := bldr.Get(
workdir,
cCtx.Args().Get(0),
cCtx.Args().Get(1),
)
if err != nil {
return err
}
fmt.Printf(
"%s installed to %s\n", cCtx.Args().Get(0),
installedPath,
)
return nil
},
},
{
Name: "run",
Usage: "Run neva program from source code in interpreter mode",
Args: true,
ArgsUsage: "Provide path to the executable package",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Usage: "Show message events in stdout",
Destination: &debug,
},
},
Action: func(cCtx *cli.Context) error {
dirFromArg, err := getMainPkgFromArgs(cCtx)
if err != nil {
return err
}
intr := interpreter.New(bldr, goc, debug)
if err := intr.Interpret(
context.Background(),
workdir,
dirFromArg,
); err != nil {
return err
}
return nil
},
},
{
Name: "build",
Usage: "Build executable binary from neva program source code",
Args: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "target",
Required: false,
Usage: "Emit Go or WASM instead of machine code",
Destination: &target,
Action: func(ctx *cli.Context, s string) error {
switch s {
case "go", "wasm", "native", "json", "dot":
default:
return fmt.Errorf("Unknown target %s", s)
}
return nil
},
},
},
ArgsUsage: "Provide path to the executable package",
Action: func(cCtx *cli.Context) error {
dirFromArg, err := getMainPkgFromArgs(cCtx)
if err != nil {
return err
}
switch target {
case "go":
return goc.Compile(
workdir, dirFromArg, workdir,
)
case "wasm":
return wasmc.Compile(
workdir, dirFromArg, workdir,
)
case "json":
return jsonc.Compile(
workdir, dirFromArg, workdir,
)
case "dot":
return dotc.Compile(
workdir, dirFromArg, workdir,
)
default:
return nativec.Compile(
workdir, dirFromArg, workdir,
)
}
},
},
},
}
}
func getMainPkgFromArgs(cCtx *cli.Context) (string, error) {
firstArg := cCtx.Args().First()
dirFromArg := strings.TrimSuffix(firstArg, "/main.neva")
if filepath.Ext(dirFromArg) != "" {
return "", errors.New(
"Use path to directory with executable package, relative to module root",
)
}
return dirFromArg, nil
}
func createNevaMod(path string) error {
// Create neva.yml file
nevaYmlContent := fmt.Sprintf("neva: %s", pkg.Version)
if err := os.WriteFile(
filepath.Join(path, "neva.yml"),
[]byte(nevaYmlContent),
0644,
); err != nil {
return err
}
// Create src sub-directory
srcPath := filepath.Join(path, "src")
if err := os.Mkdir(srcPath, 0755); err != nil {
return err
}
// Create main.neva file
mainNevaContent := `component Main(start) (stop) {
nodes { Println }
net {
:start -> ('Hello, World!' -> println -> :stop)
}
}`
if err := os.WriteFile(
filepath.Join(srcPath, "main.neva"),
[]byte(mainNevaContent),
0644,
); err != nil {
return err
}
return nil
}