|  | 
|  | 1 | +using Flow.Launcher.Infrastructure.Logger; | 
|  | 2 | +using Flow.Launcher.Infrastructure.UserSettings; | 
|  | 3 | +using Flow.Launcher.Plugin; | 
|  | 4 | +using Flow.Launcher.Plugin.SharedCommands; | 
|  | 5 | +using System; | 
|  | 6 | +using System.Collections.Generic; | 
|  | 7 | +using System.IO; | 
|  | 8 | +using System.Linq; | 
|  | 9 | +using System.Windows.Forms; | 
|  | 10 | + | 
|  | 11 | +namespace Flow.Launcher.Core.ExternalPlugins.Environments | 
|  | 12 | +{ | 
|  | 13 | +    public abstract class AbstractPluginEnvironment | 
|  | 14 | +    { | 
|  | 15 | +        internal abstract string Language { get; } | 
|  | 16 | + | 
|  | 17 | +        internal abstract string EnvName { get; } | 
|  | 18 | + | 
|  | 19 | +        internal abstract string EnvPath { get; } | 
|  | 20 | + | 
|  | 21 | +        internal abstract string InstallPath { get; } | 
|  | 22 | + | 
|  | 23 | +        internal abstract string ExecutablePath { get; } | 
|  | 24 | + | 
|  | 25 | +        internal virtual string FileDialogFilter => string.Empty; | 
|  | 26 | + | 
|  | 27 | +        internal  abstract string PluginsSettingsFilePath { get; set; } | 
|  | 28 | + | 
|  | 29 | +        internal List<PluginMetadata> PluginMetadataList; | 
|  | 30 | + | 
|  | 31 | +        internal PluginsSettings PluginSettings; | 
|  | 32 | + | 
|  | 33 | +        internal AbstractPluginEnvironment(List<PluginMetadata> pluginMetadataList, PluginsSettings pluginSettings) | 
|  | 34 | +        { | 
|  | 35 | +            PluginMetadataList = pluginMetadataList; | 
|  | 36 | +            PluginSettings = pluginSettings; | 
|  | 37 | +        } | 
|  | 38 | + | 
|  | 39 | +        internal IEnumerable<PluginPair> Setup() | 
|  | 40 | +        { | 
|  | 41 | +            if (!PluginMetadataList.Any(o => o.Language.Equals(Language, StringComparison.OrdinalIgnoreCase))) | 
|  | 42 | +                return new List<PluginPair>(); | 
|  | 43 | + | 
|  | 44 | +            // TODO: Remove. This is backwards compatibility for 1.10.0 release- changed PythonEmbeded to Environments/Python | 
|  | 45 | +            if (Language.Equals(AllowedLanguage.Python, StringComparison.OrdinalIgnoreCase)) | 
|  | 46 | +            { | 
|  | 47 | +                FilesFolders.RemoveFolderIfExists(Path.Combine(DataLocation.DataDirectory(), "PythonEmbeddable")); | 
|  | 48 | + | 
|  | 49 | +                if (!string.IsNullOrEmpty(PluginSettings.PythonDirectory) && PluginSettings.PythonDirectory.StartsWith(Path.Combine(DataLocation.DataDirectory(), "PythonEmbeddable"))) | 
|  | 50 | +                { | 
|  | 51 | +                    InstallEnvironment(); | 
|  | 52 | +                    PluginSettings.PythonDirectory = string.Empty; | 
|  | 53 | +                } | 
|  | 54 | +            } | 
|  | 55 | + | 
|  | 56 | +            if (!string.IsNullOrEmpty(PluginsSettingsFilePath) && FilesFolders.FileExists(PluginsSettingsFilePath)) | 
|  | 57 | +            { | 
|  | 58 | +                // Ensure latest only if user is using Flow's environment setup. | 
|  | 59 | +                if (PluginsSettingsFilePath.StartsWith(EnvPath, StringComparison.OrdinalIgnoreCase)) | 
|  | 60 | +                    EnsureLatestInstalled(ExecutablePath, PluginsSettingsFilePath, EnvPath); | 
|  | 61 | + | 
|  | 62 | +                return SetPathForPluginPairs(PluginsSettingsFilePath, Language); | 
|  | 63 | +            } | 
|  | 64 | + | 
|  | 65 | +            if (MessageBox.Show($"Flow detected you have installed {Language} plugins, which " + | 
|  | 66 | +                                $"will require {EnvName} to run. Would you like to download {EnvName}? " + | 
|  | 67 | +                                Environment.NewLine + Environment.NewLine + | 
|  | 68 | +                                "Click no if it's already installed, " + | 
|  | 69 | +                                $"and you will be prompted to select the folder that contains the {EnvName} executable", | 
|  | 70 | +                    string.Empty, MessageBoxButtons.YesNo) == DialogResult.No) | 
|  | 71 | +            { | 
|  | 72 | +                var msg = $"Please select the {EnvName} executable"; | 
|  | 73 | +                var selectedFile = string.Empty; | 
|  | 74 | + | 
|  | 75 | +                selectedFile = GetFileFromDialog(msg, FileDialogFilter); | 
|  | 76 | + | 
|  | 77 | +                if (!string.IsNullOrEmpty(selectedFile)) | 
|  | 78 | +                    PluginsSettingsFilePath = selectedFile; | 
|  | 79 | + | 
|  | 80 | +                // Nothing selected because user pressed cancel from the file dialog window | 
|  | 81 | +                if (string.IsNullOrEmpty(selectedFile)) | 
|  | 82 | +                    InstallEnvironment(); | 
|  | 83 | +            } | 
|  | 84 | +            else | 
|  | 85 | +            { | 
|  | 86 | +                InstallEnvironment(); | 
|  | 87 | +            } | 
|  | 88 | + | 
|  | 89 | +            if (FilesFolders.FileExists(PluginsSettingsFilePath)) | 
|  | 90 | +            { | 
|  | 91 | +                return SetPathForPluginPairs(PluginsSettingsFilePath, Language); | 
|  | 92 | +            } | 
|  | 93 | +            else | 
|  | 94 | +            { | 
|  | 95 | +                MessageBox.Show( | 
|  | 96 | +                    $"Unable to set {Language} executable path, please try from Flow's settings (scroll down to the bottom)."); | 
|  | 97 | +                Log.Error("PluginsLoader", | 
|  | 98 | +                    $"Not able to successfully set {EnvName} path, setting's plugin executable path variable is still an empty string.", | 
|  | 99 | +                    $"{Language}Environment"); | 
|  | 100 | + | 
|  | 101 | +                return new List<PluginPair>(); | 
|  | 102 | +            } | 
|  | 103 | +        } | 
|  | 104 | + | 
|  | 105 | +        internal abstract void InstallEnvironment(); | 
|  | 106 | + | 
|  | 107 | +        private void EnsureLatestInstalled(string expectedPath, string currentPath, string installedDirPath) | 
|  | 108 | +        { | 
|  | 109 | +            if (expectedPath == currentPath) | 
|  | 110 | +                return; | 
|  | 111 | + | 
|  | 112 | +            FilesFolders.RemoveFolderIfExists(installedDirPath); | 
|  | 113 | + | 
|  | 114 | +            InstallEnvironment(); | 
|  | 115 | + | 
|  | 116 | +        } | 
|  | 117 | + | 
|  | 118 | +        internal abstract PluginPair CreatePluginPair(string filePath, PluginMetadata metadata); | 
|  | 119 | + | 
|  | 120 | +        private IEnumerable<PluginPair> SetPathForPluginPairs(string filePath, string languageToSet) | 
|  | 121 | +        { | 
|  | 122 | +            var pluginPairs = new List<PluginPair>(); | 
|  | 123 | + | 
|  | 124 | +            foreach (var metadata in PluginMetadataList) | 
|  | 125 | +            { | 
|  | 126 | +                if (metadata.Language.Equals(languageToSet, StringComparison.OrdinalIgnoreCase)) | 
|  | 127 | +                    pluginPairs.Add(CreatePluginPair(filePath, metadata)); | 
|  | 128 | +            } | 
|  | 129 | + | 
|  | 130 | +            return pluginPairs; | 
|  | 131 | +        } | 
|  | 132 | + | 
|  | 133 | +        private string GetFileFromDialog(string title, string filter = "") | 
|  | 134 | +        { | 
|  | 135 | +            var dlg = new OpenFileDialog | 
|  | 136 | +            { | 
|  | 137 | +                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), | 
|  | 138 | +                Multiselect = false, | 
|  | 139 | +                CheckFileExists = true, | 
|  | 140 | +                CheckPathExists = true, | 
|  | 141 | +                Title = title, | 
|  | 142 | +                Filter = filter | 
|  | 143 | +            }; | 
|  | 144 | + | 
|  | 145 | +            var result = dlg.ShowDialog(); | 
|  | 146 | +            if (result == DialogResult.OK) | 
|  | 147 | +            { | 
|  | 148 | +                return dlg.FileName; | 
|  | 149 | +            } | 
|  | 150 | +            else | 
|  | 151 | +            { | 
|  | 152 | +                return string.Empty; | 
|  | 153 | +            } | 
|  | 154 | +        } | 
|  | 155 | + | 
|  | 156 | +        /// <summary> | 
|  | 157 | +        /// After app updated while in portable mode or switched between portable/roaming mode, | 
|  | 158 | +        /// need to update each plugin's executable path so user will not be prompted again to reinstall the environments. | 
|  | 159 | +        /// </summary> | 
|  | 160 | +        /// <param name="settings"></param> | 
|  | 161 | +        public static void PreStartPluginExecutablePathUpdate(Settings settings) | 
|  | 162 | +        { | 
|  | 163 | +            if (DataLocation.PortableDataLocationInUse()) | 
|  | 164 | +            { | 
|  | 165 | +                // When user is using portable but has moved flow to a different location | 
|  | 166 | +                if (IsUsingPortablePath(settings.PluginSettings.PythonExecutablePath, DataLocation.PythonEnvironmentName) | 
|  | 167 | +                    && !settings.PluginSettings.PythonExecutablePath.StartsWith(DataLocation.PortableDataPath)) | 
|  | 168 | +                { | 
|  | 169 | +                    settings.PluginSettings.PythonExecutablePath | 
|  | 170 | +                        = GetUpdatedEnvironmentPath(settings.PluginSettings.PythonExecutablePath); | 
|  | 171 | +                } | 
|  | 172 | + | 
|  | 173 | +                if (IsUsingPortablePath(settings.PluginSettings.NodeExecutablePath, DataLocation.NodeEnvironmentName) | 
|  | 174 | +                    && !settings.PluginSettings.NodeExecutablePath.StartsWith(DataLocation.PortableDataPath)) | 
|  | 175 | +                { | 
|  | 176 | +                    settings.PluginSettings.NodeExecutablePath | 
|  | 177 | +                        = GetUpdatedEnvironmentPath(settings.PluginSettings.NodeExecutablePath); | 
|  | 178 | +                } | 
|  | 179 | + | 
|  | 180 | +                // When user has switched from roaming to portable | 
|  | 181 | +                if (IsUsingRoamingPath(settings.PluginSettings.PythonExecutablePath)) | 
|  | 182 | +                { | 
|  | 183 | +                    settings.PluginSettings.PythonExecutablePath | 
|  | 184 | +                        = settings.PluginSettings.PythonExecutablePath.Replace(DataLocation.RoamingDataPath, DataLocation.PortableDataPath); | 
|  | 185 | +                } | 
|  | 186 | + | 
|  | 187 | +                if (IsUsingRoamingPath(settings.PluginSettings.NodeExecutablePath)) | 
|  | 188 | +                { | 
|  | 189 | +                    settings.PluginSettings.NodeExecutablePath | 
|  | 190 | +                        = settings.PluginSettings.NodeExecutablePath.Replace(DataLocation.RoamingDataPath, DataLocation.PortableDataPath); | 
|  | 191 | +                } | 
|  | 192 | +            } | 
|  | 193 | +            else | 
|  | 194 | +            { | 
|  | 195 | +                if (IsUsingPortablePath(settings.PluginSettings.PythonExecutablePath, DataLocation.PythonEnvironmentName)) | 
|  | 196 | +                    settings.PluginSettings.PythonExecutablePath | 
|  | 197 | +                        = GetUpdatedEnvironmentPath(settings.PluginSettings.PythonExecutablePath); | 
|  | 198 | + | 
|  | 199 | +                if (IsUsingPortablePath(settings.PluginSettings.NodeExecutablePath, DataLocation.NodeEnvironmentName)) | 
|  | 200 | +                    settings.PluginSettings.NodeExecutablePath | 
|  | 201 | +                        = GetUpdatedEnvironmentPath(settings.PluginSettings.NodeExecutablePath); | 
|  | 202 | +            } | 
|  | 203 | +        } | 
|  | 204 | + | 
|  | 205 | +        private static bool IsUsingPortablePath(string filePath, string pluginEnvironmentName) | 
|  | 206 | +        { | 
|  | 207 | +            if (string.IsNullOrEmpty(filePath)) | 
|  | 208 | +                return false; | 
|  | 209 | + | 
|  | 210 | +            // DataLocation.PortableDataPath returns the current portable path, this determines if an out | 
|  | 211 | +            // of date path is also a portable path. | 
|  | 212 | +            var portableAppEnvLocation = $"UserData\\{DataLocation.PluginEnvironments}\\{pluginEnvironmentName}"; | 
|  | 213 | + | 
|  | 214 | +            return filePath.Contains(portableAppEnvLocation); | 
|  | 215 | +        } | 
|  | 216 | + | 
|  | 217 | +        private static bool IsUsingRoamingPath(string filePath) | 
|  | 218 | +        { | 
|  | 219 | +            if (string.IsNullOrEmpty(filePath)) | 
|  | 220 | +                return false; | 
|  | 221 | + | 
|  | 222 | +            return filePath.StartsWith(DataLocation.RoamingDataPath); | 
|  | 223 | +        } | 
|  | 224 | + | 
|  | 225 | +        private static string GetUpdatedEnvironmentPath(string filePath) | 
|  | 226 | +        { | 
|  | 227 | +            var index = filePath.IndexOf(DataLocation.PluginEnvironments); | 
|  | 228 | +             | 
|  | 229 | +            // get the substring after "Environments" because we can not determine it dynamically | 
|  | 230 | +            var ExecutablePathSubstring = filePath.Substring(index + DataLocation.PluginEnvironments.Count()); | 
|  | 231 | +            return $"{DataLocation.PluginEnvironmentsPath}{ExecutablePathSubstring}"; | 
|  | 232 | +        } | 
|  | 233 | +    } | 
|  | 234 | +} | 
0 commit comments