11
11
using Flow . Launcher . Infrastructure . UserSettings ;
12
12
using Flow . Launcher . Plugin ;
13
13
using Flow . Launcher . Plugin . SharedCommands ;
14
+ using System . Diagnostics ;
15
+ using Stopwatch = Flow . Launcher . Infrastructure . Stopwatch ;
14
16
15
17
namespace Flow . Launcher . Core . Plugin
16
18
{
@@ -85,11 +87,7 @@ public static IEnumerable<PluginPair> DotNetPlugins(List<PluginMetadata> source)
85
87
return ;
86
88
}
87
89
88
- plugins . Add ( new PluginPair
89
- {
90
- Plugin = plugin ,
91
- Metadata = metadata
92
- } ) ;
90
+ plugins . Add ( new PluginPair { Plugin = plugin , Metadata = metadata } ) ;
93
91
} ) ;
94
92
metadata . InitTime += milliseconds ;
95
93
}
@@ -123,24 +121,42 @@ public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source,
123
121
// PATH or from the given pythonDirectory
124
122
if ( string . IsNullOrEmpty ( settings . PythonDirectory ) )
125
123
{
126
- var paths = Environment . GetEnvironmentVariable ( PATH ) ;
127
- if ( paths != null )
124
+ var whereProcess = Process . Start ( new ProcessStartInfo
125
+ {
126
+ FileName = "where.exe" ,
127
+ Arguments = "pythonw" ,
128
+ RedirectStandardOutput = true ,
129
+ CreateNoWindow = true ,
130
+ UseShellExecute = false
131
+ } ) ;
132
+
133
+ var pythonPath = whereProcess ? . StandardOutput . ReadToEnd ( ) . Trim ( ) ;
134
+
135
+ if ( ! string . IsNullOrEmpty ( pythonPath ) )
128
136
{
129
- var pythonInPath = paths
137
+ pythonPath = FilesFolders . GetPreviousExistingDirectory ( FilesFolders . LocationExists , pythonPath ) ;
138
+ }
139
+
140
+ if ( string . IsNullOrEmpty ( pythonPath ) )
141
+ {
142
+ var paths = Environment . GetEnvironmentVariable ( PATH ) ;
143
+
144
+ pythonPath = paths ?
130
145
. Split ( ';' )
131
- . Where ( p => p . ToLower ( ) . Contains ( Python ) )
132
- . Any ( ) ;
146
+ . FirstOrDefault ( p => p . ToLower ( ) . Contains ( Python ) ) ;
147
+ }
133
148
134
- if ( pythonInPath )
135
- {
136
- Constant . PythonPath =
137
- Path . Combine ( paths . Split ( ';' ) . Where ( p => p . ToLower ( ) . Contains ( Python ) ) . FirstOrDefault ( ) , PythonExecutable ) ;
138
- settings . PythonDirectory = FilesFolders . GetPreviousExistingDirectory ( FilesFolders . LocationExists , Constant . PythonPath ) ;
139
- }
140
- else
141
- {
142
- Log . Error ( "PluginsLoader" , "Failed to set Python path despite the environment variable PATH is found" , "PythonPlugins" ) ;
143
- }
149
+ if ( ! string . IsNullOrEmpty ( pythonPath ) )
150
+ {
151
+ Constant . PythonPath = Path . Combine ( pythonPath , PythonExecutable ) ;
152
+ settings . PythonDirectory =
153
+ FilesFolders . GetPreviousExistingDirectory ( FilesFolders . LocationExists , Constant . PythonPath ) ;
154
+ }
155
+ else
156
+ {
157
+ Log . Error ( "PluginsLoader" ,
158
+ "Failed to set Python path despite the environment variable PATH is found" ,
159
+ "PythonPlugins" ) ;
144
160
}
145
161
}
146
162
else
@@ -153,17 +169,17 @@ public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source,
153
169
else
154
170
{
155
171
Log . Error ( "PluginsLoader" , $ "Tried to automatically set from Settings.PythonDirectory " +
156
- $ "but can't find python executable in { path } ", "PythonPlugins" ) ;
172
+ $ "but can't find python executable in { path } ", "PythonPlugins" ) ;
157
173
}
158
174
}
159
175
160
176
if ( string . IsNullOrEmpty ( settings . PythonDirectory ) )
161
177
{
162
178
if ( MessageBox . Show ( "Flow detected you have installed Python plugins, " +
163
- "would you like to install Python to run them? " +
164
- Environment . NewLine + Environment . NewLine +
165
- "Click no if it's already installed, " +
166
- "and you will be prompted to select the folder that contains the Python executable" ,
179
+ "would you like to install Python to run them? " +
180
+ Environment . NewLine + Environment . NewLine +
181
+ "Click no if it's already installed, " +
182
+ "and you will be prompted to select the folder that contains the Python executable" ,
167
183
string . Empty , MessageBoxButtons . YesNo ) == DialogResult . No
168
184
&& string . IsNullOrEmpty ( settings . PythonDirectory ) )
169
185
{
@@ -196,7 +212,8 @@ public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source,
196
212
DroplexPackage . Drop ( App . python3_9_1 ) . Wait ( ) ;
197
213
198
214
var installedPythonDirectory =
199
- Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) , @"Programs\Python\Python39" ) ;
215
+ Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) ,
216
+ @"Programs\Python\Python39" ) ;
200
217
var pythonPath = Path . Combine ( installedPythonDirectory , PythonExecutable ) ;
201
218
if ( FilesFolders . FileExists ( pythonPath ) )
202
219
{
@@ -214,7 +231,8 @@ public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source,
214
231
215
232
if ( string . IsNullOrEmpty ( settings . PythonDirectory ) )
216
233
{
217
- MessageBox . Show ( "Unable to set Python executable path, please try from Flow's settings (scroll down to the bottom)." ) ;
234
+ MessageBox . Show (
235
+ "Unable to set Python executable path, please try from Flow's settings (scroll down to the bottom)." ) ;
218
236
Log . Error ( "PluginsLoader" ,
219
237
$ "Not able to successfully set Python path, the PythonDirectory variable is still an empty string.",
220
238
"PythonPlugins" ) ;
@@ -226,8 +244,7 @@ public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source,
226
244
. Where ( o => o . Language . ToUpper ( ) == AllowedLanguage . Python )
227
245
. Select ( metadata => new PluginPair
228
246
{
229
- Plugin = new PythonPlugin ( Constant . PythonPath ) ,
230
- Metadata = metadata
247
+ Plugin = new PythonPlugin ( Constant . PythonPath ) , Metadata = metadata
231
248
} )
232
249
. ToList ( ) ;
233
250
}
@@ -238,9 +255,8 @@ public static IEnumerable<PluginPair> ExecutablePlugins(IEnumerable<PluginMetada
238
255
. Where ( o => o . Language . ToUpper ( ) == AllowedLanguage . Executable )
239
256
. Select ( metadata => new PluginPair
240
257
{
241
- Plugin = new ExecutablePlugin ( metadata . ExecuteFilePath ) ,
242
- Metadata = metadata
258
+ Plugin = new ExecutablePlugin ( metadata . ExecuteFilePath ) , Metadata = metadata
243
259
} ) ;
244
260
}
245
261
}
246
- }
262
+ }
0 commit comments