-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiny_verify_proyect.go
More file actions
159 lines (141 loc) · 4.39 KB
/
Copy pathtiny_verify_proyect.go
File metadata and controls
159 lines (141 loc) · 4.39 KB
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
package client
import (
"fmt"
"os"
"os/exec"
"path/filepath"
. "github.com/tinywasm/fmt"
"github.com/tinywasm/tinygo"
)
// TinyGoCompiler returns if TinyGo compiler should be used (dynamic based on configuration)
func (w *WasmClient) TinyGoCompiler() bool {
return w.TinyGoCompilerFlag && w.TinyGoInstalled
}
// RequiresTinyGo checks if the mode requires TinyGo compiler
func (w *WasmClient) RequiresTinyGo(mode string) bool {
return mode == w.buildMediumSizeShortcut || mode == w.buildSmallSizeShortcut
}
// handleTinyGoMissing installs TinyGo if absent and adds its bin dir to PATH
// so subsequent exec.Command("tinygo") calls find it without requiring the
// caller to have /usr/local/tinygo/bin in their login PATH.
func (w *WasmClient) handleTinyGoMissing() error {
installedPath, err := tinygo.EnsureInstalled()
if err != nil {
return Err("Error:", "cannot", "install TinyGo:", err.Error())
}
binDir := filepath.Dir(installedPath)
if current := os.Getenv("PATH"); current != "" {
os.Setenv("PATH", current+string(os.PathListSeparator)+binDir)
} else {
os.Setenv("PATH", binDir)
}
fmt.Println("TinyGo installed:", installedPath)
return nil
}
// ensureTinyGoInPath adds the tinygo bin dir to PATH if tinygo is installed
// but not reachable via PATH. This handles the case where EnsureInstalled()
// placed tinygo at /usr/local/tinygo/bin/tinygo (found via stat) but that
// directory is absent from PATH, causing exec.Command("tinygo") to fail.
func ensureTinyGoInPath() {
p, err := tinygo.GetPath()
if err != nil {
return
}
if _, lookErr := exec.LookPath("tinygo"); lookErr == nil {
return // already reachable via PATH
}
binDir := filepath.Dir(p)
if current := os.Getenv("PATH"); current != "" {
os.Setenv("PATH", current+string(os.PathListSeparator)+binDir)
} else {
os.Setenv("PATH", binDir)
}
}
// VerifyTinyGoInstallation checks and caches TinyGo installation status
func (w *WasmClient) verifyTinyGoInstallationStatus() {
w.TinyGoInstalled = w.VerifyTinyGoInstallation() == nil
if w.TinyGoInstalled {
ensureTinyGoInPath()
}
}
// VerifyTinyGoProjectCompatibility checks if the project is compatible with TinyGo compilation
func (w *WasmClient) VerifyTinyGoProjectCompatibility() {
// Verify tinystring library dependencies
w.Logger("=== TinyString Library TinyGo Compatibility Check ===")
// Verify the library directory exists
libPath := "./tinystring"
if _, err := os.Stat(libPath); os.IsNotExist(err) {
libPath = "."
}
// Check for problematic imports
problematicImports := []string{"fmt", "strings", "strconv"}
found := false
err := filepath.Walk(libPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Ext(path) != ".go" || filepath.Base(path) == "verify_tinygo.go" {
return nil
}
// Skip test files since they're not part of the compiled library
fileName := filepath.Base(path)
if len(fileName) > 8 && fileName[len(fileName)-8:] == "_test.go" {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// Read file content (simplified check)
buffer := make([]byte, 1024)
n, _ := file.Read(buffer)
content := string(buffer[:n])
for _, imp := range problematicImports {
importStr := fmt.Sprintf("\"%s\"", imp)
if contains(content, importStr) {
w.Logger(fmt.Sprintf("❌ Found problematic import %s in %s", imp, path))
found = true
}
}
return nil
})
if err != nil {
w.Logger("Error walking directory:", err)
return
}
if !found {
w.Logger("✅ No problematic standard library imports found!")
w.Logger("✅ TinyString library is TinyGo compatible!")
w.Logger("")
w.Logger("Key Features:")
w.Logger("- Zero dependency on fmt, strings, strconv packages")
w.Logger("- Manual implementations for string/number conversions")
w.Logger("- Optimized for minimal binary size")
w.Logger("- Compatible with embedded systems and WebAssembly")
} else {
w.Logger("❌ TinyString library still has standard library dependencies")
}
}
// contains is a simple string contains function to avoid using strings package
func contains(s, substr string) bool {
if len(substr) == 0 {
return true
}
if len(substr) > len(s) {
return false
}
for i := 0; i <= len(s)-len(substr); i++ {
match := true
for j := 0; j < len(substr); j++ {
if s[i+j] != substr[j] {
match = false
break
}
}
if match {
return true
}
}
return false
}