29
29
30
30
package cc .arduino .contributions .libraries ;
31
31
32
- import cc .arduino .Constants ;
33
- import cc .arduino .contributions .DownloadableContributionsDownloader ;
34
- import cc .arduino .contributions .GZippedJsonDownloader ;
32
+ import static processing .app .I18n .tr ;
33
+
34
+ import java .util .Optional ;
35
+
35
36
import cc .arduino .contributions .ProgressListener ;
36
- import cc .arduino .utils .ArchiveExtractor ;
37
37
import cc .arduino .utils .MultiStepProgress ;
38
38
import processing .app .BaseNoGui ;
39
39
import processing .app .I18n ;
40
- import processing .app .Platform ;
41
- import processing .app .helpers .FileUtils ;
42
-
43
- import java .io .File ;
44
- import java .io .IOException ;
45
- import java .net .URL ;
46
- import java .util .Optional ;
47
-
48
- import static processing .app .I18n .tr ;
40
+ import processing .app .helpers .ProcessUtils ;
49
41
50
42
public class LibraryInstaller {
51
43
52
- private final Platform platform ;
53
-
54
- public LibraryInstaller (Platform platform ) {
55
- this .platform = platform ;
56
- }
57
-
58
44
public synchronized void updateIndex (ProgressListener progressListener ) throws Exception {
59
45
final MultiStepProgress progress = new MultiStepProgress (3 );
60
46
61
- DownloadableContributionsDownloader downloader = new DownloadableContributionsDownloader (BaseNoGui .librariesIndexer .getStagingFolder ());
62
- // Step 1: Download index
63
- File outputFile = BaseNoGui .librariesIndexer .getIndexFile ();
64
- File tmpFile = new File (outputFile .getAbsolutePath () + ".tmp" );
65
- try {
66
- GZippedJsonDownloader gZippedJsonDownloader = new GZippedJsonDownloader (downloader , new URL (Constants .LIBRARY_INDEX_URL ), new URL (Constants .LIBRARY_INDEX_URL_GZ ));
67
- gZippedJsonDownloader .download (tmpFile , progress , tr ("Downloading libraries index..." ), progressListener );
68
- } catch (InterruptedException e ) {
69
- // Download interrupted... just exit
70
- return ;
71
- }
47
+ progress .setStatus (tr ("Updating library index" ));
72
48
progress .stepDone ();
49
+ progressListener .onProgress (progress );
73
50
74
- // TODO: Check downloaded index
75
-
76
- // Replace old index with the updated one
77
- if (outputFile .exists ())
78
- outputFile .delete ();
79
- if (!tmpFile .renameTo (outputFile ))
51
+ Process pr = ProcessUtils .exec (new String [] { //
52
+ BaseNoGui .getArduinoCliPath (), "lib" , "update-index" });
53
+ int exitCode = pr .waitFor ();
54
+ if (exitCode != 0 ) {
80
55
throw new Exception (tr ("An error occurred while updating libraries index!" ));
56
+ }
57
+ progress .stepDone ();
81
58
82
59
// Step 2: Parse index
83
60
BaseNoGui .librariesIndexer .parseIndex ();
@@ -86,65 +63,55 @@ public synchronized void updateIndex(ProgressListener progressListener) throws E
86
63
rescanLibraryIndex (progress , progressListener );
87
64
}
88
65
89
- public synchronized void install (ContributedLibrary lib , Optional <ContributedLibrary > mayReplacedLib , ProgressListener progressListener ) throws Exception {
66
+ public synchronized void install (ContributedLibrary lib , ProgressListener progressListener ) throws Exception {
67
+ if (lib .isIDEBuiltIn ()) {
68
+ // If the desired library is available as builtin in the IDE just remove
69
+ // the other installed in sketchbook...
70
+ Optional <ContributedLibrary > current = lib .getReleases ().getInstalled ();
71
+ if (current .isPresent ()) {
72
+ System .out .println (
73
+ I18n .format (tr ("Library {0} is available as built-in in the IDE.\n Removing the other version {1} installed in the sketchbook..." ),
74
+ lib .getName () + ":" + lib .getVersion (), current .get ().getVersion ()));
75
+ remove (current .get (), progressListener );
76
+ return ;
77
+ }
78
+ // ...otherwise output "library already installed"
79
+ }
80
+
90
81
if (lib .isLibraryInstalled ()) {
91
- System .out .println (I18n .format (tr ("Library is already installed: {0}:{1}" ), lib .getName (), lib .getParsedVersion ()));
82
+ System .out .println (I18n .format (tr ("Library is already installed: {0}:{1}" ), lib .getName (), lib .getVersion ()));
92
83
return ;
93
84
}
94
85
95
- DownloadableContributionsDownloader downloader = new DownloadableContributionsDownloader (BaseNoGui .librariesIndexer .getStagingFolder ());
96
-
97
86
final MultiStepProgress progress = new MultiStepProgress (3 );
98
87
99
88
// Step 1: Download library
100
- try {
101
- downloader .download (lib , progress , I18n .format (tr ("Downloading library: {0}" ), lib .getName ()), progressListener );
102
- } catch (InterruptedException e ) {
103
- // Download interrupted... just exit
104
- return ;
89
+ String libTag = lib .getName () + "@" + lib .getVersion ();
90
+ Process pr = ProcessUtils .exec (new String [] { //
91
+ BaseNoGui .getArduinoCliPath (), "lib" , "install" , libTag });
92
+ int exitCode = pr .waitFor ();
93
+ if (exitCode != 0 ) {
94
+ throw new Exception (tr ("An error occurred while installing library!" ));
105
95
}
106
96
107
- // TODO: Extract to temporary folders and move to the final destination only
108
- // once everything is successfully unpacked. If the operation fails remove
109
- // all the temporary folders and abort installation.
110
-
111
97
// Step 2: Unpack library on the correct location
112
- progress .setStatus (I18n .format (tr ("Installing library: {0}:{1}" ), lib .getName (), lib .getParsedVersion ()));
113
- progressListener .onProgress (progress );
114
- File libsFolder = BaseNoGui .getSketchbookLibrariesFolder ().folder ;
115
- File tmpFolder = FileUtils .createTempFolder (libsFolder );
116
- try {
117
- new ArchiveExtractor (platform ).extract (lib .getDownloadedFile (), tmpFolder , 1 );
118
- } catch (Exception e ) {
119
- if (tmpFolder .exists ())
120
- FileUtils .recursiveDelete (tmpFolder );
121
- }
122
- progress .stepDone ();
123
-
124
- // Step 3: Remove replaced library and move installed one to the correct location
125
- // TODO: Fix progress bar...
126
- if (mayReplacedLib .isPresent ()) {
127
- remove (mayReplacedLib .get (), progressListener );
128
- }
129
- File destFolder = new File (libsFolder , lib .getName ().replaceAll (" " , "_" ));
130
- tmpFolder .renameTo (destFolder );
131
98
progress .stepDone ();
132
99
133
- // Step 4 : Rescan index
100
+ // Step 3 : Rescan index
134
101
rescanLibraryIndex (progress , progressListener );
135
102
}
136
103
137
- public synchronized void remove (ContributedLibrary lib , ProgressListener progressListener ) throws IOException {
138
- if (lib .isIDEBuiltIn ()) {
139
- return ;
140
- }
141
-
104
+ public synchronized void remove (ContributedLibrary lib , ProgressListener progressListener ) throws Exception {
142
105
final MultiStepProgress progress = new MultiStepProgress (2 );
143
106
144
107
// Step 1: Remove library
145
- progress .setStatus (I18n .format (tr ("Removing library: {0}:{1}" ), lib .getName (), lib .getParsedVersion ()));
146
- progressListener .onProgress (progress );
147
- FileUtils .recursiveDelete (lib .getInstalledLibrary ().get ().getInstalledFolder ());
108
+ String libTag = lib .getName () + "@" + lib .getVersion ();
109
+ Process pr = ProcessUtils .exec (new String [] { //
110
+ BaseNoGui .getArduinoCliPath (), "lib" , "uninstall" , libTag });
111
+ int exitCode = pr .waitFor ();
112
+ if (exitCode != 0 ) {
113
+ throw new Exception (tr ("An error occurred while uninstalling library!" ));
114
+ }
148
115
progress .stepDone ();
149
116
150
117
// Step 2: Rescan index
@@ -157,4 +124,11 @@ private void rescanLibraryIndex(MultiStepProgress progress, ProgressListener pro
157
124
BaseNoGui .librariesIndexer .rescanLibraries ();
158
125
progress .stepDone ();
159
126
}
127
+
128
+ // TODO: legacy messages already translated, keeping here until reused
129
+ static {
130
+ I18n .format (tr ("Downloading library: {0}" ), "libname" );
131
+ I18n .format (tr ("Installing library: {0}:{1}" ), "libname" , "libversion" );
132
+ I18n .format (tr ("Removing library: {0}:{1}" ), "libname" , "libversion" );
133
+ }
160
134
}
0 commit comments