-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
206 lines (167 loc) · 4.23 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
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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"ella.to/ella/internal/ast"
"ella.to/ella/internal/code"
"ella.to/ella/internal/code/golang"
"ella.to/ella/internal/code/typescript"
"ella.to/ella/internal/parser"
"ella.to/ella/internal/validator"
)
const Version = "0.0.7"
const usage = `
███████╗██╗░░░░░██╗░░░░░░█████╗░
██╔════╝██║░░░░░██║░░░░░██╔══██╗
█████╗░░██║░░░░░██║░░░░░███████║
██╔══╝░░██║░░░░░██║░░░░░██╔══██║
███████╗███████╗███████╗██║░░██║
╚══════╝╚══════╝╚══════╝╚═╝░░╚═╝ v` + Version + `
Usage: ella [command]
Commands:
- fmt Format one or many files in place using glob pattern
ella fmt <glob path>
- gen Generate code from a folder to a file and currently
supports .go and .ts extensions
ella gen <pkg> <output path to file> <search glob paths...>
- ver Print the version of ella
example:
ella fmt ./path/to/*.ella
ella gen rpc ./path/to/output.go ./path/to/*.ella
ella gen rpc ./path/to/output.ts ./path/to/*.ella ./path/to/other/*.ella
`
func main() {
if len(os.Args) < 2 {
fmt.Print(usage)
os.Exit(0)
}
var err error
switch os.Args[1] {
case "fmt":
if len(os.Args) < 3 {
fmt.Print(usage)
os.Exit(0)
}
err = format(os.Args[2])
case "gen":
if len(os.Args) < 5 {
fmt.Print(usage)
os.Exit(0)
}
err = gen(os.Args[2], os.Args[3], os.Args[4:]...)
case "ver":
fmt.Println(Version)
default:
fmt.Print(usage)
os.Exit(0)
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func format(path string) error {
filenames, err := filepath.Glob(path)
if err != nil {
return err
}
for _, filename := range filenames {
prog, err := parse(filename)
if err != nil {
return err
}
err = os.WriteFile(filename, []byte(prog.String()), os.ModePerm)
if err != nil {
return err
}
}
return nil
}
func gen(pkg, out string, searchPaths ...string) (err error) {
var code code.Generator
defer func() {
if err != nil {
//os.Remove(out)
}
}()
filenames, err := mergeAllFiles(searchPaths...)
if err != nil {
return err
}
if len(filenames) == 0 {
return fmt.Errorf("no ella's files found in the following paths: %s", strings.Join(searchPaths, ", "))
}
content, err := combine(filenames...)
if err != nil {
return err
}
prog, err := parser.ParseProgram(parser.New(content))
if err != nil {
return err
}
err = validator.Validate(prog)
if err != nil {
return err
}
ext := filepath.Ext(out)
switch ext {
case ".go":
code = golang.New(pkg)
case ".ts":
code = typescript.New()
default:
return fmt.Errorf("unknown extension %s", out)
}
if err = code.Generate(out, prog); err != nil {
return err
}
return nil
}
func parse(filename string) (*ast.Program, error) {
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return parser.ParseProgram(
parser.New(string(content)),
)
}
func mergeAllFiles(paths ...string) ([]string, error) {
filenamesMap := make(map[string]struct{})
for _, path := range paths {
filenames, err := filepath.Glob(path)
if err != nil {
return nil, err
}
for _, filename := range filenames {
filenamesMap[filename] = struct{}{}
}
}
filenames := make([]string, 0, len(filenamesMap))
for filename := range filenamesMap {
filenames = append(filenames, filename)
}
return filenames, nil
}
// combine is a helper function to concatenate multiple files into a single string
// and returns an error if any of the files have an invalid extension or file cannot
// be read.
func combine(filenames ...string) (string, error) {
var sb strings.Builder
for i, filename := range filenames {
if !strings.HasSuffix(filename, ".ella") {
return "", fmt.Errorf("invalid file extension %s", filename)
}
content, err := os.ReadFile(filename)
if err != nil {
return "", err
}
if i != 0 {
sb.WriteString("\n")
}
sb.Write(content)
}
return sb.String(), nil
}