-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbuilder.go
156 lines (138 loc) · 4.29 KB
/
builder.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package handler
import (
"bytes"
"context"
"encoding/json"
"log"
"strings"
"time"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/executils"
"github.com/arduino/arduino-language-server/lsp"
"github.com/arduino/arduino-language-server/streams"
"github.com/arduino/go-paths-helper"
"github.com/pkg/errors"
)
func (handler *InoHandler) scheduleRebuildEnvironment() {
handler.rebuildSketchDeadlineMutex.Lock()
defer handler.rebuildSketchDeadlineMutex.Unlock()
d := time.Now().Add(time.Second)
handler.rebuildSketchDeadline = &d
}
func (handler *InoHandler) rebuildEnvironmentLoop() {
defer streams.CatchAndLogPanic()
grabDeadline := func() *time.Time {
handler.rebuildSketchDeadlineMutex.Lock()
defer handler.rebuildSketchDeadlineMutex.Unlock()
res := handler.rebuildSketchDeadline
handler.rebuildSketchDeadline = nil
return res
}
for {
// Wait for someone to schedule a preprocessing...
time.Sleep(100 * time.Millisecond)
deadline := grabDeadline()
if deadline == nil {
continue
}
for time.Now().Before(*deadline) {
time.Sleep(100 * time.Millisecond)
if d := grabDeadline(); d != nil {
deadline = d
}
}
// Regenerate preprocessed sketch!
done := make(chan bool)
go func() {
defer streams.CatchAndLogPanic()
handler.progressHandler.Create("arduinoLanguageServerRebuild")
handler.progressHandler.Begin("arduinoLanguageServerRebuild", &lsp.WorkDoneProgressBegin{
Title: "Building sketch",
})
count := 0
dots := []string{".", "..", "..."}
for {
select {
case <-time.After(time.Millisecond * 400):
msg := "compiling" + dots[count%3]
count++
handler.progressHandler.Report("arduinoLanguageServerRebuild", &lsp.WorkDoneProgressReport{Message: &msg})
case <-done:
msg := "done"
handler.progressHandler.End("arduinoLanguageServerRebuild", &lsp.WorkDoneProgressEnd{Message: &msg})
return
}
}
}()
handler.dataLock("RBLD---")
handler.initializeWorkbench(context.Background(), nil)
handler.dataUnlock("RBLD---")
done <- true
close(done)
}
}
func (handler *InoHandler) generateBuildEnvironment(buildPath *paths.Path) error {
sketchDir := handler.sketchRoot
fqbn := handler.config.SelectedBoard.Fqbn
// Export temporary files
type overridesFile struct {
Overrides map[string]string `json:"overrides"`
}
data := overridesFile{Overrides: map[string]string{}}
for uri, trackedFile := range handler.docs {
rel, err := paths.New(uri).RelFrom(handler.sketchRoot)
if err != nil {
return errors.WithMessage(err, "dumping tracked files")
}
data.Overrides[rel.String()] = trackedFile.Text
}
var overridesJSON *paths.Path
if jsonBytes, err := json.MarshalIndent(data, "", " "); err != nil {
return errors.WithMessage(err, "dumping tracked files")
} else if tmpFile, err := paths.WriteToTempFile(jsonBytes, nil, ""); err != nil {
return errors.WithMessage(err, "dumping tracked files")
} else {
overridesJSON = tmpFile
defer tmpFile.Remove()
}
// XXX: do this from IDE or via gRPC
args := []string{globalCliPath,
"--config-file", globalCliConfigPath,
"compile",
"--fqbn", fqbn,
"--only-compilation-database",
"--clean",
"--source-override", overridesJSON.String(),
"--build-path", buildPath.String(),
"--format", "json",
sketchDir.String(),
}
cmd, err := executils.NewProcess(args...)
if err != nil {
return errors.Errorf("running %s: %s", strings.Join(args, " "), err)
}
cmdOutput := &bytes.Buffer{}
cmd.RedirectStdoutTo(cmdOutput)
cmd.SetDirFromPath(sketchDir)
log.Println("running: ", strings.Join(args, " "))
if err := cmd.Run(); err != nil {
return errors.Errorf("running %s: %s", strings.Join(args, " "), err)
}
// Currently those values are not used, keeping here for future improvements
type cmdBuilderRes struct {
BuildPath *paths.Path `json:"build_path"`
UsedLibraries []*libraries.Library
}
type cmdRes struct {
CompilerOut string `json:"compiler_out"`
CompilerErr string `json:"compiler_err"`
BuilderResult cmdBuilderRes `json:"builder_result"`
Success bool `json:"success"`
}
var res cmdRes
if err := json.Unmarshal(cmdOutput.Bytes(), &res); err != nil {
return errors.Errorf("parsing arduino-cli output: %s", err)
}
log.Println("arduino-cli output:", cmdOutput)
return nil
}