-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasm_builder.go
95 lines (78 loc) · 2.7 KB
/
wasm_builder.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
func BuildWasmFromMainRS(cargoToml string, mainRSContent string) RedisObject {
result := RedisObject{
Success: false,
}
projectName := fetchProjectName(cargoToml)
tmpDir, err := os.MkdirTemp("", "rust-project")
if err != nil {
result.Message = base64Encoder(fmt.Sprintf("failed to create temporary directory: %v", err))
return result
}
defer os.RemoveAll(tmpDir)
if err := os.Chdir(tmpDir); err != nil {
result.Message = base64Encoder(fmt.Sprintf("failed to change directory: %v", err))
return result
}
projectDir := filepath.Join(tmpDir, projectName)
if err := exec.Command("cargo", "new", "--lib", projectDir).Run(); err != nil {
result.Message = base64Encoder(fmt.Sprintf("failed to create rust project: %v", err))
return result
}
if err := os.WriteFile(filepath.Join(projectDir, "Cargo.toml"), []byte(cargoToml), 0644); err != nil {
result.Message = base64Encoder(fmt.Sprintf("failed to write cargo.toml file: %v", err))
return result
}
if err := os.WriteFile(filepath.Join(projectDir, "src", "lib.rs"), []byte(mainRSContent), 0644); err != nil {
result.Message = base64Encoder(fmt.Sprintf("failed to write lib.rs file: %v", err))
return result
}
if err := os.Chdir(projectDir); err != nil {
result.Message = base64Encoder(fmt.Sprintf("failed to change directory: %v", err))
return result
}
cmd := exec.Command("cargo", "build", "--target", "wasm32-unknown-unknown", "--release")
stderr, err := cmd.CombinedOutput()
if err != nil {
errorMessage := strings.TrimSpace(string(stderr))
result.Message = base64Encoder(fmt.Sprintf("failed to compile rust project: %v", errorMessage))
return result
}
wasmFilePath := filepath.Join(projectDir, "target", "wasm32-unknown-unknown", "release", projectName+".wasm")
if _, err := os.Stat(wasmFilePath); os.IsNotExist(err) {
result.Message = base64Encoder(fmt.Sprintf("wasm file does not exist: %v", err))
return result
}
wasmData, err := os.ReadFile(wasmFilePath)
if err != nil {
result.Message = base64Encoder(fmt.Sprintf("failed to read wasm file: %v", err))
return result
}
b64EncodedWasmFile := base64Encoder(string(wasmData))
result.Success = true
result.Message = base64Encoder("Compilation successful!")
result.Wasm = b64EncodedWasmFile
return result
}
func fetchProjectName(file string) string {
lines := strings.Split(file, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "name") {
parts := strings.Split(line, "=")
if len(parts) == 2 {
projectName := strings.TrimSpace(parts[1])
projectName = strings.Trim(projectName, `'"`)
return projectName
}
}
}
return "sorobix_temp"
}