-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdev.go
113 lines (104 loc) · 2.35 KB
/
dev.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/howeyc/fsnotify"
"github.com/nf/nux/varvara"
)
func devMode(enableGUI, enableDebug bool, talFile string) error {
talFile = filepath.Clean(talFile)
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
if err := watcher.Watch(filepath.Dir(talFile)); err != nil {
return err
}
tmp, err := os.MkdirTemp("", "nux-dev-*")
if err != nil {
return err
}
defer os.RemoveAll(tmp)
romFile := filepath.Join(tmp, filepath.Base(talFile)+".rom")
var (
runner *varvara.Runner
debug *Debugger
)
if enableDebug {
debug = NewDebugger()
runner = varvara.NewRunner(enableGUI, true, debug.StateFunc)
runner.SetOutput(debug.Log)
debug.Runner = runner
log.SetPrefix("")
log.SetOutput(debug.log)
go func() {
if err := debug.Run(); err != nil {
log.Fatalf("debug: %v", err)
}
log.SetOutput(os.Stderr)
log.SetPrefix("nux: ")
runner.Debug("exit", 0)
}()
} else {
runner = varvara.NewRunner(enableGUI, true, nil)
}
romCh := make(chan []byte)
go func() {
started := false
run := time.After(1 * time.Millisecond)
for {
select {
case <-run:
log.Printf("dev: build %s", filepath.Base(talFile))
var out io.Writer = os.Stderr
if debug != nil {
out = debug.Log
}
rom, err := devBuild(out, talFile, romFile)
if err != nil {
log.Printf("dev: %v", err)
break
}
if debug != nil {
syms, err := parseSymbols(romFile + ".sym")
if err != nil {
log.Printf("dev: reading symbols: %v", err)
break
}
debug.SetSymbols(syms)
}
if !started {
log.Printf("dev: start")
romCh <- rom
started = true
} else {
log.Printf("dev: reset")
runner.Swap(rom)
}
case ev := <-watcher.Event:
if ev.Name == talFile && !ev.IsAttrib() {
run = time.After(100 * time.Millisecond)
}
case err := <-watcher.Error:
log.Printf("dev: watcher: %v", err)
}
}
}()
code := runner.Run((<-romCh))
return fmt.Errorf("dev: exit code: %d", code)
}
func devBuild(out io.Writer, talFile, romFile string) ([]byte, error) {
cmd := exec.Command("uxnasm", talFile, romFile)
cmd.Stdout = out
cmd.Stderr = out
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("uxnasm: %v", err)
}
return os.ReadFile(romFile)
}