-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile.go
73 lines (61 loc) · 1.54 KB
/
compile.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
package core
import (
"time"
log "github.com/sirupsen/logrus"
"github.com/robgonnella/ardi/v2/rpc"
)
// CompileCore represents core module for compile commands
type CompileCore struct {
logger *log.Logger
client rpc.Client
compiling bool
}
// NewCompileCore instance of core module for compile commands
func NewCompileCore(client rpc.Client, logger *log.Logger) *CompileCore {
return &CompileCore{
logger: logger,
client: client,
compiling: false,
}
}
// Compile compiles a given project sketch
func (c *CompileCore) Compile(opts rpc.CompileOpts) error {
c.waitForCompilationsToFinish()
c.compiling = true
if err := c.client.Compile(opts); err != nil {
c.compiling = false
return err
}
c.compiling = false
return nil
}
// WatchForChanges watches sketch for changes and recompiles
func (c *CompileCore) WatchForChanges(opts rpc.CompileOpts) error {
watcher, err := NewFileWatcher(opts.SketchPath, c.logger)
if err != nil {
return nil
}
watcher.AddListener(func() {
c.logger.Infof("Recompiling %s", opts.SketchPath)
if err := c.Compile(opts); err != nil {
c.logger.WithError(err).Error("Compilation failed")
}
c.logger.Info("Compilation successful")
})
watcher.Watch()
return nil
}
// IsCompiling returns if core is currently compiling
func (c *CompileCore) IsCompiling() bool {
return c.compiling
}
// private
func (c *CompileCore) waitForCompilationsToFinish() {
for {
if !c.IsCompiling() {
break
}
c.logger.Info("Waiting for previous compilations to finish...")
time.Sleep(time.Second)
}
}