Skip to content

Commit b17dbcc

Browse files
committed
refactor
1 parent 4365520 commit b17dbcc

File tree

2 files changed

+66
-83
lines changed

2 files changed

+66
-83
lines changed

Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs

Lines changed: 48 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,24 @@ internal PluginsManager(PluginInitContext context)
2424
}
2525
internal void InstallOrUpdate(UserPlugin plugin)
2626
{
27-
if (PluginExists())
27+
if (PluginExists(plugin.ID))
2828
{
29-
//prompt user if want to install
29+
var updateMessage = $"Do you want to update following plugin?{Environment.NewLine}{Environment.NewLine}" +
30+
$"Name: {plugin.Name}{Environment.NewLine}" +
31+
$"{Environment.NewLine}New Version: {plugin.Version}" +
32+
$"{Environment.NewLine}Author: {plugin.Author}";
3033

31-
return;
34+
throw new NotImplementedException();
3235
}
3336

37+
var message = $"Do you want to install following plugin?{Environment.NewLine}{Environment.NewLine}" +
38+
$"Name: {plugin.Name}{Environment.NewLine}" +
39+
$"Version: {plugin.Version}{Environment.NewLine}" +
40+
$"Author: {plugin.Author}";
41+
42+
if(MessageBox.Show(message, "Install plugin", MessageBoxButton.YesNo) == MessageBoxResult.No)
43+
return;
44+
3445
var filePath = Path.Combine(DataLocation.PluginsDirectory, $"{plugin.Name}{plugin.ID}.zip");
3546

3647
try
@@ -53,17 +64,18 @@ internal void InstallOrUpdate(UserPlugin plugin)
5364

5465
internal void Update()
5566
{
56-
67+
throw new NotImplementedException();
5768
}
5869

59-
internal bool PluginExists()
70+
internal bool PluginExists(string id)
6071
{
61-
return false;
72+
return Context.API.GetAllPlugins().Any(x => x.Metadata.ID == id);
6273
}
6374

6475
internal void PluginsManifestSiteOpen()
6576
{
6677
//Open from context menu https://git.vcmq.workers.dev/Flow-Launcher/Flow.Launcher.PluginsManifest
78+
throw new NotImplementedException();
6779
}
6880

6981
internal List<Result> Search(List<Result> results, string searchName)
@@ -105,89 +117,43 @@ internal List<Result> RequestInstallOrUpdate(string searchName)
105117

106118
private void Install(UserPlugin plugin, string downloadedFilePath)
107119
{
108-
if (File.Exists(downloadedFilePath))
109-
{
110-
var tempFolderPath = Path.Combine(Path.GetTempPath(), "flowlauncher");
111-
var tempPluginFolderPath = Path.Combine(tempFolderPath, "plugin");
112-
113-
if (Directory.Exists(tempFolderPath))
114-
{
115-
Directory.Delete(tempFolderPath, true);
116-
}
117-
118-
Directory.CreateDirectory(tempFolderPath);
119-
120-
var zipFilePath = Path.Combine(tempFolderPath, Path.GetFileName(downloadedFilePath));
121-
122-
File.Move(downloadedFilePath, zipFilePath);
123-
124-
Utilities.UnZip(zipFilePath, tempPluginFolderPath, true);
125-
126-
var unzippedParentFolderPath = tempPluginFolderPath;
127-
128-
var metadataJsonFilePath = string.Empty;
129-
130-
var pluginFolderPath = string.Empty;
131-
132-
var unzippedFolderCount = Directory.GetDirectories(unzippedParentFolderPath).Length;
133-
var unzippedFilesCount = Directory.GetFiles(unzippedParentFolderPath).Length;
134-
135-
// addjust path depending on how the plugin is zipped up
136-
// the recommended should be to zip up the folder not the contents
137-
if (unzippedFolderCount == 1 && unzippedFilesCount == 0)
138-
// folder is zipped up, unzipped plugin directory structure: tempPath/unzippedParentPluginFolder/pluginFolderName/
139-
pluginFolderPath = Directory.GetDirectories(unzippedParentFolderPath)[0];
140-
141-
if (unzippedFilesCount > 1)
142-
// content is zipped up, unzipped plugin directory structure: tempPath/unzippedParentPluginFolder/
143-
pluginFolderPath = unzippedParentFolderPath;
120+
if (!File.Exists(downloadedFilePath))
121+
return;
122+
123+
var tempFolderPath = Path.Combine(Path.GetTempPath(), "flowlauncher");
124+
var tempFolderPluginPath = Path.Combine(tempFolderPath, "plugin");
125+
126+
if (Directory.Exists(tempFolderPath))
127+
Directory.Delete(tempFolderPath, true);
144128

145-
if (File.Exists(Path.Combine(pluginFolderPath, Constant.PluginMetadataFileName)))
146-
metadataJsonFilePath = Path.Combine(pluginFolderPath, Constant.PluginMetadataFileName);
129+
Directory.CreateDirectory(tempFolderPath);
147130

148-
if (string.IsNullOrEmpty(metadataJsonFilePath) || string.IsNullOrEmpty(pluginFolderPath))
149-
{
150-
MessageBox.Show("Install failed: unable to find the plugin.json metadata file");
151-
return;
152-
}
131+
var zipFilePath = Path.Combine(tempFolderPath, Path.GetFileName(downloadedFilePath));
153132

154-
string newPluginPath = Path.Combine(DataLocation.PluginsDirectory, $"{plugin.Name}{plugin.ID}");
133+
File.Move(downloadedFilePath, zipFilePath);
155134

156-
string content = $"Do you want to install following plugin?{Environment.NewLine}{Environment.NewLine}" +
157-
$"Name: {plugin.Name}{Environment.NewLine}" +
158-
$"Version: {plugin.Version}{Environment.NewLine}" +
159-
$"Author: {plugin.Author}";
135+
Utilities.UnZip(zipFilePath, tempFolderPluginPath, true);
160136

161-
var existingPlugin = Context.API.GetAllPlugins().Where(x => x.Metadata.ID == plugin.ID).FirstOrDefault();
137+
var pluginFolderPath = Utilities.GetContainingFolderPathAfterUnzip(tempFolderPluginPath);
162138

163-
if (existingPlugin != null)
164-
{
165-
content = $"Do you want to update following plugin?{Environment.NewLine}{Environment.NewLine}" +
166-
$"Name: {plugin.Name}{Environment.NewLine}" +
167-
$"Old Version: {existingPlugin.Metadata.Version}" +
168-
$"{Environment.NewLine}New Version: {plugin.Version}" +
169-
$"{Environment.NewLine}Author: {plugin.Author}";
170-
}
139+
var metadataJsonFilePath = string.Empty;
140+
if (File.Exists(Path.Combine(pluginFolderPath, Constant.PluginMetadataFileName)))
141+
metadataJsonFilePath = Path.Combine(pluginFolderPath, Constant.PluginMetadataFileName);
171142

172-
var result = MessageBox.Show(content, "Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question);
173-
if (result == MessageBoxResult.Yes)
174-
{
175-
if (existingPlugin != null && Directory.Exists(existingPlugin.Metadata.PluginDirectory))
176-
{
177-
//when plugin is in use, we can't delete them. That's why we need to make plugin folder a random name
178-
File.Create(Path.Combine(existingPlugin.Metadata.PluginDirectory, "NeedDelete.txt")).Close();
179-
}
143+
if (string.IsNullOrEmpty(metadataJsonFilePath) || string.IsNullOrEmpty(pluginFolderPath))
144+
{
145+
MessageBox.Show("Install failed: unable to find the plugin.json metadata file from the new plugin");
146+
return;
147+
}
180148

181-
Directory.Move(pluginFolderPath, newPluginPath);
149+
string newPluginPath = Path.Combine(DataLocation.PluginsDirectory, $"{plugin.Name}{plugin.ID}");
150+
151+
Directory.Move(pluginFolderPath, newPluginPath);
182152

183-
if (MessageBox.Show($"You have installed plugin {plugin.Name} successfully.{Environment.NewLine}" +
184-
"Restart Flow Launcher to take effect?",
185-
"Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
186-
{
187-
Context.API.RestartApp();
188-
}
189-
}
190-
}
153+
if (MessageBox.Show($"You have installed plugin {plugin.Name} successfully.{Environment.NewLine}" +
154+
"Restart Flow Launcher to take effect?",
155+
"Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
156+
Context.API.RestartApp();
191157
}
192158

193159
internal List<Result> RequestUninstall(string search)
@@ -221,14 +187,13 @@ private void Uninstall(PluginMetadata plugin)
221187

222188
if (MessageBox.Show(message, "Flow Launcher", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
223189
{
224-
File.Create(Path.Combine(plugin.PluginDirectory, "NeedDelete.txt")).Close();
190+
using var _ = File.CreateText(Path.Combine(plugin.PluginDirectory, "NeedDelete.txt"));
191+
225192
var result = MessageBox.Show($"You have uninstalled plugin {plugin.Name} successfully.{Environment.NewLine}" +
226193
"Restart Flow Launcher to take effect?",
227194
"Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question);
228195
if (result == MessageBoxResult.Yes)
229-
{
230196
Context.API.RestartApp();
231-
}
232197
}
233198
}
234199
}

Plugins/Flow.Launcher.Plugin.PluginsManager/Utilities.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ internal static void UnZip(string zipFilePath, string strDirectory, bool overwri
4040
}
4141
}
4242

43+
internal static string GetContainingFolderPathAfterUnzip(string unzippedParentFolderPath)
44+
{
45+
var unzippedFolderCount = Directory.GetDirectories(unzippedParentFolderPath).Length;
46+
var unzippedFilesCount = Directory.GetFiles(unzippedParentFolderPath).Length;
47+
48+
// addjust path depending on how the plugin is zipped up
49+
// the recommended should be to zip up the folder not the contents
50+
if (unzippedFolderCount == 1 && unzippedFilesCount == 0)
51+
// folder is zipped up, unzipped plugin directory structure: tempPath/unzippedParentPluginFolder/pluginFolderName/
52+
return Directory.GetDirectories(unzippedParentFolderPath)[0];
53+
54+
if (unzippedFilesCount > 1)
55+
// content is zipped up, unzipped plugin directory structure: tempPath/unzippedParentPluginFolder/
56+
return unzippedParentFolderPath;
57+
58+
return string.Empty;
59+
}
60+
4361
internal static void Download(string downloadUrl, string toFilePath)
4462
{
4563
using var wc = new WebClient { Proxy = Http.WebProxy() };

0 commit comments

Comments
 (0)