This commit is contained in:
Jeremy Wu 2022-11-22 09:20:27 +11:00
parent c7cee4ae85
commit 4e48c3eb10
10 changed files with 282 additions and 294 deletions

View file

@ -0,0 +1,148 @@
using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedCommands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Flow.Launcher.Core.ExternalPlugins.Environments
{
internal abstract class AbstractPluginEnvironment
{
internal abstract string Language { get; }
internal const string Environments = "Environments";
internal abstract string EnvName { get; }
internal abstract string EnvPath { get; }
internal abstract string InstallPath { get; }
internal abstract string ExecutablePath { get; }
internal virtual string FileDialogFilter => string.Empty;
internal abstract string PluginsSettingsFilePath { get; set; }
internal List<PluginMetadata> PluginMetadataList;
internal PluginsSettings PluginSettings;
internal AbstractPluginEnvironment(List<PluginMetadata> pluginMetadataList, PluginsSettings pluginSettings)
{
PluginMetadataList = pluginMetadataList;
PluginSettings = pluginSettings;
}
//TODO: CHECK IF NEED TO RESET PATH AFTER FLOW UPDATE
internal IEnumerable<PluginPair> Setup()
{
if (!PluginMetadataList.Any(o => o.Language.Equals(Language, StringComparison.OrdinalIgnoreCase)))
return new List<PluginPair>();
if (!string.IsNullOrEmpty(PluginsSettingsFilePath) && FilesFolders.FileExists(PluginsSettingsFilePath))
{
EnsureLatestInstalled(ExecutablePath, PluginsSettingsFilePath, EnvPath);
return SetPathForPluginPairs(PluginsSettingsFilePath, Language);
}
if (MessageBox.Show($"Flow detected you have installed {Language} plugins, which " +
$"will require {EnvName} to run. Would you like to download {EnvName}? " +
Environment.NewLine + Environment.NewLine +
"Click no if it's already installed, " +
$"and you will be prompted to select the folder that contains the {EnvName} executable",
string.Empty, MessageBoxButtons.YesNo) == DialogResult.No)
{
var msg = $"Please select the {EnvName} executable";
var selectedFile = string.Empty;
selectedFile = GetFileFromDialog(msg, FileDialogFilter);
if (!string.IsNullOrEmpty(selectedFile))
PluginsSettingsFilePath = selectedFile;
// Nothing selected because user pressed cancel from the file dialog window
if (string.IsNullOrEmpty(selectedFile))
InstallEnvironment();
}
else
{
InstallEnvironment();
}
if (FilesFolders.FileExists(PluginsSettingsFilePath))
{
return SetPathForPluginPairs(PluginsSettingsFilePath, Language);
}
else
{
MessageBox.Show(
$"Unable to set {Language} executable path, please try from Flow's settings (scroll down to the bottom).");
Log.Error("PluginsLoader",
$"Not able to successfully set {EnvName} path, setting's plugin executable path variable is still an empty string.",
$"{Language}Environment");
return new List<PluginPair>();
}
}
internal abstract void InstallEnvironment();
private void EnsureLatestInstalled(string expectedPath, string currentPath, string installedDirPath)
{
if (expectedPath == currentPath)
return;
FilesFolders.RemoveFolderIfExists(installedDirPath);
InstallEnvironment();
}
private IEnumerable<PluginPair> SetPathForPluginPairs(string filePath, string languageToSet)
{
var pluginPairs = new List<PluginPair>();
foreach (var metadata in PluginMetadataList)
{
if (metadata.Language.Equals(languageToSet, StringComparison.OrdinalIgnoreCase))
{
pluginPairs.Add(new PluginPair
{
Plugin = new PythonPlugin(filePath),
Metadata = metadata
});
}
}
return pluginPairs;
}
private string GetFileFromDialog(string title, string filter = "")
{
var dlg = new OpenFileDialog
{
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
Multiselect = false,
CheckFileExists = true,
CheckPathExists = true,
Title = title,
Filter = filter
};
var result = dlg.ShowDialog();
if (result == DialogResult.OK)
{
return dlg.FileName;
}
else
{
return string.Empty;
}
}
}
}

