4343import java .io .File ;
4444import java .io .IOException ;
4545import java .net .URL ;
46+ import java .util .ArrayList ;
47+ import java .util .List ;
4648import java .util .Optional ;
4749
4850import 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