Skip to content

Commit cc78bd0

Browse files
committed
Graph examples embedded with executable, fibinacii sphere and spiral nodes, TRS repeat node
1 parent 22e3c67 commit cc78bd0

24 files changed

+1088
-127
lines changed

cmd/polywasm/build.go

+23-93
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package main
22

33
import (
44
// "bufio"
5-
"bytes"
5+
66
// "compress/gzip"
7+
"embed"
78
_ "embed"
89
"io"
9-
"log"
10+
"io/fs"
1011
"os"
11-
"os/exec"
1212
"path/filepath"
13-
"strings"
1413

1514
"github.com/urfave/cli/v2"
1615
)
@@ -21,6 +20,9 @@ var htmlContents []byte
2120
//go:embed sw.js
2221
var swJsContents []byte
2322

23+
//go:embed icons/*
24+
var iconFs embed.FS
25+
2426
func Copy(srcpath, dstpath string) (err error) {
2527
r, err := os.Open(srcpath)
2628
if err != nil {
@@ -46,49 +48,6 @@ func Copy(srcpath, dstpath string) (err error) {
4648
return err
4749
}
4850

49-
func TinygoRoot() (string, error) {
50-
cmd := exec.Command(
51-
"tinygo",
52-
"env",
53-
"TINYGOROOT",
54-
)
55-
var outb bytes.Buffer
56-
cmd.Stdout = &outb
57-
err := cmd.Run()
58-
return strings.TrimSpace(outb.String()), err
59-
}
60-
61-
func RegularGo(out, programToBuild string, stripDebug bool) (string, string, error) {
62-
args := []string{
63-
"build",
64-
}
65-
66-
if stripDebug {
67-
// args = append(args, "-ldflags", "-s -w")
68-
}
69-
70-
args = append(
71-
args,
72-
"-o", out,
73-
programToBuild,
74-
)
75-
76-
log.Print(args)
77-
cmd := exec.Command("go", args...)
78-
cmd.Env = []string{
79-
"GOOS=js",
80-
"GOARCH=wasm",
81-
}
82-
var outb bytes.Buffer
83-
var outErr bytes.Buffer
84-
85-
cmd.Stdout = &outb
86-
cmd.Stderr = &outErr
87-
err := cmd.Run()
88-
89-
return strings.TrimSpace(outb.String()), strings.TrimSpace(outErr.String()), err
90-
}
91-
9251
func buildCommand() *cli.Command {
9352
return &cli.Command{
9453
Name: "build",
@@ -105,11 +64,6 @@ func buildCommand() *cli.Command {
10564
Usage: "folder to write all contents to",
10665
Value: "html",
10766
},
108-
&cli.BoolFlag{
109-
Name: "no-debug",
110-
Usage: "whether or not to strip debug symbols (reduces file size)",
111-
Value: true,
112-
},
11367
},
11468
Action: func(ctx *cli.Context) error {
11569
outFolder := ctx.String("out")
@@ -118,46 +72,7 @@ func buildCommand() *cli.Command {
11872
return err
11973
}
12074

121-
wasmFile, err := os.Open(ctx.String("wasm"))
122-
if err != nil {
123-
return err
124-
}
125-
defer wasmFile.Close()
126-
127-
// gzippedWasmFile, err := os.Create(filepath.Join(outFolder, "main.wasm.gz"))
128-
// if err != nil {
129-
// return err
130-
// }
131-
// defer gzippedWasmFile.Close()
132-
133-
// gzipWriter := gzip.NewWriter(gzippedWasmFile)
134-
// chunkSize := 1024 // Adjust as needed
135-
// reader := bufio.NewReader(wasmFile)
136-
137-
// for {
138-
// buf := make([]byte, chunkSize)
139-
// n, err := reader.Read(buf)
140-
// if err != nil {
141-
// if err.Error() == "EOF" {
142-
// break // Reached end of file
143-
// }
144-
// return err
145-
// }
146-
// _, err = gzipWriter.Write(buf[:n])
147-
// if err != nil {
148-
// return err
149-
// }
150-
// }
151-
152-
// gzipWriter.Close()
153-
154-
outFile, err := os.Create(filepath.Join(outFolder, "main.wasm"))
155-
if err != nil {
156-
return err
157-
}
158-
defer outFile.Close()
159-
160-
_, err = io.Copy(outFile, wasmFile)
75+
err = Copy(ctx.String("wasm"), filepath.Join(outFolder, "main.wasm"))
16176
if err != nil {
16277
return err
16378
}
@@ -172,7 +87,22 @@ func buildCommand() *cli.Command {
17287
return err
17388
}
17489

175-
return err
90+
return fs.WalkDir(iconFs, ".", func(path string, d fs.DirEntry, err error) error {
91+
if err != nil {
92+
return err
93+
}
94+
95+
if d.IsDir() {
96+
return nil
97+
}
98+
99+
content, err := iconFs.ReadFile(path)
100+
if err != nil {
101+
return err
102+
}
103+
104+
return os.WriteFile(filepath.Join(outFolder, filepath.Base(path)), content, 0666)
105+
})
176106
},
177107
}
178108
}
44.9 KB
Loading
206 KB
Loading
40.4 KB
Loading

cmd/polywasm/icons/favicon-16x16.png

740 Bytes
Loading

cmd/polywasm/icons/favicon-32x32.png

2.27 KB
Loading

cmd/polywasm/icons/favicon.ico

15 KB
Binary file not shown.

cmd/polywasm/icons/site.webmanifest

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}

