|
| 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