Skip to content

Commit 7de9a3e

Browse files
cmagliefacchinm
authored andcommitted
Calling arduino builder with GPRC API [experimental]
1 parent 116cad0 commit 7de9a3e

25 files changed

+3098
-1
lines changed

app/src/processing/app/SketchController.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import cc.arduino.Compiler;
2727
import cc.arduino.CompilerProgressListener;
2828
import cc.arduino.UploaderUtils;
29+
import cc.arduino.builder.ArduinoBuilder;
2930
import cc.arduino.packages.Uploader;
3031
import processing.app.debug.RunnerException;
3132
import processing.app.forms.PasswordAuthorizationDialog;
@@ -58,10 +59,18 @@
5859
public class SketchController {
5960
private final Editor editor;
6061
private final Sketch sketch;
62+
private final ArduinoBuilder builder;
6163

6264
public SketchController(Editor _editor, Sketch _sketch) {
65+
ArduinoBuilder _builder = null;
66+
try {
67+
_builder = new ArduinoBuilder();
68+
} catch (IOException e) {
69+
e.printStackTrace();
70+
}
6371
editor = _editor;
6472
sketch = _sketch;
73+
builder = _builder;
6574
}
6675

6776
private boolean renamingCode;
@@ -706,7 +715,8 @@ public String codeComplete(SketchFile file, int line, int col) throws RunnerExce
706715
}
707716

