4
4
using Flow . Launcher . Infrastructure . UserSettings ;
5
5
using Flow . Launcher . Plugin . PluginsManager . Models ;
6
6
using ICSharpCode . SharpZipLib . Zip ;
7
+ using Newtonsoft . Json ;
7
8
using System ;
8
9
using System . Collections . Generic ;
9
10
using System . IO ;
@@ -37,7 +38,7 @@ internal void PluginInstall(UserPlugin plugin)
37
38
38
39
var filePath = Path . Combine ( DataLocation . PluginsDirectory , $ "{ plugin . Name } { plugin . ID } .zip") ;
39
40
PluginDownload ( plugin . UrlDownload , filePath ) ;
40
- context . API . InstallPlugin ( filePath ) ;
41
+ Application . Current . Dispatcher . Invoke ( ( ) => Install ( plugin , filePath ) ) ;
41
42
}
42
43
43
44
private void PluginDownload ( string downloadUrl , string toFilePath )
@@ -108,51 +109,62 @@ internal List<Result> PluginsSearch(string searchName)
108
109
. ToList ( ) ;
109
110
}
110
111
111
- private void Install ( string path )
112
+ private void Install ( UserPlugin plugin , string downloadedFilePath )
112
113
{
113
- if ( File . Exists ( path ) )
114
+ if ( File . Exists ( downloadedFilePath ) )
114
115
{
115
- string tempFolder = Path . Combine ( Path . GetTempPath ( ) , "flowlauncher" , "plugins" ) ;
116
- if ( Directory . Exists ( tempFolder ) )
116
+ var tempFolderPath = Path . Combine ( Path . GetTempPath ( ) , "flowlauncher" ) ;
117
+ var tempPluginFolderPath = Path . Combine ( tempFolderPath , "plugin" ) ;
118
+
119
+ if ( Directory . Exists ( tempFolderPath ) )
117
120
{
118
- Directory . Delete ( tempFolder , true ) ;
121
+ Directory . Delete ( tempFolderPath , true ) ;
119
122
}
120
- UnZip ( path , tempFolder , true ) ;
121
123
122
- string jsonPath = Path . Combine ( tempFolder , Constant . PluginMetadataFileName ) ;
123
- if ( ! File . Exists ( jsonPath ) )
124
- {
125
- MessageBox . Show ( "Install failed: plugin config is missing" ) ;
126
- return ;
127
- }
124
+ Directory . CreateDirectory ( tempFolderPath ) ;
125
+
126
+ var zipFilePath = Path . Combine ( tempFolderPath , Path . GetFileName ( downloadedFilePath ) ) ;
127
+
128
+ File . Move ( downloadedFilePath , zipFilePath ) ;
129
+
130
+ UnZip ( zipFilePath , tempPluginFolderPath , true ) ;
131
+
132
+ var unzippedParentFolderPath = tempPluginFolderPath ;
133
+
134
+ var metadataJsonFilePath = string . Empty ;
135
+
136
+ var pluginFolderPath = string . Empty ;
128
137
129
- PluginMetadata plugin = GetMetadataFromJson ( tempFolder ) ;
130
- if ( plugin == null || plugin . Name == null )
138
+ var unzippedFolderCount = Directory . GetDirectories ( unzippedParentFolderPath ) . Length ;
139
+ var unzippedFilesCount = Directory . GetFiles ( unzippedParentFolderPath ) . Length ;
140
+
141
+ // addjust path depending on how the plugin is zipped up
142
+ // the recommended should be to zip up the folder not the contents
143
+ if ( unzippedFolderCount == 1 && unzippedFilesCount == 0 )
144
+ // folder is zipped up, unzipped plugin directory structure: tempPath/unzippedParentPluginFolder/pluginFolderName/
145
+ pluginFolderPath = Directory . GetDirectories ( unzippedParentFolderPath ) [ 0 ] ;
146
+
147
+ if ( unzippedFilesCount > 1 )
148
+ // content is zipped up, unzipped plugin directory structure: tempPath/unzippedParentPluginFolder/
149
+ pluginFolderPath = unzippedParentFolderPath ;
150
+
151
+ if ( File . Exists ( Path . Combine ( pluginFolderPath , Constant . PluginMetadataFileName ) ) )
152
+ metadataJsonFilePath = Path . Combine ( pluginFolderPath , Constant . PluginMetadataFileName ) ;
153
+
154
+ if ( string . IsNullOrEmpty ( metadataJsonFilePath ) || string . IsNullOrEmpty ( pluginFolderPath ) )
131
155
{
132
- MessageBox . Show ( "Install failed: plugin config is invalid " ) ;
156
+ MessageBox . Show ( "Install failed: unable to find the plugin.json metadata file " ) ;
133
157
return ;
134
158
}
135
159
136
- string pluginFolderPath = Infrastructure . UserSettings . DataLocation . PluginsDirectory ;
137
-
138
- string newPluginName = plugin . Name
139
- . Replace ( "/" , "_" )
140
- . Replace ( "\\ " , "_" )
141
- . Replace ( ":" , "_" )
142
- . Replace ( "<" , "_" )
143
- . Replace ( ">" , "_" )
144
- . Replace ( "?" , "_" )
145
- . Replace ( "*" , "_" )
146
- . Replace ( "|" , "_" )
147
- + "-" + Guid . NewGuid ( ) ;
148
-
149
- string newPluginPath = Path . Combine ( pluginFolderPath , newPluginName ) ;
160
+ string newPluginPath = Path . Combine ( DataLocation . PluginsDirectory , $ "{ plugin . Name } { plugin . ID } ") ;
150
161
151
162
string content = $ "Do you want to install following plugin?{ Environment . NewLine } { Environment . NewLine } " +
152
163
$ "Name: { plugin . Name } { Environment . NewLine } " +
153
164
$ "Version: { plugin . Version } { Environment . NewLine } " +
154
165
$ "Author: { plugin . Author } ";
155
- PluginPair existingPlugin = PluginManager . GetPluginForId ( plugin . ID ) ;
166
+
167
+ var existingPlugin = context . API . GetAllPlugins ( ) . Where ( x => x . Metadata . ID == plugin . ID ) . FirstOrDefault ( ) ;
156
168
157
169
if ( existingPlugin != null )
158
170
{
@@ -172,7 +184,7 @@ private void Install(string path)
172
184
File . Create ( Path . Combine ( existingPlugin . Metadata . PluginDirectory , "NeedDelete.txt" ) ) . Close ( ) ;
173
185
}
174
186
175
- Directory . Move ( tempFolder , newPluginPath ) ;
187
+ Directory . Move ( pluginFolderPath , newPluginPath ) ;
176
188
177
189
//exsiting plugins may be has loaded by application,
178
190
//if we try to delelte those kind of plugins, we will get a error that indicate the
@@ -186,73 +198,38 @@ private void Install(string path)
186
198
"Restart Flow Launcher to take effect?" ,
187
199
"Install plugin" , MessageBoxButton . YesNo , MessageBoxImage . Question ) == MessageBoxResult . Yes )
188
200
{
189
- PluginManager . API . RestartApp ( ) ;
201
+ context . API . RestartApp ( ) ;
190
202
}
191
203
}
192
204
}
193
205
}
194
206
195
- private PluginMetadata GetMetadataFromJson ( string pluginDirectory )
196
- {
197
- string configPath = Path . Combine ( pluginDirectory , Constant . PluginMetadataFileName ) ;
198
- PluginMetadata metadata ;
199
-
200
- if ( ! File . Exists ( configPath ) )
201
- {
202
- return null ;
203
- }
204
-
205
- try
206
- {
207
- metadata = JsonConvert . DeserializeObject < PluginMetadata > ( File . ReadAllText ( configPath ) ) ;
208
- metadata . PluginDirectory = pluginDirectory ;
209
- }
210
- catch ( Exception e )
211
- {
212
- Log . Exception ( $ "|PluginInstaller.GetMetadataFromJson|plugin config { configPath } failed: invalid json format", e ) ;
213
- return null ;
214
- }
215
-
216
- if ( ! AllowedLanguage . IsAllowed ( metadata . Language ) )
217
- {
218
- Log . Error ( $ "|PluginInstaller.GetMetadataFromJson|plugin config { configPath } failed: invalid language { metadata . Language } ") ;
219
- return null ;
220
- }
221
- if ( ! File . Exists ( metadata . ExecuteFilePath ) )
222
- {
223
- Log . Error ( $ "|PluginInstaller.GetMetadataFromJson|plugin config { configPath } failed: file { metadata . ExecuteFilePath } doesn't exist") ;
224
- return null ;
225
- }
226
-
227
- return metadata ;
228
- }
229
-
230
207
/// <summary>
231
208
/// unzip plugin contents to the given directory.
232
209
/// </summary>
233
- /// <param name="zipFile ">The path to the zip file.</param>
210
+ /// <param name="zipFilePath ">The path to the zip file.</param>
234
211
/// <param name="strDirectory">The output directory.</param>
235
- /// <param name="overWrite">overwirte </param>
236
- private void UnZip ( string zipFile , string strDirectory , bool overWrite )
212
+ /// <param name="overwrite">overwrite </param>
213
+ private void UnZip ( string zipFilePath , string strDirectory , bool overwrite )
237
214
{
238
215
if ( strDirectory == "" )
239
216
strDirectory = Directory . GetCurrentDirectory ( ) ;
240
217
241
- using ( ZipInputStream zipStream = new ZipInputStream ( File . OpenRead ( zipFile ) ) )
218
+ using ( ZipInputStream zipStream = new ZipInputStream ( File . OpenRead ( zipFilePath ) ) )
242
219
{
243
220
ZipEntry theEntry ;
244
221
245
222
while ( ( theEntry = zipStream . GetNextEntry ( ) ) != null )
246
223
{
247
224
var pathToZip = theEntry . Name ;
248
- var directoryName = String . IsNullOrEmpty ( pathToZip ) ? "" : Path . GetDirectoryName ( pathToZip ) ;
225
+ var directoryName = string . IsNullOrEmpty ( pathToZip ) ? "" : Path . GetDirectoryName ( pathToZip ) ;
249
226
var fileName = Path . GetFileName ( pathToZip ) ;
250
227
var destinationDir = Path . Combine ( strDirectory , directoryName ) ;
251
228
var destinationFile = Path . Combine ( destinationDir , fileName ) ;
252
229
253
230
Directory . CreateDirectory ( destinationDir ) ;
254
231
255
- if ( String . IsNullOrEmpty ( fileName ) || ( File . Exists ( destinationFile ) && ! overWrite ) )
232
+ if ( string . IsNullOrEmpty ( fileName ) || ( File . Exists ( destinationFile ) && ! overwrite ) )
256
233
continue ;
257
234
258
235
using ( FileStream streamWriter = File . Create ( destinationFile ) )
@@ -262,5 +239,7 @@ private void UnZip(string zipFile, string strDirectory, bool overWrite)
262
239
}
263
240
}
264
241
}
242
+
243
+ //delete the zip file when done
265
244
}
266
245
}
0 commit comments