Skip to content

Commit 1734b76

Browse files
committed
vsc => vstruct
1 parent 1419b46 commit 1734b76

8 files changed

Lines changed: 268 additions & 262 deletions

File tree

.goreleaser.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ before:
99
builds:
1010
- env:
1111
- CGO_ENABLED=0
12-
main: ./cli/vsc
12+
main: ./cli/vstruct
1313
goos:
1414
- linux
1515
- windows
@@ -30,7 +30,7 @@ builds:
3030
- -X main.Version={{.Version}}
3131
archives:
3232
- files:
33-
- ./cli/vsc/LICENSE.txt
33+
- ./cli/vstruct/LICENSE.txt
3434
- ./README.md
3535
checksum:
3636
name_template: 'checksums.txt'

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ Code Generation Based High Speed Data Serialization Tool
1515
npm install -g vstruct
1616
```
1717

18-
## 2. From Source
18+
## 2. Go Get (Go Install) (also recommended)
19+
20+
```bash
21+
go install github.com/lemon-mint/vstruct/cli/vstruct@latest
22+
```
23+
24+
## 3. From Source
1925

2026
```
2127
git clone https://github.com/lemon-mint/vstruct.git
2228
cd vstruct
23-
go build -o vstruct ./cli/vsc
29+
go build -o vstruct ./cli/vstruct
2430
```
2531

26-
## 3. Pre-compiled binaries
32+
## 4. Pre-compiled binaries
2733

2834
[https://github.com/lemon-mint/vstruct/releases/latest](https://github.com/lemon-mint/vstruct/releases/latest)
2935

cli/vsc/main.go

Lines changed: 0 additions & 155 deletions
This file was deleted.

cli/vstruct.old/main.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"log"
8+
"os"
9+
"path/filepath"
10+
"strings"
11+
12+
"github.com/lemon-mint/vstruct/compile/backend/dart"
13+
"github.com/lemon-mint/vstruct/compile/backend/golang"
14+
"github.com/lemon-mint/vstruct/compile/backend/python"
15+
"github.com/lemon-mint/vstruct/compile/backend/rust"
16+
"github.com/lemon-mint/vstruct/compile/frontend"
17+
"github.com/lemon-mint/vstruct/ir"
18+
"github.com/lemon-mint/vstruct/lexer"
19+
"github.com/lemon-mint/vstruct/parser"
20+
)
21+
22+
func ReadFileAsString(fileName string) string {
23+
os.Open(fileName)
24+
file, err := os.Open(fileName)
25+
if err != nil {
26+
panic(err)
27+
}
28+
defer file.Close()
29+
data, err := io.ReadAll(file)
30+
if err != nil {
31+
panic(err)
32+
}
33+
return string(data)
34+
}
35+
36+
func CreateDirectory(path string) error {
37+
if _, err := os.Stat(path); os.IsNotExist(err) {
38+
err := os.MkdirAll(path, 0755)
39+
if err != nil {
40+
return fmt.Errorf("error creating directory: %w", err)
41+
}
42+
}
43+
return nil
44+
}
45+
46+
func main() {
47+
if len(os.Args) < 3 {
48+
fmt.Println("Usage: vstruct <language: go, rust, dart, py> <file>...")
49+
os.Exit(1)
50+
}
51+
generate := (func(w io.Writer, i *ir.IR, packageName string) error)(nil)
52+
modelPath := filepath.Join("vstruct", "model")
53+
ext := ""
54+
55+
language := os.Args[1]
56+
switch language {
57+
case "go":
58+
generate = golang.Generate
59+
modelPath = filepath.Join(".", modelPath)
60+
ext = ".go"
61+
case "rust":
62+
generate = rust.Generate
63+
modelPath = filepath.Join("src", modelPath)
64+
ext = ".rs"
65+
case "dart":
66+
generate = dart.Generate
67+
modelPath = filepath.Join("lib", modelPath)
68+
ext = ".dart"
69+
case "py":
70+
generate = python.Generate
71+
modelPath = filepath.Join(".", modelPath)
72+
ext = ".py"
73+
default:
74+
fmt.Printf("Unknown language: %s\n", language)
75+
os.Exit(1)
76+
}
77+
78+
for _, path := range os.Args[2:] {
79+
if !strings.HasSuffix(path, ".vstruct") {
80+
path = path + ".vstruct"
81+
}
82+
83+
filename, err := filepath.Abs(path)
84+
if err != nil {
85+
log.Println(err)
86+
continue
87+
}
88+
89+
input := ReadFileAsString(filename)
90+
lex := lexer.NewLexer([]rune(input), path)
91+
p := parser.New(lex)
92+
file, err := p.Parse()
93+
if err != nil {
94+
panic(err)
95+
}
96+
front := frontend.New(file)
97+
err = front.Compile()
98+
if err != nil {
99+
panic(err)
100+
}
101+
goir := front.Output()
102+
goir.Options.UseUnsafe = true
103+
104+
packageName := filepath.Base(path)
105+
packageName = strings.TrimSuffix(packageName, ".vstruct")
106+
107+
var buf bytes.Buffer
108+
err = generate(&buf, goir, packageName)
109+
if err != nil {
110+
fmt.Println(buf.String())
111+
panic(err)
112+
}
113+
out := buf.String()
114+
fmt.Println(out)
115+
116+
if err := CreateDirectory(filepath.Join(modelPath, packageName)); err != nil {
117+
panic(err)
118+
}
119+
f, err := os.Create(filepath.Join(modelPath, packageName, packageName+ext))
120+
if err != nil {
121+
panic(err)
122+
}
123+
f.WriteString(out)
124+
f.Close()
125+
}
126+
}

0 commit comments

Comments
 (0)