Skip to content

Commit c9a351a

Browse files
cmagliefacchinm
authored andcommitted
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 0cd2b50 commit c9a351a

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected void onInstall(ContributedLibrary selectedLibrary, ContributedLibrary
8484
if (selectedLibrary.isReadOnly()) {
8585
onRemovePressed(installedLibrary);
8686
} else {
87-
onInstallPressed(selectedLibrary, installedLibrary);
87+
onInstallPressed(selectedLibrary);
8888
}
8989
}
9090

@@ -219,12 +219,12 @@ protected void onUpdatePressed() {
219219
installerThread.start();
220220
}
221221

222-
public void onInstallPressed(final ContributedLibrary lib, final ContributedLibrary replaced) {
222+
public void onInstallPressed(final ContributedLibrary lib) {
223223
clearErrorMessage();
224224
installerThread = new Thread(() -> {
225225
try {
226226
setProgressVisible(true, tr("Installing..."));
227-
installer.install(lib, replaced, this::setProgress);
227+
installer.install(lib, this::setProgress);
228228
onIndexesUpdated(); // TODO: Do a better job in refreshing only the needed element
229229
//getContribModel().updateLibrary(lib);
230230
} catch (Exception e) {

app/src/processing/app/Base.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ public Base(String[] args) throws Exception {
362362
if (selected.isReadOnly()) {
363363
libraryInstaller.remove(installed, progressListener);
364364
} else {
365-
libraryInstaller.install(selected, installed, progressListener);
365+
libraryInstaller.install(selected, progressListener);
366366
}
367367
}
368368

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

+16-5
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,35 @@ public synchronized void updateIndex(ProgressListener progressListener) throws E
8282
rescanLibraryIndex(progress, progressListener);
8383
}
8484

85-
public synchronized void install(ContributedLibrary lib, ContributedLibrary replacedLib, ProgressListener progressListener) throws Exception {
85+
public synchronized void install(ContributedLibrary lib, ProgressListener progressListener) throws Exception {
8686
final MultiStepProgress progress = new MultiStepProgress(4);
8787

8888
// Do install library (3 steps)
89-
performInstall(lib, replacedLib, progressListener, progress);
89+
performInstall(lib, progressListener, progress);
9090

9191
// Rescan index (1 step)
9292
rescanLibraryIndex(progress, progressListener);
9393
}
9494

95-
private void performInstall(ContributedLibrary lib, ContributedLibrary replacedLib, ProgressListener progressListener, MultiStepProgress progress) throws Exception {
95+
private void performInstall(ContributedLibrary lib, ProgressListener progressListener, MultiStepProgress progress) throws Exception {
9696
if (lib.isInstalled()) {
9797
System.out.println(I18n.format(tr("Library is already installed: {0} version {1}"), lib.getName(), lib.getParsedVersion()));
9898
return;
9999
}
100100

101+
File libsFolder = BaseNoGui.librariesIndexer.getSketchbookLibrariesFolder();
102+
File destFolder = new File(libsFolder, lib.getName().replaceAll(" ", "_"));
103+
104+
// Check if we are replacing an already installed lib
105+
ContributedLibrary replacedLib = null;
106+
LibrariesIndex index = BaseNoGui.librariesIndexer.getIndex();
107+
for (ContributedLibrary l : index.find(lib.getName())) {
108+
if (l.isInstalled() && l.getInstalledFolder().equals(destFolder)) {
109+
replacedLib = l;
110+
break;
111+
}
112+
}
113+
101114
DownloadableContributionsDownloader downloader = new DownloadableContributionsDownloader(BaseNoGui.librariesIndexer.getStagingFolder());
102115

103116
// Step 1: Download library
@@ -116,7 +129,6 @@ private void performInstall(ContributedLibrary lib, ContributedLibrary replacedL
116129
// Step 2: Unpack library on the correct location
117130
progress.setStatus(I18n.format(tr("Installing library: {0}"), lib.getName()));
118131
progressListener.onProgress(progress);
119-
File libsFolder = BaseNoGui.librariesIndexer.getSketchbookLibrariesFolder();
120132
File tmpFolder = FileUtils.createTempFolder(libsFolder);
121133
try {
122134
new ArchiveExtractor(platform).extract(lib.getDownloadedFile(), tmpFolder, 1);
@@ -129,7 +141,6 @@ private void performInstall(ContributedLibrary lib, ContributedLibrary replacedL
129141
// Step 3: Remove replaced library and move installed one to the correct location
130142
// TODO: Fix progress bar...
131143
remove(replacedLib, progressListener);
132-
File destFolder = new File(libsFolder, lib.getName().replaceAll(" ", "_"));
133144
tmpFolder.renameTo(destFolder);
134145
progress.stepDone();
135146
}

0 commit comments

Comments
 (0)