Skip to content

Commit 18e90ea

Browse files
committed
LibraryInstaller now autodetects if a library is being replaced
It's no more required to pass this information from outside, just library that is being installed is now sufficient.
1 parent 74f8fb5 commit 18e90ea

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected void onInstall(ContributedLibrary selectedLibrary, Optional<Contribute
8585
if (mayInstalledLibrary.isPresent() && selectedLibrary.isIDEBuiltIn()) {
8686
onRemovePressed(mayInstalledLibrary.get());
8787
} else {
88-
onInstallPressed(selectedLibrary, mayInstalledLibrary);
88+
onInstallPressed(selectedLibrary);
8989
}
9090
}
9191

@@ -213,12 +213,12 @@ protected void onUpdatePressed() {
213213
installerThread.start();
214214
}
215215

216-
public void onInstallPressed(final ContributedLibrary lib, final Optional<ContributedLibrary> mayReplaced) {
216+
public void onInstallPressed(final ContributedLibrary lib) {
217217
clearErrorMessage();
218218
installerThread = new Thread(() -> {
219219
try {
220220
setProgressVisible(true, tr("Installing..."));
221-
installer.install(lib, mayReplaced, this::setProgress);
221+
installer.install(lib, this::setProgress);
222222
// TODO: Do a better job in refreshing only the needed element
223223
if (contribTable.getCellEditor() != null) {
224224
contribTable.getCellEditor().stopCellEditing();

app/src/processing/app/Base.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public Base(String[] args) throws Exception {
384384
library, mayInstalled.get().getParsedVersion())));
385385
libraryInstaller.remove(mayInstalled.get(), progressListener);
386386
} else {
387-
libraryInstaller.install(selected, mayInstalled, progressListener);
387+
libraryInstaller.install(selected, progressListener);
388388
}
389389
}
390390

arduino-core/src/cc/arduino/contributions/libraries/LibraryInstaller.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import java.io.File;
4444
import java.io.IOException;
4545
import java.net.URL;
46+
import java.util.ArrayList;
47+
import java.util.List;
4648
import java.util.Optional;
4749

4850
import static processing.app.I18n.tr;
@@ -86,22 +88,35 @@ public synchronized void updateIndex(ProgressListener progressListener) throws E
8688
rescanLibraryIndex(progress, progressListener);
8789
}
8890

89-
public synchronized void install(ContributedLibrary lib, Optional<ContributedLibrary> mayReplacedLib, ProgressListener progressListener) throws Exception {
91+
public synchronized void install(ContributedLibrary lib, ProgressListener progressListener) throws Exception {
9092
final MultiStepProgress progress = new MultiStepProgress(4);
9193

9294
// Do install library (3 steps)
93-
performInstall(lib, mayReplacedLib, progressListener, progress);
95+
performInstall(lib, progressListener, progress);
9496

9597
// Rescan index (1 step)
9698
rescanLibraryIndex(progress, progressListener);
9799
}
98100

99-
private void performInstall(ContributedLibrary lib, Optional<ContributedLibrary> mayReplacedLib, ProgressListener progressListener, MultiStepProgress progress) throws Exception {
101+
private void performInstall(ContributedLibrary lib, ProgressListener progressListener, MultiStepProgress progress) throws Exception {
100102
if (lib.isLibraryInstalled()) {
101103
System.out.println(I18n.format(tr("Library is already installed: {0}:{1}"), lib.getName(), lib.getParsedVersion()));
102104
return;
103105
}
104106

107+
File libsFolder = BaseNoGui.getSketchbookLibrariesFolder().folder;
108+
File destFolder = new File(libsFolder, lib.getName().replaceAll(" ", "_"));
109+
110+
// Check if we are replacing an already installed lib
111+
LibrariesIndex index = BaseNoGui.librariesIndexer.getIndex();
112+
Optional<ContributedLibrary> replacedLib = index.find(lib.getName()).stream() //
113+
.filter(l -> l.getInstalledLibrary().isPresent()) //
114+
.filter(l -> l.getInstalledLibrary().get().getInstalledFolder().equals(destFolder)) //
115+
.findAny();
116+
if (!replacedLib.isPresent() && destFolder.exists()) {
117+
System.out.println(I18n.format(tr("Library {0} is already installed in: {1}"), lib.getName(), destFolder));
118+
return;
119+
}
105120
DownloadableContributionsDownloader downloader = new DownloadableContributionsDownloader(BaseNoGui.librariesIndexer.getStagingFolder());
106121

107122
// Step 1: Download library
@@ -120,7 +135,6 @@ private void performInstall(ContributedLibrary lib, Optional<ContributedLibrary>
120135
// Step 2: Unpack library on the correct location
121136
progress.setStatus(I18n.format(tr("Installing library: {0}:{1}"), lib.getName(), lib.getParsedVersion()));
122137
progressListener.onProgress(progress);
123-
File libsFolder = BaseNoGui.getSketchbookLibrariesFolder().folder;
124138
File tmpFolder = FileUtils.createTempFolder(libsFolder);
125139
try {
126140
new ArchiveExtractor(platform).extract(lib.getDownloadedFile(), tmpFolder, 1);
@@ -132,10 +146,9 @@ private void performInstall(ContributedLibrary lib, Optional<ContributedLibrary>
132146

133147
// Step 3: Remove replaced library and move installed one to the correct location
134148
// TODO: Fix progress bar...
135-
if (mayReplacedLib.isPresent()) {
136-
remove(mayReplacedLib.get(), progressListener);
149+
if (replacedLib.isPresent()) {
150+
remove(replacedLib.get(), progressListener);
137151
}
138-
File destFolder = new File(libsFolder, lib.getName().replaceAll(" ", "_"));
139152
tmpFolder.renameTo(destFolder);
140153
progress.stepDone();
141154
}

0 commit comments

Comments
 (0)