@@ -18,8 +18,6 @@ namespace Flow.Launcher.Core.Plugin
1818{
1919 public static class PluginsLoader
2020 {
21- public const string PATH = "PATH" ;
22- public const string Python = "python" ;
2321 public const string PythonExecutable = "pythonw.exe" ;
2422
2523 public static List < PluginPair > Plugins ( List < PluginMetadata > metadatas , PluginsSettings settings )
@@ -117,119 +115,65 @@ public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source,
117115 if ( ! source . Any ( o => o . Language . ToUpper ( ) == AllowedLanguage . Python ) )
118116 return new List < PluginPair > ( ) ;
119117
120- // Try setting Constant.PythonPath first, either from
121- // PATH or from the given pythonDirectory
122- if ( string . IsNullOrEmpty ( settings . PythonDirectory ) )
118+ if ( ! string . IsNullOrEmpty ( settings . PythonDirectory ) && FilesFolders . LocationExists ( settings . PythonDirectory ) )
119+ return SetPythonPathForPluginPairs ( source , Path . Combine ( settings . PythonDirectory , PythonExecutable ) ) ;
120+
121+ var pythonPath = string . Empty ;
122+
123+ if ( MessageBox . Show ( "Flow detected you have installed Python plugins, " +
124+ "would you like to install Python to run them? " +
125+ Environment . NewLine + Environment . NewLine +
126+ "Click no if it's already installed, " +
127+ "and you will be prompted to select the folder that contains the Python executable" ,
128+ string . Empty , MessageBoxButtons . YesNo ) == DialogResult . No
129+ && string . IsNullOrEmpty ( settings . PythonDirectory ) )
123130 {
124- var whereProcess = Process . Start ( new ProcessStartInfo
131+ var dlg = new FolderBrowserDialog
125132 {
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 ) )
136- {
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 ?
145- . Split ( ';' )
146- . FirstOrDefault ( p => p . ToLower ( ) . Contains ( Python ) ) ;
147- }
133+ SelectedPath = Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFiles )
134+ } ;
148135
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
136+ var result = dlg . ShowDialog ( ) ;
137+ if ( result == DialogResult . OK )
156138 {
157- Log . Error ( "PluginsLoader" ,
158- "Failed to set Python path despite the environment variable PATH is found" ,
159- "PythonPlugins" ) ;
139+ string pythonDirectory = dlg . SelectedPath ;
140+ if ( ! string . IsNullOrEmpty ( pythonDirectory ) )
141+ {
142+ pythonPath = Path . Combine ( pythonDirectory , PythonExecutable ) ;
143+ if ( File . Exists ( pythonPath ) )
144+ {
145+ settings . PythonDirectory = pythonDirectory ;
146+ Constant . PythonPath = pythonPath ;
147+ }
148+ else
149+ {
150+ MessageBox . Show ( "Can't find python in given directory" ) ;
151+ }
152+ }
160153 }
161154 }
162155 else
163156 {
164- var path = Path . Combine ( settings . PythonDirectory , PythonExecutable ) ;
165- if ( File . Exists ( path ) )
166- {
167- Constant . PythonPath = path ;
168- }
169- else
170- {
171- Log . Error ( "PluginsLoader" , $ "Tried to automatically set from Settings.PythonDirectory " +
172- $ "but can't find python executable in { path } ", "PythonPlugins" ) ;
173- }
174- }
157+ var installedPythonDirectory = Path . Combine ( DataLocation . DataDirectory ( ) , "Python Embeddable" ) ;
175158
176- if ( string . IsNullOrEmpty ( settings . PythonDirectory ) )
177- {
178- if ( MessageBox . Show ( "Flow detected you have installed Python plugins, " +
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" ,
183- string . Empty , MessageBoxButtons . YesNo ) == DialogResult . No
184- && string . IsNullOrEmpty ( settings . PythonDirectory ) )
185- {
186- var dlg = new FolderBrowserDialog
187- {
188- SelectedPath = Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFiles )
189- } ;
159+ // Python 3.8.9 is used for Windows 7 compatibility
160+ DroplexPackage . Drop ( App . python_3_8_9_embeddable , installedPythonDirectory ) . Wait ( ) ;
190161
191- var result = dlg . ShowDialog ( ) ;
192- if ( result == DialogResult . OK )
193- {
194- string pythonDirectory = dlg . SelectedPath ;
195- if ( ! string . IsNullOrEmpty ( pythonDirectory ) )
196- {
197- var pythonPath = Path . Combine ( pythonDirectory , PythonExecutable ) ;
198- if ( File . Exists ( pythonPath ) )
199- {
200- settings . PythonDirectory = pythonDirectory ;
201- Constant . PythonPath = pythonPath ;
202- }
203- else
204- {
205- MessageBox . Show ( "Can't find python in given directory" ) ;
206- }
207- }
208- }
162+ pythonPath = Path . Combine ( installedPythonDirectory , PythonExecutable ) ;
163+ if ( FilesFolders . FileExists ( pythonPath ) )
164+ {
165+ settings . PythonDirectory = installedPythonDirectory ;
166+ Constant . PythonPath = pythonPath ;
209167 }
210168 else
211169 {
212- DroplexPackage . Drop ( App . python3_9_1 ) . Wait ( ) ;
213-
214- var installedPythonDirectory =
215- Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) ,
216- @"Programs\Python\Python39" ) ;
217- var pythonPath = Path . Combine ( installedPythonDirectory , PythonExecutable ) ;
218- if ( FilesFolders . FileExists ( pythonPath ) )
219- {
220- settings . PythonDirectory = installedPythonDirectory ;
221- Constant . PythonPath = pythonPath ;
222- }
223- else
224- {
225- Log . Error ( "PluginsLoader" ,
226- $ "Failed to set Python path after Droplex install, { pythonPath } does not exist",
227- "PythonPlugins" ) ;
228- }
170+ Log . Error ( "PluginsLoader" ,
171+ $ "Failed to set Python path after Droplex install, { pythonPath } does not exist",
172+ "PythonPlugins" ) ;
229173 }
230174 }
231175
232- if ( string . IsNullOrEmpty ( settings . PythonDirectory ) )
176+ if ( string . IsNullOrEmpty ( settings . PythonDirectory ) || string . IsNullOrEmpty ( pythonPath ) )
233177 {
234178 MessageBox . Show (
235179 "Unable to set Python executable path, please try from Flow's settings (scroll down to the bottom)." ) ;
@@ -240,16 +184,20 @@ public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source,
240184 return new List < PluginPair > ( ) ;
241185 }
242186
243- return source
187+ return SetPythonPathForPluginPairs ( source , pythonPath ) ;
188+ }
189+
190+ private static IEnumerable < PluginPair > SetPythonPathForPluginPairs ( List < PluginMetadata > source , string pythonPath )
191+ => source
244192 . Where ( o => o . Language . ToUpper ( ) == AllowedLanguage . Python )
245193 . Select ( metadata => new PluginPair
246194 {
247- Plugin = new PythonPlugin ( Constant . PythonPath ) , Metadata = metadata
195+ Plugin = new PythonPlugin ( pythonPath ) ,
196+ Metadata = metadata
248197 } )
249198 . ToList ( ) ;
250- }
251199
252- public static IEnumerable < PluginPair > ExecutablePlugins ( IEnumerable < PluginMetadata > source )
200+ public static IEnumerable < PluginPair > ExecutablePlugins ( IEnumerable < PluginMetadata > source )
253201 {
254202 return source
255203 . Where ( o => o . Language . ToUpper ( ) == AllowedLanguage . Executable )
0 commit comments