Skip to content

Commit

Permalink
LibraryInstaller now autodetects if a library is being replaced
Browse files Browse the repository at this point in the history
It's no more required to pass this information from outside,
just library that is being installed is now sufficient.
  • Loading branch information
cmaglie authored and facchinm committed Jul 18, 2019
1 parent 4266b3a commit 67e38bc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected void onInstall(ContributedLibrary selectedLibrary, Optional<Contribute
if (mayInstalledLibrary.isPresent() && selectedLibrary.isIDEBuiltIn()) {
onRemovePressed(mayInstalledLibrary.get());
} else {
onInstallPressed(selectedLibrary, mayInstalledLibrary);
onInstallPressed(selectedLibrary);
}
}

Expand Down Expand Up @@ -213,12 +213,12 @@ protected void onUpdatePressed() {
installerThread.start();
}

public void onInstallPressed(final ContributedLibrary lib, final Optional<ContributedLibrary> mayReplaced) {
public void onInstallPressed(final ContributedLibrary lib) {
clearErrorMessage();
installerThread = new Thread(() -> {
try {
setProgressVisible(true, tr("Installing..."));
installer.install(lib, mayReplaced, this::setProgress);
installer.install(lib, this::setProgress);
// TODO: Do a better job in refreshing only the needed element
if (contribTable.getCellEditor() != null) {
contribTable.getCellEditor().stopCellEditing();
Expand Down
2 changes: 1 addition & 1 deletion app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public Base(String[] args) throws Exception {
library, mayInstalled.get().getParsedVersion())));
libraryInstaller.remove(mayInstalled.get(), progressListener);
} else {
libraryInstaller.install(selected, mayInstalled, progressListener);
libraryInstaller.install(selected, progressListener);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

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

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

// Do install library (3 steps)
performInstall(lib, mayReplacedLib, progressListener, progress);
performInstall(lib, progressListener, progress);

// Rescan index (1 step)
rescanLibraryIndex(progress, progressListener);
}

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

File libsFolder = BaseNoGui.getSketchbookLibrariesFolder().folder;
File destFolder = new File(libsFolder, lib.getName().replaceAll(" ", "_"));

// Check if we are replacing an already installed lib
LibrariesIndex index = BaseNoGui.librariesIndexer.getIndex();
Optional<ContributedLibrary> replacedLib = index.find(lib.getName()).stream() //
.filter(l -> l.getInstalledLibrary().isPresent()) //
.filter(l -> l.getInstalledLibrary().get().getInstalledFolder().equals(destFolder)) //
.findAny();
if (!replacedLib.isPresent() && destFolder.exists()) {
System.out.println(I18n.format(tr("Library {0} is already installed in: {1}"), lib.getName(), destFolder));
return;
}
DownloadableContributionsDownloader downloader = new DownloadableContributionsDownloader(BaseNoGui.librariesIndexer.getStagingFolder());

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

// Step 3: Remove replaced library and move installed one to the correct location
// TODO: Fix progress bar...
if (mayReplacedLib.isPresent()) {
remove(mayReplacedLib.get(), progressListener);
if (replacedLib.isPresent()) {
remove(replacedLib.get(), progressListener);
}
File destFolder = new File(libsFolder, lib.getName().replaceAll(" ", "_"));
tmpFolder.renameTo(destFolder);
progress.stepDone();
}
Expand Down

0 comments on commit 67e38bc

Please sign in to comment.