43
43
import java .io .File ;
44
44
import java .io .IOException ;
45
45
import java .net .URL ;
46
+ import java .util .ArrayList ;
47
+ import java .util .List ;
46
48
import java .util .Optional ;
47
49
48
50
import static processing .app .I18n .tr ;
@@ -86,22 +88,35 @@ public synchronized void updateIndex(ProgressListener progressListener) throws E
86
88
rescanLibraryIndex (progress , progressListener );
87
89
}
88
90
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 {
90
92
final MultiStepProgress progress = new MultiStepProgress (4 );
91
93
92
94
// Do install library (3 steps)
93
- performInstall (lib , mayReplacedLib , progressListener , progress );
95
+ performInstall (lib , progressListener , progress );
94
96
95
97
// Rescan index (1 step)
96
98
rescanLibraryIndex (progress , progressListener );
97
99
}
98
100
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 {
100
102
if (lib .isLibraryInstalled ()) {
101
103
System .out .println (I18n .format (tr ("Library is already installed: {0}:{1}" ), lib .getName (), lib .getParsedVersion ()));
102
104
return ;
103
105
}
104
106
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
+ }
105
120
DownloadableContributionsDownloader downloader = new DownloadableContributionsDownloader (BaseNoGui .librariesIndexer .getStagingFolder ());
106
121
107
122
// Step 1: Download library
@@ -120,7 +135,6 @@ private void performInstall(ContributedLibrary lib, Optional<ContributedLibrary>
120
135
// Step 2: Unpack library on the correct location
121
136
progress .setStatus (I18n .format (tr ("Installing library: {0}:{1}" ), lib .getName (), lib .getParsedVersion ()));
122
137
progressListener .onProgress (progress );
123
- File libsFolder = BaseNoGui .getSketchbookLibrariesFolder ().folder ;
124
138
File tmpFolder = FileUtils .createTempFolder (libsFolder );
125
139
try {
126
140
new ArchiveExtractor (platform ).extract (lib .getDownloadedFile (), tmpFolder , 1 );
@@ -132,10 +146,9 @@ private void performInstall(ContributedLibrary lib, Optional<ContributedLibrary>
132
146
133
147
// Step 3: Remove replaced library and move installed one to the correct location
134
148
// TODO: Fix progress bar...
135
- if (mayReplacedLib .isPresent ()) {
136
- remove (mayReplacedLib .get (), progressListener );
149
+ if (replacedLib .isPresent ()) {
150
+ remove (replacedLib .get (), progressListener );
137
151
}
138
- File destFolder = new File (libsFolder , lib .getName ().replaceAll (" " , "_" ));
139
152
tmpFolder .renameTo (destFolder );
140
153
progress .stepDone ();
141
154
}
0 commit comments