View file

@ -0,0 +1,14 @@
using System.Collections.Generic;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
namespace Flow.Launcher.Core.ExternalPlugins.Environments
{
internal class JavaScriptEnvironment : TypeScriptEnvironment
{
internal override string Language => AllowedLanguage.JavaScript;
internal JavaScriptEnvironment(List<PluginMetadata> pluginMetadataList, PluginsSettings pluginSettings) : base(pluginMetadataList, pluginSettings) { }
}
}

View file

@ -0,0 +1,38 @@
using Droplex;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedCommands;
using System.Collections.Generic;
using System.IO;
namespace Flow.Launcher.Core.ExternalPlugins.Environments
{
internal class PythonEnvironment : AbstractPluginEnvironment
{
internal override string Language => AllowedLanguage.Python;
internal override string EnvName => "Python";
internal override string EnvPath => Path.Combine(DataLocation.DataDirectory(), Environments, EnvName);
internal override string InstallPath => Path.Combine(EnvPath, "PythonEmbeddable-v3.8.9");
internal override string ExecutablePath => Path.Combine(InstallPath, "pythonw.exe");
internal override string FileDialogFilter => "Python|pythonw.exe";
internal override string PluginsSettingsFilePath { get => PluginSettings.PythonExecutablePath; set => PluginSettings.PythonExecutablePath = value; }
internal PythonEnvironment(List<PluginMetadata> pluginMetadataList, PluginsSettings pluginSettings) : base(pluginMetadataList, pluginSettings) { }
internal override void InstallEnvironment()
{
FilesFolders.RemoveFolderIfExists(InstallPath);
// Python 3.8.9 is used for Windows 7 compatibility
DroplexPackage.Drop(App.python_3_8_9_embeddable, InstallPath).Wait();
PluginsSettingsFilePath = ExecutablePath;
}
}
}

View file

@ -0,0 +1,34 @@
using System.Collections.Generic;
using Droplex;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin.SharedCommands;
using Flow.Launcher.Plugin;
using System.IO;
namespace Flow.Launcher.Core.ExternalPlugins.Environments
{
internal class TypeScriptEnvironment : AbstractPluginEnvironment
{
internal override string Language => AllowedLanguage.TypeScript;
internal override string EnvName => "Node.js";
internal override string EnvPath => Path.Combine(DataLocation.DataDirectory(), Environments, EnvName);
internal override string InstallPath => Path.Combine(EnvPath, "Node-v16.18.0");
internal override string ExecutablePath => Path.Combine(InstallPath, "node-v16.18.0-win-x64\\node.exe");
internal override string PluginsSettingsFilePath { get => PluginSettings.NodeExecutablePath; set => PluginSettings.NodeExecutablePath = value; }
internal TypeScriptEnvironment(List<PluginMetadata> pluginMetadataList, PluginsSettings pluginSettings) : base(pluginMetadataList, pluginSettings) { }
internal override void InstallEnvironment()
{
FilesFolders.RemoveFolderIfExists(InstallPath);
DroplexPackage.Drop(App.nodejs_16_18_0, InstallPath).Wait();
PluginsSettingsFilePath = ExecutablePath;
}
}
}

View file

