Skip to content

Commit 35cf51f

Browse files
committed
refactor Core.Plugin.PluginsLoader
1 parent 94a2393 commit 35cf51f

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

Flow.Launcher.Core/Plugin/PluginsLoader.cs

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -127,65 +127,74 @@ public static IEnumerable<PluginPair> DotNetPlugins(List<PluginMetadata> source)
127127
return plugins;
128128
}
129129

130-
public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source, string pythonDirecotry)
130+
public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source, string pythonDirectory)
131131
{
132-
var metadatas = source.Where(o => o.Language.ToUpper() == AllowedLanguage.Python);
133-
string filename;
134-
135-
if (string.IsNullOrEmpty(pythonDirecotry))
132+
// try to set Constant.PythonPath, either from
133+
// PATH or from the given pythonDirectory
134+
if (string.IsNullOrEmpty(pythonDirectory))
136135
{
137136
var paths = Environment.GetEnvironmentVariable(PATH);
138137
if (paths != null)
139138
{
140-
var pythonPaths = paths.Split(';').Where(p => p.ToLower().Contains(Python));
141-
if (pythonPaths.Any())
139+
var pythonInPath = paths
140+
.Split(';')
141+
.Where(p => p.ToLower().Contains(Python))
142+
.Any();
143+
144+
if (pythonInPath)
142145
{
143-
filename = PythonExecutable;
146+
Constant.PythonPath = PythonExecutable;
144147
}
145148
else
146149
{
147150
Log.Error("|PluginsLoader.PythonPlugins|Python can't be found in PATH.");
148-
return new List<PluginPair>();
149151
}
150152
}
151153
else
152154
{
153155
Log.Error("|PluginsLoader.PythonPlugins|PATH environment variable is not set.");
154-
return new List<PluginPair>();
155156
}
156157
}
157158
else
158159
{
159-
var path = Path.Combine(pythonDirecotry, PythonExecutable);
160+
var path = Path.Combine(pythonDirectory, PythonExecutable);
160161
if (File.Exists(path))
161162
{
162-
filename = path;
163+
Constant.PythonPath = path;
163164
}
164165
else
165166
{
166-
Log.Error("|PluginsLoader.PythonPlugins|Can't find python executable in <b ");
167-
return new List<PluginPair>();
167+
Log.Error($"|PluginsLoader.PythonPlugins|Can't find python executable in {path}");
168168
}
169169
}
170-
Constant.PythonPath = filename;
171-
var plugins = metadatas.Select(metadata => new PluginPair
170+
171+
// if we have a path to the python executable,
172+
// load every python plugin pair.
173+
if (String.IsNullOrEmpty(Constant.PythonPath))
172174
{
173-
Plugin = new PythonPlugin(filename),
174-
Metadata = metadata
175-
});
176-
return plugins;
175+
return new List<PluginPair>();
176+
}
177+
else
178+
{
179+
return source
180+
.Where(o => o.Language.ToUpper() == AllowedLanguage.Python)
181+
.Select(metadata => new PluginPair
182+
{
183+
Plugin = new PythonPlugin(Constant.PythonPath),
184+
Metadata = metadata
185+
});
186+
}
177187
}
178188

179189
public static IEnumerable<PluginPair> ExecutablePlugins(IEnumerable<PluginMetadata> source)
180190
{
181-
var metadatas = source.Where(o => o.Language.ToUpper() == AllowedLanguage.Executable);
182-
183-
var plugins = metadatas.Select(metadata => new PluginPair
184-
{
185-
Plugin = new ExecutablePlugin(metadata.ExecuteFilePath),
186-
Metadata = metadata
187-
});
188-
return plugins;
191+
return source
192+
.Where(o => o.Language.ToUpper() == AllowedLanguage.Executable)
193+
.Select(metadata => new PluginPair
194+
{
195+
Plugin = new ExecutablePlugin(metadata.ExecuteFilePath),
196+
Metadata = metadata
197+
});
189198
}
190199

191200
}

0 commit comments

Comments
 (0)