-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch.go
138 lines (117 loc) · 2.91 KB
/
watch.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
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
package core
import (
"errors"
"github.com/robgonnella/ardi/v2/rpc"
log "github.com/sirupsen/logrus"
)
// WatchCore represents core module for adi go commands
type WatchCore struct {
logger *log.Logger
uploader *UploadCore
compiler *CompileCore
port SerialPort
watcher *FileWatcher
board *rpc.Board
compileOpts *rpc.CompileOpts
baud int
}
// WatchCoreTargets targets for watching, recompiling, and reuploading
type WatchCoreTargets struct {
Board *rpc.Board
CompileOpts *rpc.CompileOpts
Baud int
Port SerialPort
}
// NewWatchCore returns new Project instance
func NewWatchCore(compiler *CompileCore, uploader *UploadCore, logger *log.Logger) *WatchCore {
return &WatchCore{
uploader: uploader,
compiler: compiler,
logger: logger,
}
}
// SetTargets sets the board and compile options for the watcher
func (w *WatchCore) SetTargets(targets WatchCoreTargets) error {
if w.watcher != nil {
w.watcher.Close()
w.watcher = nil
}
board := targets.Board
compileOpts := targets.CompileOpts
baud := targets.Baud
watcher, err := NewFileWatcher(compileOpts.SketchPath, w.logger)
if err != nil {
return err
}
w.watcher = watcher
if targets.Port != nil {
w.port = targets.Port
} else {
w.port = NewArdiSerialPort(board.Port, baud, w.logger)
}
w.board = board
w.compileOpts = compileOpts
w.baud = baud
watcher.AddListener(w.onFileChange)
return nil
}
// Watch responds to changes in a given sketch file by automatically
// recompiling and re-uploading.
func (w *WatchCore) Watch() error {
if !w.hasTargets() {
return errors.New("Must call SetTargets before calling watch")
}
w.port.Stop()
go w.port.Watch()
w.watcher.Watch()
return nil
}
// Stop deletes watcher and unattaches from port
func (w *WatchCore) Stop() {
if w.watcher != nil {
w.watcher.Close()
w.watcher = nil
}
if w.port != nil {
w.port.Stop()
w.port = nil
}
w.baud = 0
w.board = nil
w.compileOpts = nil
}
// private
func (w *WatchCore) onFileChange() {
if !w.hasTargets() {
err := errors.New("watch targets have gone missing")
w.logger.WithError(err).Error()
return
}
w.logger.Info("Stopping file watcher")
w.watcher.Stop()
w.logger.Info("Stopping serial port")
w.port.Stop()
defer func() {
w.logger.Info("Restarting file watcher")
w.watcher.Restart()
}()
w.logger.Info("Recompiling")
err := w.compiler.Compile(*w.compileOpts)
if err != nil {
w.logger.WithError(err).Error("Failed to compile")
return
}
w.logger.Info("Compilation successful")
w.logger.Info("Reuploading")
err = w.uploader.Upload(w.board, w.compileOpts.SketchDir)
if err != nil {
w.logger.WithError(err).Error("Failed to upload")
return
}
w.logger.Info("Upload successful")
w.logger.Info("Restarting port watcher")
go w.port.Watch()
}
func (w *WatchCore) hasTargets() bool {
return w.port != nil && w.board != nil && w.compileOpts != nil && w.baud != 0 && w.watcher != nil
}