@ -1,264 +0,0 @@
using Droplex;
using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedCommands;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace Flow.Launcher.Core.ExternalPlugins
{
public class PluginEnvironment
{
private const string environments = "Environments";
private const string python = "Python";
private static string pythonEnvDirPath = Path.Combine(DataLocation.DataDirectory(), environments, python);
private static string pythonDirPath = Path.Combine(pythonEnvDirPath, "PythonEmbeddable-v3.8.9");
private string pythonFilePath = Path.Combine(pythonDirPath, "pythonw.exe");
private const string nodejs = "Node.js";
private static string nodeEnvDirPath = Path.Combine(DataLocation.DataDirectory(), environments, nodejs);
private static string nodeDirPath = Path.Combine(nodeEnvDirPath, "Node-v16.18.0");
private string nodeFilePath = Path.Combine(nodeDirPath, $"node-v16.18.0-win-x64\\node.exe");
private List<PluginMetadata> pluginMetadataList;
private PluginsSettings pluginSettings;
internal PluginEnvironment(List<PluginMetadata> pluginMetadataList, PluginsSettings pluginSettings)
{
this.pluginMetadataList = pluginMetadataList;
this.pluginSettings = pluginSettings;
}
//TODO: CHECK IF NEED TO RESET PATH AFTER FLOW UPDATE
internal IEnumerable<PluginPair> PythonSetup()
{
return Setup(AllowedLanguage.Python, python);
}
internal IEnumerable<PluginPair> TypeScriptSetup()
{
return Setup(AllowedLanguage.TypeScript, nodejs);
}
internal IEnumerable<PluginPair> JavaScriptSetup()
{
return Setup(AllowedLanguage.JavaScript, nodejs);
}
private IEnumerable<PluginPair> Setup(string languageType, string environment)
{
if (!pluginMetadataList.Any(o => o.Language.Equals(languageType, StringComparison.OrdinalIgnoreCase)))
return new List<PluginPair>();
var envFilePath = string.Empty;
switch (languageType)
{
case AllowedLanguage.Python:
if (!string.IsNullOrEmpty(pluginSettings.PythonFilePath) && FilesFolders.FileExists(pluginSettings.PythonFilePath))
{
EnsureLatestInstalled(
pythonFilePath,
pluginSettings.PythonFilePath,
pythonEnvDirPath,
languageType);
return SetPathForPluginPairs(pluginSettings.PythonFilePath, languageType);
}
break;
case AllowedLanguage.TypeScript:
case AllowedLanguage.JavaScript:
if (!string.IsNullOrEmpty(pluginSettings.NodeFilePath) && FilesFolders.FileExists(pluginSettings.NodeFilePath))
{
EnsureLatestInstalled(
nodeFilePath,
pluginSettings.NodeFilePath,
nodeEnvDirPath,
languageType);
return SetPathForPluginPairs(pluginSettings.NodeFilePath, languageType);
}
break;
default:
break;
}
if (MessageBox.Show($"Flow detected you have installed {languageType} plugins, which " +
$"will require {environment} to run. Would you like to download {environment}? " +
Environment.NewLine + Environment.NewLine +
"Click no if it's already installed, " +
$"and you will be prompted to select the folder that contains the {environment} executable",
string.Empty, MessageBoxButtons.YesNo) == DialogResult.No)
{
var msg = $"Please select the {environment} executable";
var selectedFile = string.Empty;
switch (languageType)
{
case AllowedLanguage.Python:
selectedFile = GetFileFromDialog(msg, "Python|pythonw.exe");
if (!string.IsNullOrEmpty(selectedFile))
pluginSettings.PythonFilePath = selectedFile;
break;
case AllowedLanguage.TypeScript:
case AllowedLanguage.JavaScript:
selectedFile = GetFileFromDialog(msg);
if (!string.IsNullOrEmpty(selectedFile))
pluginSettings.NodeFilePath = selectedFile;
break;
default:
break;
}
// Nothing selected because user pressed cancel from the file dialog window
if (string.IsNullOrEmpty(selectedFile))
InstallEnvironment(languageType);
}
else
{
InstallEnvironment(languageType);
}
switch (languageType)
{
case AllowedLanguage.Python when FilesFolders.FileExists(pluginSettings.PythonFilePath):
return SetPathForPluginPairs(pluginSettings.PythonFilePath, languageType);
case AllowedLanguage.TypeScript when FilesFolders.FileExists(pluginSettings.NodeFilePath):
case AllowedLanguage.JavaScript when FilesFolders.FileExists(pluginSettings.NodeFilePath):
return SetPathForPluginPairs(pluginSettings.NodeFilePath, languageType);
default:
MessageBox.Show(
"Unable to set Python executable path, please try from Flow's settings (scroll down to the bottom).");
Log.Error("PluginsLoader",
$"Not able to successfully set Python path, setting's PythonFilePath variable is still an empty string.",
"PluginEnvironment");
return new List<PluginPair>();
}
}
private void InstallEnvironment(string languageType)
{
switch (languageType)
{
case AllowedLanguage.Python:
FilesFolders.RemoveFolderIfExists(pythonDirPath);
// Python 3.8.9 is used for Windows 7 compatibility
DroplexPackage.Drop(App.python_3_8_9_embeddable, pythonDirPath).Wait();
pluginSettings.PythonFilePath = pythonFilePath;
break;
case AllowedLanguage.TypeScript:
case AllowedLanguage.JavaScript:
FilesFolders.RemoveFolderIfExists(nodeDirPath);
DroplexPackage.Drop(App.nodejs_16_18_0, nodeDirPath).Wait();
pluginSettings.NodeFilePath = nodeFilePath;
break;
default:
break;
}
}
private void EnsureLatestInstalled(string expectedPath, string currentPath, string installedDirPath, string languagType)
{
if (expectedPath == currentPath)
return;
FilesFolders.RemoveFolderIfExists(installedDirPath);
InstallEnvironment(languagType);
}
private IEnumerable<PluginPair> SetPathForPluginPairs(string filePath, string languageToSet)
{
var pluginPairs = new List<PluginPair>();
foreach (var metadata in pluginMetadataList) {
switch (languageToSet)
{
case AllowedLanguage.Python when metadata.Language.Equals(languageToSet, StringComparison.OrdinalIgnoreCase):
pluginPairs.Add(new PluginPair
{
Plugin = new PythonPlugin(filePath),
Metadata = metadata
});
break;
case AllowedLanguage.TypeScript when metadata.Language.Equals(languageToSet, StringComparison.OrdinalIgnoreCase):
pluginPairs.Add(new PluginPair
{
Plugin = new NodePlugin(filePath),
Metadata = metadata
});
break;
case AllowedLanguage.JavaScript when metadata.Language.Equals(languageToSet, StringComparison.OrdinalIgnoreCase):
pluginPairs.Add(new PluginPair
{
Plugin = new NodePlugin(filePath),
Metadata = metadata
});
break;
default:
break;
}
}
return pluginPairs;
}
public static string GetFileFromDialog(string title, string filter="")
{
var dlg = new OpenFileDialog
{
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
Multiselect = false,
CheckFileExists = true,
CheckPathExists = true,
Title = title,
Filter = filter
};
var result = dlg.ShowDialog();
if (result == DialogResult.OK)
{
return dlg.FileName;
}
else
{
return string.Empty;
}
}
}
}

