-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.go
More file actions
77 lines (63 loc) · 2.09 KB
/
Copy pathgenerator.go
File metadata and controls
77 lines (63 loc) · 2.09 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
package client
import (
"embed"
"os"
"path/filepath"
"github.com/tinywasm/devflow"
)
//go:embed templates/*
var embeddedFS embed.FS
// CreateDefaultWasmFileClientIfNotExist creates a default WASM main.go file from the embedded markdown template
// It never overwrites an existing file and returns the WasmClient instance for method chaining.
func (t *WasmClient) CreateDefaultWasmFileClientIfNotExist(skipIDEConfig bool) *WasmClient {
// Check if generation is allowed
if t.ShouldGenerateDefaultFile != nil && !t.ShouldGenerateDefaultFile() {
return t
}
// Path to client.go
clientPath := filepath.Join(t.AppRootDir, t.Config.SourceDir(), t.MainInputFile)
if _, err := os.Stat(clientPath); os.IsNotExist(err) {
// Read embedded markdown (no template processing needed - static content)
raw, errRead := embeddedFS.ReadFile("templates/basic_wasm_client.md")
if errRead != nil {
t.Logger("Error reading embedded template:", errRead)
return t
}
// Use devflow to extract Go code from markdown
writer := func(name string, data []byte) error {
if err := os.MkdirAll(filepath.Dir(name), 0o755); err != nil {
return err
}
return os.WriteFile(name, data, 0o644)
}
// Ensure SourceDir exists
srcDir := filepath.Join(t.AppRootDir, t.Config.SourceDir())
if _, err := os.Stat(srcDir); os.IsNotExist(err) {
if err := os.MkdirAll(srcDir, 0o755); err != nil {
t.Logger("Error creating source directory:", err)
return t
}
}
m := devflow.NewMarkDown(t.AppRootDir, srcDir, writer).
InputByte(raw)
// Extract to the main file
if err := m.Extract(t.MainInputFile); err != nil {
t.Logger("Error extracting go code from markdown:", err)
return t
}
t.LogSuccessState("Generated WASM source file at", clientPath)
if !skipIDEConfig {
t.VisualStudioCodeWasmEnvConfig()
}
// Trigger compilation immediately so In-Memory mode has content to serve
t.storageMu.RLock()
store := t.Storage
t.storageMu.RUnlock()
if store != nil {
if err := store.Compile(); err != nil {
t.Logger("Error compiling generated client:", err)
}
}
}
return t
}