708717
try {
709-
return new Compiler(pathToSketch, sketch).codeComplete(editor.status.getCompilerProgressListeners(), requestedFile, line, col);
718+
return builder.codeComplete(BaseNoGui.getTargetBoard(), pathToSketch, requestedFile, line, col);
719+
//return new Compiler(pathToSketch, sketch).codeComplete(editor.status.getCompilerProgressListeners(), requestedFile, line, col);
710720
} finally {
711721
// Make sure we clean up any temporary sketch copy
712722
if (deleteTemp)

arduino-core/.classpath

+15
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,20 @@
2626
<classpathentry kind="lib" path="lib/commons-lang3-3.8.1.jar"/>
2727
<classpathentry kind="lib" path="lib/jssc-2.8.0-arduino4.jar"/>
2828
<classpathentry kind="lib" path="lib/commons-io-2.6.jar"/>
29+
<classpathentry kind="lib" path="lib/grpc-netty-1.6.1.jar"/>
30+
<classpathentry kind="lib" path="lib/grpc-protobuf-1.6.1.jar"/>
31+
<classpathentry kind="lib" path="lib/grpc-stub-1.6.1.jar"/>
32+
<classpathentry kind="lib" path="lib/grpc-core-1.6.1.jar"/>
33+
<classpathentry kind="lib" path="lib/guava-19.0.jar"/>
34+
<classpathentry kind="lib" path="lib/grpc-context-1.6.1.jar"/>
35+
<classpathentry kind="lib" path="lib/protobuf-java-3.4.0.jar"/>
36+
<classpathentry kind="lib" path="lib/grpc-services-1.6.1.jar"/>
37+
<classpathentry kind="lib" path="lib/grpc-grpclb-1.6.1.jar"/>
38+
<classpathentry kind="lib" path="lib/grpc-auth-1.6.1.jar"/>
39+
<classpathentry kind="lib" path="lib/grpc-okhttp-1.6.1.jar"/>
40+
<classpathentry kind="lib" path="lib/grpc-protobuf-nano-1.6.1.jar"/>
41+
<classpathentry kind="lib" path="lib/netty-all-4.1.15.Final.jar"/>
42+
<classpathentry kind="lib" path="lib/instrumentation-api-0.4.3.jar"/>
43+
<classpathentry kind="lib" path="lib/grpc-protobuf-lite-1.6.1.jar"/>
2944
<classpathentry kind="output" path="bin"/>
3045
</classpath>

arduino-core/lib/grpc-auth-1.6.1.jar

12.1 KB
Binary file not shown.
20.1 KB
Binary file not shown.

arduino-core/lib/grpc-core-1.6.1.jar

471 KB
Binary file not shown.
131 KB
Binary file not shown.

arduino-core/lib/grpc-netty-1.6.1.jar

173 KB
Binary file not shown.
144 KB
Binary file not shown.
5.76 KB
Binary file not shown.
7.5 KB
Binary file not shown.
4.1 KB
Binary file not shown.
446 KB
Binary file not shown.

arduino-core/lib/grpc-stub-1.6.1.jar

36.3 KB
Binary file not shown.

arduino-core/lib/guava-19.0.jar

2.2 MB
Binary file not shown.
91.4 KB
Binary file not shown.

arduino-core/lib/jsr305-3.0.2.jar

19.5 KB
Binary file not shown.
3.52 MB
Binary file not shown.
1.32 MB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package cc.arduino.builder;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
import cc.arduino.Compiler;
10+
import cc.arduino.builder.BuilderGrpc.BuilderBlockingStub;
11+
import io.grpc.ManagedChannel;
12+
import io.grpc.ManagedChannelBuilder;
13+
import processing.app.BaseNoGui;
14+
import processing.app.debug.MessageSiphon;
15+
import processing.app.debug.TargetBoard;
16+
import processing.app.helpers.ProcessUtils;
17+
18+
public class ArduinoBuilder {
19+
20+
private ManagedChannel channel;
21+
private BuilderBlockingStub blockingStub;
22+
// private BuilderStub asyncStub;
23+
24+
private Process builder;
25+
private MessageSiphon builderOut, builderErr;
26+
// private Exception exception = null;
27+
28+
public ArduinoBuilder() throws IOException {
29+
channel = ManagedChannelBuilder.forAddress("127.0.0.1", 12345).usePlaintext(true).build();
30+
blockingStub = BuilderGrpc.newBlockingStub(channel);
31+
// asyncStub = BuilderGrpc.newStub(channel);
32+
33+
List<String> cmd = new ArrayList<>();
34+
cmd.add(BaseNoGui.getContentFile("arduino-builder").getAbsolutePath());
35+
cmd.add("-daemon");
36+
builder = ProcessUtils.exec(cmd.toArray(new String[0]));
37+
builderOut = new MessageSiphon(builder.getInputStream(), (msg) -> {
38+
System.out.println(msg);
39+
// try {
40+
// xxx.write(msg.getBytes());
41+
// } catch (Exception e) {
42+
// exception = new RunnerException(e);
43+
// }
44+
});
45+
builderErr = new MessageSiphon(builder.getErrorStream(), (msg) -> {
46+
System.err.println(msg);
47+
// try {
48+
// xxx.write(msg.getBytes());
49+
// } catch (Exception e) {
50+
// exception = new RunnerException(e);
51+
// }
52+
});
53+
}
54+
55+
public int close() throws InterruptedException {
56+
builder.destroy();
57+
builderOut.join();
58+
builderErr.join();
59+
return builder.waitFor();
60+
}
61+
62+
public String codeComplete(TargetBoard board, File pathToSketch, File requestedFile, int line, int col) {
63+
BuildParams.Builder request = BuildParams.newBuilder();
64+
65+
File builtInLibs = BaseNoGui.getContentFile("libraries");
66+
if (builtInLibs.isDirectory()) {
67+
request.setBuiltInLibrariesFolders(builtInLibs.getAbsolutePath());
68+
}
69+
request.setCodeCompleteAt(requestedFile.getAbsolutePath() + ":" + line + ":" + col);
70+
request.setFQBN(Compiler.getBoardFQBN(board));
71+
request.setCustomBuildProperties("build.warn_data_percentage=75");
72+
String hardwareFolders = BaseNoGui.getAllHardwareFolders().stream().map(x -> x.getAbsolutePath()).collect(Collectors.joining(","));
73+
request.setHardwareFolders(hardwareFolders);
74+
request.setOtherLibrariesFolders(BaseNoGui.getSketchbookLibrariesFolder().folder.getAbsolutePath());
75+
request.setArduinoAPIVersion("10805");
76+
request.setSketchLocation(pathToSketch.getAbsolutePath());
77+
String toolsFolders = BaseNoGui.getAllToolsFolders().stream().map(x->x.getAbsolutePath()).collect(Collectors.joining(","));
78+
request.setToolsFolders(toolsFolders);
79+
//request.setVerbose(true);
80+
//request.setWarningsLevel("all");
81+
//request.setBuildCachePath("/tmp/arduino_cache_761418/");
82+
Response resp = blockingStub.autocomplete(request.build());
83+
return resp.getLine();
84+
}
85+
86+
}

0 commit comments

Comments
 (0)