Skip to content

Commit 518b28a

Browse files
committed
Library manager through arduino-cli (WIP, part 1)
- library index is now fetched arduino-cli - ContributedLibraries and derivatives classes have been adapted to arduino-cli structure - install/update/remove are temporary disabled - library index updated is now done trough arduino-cli - added progress wrapper Next steps: - detect installed libraries using arduino-cli - implement install/update/remove using arduino-cli
1 parent 3a4b1da commit 518b28a

19 files changed

+410
-581
lines changed

app/src/cc/arduino/contributions/libraries/filters/UpdatableLibraryPredicate.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@
2929

3030
package cc.arduino.contributions.libraries.filters;
3131

32-
import java.util.List;
3332
import java.util.function.Predicate;
3433

35-
import cc.arduino.contributions.VersionComparator;
34+
import cc.arduino.contributions.libraries.ContributedLibrary;
3635
import cc.arduino.contributions.libraries.ContributedLibraryRelease;
3736
import cc.arduino.contributions.libraries.LibrariesIndexer;
3837
import processing.app.BaseNoGui;
3938

40-
public class UpdatableLibraryPredicate implements Predicate<ContributedLibraryRelease> {
39+
public class UpdatableLibraryPredicate implements Predicate<ContributedLibrary> {
4140

4241
LibrariesIndexer librariesIndexer;
4342

@@ -50,13 +49,11 @@ public UpdatableLibraryPredicate(LibrariesIndexer indexer) {
5049
}
5150

5251
@Override
53-
public boolean test(ContributedLibraryRelease lib) {
54-
if (!lib.isLibraryInstalled()) {
52+
public boolean test(ContributedLibrary lib) {
53+
if (!lib.getInstalled().isPresent()) {
5554
return false;
5655
}
57-
String libraryName = lib.getName();
58-
List<ContributedLibraryRelease> libraries = librariesIndexer.getIndex().find(libraryName);
59-
ContributedLibraryRelease latest = libraries.stream().reduce(VersionComparator::max).get();
56+
ContributedLibraryRelease latest = lib.getLatest().get(); // it must be present
6057
return !latest.isLibraryInstalled();
6158
}
6259
}

app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleasesComparator.java renamed to app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryComparatorWithTypePriority.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@
3434

3535
import java.util.Comparator;
3636

37-
public class ContributedLibraryReleasesComparator implements Comparator<ContributedLibrary> {
37+
public class ContributedLibraryComparatorWithTypePriority implements Comparator<ContributedLibrary> {
3838

3939
private final String firstType;
4040

41-
public ContributedLibraryReleasesComparator(String firstType) {
41+
public ContributedLibraryComparatorWithTypePriority(String firstType) {
4242
this.firstType = firstType;
4343
}
4444

4545
@Override
4646
public int compare(ContributedLibrary o1, ContributedLibrary o2) {
47-
ContributedLibraryRelease lib1 = o1.getLatest();
48-
ContributedLibraryRelease lib2 = o2.getLatest();
47+
ContributedLibraryRelease lib1 = o1.getLatest().get();
48+
ContributedLibraryRelease lib2 = o2.getLatest().get();
4949

5050
if (lib1.getTypes() == null || lib2.getTypes() == null) {
5151
return compareName(lib1, lib2);

app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
import java.awt.Color;
3535
import java.awt.Component;
36+
import java.util.ArrayList;
37+
import java.util.Collection;
3638
import java.util.Collections;
3739
import java.util.LinkedList;
3840
import java.util.List;
@@ -43,52 +45,52 @@
4345

4446
import cc.arduino.contributions.DownloadableContributionVersionComparator;
4547
import cc.arduino.contributions.VersionComparator;
46-
import cc.arduino.contributions.libraries.ContributedLibraryRelease;
4748
import cc.arduino.contributions.libraries.ContributedLibrary;
49+
import cc.arduino.contributions.libraries.ContributedLibraryRelease;
4850
import cc.arduino.contributions.ui.InstallerTableCell;
4951
import cc.arduino.utils.ReverseComparator;
5052

5153
@SuppressWarnings("serial")
5254
public class ContributedLibraryTableCellEditor extends InstallerTableCell {
5355

54-
private ContributedLibrary editorValue;
56+
private ContributedLibrary editorLibrary;
5557
private ContributedLibraryTableCellJPanel editorCell;
5658

5759
@Override
5860
public Object getCellEditorValue() {
59-
return editorValue;
61+
return editorLibrary;
6062
}
6163

6264
@Override
6365
public Component getTableCellEditorComponent(JTable table, Object value,
6466
boolean isSelected, int row,
6567
int column) {
66-
editorValue = (ContributedLibrary) value;
68+
editorLibrary = (ContributedLibrary) value;
6769

6870
editorCell = new ContributedLibraryTableCellJPanel(table, value, true);
6971
editorCell.installButton
70-
.addActionListener(e -> onInstall(editorValue.getSelected(),
71-
editorValue.getInstalled()));
72+
.addActionListener(e -> onInstall(editorLibrary.getSelected().get(),
73+
editorLibrary.getInstalled()));
7274
editorCell.downgradeButton.addActionListener(e -> {
7375
JComboBox chooser = editorCell.downgradeChooser;
7476
ContributedLibraryRelease lib = (ContributedLibraryRelease) chooser.getSelectedItem();
75-
onInstall(lib, editorValue.getInstalled());
77+
onInstall(lib, editorLibrary.getInstalled());
7678
});
7779
editorCell.versionToInstallChooser.addActionListener(e -> {
78-
editorValue.select((ContributedLibraryRelease) editorCell.versionToInstallChooser.getSelectedItem());
80+
editorLibrary.select((ContributedLibraryRelease) editorCell.versionToInstallChooser.getSelectedItem());
7981
if (editorCell.versionToInstallChooser.getSelectedIndex() != 0) {
8082
InstallerTableCell.dropdownSelected(true);
8183
}
8284
});
8385

8486
setEnabled(true);
8587

86-
final Optional<ContributedLibraryRelease> mayInstalled = editorValue.getInstalled();
88+
final Optional<ContributedLibraryRelease> mayInstalled = editorLibrary.getInstalled();
8789

88-
List<ContributedLibraryRelease> releases = editorValue.getReleases();
89-
List<ContributedLibraryRelease> notInstalled = new LinkedList<>(releases);
90+
Collection<ContributedLibraryRelease> releases = editorLibrary.getReleases();
91+
List<ContributedLibraryRelease> notInstalled = new ArrayList<>(releases);
9092
if (mayInstalled.isPresent()) {
91-
notInstalled.remove(editorValue.getInstalled().get());
93+
notInstalled.remove(mayInstalled.get());
9294
}
9395

9496
Collections.sort(notInstalled, new ReverseComparator<>(

app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellJPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public ContributedLibraryTableCellJPanel(JTable parentTable, Object value,
119119
if (releases == null)
120120
return;
121121

122-
ContributedLibraryRelease selected = releases.getSelected();
122+
ContributedLibraryRelease selected = releases.getSelected().get();
123123
TitledBorder titledBorder = BorderFactory.createTitledBorder(selected.getName());
124124
titledBorder.setTitleFont(getFont().deriveFont(Font.BOLD));
125125
setBorder(titledBorder);

app/src/cc/arduino/contributions/libraries/ui/DropdownLibraryOfCategoryItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public Predicate<ContributedLibrary> getFilterPredicate() {
5454
return new Predicate<ContributedLibrary>() {
5555
@Override
5656
public boolean test(ContributedLibrary rel) {
57-
ContributedLibraryRelease lib = rel.getLatest();
57+
ContributedLibraryRelease lib = rel.getLatest().get();
5858
return category.equals(lib.getCategory());
5959
}
6060
};

app/src/cc/arduino/contributions/libraries/ui/DropdownLibraryOfTypeItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public Predicate<ContributedLibrary> getFilterPredicate() {
5454
return new Predicate<ContributedLibrary>() {
5555
@Override
5656
public boolean test(ContributedLibrary lib) {
57-
List<String> types = lib.getLatest().getTypes();
57+
List<String> types = lib.getLatest().get().getTypes();
5858
return types != null && types.contains(type);
5959
}
6060
};

app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import processing.app.BaseNoGui;
3737

3838
import java.util.ArrayList;
39+
import java.util.Collection;
3940
import java.util.Collections;
4041
import java.util.List;
4142
import java.util.function.Predicate;
@@ -131,7 +132,7 @@ public ContributedLibrary getReleases(int row) {
131132
}
132133

133134
public ContributedLibraryRelease getSelectedRelease(int row) {
134-
return contributions.get(row).getSelected();
135+
return contributions.get(row).getSelected().get();
135136
}
136137

137138
public void update() {
@@ -144,7 +145,7 @@ private boolean filterCondition(ContributedLibrary lib) {
144145
return false;
145146
}
146147

147-
ContributedLibraryRelease latest = lib.getLatest();
148+
ContributedLibraryRelease latest = lib.getLatest().get();
148149
String compoundTargetSearchText = latest.getName() + " "
149150
+ latest.getParagraph() + " "
150151
+ latest.getSentence();
@@ -158,55 +159,12 @@ private boolean filterCondition(ContributedLibrary lib) {
158159
return true;
159160
}
160161

161-
public void updateLibrary(ContributedLibraryRelease lib) {
162-
// Find the row interested in the change
163-
int row = -1;
164-
for (ContributedLibrary releases : contributions) {
165-
if (releases.shouldContain(lib))
166-
row = contributions.indexOf(releases);
167-
}
168-
169-
updateContributions();
170-
171-
// If the library is found in the list send update event
172-
// or insert event on the specific row...
173-
for (ContributedLibrary releases : contributions) {
174-
if (releases.shouldContain(lib)) {
175-
if (row == -1) {
176-
row = contributions.indexOf(releases);
177-
fireTableRowsInserted(row, row);
178-
} else {
179-
fireTableRowsUpdated(row, row);
180-
}
181-
return;
182-
}
183-
}
184-
// ...otherwise send a row deleted event
185-
fireTableRowsDeleted(row, row);
186-
}
187-
188-
private List<ContributedLibrary> rebuildContributionsFromIndex() {
189-
List<ContributedLibrary> res = new ArrayList<>();
190-
BaseNoGui.librariesIndexer.getIndex().getLibraries(). //
191-
forEach(lib -> {
192-
for (ContributedLibrary contribution : res) {
193-
if (!contribution.shouldContain(lib))
194-
continue;
195-
contribution.add(lib);
196-
return;
197-
}
198-
199-
res.add(new ContributedLibrary(lib));
200-
});
201-
return res;
202-
}
203-
204162
private void updateContributions() {
205-
List<ContributedLibrary> all = rebuildContributionsFromIndex();
163+
Collection<ContributedLibrary> all = BaseNoGui.librariesIndexer.getIndex().getLibraries();
206164
contributions.clear();
207165
all.stream().filter(this::filterCondition).forEach(contributions::add);
208166
Collections.sort(contributions,
209-
new ContributedLibraryReleasesComparator("Arduino"));
167+
new ContributedLibraryComparatorWithTypePriority("Arduino"));
210168
}
211169

212170
}

app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ protected void onUpdatePressed() {
200200
installerThread = new Thread(() -> {
201201
try {
202202
setProgressVisible(true, "");
203-
installer.updateIndex(this::setProgress);
203+
BaseNoGui.getArduinoCoreService().updateLibrariesIndex(this::setProgress);
204204
((LibrariesIndexTableModel) contribModel).update();
205205
onIndexesUpdated();
206206
} catch (Exception e) {
@@ -215,7 +215,7 @@ protected void onUpdatePressed() {
215215
}
216216

217217
public void onInstallPressed(final ContributedLibraryRelease lib) {
218-
List<ContributedLibraryRelease> deps = BaseNoGui.librariesIndexer.getIndex().resolveDependeciesOf(lib);
218+
List<ContributedLibraryRelease> deps = BaseNoGui.getArduinoCoreService().libraryResolveDependecies(lib);
219219
boolean depsInstalled = deps.stream().allMatch(l -> l.getInstalledLibrary().isPresent() || l.getName().equals(lib.getName()));
220220
Result installDeps;
221221
if (!depsInstalled) {

app/src/processing/app/Base.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import cc.arduino.UpdatableBoardsLibsFakeURLsHandler;
2828
import cc.arduino.UploaderUtils;
2929
import cc.arduino.contributions.*;
30+
import cc.arduino.contributions.libraries.ContributedLibrary;
3031
import cc.arduino.contributions.libraries.ContributedLibraryRelease;
3132
import cc.arduino.contributions.libraries.LibrariesIndexer;
3233
import cc.arduino.contributions.libraries.LibraryInstaller;
@@ -298,7 +299,7 @@ public Base(String[] args) throws Exception {
298299

299300
final GPGDetachedSignatureVerifier gpgDetachedSignatureVerifier = new GPGDetachedSignatureVerifier();
300301
contributionInstaller = new ContributionInstaller(BaseNoGui.getPlatform(), gpgDetachedSignatureVerifier);
301-
libraryInstaller = new LibraryInstaller(BaseNoGui.getPlatform(), gpgDetachedSignatureVerifier);
302+
libraryInstaller = new LibraryInstaller(BaseNoGui.getPlatform());
302303

303304
parser.parseArgumentsPhase2();
304305

@@ -358,15 +359,15 @@ public Base(String[] args) throws Exception {
358359
BaseNoGui.onBoardOrPortChange();
359360

360361
ProgressListener progressListener = new ConsoleProgressListener();
361-
libraryInstaller.updateIndex(progressListener);
362+
BaseNoGui.getArduinoCoreService().updateLibrariesIndex(progressListener);
362363

363-
LibrariesIndexer indexer = new LibrariesIndexer(BaseNoGui.getSettingsFolder());
364-
indexer.parseIndex();
364+
LibrariesIndexer indexer = new LibrariesIndexer(BaseNoGui.getArduinoCoreService());
365+
indexer.regenerateIndex();
365366
indexer.setLibrariesFolders(BaseNoGui.getLibrariesFolders());
366367
indexer.rescanLibraries();
367368

368-
for (String library : parser.getLibraryToInstall().split(",")) {
369-
String[] libraryToInstallParts = library.split(":");
369+
for (String libraryArg : parser.getLibraryToInstall().split(",")) {
370+
String[] libraryToInstallParts = libraryArg.split(":");
370371

371372
ContributedLibraryRelease selected = null;
372373
if (libraryToInstallParts.length == 2) {
@@ -375,12 +376,11 @@ public Base(String[] args) throws Exception {
375376
System.out.println(format(tr("Invalid version {0}"), libraryToInstallParts[1]));
376377
System.exit(1);
377378
}
378-
selected = indexer.getIndex().find(libraryToInstallParts[0], version.get().toString());
379+
selected = indexer.getIndex().find(libraryToInstallParts[0], version.get().toString()).orElse(null);
379380
} else if (libraryToInstallParts.length == 1) {
380-
List<ContributedLibraryRelease> librariesByName = indexer.getIndex().find(libraryToInstallParts[0]);
381-
Collections.sort(librariesByName, new DownloadableContributionVersionComparator());
382-
if (!librariesByName.isEmpty()) {
383-
selected = librariesByName.get(librariesByName.size() - 1);
381+
ContributedLibrary library = indexer.getIndex().find(libraryToInstallParts[0]).orElse(null);
382+
if (library != null) {
383+
selected = library.getLatest().orElse(null);
384384
}
385385
}
386386
if (selected == null) {
@@ -392,7 +392,7 @@ public Base(String[] args) throws Exception {
392392
if (mayInstalled.isPresent() && selected.isIDEBuiltIn()) {
393393
System.out.println(tr(I18n
394394
.format("Library {0} is available as built-in in the IDE.\nRemoving the other version {1} installed in the sketchbook...",
395-
library, mayInstalled.get().getParsedVersion())));
395+
libraryArg, mayInstalled.get().getParsedVersion())));
396396
libraryInstaller.remove(mayInstalled.get(), progressListener);
397397
} else {
398398
libraryInstaller.install(selected, progressListener);

0 commit comments

Comments
 (0)