View file

@ -4,7 +4,7 @@ using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
using Flow.Launcher.Core.ExternalPlugins;
using Flow.Launcher.Core.ExternalPlugins.Environments;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
@ -14,16 +14,16 @@ namespace Flow.Launcher.Core.Plugin
{
public static class PluginsLoader
{
public const string PythonExecutable = "pythonw.exe";
public static List<PluginPair> Plugins(List<PluginMetadata> metadatas, PluginsSettings settings)
{
var dotnetPlugins = DotNetPlugins(metadatas);
var pluginEnv = new PluginEnvironment(metadatas, settings);
var pythonPlugins = pluginEnv.PythonSetup();
var tsPlugins = pluginEnv.TypeScriptSetup();
var jsPlugins = pluginEnv.JavaScriptSetup();
var pythonEnv = new PythonEnvironment(metadatas, settings);
var tsEnv = new TypeScriptEnvironment(metadatas, settings);
var jsEnv = new JavaScriptEnvironment(metadatas, settings);
var pythonPlugins = pythonEnv.Setup();
var tsPlugins = tsEnv.Setup();
var jsPlugins = jsEnv.Setup();
var executablePlugins = ExecutablePlugins(metadatas);

View file

@ -5,23 +5,23 @@ namespace Flow.Launcher.Infrastructure.UserSettings
{
public class PluginsSettings : BaseModel
{
private string pythonFilePath;
public string PythonFilePath {
get { return pythonFilePath; }
private string pythonExecutablePath;
public string PythonExecutablePath {
get { return pythonExecutablePath; }
set
{
pythonFilePath = value;
pythonExecutablePath = value;
Constant.PythonPath = value;
}
}
private string nodeFilePath;
public string NodeFilePath
private string nodeExecutablePath;
public string NodeExecutablePath
{
get { return nodeFilePath; }
get { return nodeExecutablePath; }
set
{
nodeFilePath = value;
nodeExecutablePath = value;
Constant.NodePath = value;
}
}

View file

@ -944,11 +944,11 @@
<TextBox
Width="300"
Height="34"
Text="{Binding Settings.PluginSettings.PythonFilePath, TargetNullValue='None'}" />
Text="{Binding Settings.PluginSettings.PythonExecutablePath, TargetNullValue='None'}" />
<Button
Height="34"
Margin="10,10,18,10"
Click="OnSelectPythonFilePathClick"
Click="OnSelectPythonPathClick"
Content="{DynamicResource select}" />
</StackPanel>
</ItemsControl>
@ -963,11 +963,11 @@
<TextBox
Width="300"
Height="34"
Text="{Binding Settings.PluginSettings.NodeFilePath, TargetNullValue='None'}" />
Text="{Binding Settings.PluginSettings.NodeExecutablePath, TargetNullValue='None'}" />
<Button
Height="34"
Margin="10,10,18,10"
Click="OnSelectNodeFilePathClick"
Click="OnSelectNodePathClick"
Content="{DynamicResource select}" />
</StackPanel>
</ItemsControl>

View file

@ -10,25 +10,20 @@ using Flow.Launcher.ViewModel;
using ModernWpf;
using ModernWpf.Controls;
using System;
using System.Drawing.Printing;
using System.IO;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Navigation;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using Button = System.Windows.Controls.Button;
using Control = System.Windows.Controls.Control;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MessageBox = System.Windows.MessageBox;
using TextBox = System.Windows.Controls.TextBox;
using ThemeManager = ModernWpf.ThemeManager;
using OpenFileDialog = System.Windows.Forms.OpenFileDialog;
using Flow.Launcher.Core.ExternalPlugins;
namespace Flow.Launcher
{
@ -69,23 +64,23 @@ namespace Flow.Launcher
ClockDisplay();
}
private void OnSelectPythonFilePathClick(object sender, RoutedEventArgs e)
private void OnSelectPythonPathClick(object sender, RoutedEventArgs e)
{
var selectedFile = PluginEnvironment.GetFileFromDialog(
var selectedFile = viewModel.GetFileFromDialog(
InternationalizationManager.Instance.GetTranslation("selectPythonExecutable"),
"Python|pythonw.exe");
if (!string.IsNullOrEmpty(selectedFile))
settings.PluginSettings.PythonFilePath = selectedFile;
settings.PluginSettings.PythonExecutablePath = selectedFile;
}
private void OnSelectNodeFilePathClick(object sender, RoutedEventArgs e)
private void OnSelectNodePathClick(object sender, RoutedEventArgs e)
{
var selectedFile = PluginEnvironment.GetFileFromDialog(
var selectedFile = viewModel.GetFileFromDialog(
InternationalizationManager.Instance.GetTranslation("selectNodeExecutable"));
if (!string.IsNullOrEmpty(selectedFile))
settings.PluginSettings.NodeFilePath = selectedFile;
settings.PluginSettings.NodeExecutablePath = selectedFile;
}
private void OnSelectFileManagerClick(object sender, RoutedEventArgs e)

View file

@ -127,6 +127,29 @@ namespace Flow.Launcher.ViewModel
_storage.Save();
}
public string GetFileFromDialog(string title, string filter = "")
{
var dlg = new System.Windows.Forms.OpenFileDialog
{
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
Multiselect = false,
CheckFileExists = true,
CheckPathExists = true,
Title = title,
Filter = filter
};
var result = dlg.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
return dlg.FileName;
}
else
{
return string.Empty;
}
}
#region general
// todo a better name?