cmd/polywasm/index.html

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<!DOCTYPE html>
2-
32
<html>
4-
53
<head>
64
<meta charset="utf-8" />
75
<title>Polyform</title>
86
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
8+
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
9+
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
10+
<link rel="manifest" href="/site.webmanifest">
911
<style>
1012
html,
1113
body {
@@ -24,7 +26,6 @@
2426
overflow: hidden;
2527
}
2628
</style>
27-
2829
<script>
2930
/* -----------------------------------------------
3031
/* Author : Vincent Garreau - vincentgarreau.com
@@ -173,11 +174,9 @@
173174
navigator.serviceWorker
174175
.register('sw.js', { scope: window.location.pathname + "app/" })
175176
.then(registration => {
176-
console.log("register", registration)
177-
document.getElementById("status-display").innerText = "activated"
177+
document.getElementById("status-display").innerText = "Initializing"
178178

179179
const sw = registration.active ?? registration.installing ?? registration.waiting
180-
// setInterval(() => sw.postMessage(null), 15000)
181180
const redirectLocation = window.location.href + "app/"
182181

183182
if (registration.active) {
@@ -187,13 +186,13 @@
187186
}
188187

189188
sw.addEventListener('statechange', (event) => {
190-
console.log('statechange', event)
189+
console.log('statechange', event)
191190
if (event.target.state === 'activated') {
192-
console.log('activated')
191+
console.log('activated')
193192
document.getElementById("status-display").innerText = "Activated"
194193
window.location.href = redirectLocation
195194
}
196-
})
195+
})
197196
})
198197
</script>
199198

generator/app_server.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ func readJSON[T any](body io.Reader) (T, error) {
5656
}
5757

5858
type pageData struct {
59-
Title string
60-
Version string
61-
Description string
62-
AntiAlias bool
63-
XrEnabled bool
59+
Title string
60+
Version string
61+
Description string
62+
AntiAlias bool
63+
XrEnabled bool
64+
ExampleGraphs []string
6465
}
6566

6667
//go:embed html/*
@@ -100,12 +101,14 @@ func (as *AppServer) Handler() (*http.ServeMux, error) {
100101
}
101102

102103
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
104+
103105
pageToServe := pageData{
104-
Title: as.app.Name,
105-
Version: as.app.Version,
106-
Description: as.app.Description,
107-
AntiAlias: as.webscene.AntiAlias,
108-
XrEnabled: as.webscene.XrEnabled,
106+
Title: as.app.Name,
107+
Version: as.app.Version,
108+
Description: as.app.Description,
109+
AntiAlias: as.webscene.AntiAlias,
110+
XrEnabled: as.webscene.XrEnabled,
111+
ExampleGraphs: allExamples(),
109112
}
110113

111114
// Required for sharedMemoryForWorkers to work
@@ -155,6 +158,7 @@ func (as *AppServer) Handler() (*http.ServeMux, error) {
155158
mux.Handle("/parameter/value/", parameterValueEndpoint(as.app.graphInstance, graphSaver))
156159
mux.Handle("/parameter/name/", parameterNameEndpoint(as.app.graphInstance, graphSaver))
157160
mux.Handle("/parameter/description/", parameterDescriptionEndpoint(as.app.graphInstance, graphSaver))
161+
mux.Handle("/load-example", exampleGraphEndpoint(as.app))
158162
mux.Handle("/graph", graphEndpoint(as.app))
159163
mux.Handle("/graph/metadata/", graphMetadataEndpoint(as.app.graphInstance, graphSaver))
160164
mux.HandleFunc("/started", as.StartedEndpoint)

generator/app_server_graph.go

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ import (
66
"github.com/EliCDavis/polyform/generator/endpoint"
77
)
88

9+
func exampleGraphEndpoint(app *App) endpoint.Handler {
10+
return endpoint.Handler{
11+
Methods: map[string]endpoint.Method{
12+
http.MethodPost: endpoint.BodyMethod[string]{
13+
Request: endpoint.TextRequestReader{},
14+
Handler: func(request endpoint.Request[string]) error {
15+
err := app.ApplySchema(loadExample(request.Body))
16+
if err != nil {
17+
return err
18+
}
19+
return nil
20+
},
21+
},
22+
},
23+
}
24+
}
25+
926
func graphEndpoint(app *App) endpoint.Handler {
1027
return endpoint.Handler{
1128
Methods: map[string]endpoint.Method{

generator/examples.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package generator
2+
3+
import (
4+
"embed"
5+
"fmt"
6+
"io/fs"
7+
"path/filepath"
8+
)
9+
10+
//go:embed examples/*
11+
var examplesFs embed.FS
12+
13+
func allExamples() []string {
14+
entries := make([]string, 0)
15+
fs.WalkDir(examplesFs, ".", func(path string, d fs.DirEntry, err error) error {
16+
if d.IsDir() {
17+
return nil
18+
}
19+
20+
entries = append(entries, filepath.Base(path))
21+
return nil
22+
})
23+
return entries
24+
}
25+
26+
func loadExample(example string) []byte {
27+
var contents []byte
28+
found := false
29+
fs.WalkDir(examplesFs, ".", func(path string, d fs.DirEntry, err error) error {
30+
if d.IsDir() {
31+
return nil
32+
}
33+
34+
if filepath.Base(path) != example {
35+
return nil
36+
}
37+
38+
contents, err = examplesFs.ReadFile(path)
39+
found = true
40+
41+
return err
42+
})
43+
44+
if !found {
45+
panic(fmt.Errorf("example: %q doesn't exist", example))
46+
}
47+
48+
if len(contents) == 0 {
49+
panic("example loaded is empty")
50+
}
51+
52+
return contents
53+
}

generator/examples/bella.json

+148
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)