1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-03-14 11:29:26 +01:00

GRPC: Added lib index update command with progress wrapper

This commit is contained in:
Cristian Maglie 2019-11-28 16:57:51 +01:00
parent 84b9518776
commit cc26e75e96
4 changed files with 109 additions and 1 deletions

View File

@ -54,5 +54,6 @@
<classpathentry kind="lib" path="lib/jtouchbar-1.0.0.jar"/> <classpathentry kind="lib" path="lib/jtouchbar-1.0.0.jar"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.8.1.jar"/> <classpathentry kind="lib" path="lib/commons-lang3-3.8.1.jar"/>
<classpathentry kind="lib" path="lib/jssc-2.8.0-arduino4.jar"/> <classpathentry kind="lib" path="lib/jssc-2.8.0-arduino4.jar"/>
<classpathentry kind="lib" path="lib/grpc-core-1.20.0.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

Binary file not shown.

View File

@ -29,24 +29,32 @@
package cc.arduino.cli; package cc.arduino.cli;
import static processing.app.I18n.tr;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import cc.arduino.cli.commands.ArduinoCoreGrpc.ArduinoCoreBlockingStub;
import cc.arduino.cli.commands.Board.BoardDetailsReq; import cc.arduino.cli.commands.Board.BoardDetailsReq;
import cc.arduino.cli.commands.Board.BoardDetailsResp; import cc.arduino.cli.commands.Board.BoardDetailsResp;
import cc.arduino.cli.commands.Commands.DestroyReq; import cc.arduino.cli.commands.Commands.DestroyReq;
import cc.arduino.cli.commands.Commands.RescanReq; import cc.arduino.cli.commands.Commands.RescanReq;
import cc.arduino.cli.commands.Commands.UpdateLibrariesIndexReq;
import cc.arduino.cli.commands.Commands.UpdateLibrariesIndexResp;
import cc.arduino.cli.commands.Common.DownloadProgress;
import cc.arduino.cli.commands.Common.Instance; import cc.arduino.cli.commands.Common.Instance;
import cc.arduino.cli.commands.Compile.CompileReq; import cc.arduino.cli.commands.Compile.CompileReq;
import cc.arduino.cli.commands.Compile.CompileResp; import cc.arduino.cli.commands.Compile.CompileResp;
import cc.arduino.cli.commands.Lib.LibrarySearchReq; import cc.arduino.cli.commands.Lib.LibrarySearchReq;
import cc.arduino.cli.commands.Lib.LibrarySearchResp; import cc.arduino.cli.commands.Lib.LibrarySearchResp;
import cc.arduino.cli.commands.Lib.SearchedLibrary; import cc.arduino.cli.commands.Lib.SearchedLibrary;
import cc.arduino.contributions.ProgressListener;
import cc.arduino.contributions.libraries.ContributedLibraryRelease;
import io.grpc.StatusException; import io.grpc.StatusException;
import io.grpc.StatusRuntimeException; import io.grpc.StatusRuntimeException;
import cc.arduino.cli.commands.ArduinoCoreGrpc.ArduinoCoreBlockingStub;
public class ArduinoCoreInstance { public class ArduinoCoreInstance {
@ -111,6 +119,24 @@ public class ArduinoCoreInstance {
} }
} }
public void updateLibrariesIndex(ProgressListener progressListener)
throws StatusException {
try {
Iterator<UpdateLibrariesIndexResp> stream = stub
.updateLibrariesIndex(UpdateLibrariesIndexReq.newBuilder()
.setInstance(instance).build());
ProgressWrapper p = new ProgressWrapper(progressListener);
while (stream.hasNext()) {
UpdateLibrariesIndexResp resp = stream.next();
DownloadProgress progress = resp.getDownloadProgress().toBuilder()
.setFile(tr("Downloading libraries index...")).build();
p.update(progress);
}
} catch (StatusRuntimeException e) {
throw e.getStatus().asException();
}
}
// Lib functions // Lib functions
public List<SearchedLibrary> searchLibrary(String query) public List<SearchedLibrary> searchLibrary(String query)

View File

@ -0,0 +1,81 @@
/*
* This file is part of Arduino.
*
* Copyright 2019 Arduino LLC (http://www.arduino.cc/)
*
* Arduino is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*/
package cc.arduino.cli;
import static processing.app.I18n.format;
import static processing.app.I18n.tr;
import cc.arduino.cli.commands.Common.DownloadProgress;
import cc.arduino.contributions.ProgressListener;
import cc.arduino.utils.MultiStepProgress;
import cc.arduino.utils.Progress;
class ProgressWrapper {
final ProgressListener progressListener;
String file = "";
String url = "";
private long downloaded, totalSize;
Progress progress = new MultiStepProgress(1);
public ProgressWrapper(ProgressListener progressListener) {
this.progressListener = progressListener;
}
public void update(DownloadProgress d) {
if (d == null) {
return;
}
if (!d.getFile().isEmpty()) {
file = d.getFile();
}
if (!d.getUrl().isEmpty()) {
url = d.getUrl();
}
if (d.getTotalSize() > 0) {
totalSize = d.getTotalSize();
}
if (d.getDownloaded() > 0) {
downloaded = d.getDownloaded();
}
String msg = format(tr("Downloaded {0}kb of {1}kb."), downloaded, totalSize);
if (d.getCompleted()) {
file = "";
url = "";
totalSize = 0;
downloaded = 0;
} else {
progress.setStatus("Downloading " + file + url + " (" + downloaded
+ " of " + totalSize + ")");
progress.setProgress(((float) downloaded / totalSize) * 100);
}
progressListener.onProgress(progress);
}
}