From 206a118a15a83f2089c5fe1c6cba0f4e7ce0d39a Mon Sep 17 00:00:00 2001 From: KeithClinard Date: Wed, 22 Dec 2021 11:19:04 -0500 Subject: [PATCH] feat: Support hybrid WSL and Windows --- Main.cs | 34 +++++--- Models/GithubApi.cs | 4 +- Models/SettingsModel.cs | 11 ++- Plugins/Ember.cs | 36 ++++---- Plugins/Github.cs | 32 +++---- Plugins/Settings.cs | 183 +++++++++++++++++++++++----------------- Plugins/VSCode.cs | 82 ++++++++++-------- 7 files changed, 215 insertions(+), 167 deletions(-) diff --git a/Main.cs b/Main.cs index e270b5e..38794bd 100644 --- a/Main.cs +++ b/Main.cs @@ -11,7 +11,8 @@ public class Main : IPlugin private PluginInitContext context; const string ico = "Prompt.png"; const string defaultGitFolder = "C:\\git"; - Exception startupException = null; + const string defaultWslGitFolder = "/git"; + private readonly Exception startupException = null; private readonly SettingsModel settings; private readonly PluginJsonStorage storage; @@ -22,9 +23,14 @@ public Main() { storage = new PluginJsonStorage(); settings = storage.Load(); - if (String.IsNullOrEmpty(settings.gitFolder)) + if (string.IsNullOrEmpty(settings.GitFolder)) { - settings.gitFolder = defaultGitFolder; + settings.GitFolder = defaultGitFolder; + storage.Save(); + } + if (string.IsNullOrEmpty(settings.WslGitFolder)) + { + settings.WslGitFolder = defaultWslGitFolder; storage.Save(); } } @@ -49,8 +55,8 @@ public List Query(Query query) list.Add(new Result { Title = "Devbox Plugin", - SubTitle = "Error during initialization: " + startupException.Message, - IcoPath = ico + SubTitle = "Error during initialization: " + startupException.Message, + IcoPath = ico }); } else if (query.ActionKeyword.Equals("db")) @@ -65,18 +71,18 @@ public List Query(Query query) { return VSCode.Query(query, settings, context); } - else if (String.IsNullOrEmpty(settings.apiToken)) + else if (string.IsNullOrEmpty(settings.ApiToken)) { list.Add(new Result { Title = "Set Github API Token", - SubTitle = "Set this before using this plugin", - Action = (e) => - { - context.API.ChangeQuery("db apiToken "); - return false; - }, - IcoPath = ico + SubTitle = "Set this before using this plugin", + Action = (e) => + { + context.API.ChangeQuery("db apiToken "); + return false; + }, + IcoPath = ico }); } else if (query.ActionKeyword.Equals("gh")) @@ -93,7 +99,7 @@ public List Query(Query query) list.Add(new Result { Title = "Error: " + e.Message, - IcoPath = ico + IcoPath = ico }); } return list; diff --git a/Models/GithubApi.cs b/Models/GithubApi.cs index 92dd5fa..d1f833c 100644 --- a/Models/GithubApi.cs +++ b/Models/GithubApi.cs @@ -16,13 +16,13 @@ public static ApiResult QueryGithub(Query query, SettingsModel settings) | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; - String url = $"https://api.github.com/search/repositories?access_token={settings.apiToken}&q={query.Search}"; + string url = $"https://api.github.com/search/repositories?access_token={settings.ApiToken}&q={query.Search}"; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version10; request.Method = "GET"; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; WebResponse response = request.GetResponse(); - using(Stream stream = response.GetResponseStream()) + using (Stream stream = response.GetResponseStream()) { StreamReader objReader = new StreamReader(stream); var json = objReader.ReadToEnd(); diff --git a/Models/SettingsModel.cs b/Models/SettingsModel.cs index aa62212..efed511 100644 --- a/Models/SettingsModel.cs +++ b/Models/SettingsModel.cs @@ -1,11 +1,10 @@ -using System; - -namespace Wox.Plugin.Devbox.Helpers +namespace Wox.Plugin.Devbox.Helpers { class SettingsModel { - public String apiToken { get; set; } - public String gitFolder { get; set; } - public String wslName { get; set; } + public string ApiToken { get; set; } + public string GitFolder { get; set; } + public string WslGitFolder { get; set; } + public string WslDistroName { get; set; } } } diff --git a/Plugins/Ember.cs b/Plugins/Ember.cs index 68fbda5..dfcb3e6 100644 --- a/Plugins/Ember.cs +++ b/Plugins/Ember.cs @@ -17,7 +17,7 @@ private static List LoadImports(PluginInitContext context) { if (_imports == null) { - using(StreamReader stream = new StreamReader(context.CurrentPluginMetadata.PluginDirectory + "\\mappings.json")) + using (StreamReader stream = new StreamReader(context.CurrentPluginMetadata.PluginDirectory + "\\mappings.json")) { var json = stream.ReadToEnd(); _imports = JsonConvert.DeserializeObject>(json); @@ -37,16 +37,16 @@ public static List Query(Query query, SettingsModel settings, PluginInit list.Add(new Result { Title = "Lookup Ember Import", - SubTitle = "Lookup Ember import strings and copy to clipboard", - IcoPath = ico + SubTitle = "Lookup Ember import strings and copy to clipboard", + IcoPath = ico }); return list; } List results = imports.FindAll(result => { bool found = true; - String[] searchStrings = query.Search.Split(' '); - foreach (String searchSegment in searchStrings) + string[] searchStrings = query.Search.Split(' '); + foreach (string searchSegment in searchStrings) { found = found && result.global.ToLower().Contains(searchSegment.ToLower()); } @@ -57,9 +57,9 @@ public static List Query(Query query, SettingsModel settings, PluginInit { foreach (EmberImportObject item in results) { - String module = item.module; - String export = item.export; - String localName = item.localName; + string module = item.module; + string export = item.export; + string localName = item.localName; if (item.deprecated && item.replacement != null) { module = item.replacement.module; @@ -74,18 +74,18 @@ public static List Query(Query query, SettingsModel settings, PluginInit } } - String importText = export.Equals("default") ? localName : "{ " + export + " }"; - String clipboardText = "import " + importText + " from '" + module + "';"; + string importText = export.Equals("default") ? localName : "{ " + export + " }"; + string clipboardText = "import " + importText + " from '" + module + "';"; list.Add(new Result { Title = item.global, - SubTitle = clipboardText, - IcoPath = ico, - Action = (e) => - { - Clipboard.SetText(clipboardText); - return true; - } + SubTitle = clipboardText, + IcoPath = ico, + Action = (e) => + { + Clipboard.SetText(clipboardText); + return true; + } }); } } @@ -94,7 +94,7 @@ public static List Query(Query query, SettingsModel settings, PluginInit list.Add(new Result { Title = "No Results Found", - IcoPath = ico + IcoPath = ico }); } diff --git a/Plugins/Github.cs b/Plugins/Github.cs index 51ba771..57c4357 100644 --- a/Plugins/Github.cs +++ b/Plugins/Github.cs @@ -9,7 +9,7 @@ static class Github { private static readonly string ico = "Prompt.png"; - private static void openUrl(String url) + private static void OpenUrl(string url) { Process.Start(url); } @@ -23,13 +23,13 @@ public static List Query(Query query, SettingsModel settings, PluginInit list.Add(new Result { Title = "Open Github", - SubTitle = "...or keep typing to search for repositories", - Action = (e) => - { - openUrl("http://github.com/"); - return true; - }, - IcoPath = ico + SubTitle = "...or keep typing to search for repositories", + Action = (e) => + { + OpenUrl("http://github.com/"); + return true; + }, + IcoPath = ico }); return list; } @@ -43,13 +43,13 @@ public static List Query(Query query, SettingsModel settings, PluginInit list.Add(new Result { Title = result.full_name, - SubTitle = result.description, - IcoPath = ico, - Action = (e) => - { - openUrl(result.html_url); - return true; - } + SubTitle = result.description, + IcoPath = ico, + Action = (e) => + { + OpenUrl(result.html_url); + return true; + } }); } } @@ -58,7 +58,7 @@ public static List Query(Query query, SettingsModel settings, PluginInit list.Add(new Result { Title = "No Results Found", - IcoPath = ico + IcoPath = ico }); } diff --git a/Plugins/Settings.cs b/Plugins/Settings.cs index 3f4bc9f..3af50bb 100644 --- a/Plugins/Settings.cs +++ b/Plugins/Settings.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using Wox.Infrastructure.Storage; using Wox.Plugin.Devbox.Helpers; @@ -7,7 +6,6 @@ namespace Wox.Plugin.Devbox.Plugins { static class Settings { - private static readonly string ico = "Prompt.png"; public static List Query(Query query, SettingsModel settings, PluginInitContext context, PluginJsonStorage storage) @@ -19,35 +17,46 @@ public static List Query(Query query, SettingsModel settings, PluginInit list.Add(new Result { Title = "Set Github API Token", - SubTitle = $"Currently: \"{settings.apiToken}\"", - Action = (e) => - { - context.API.ChangeQuery("db apiToken "); - return false; - }, - IcoPath = ico + SubTitle = $"Currently: \"{settings.ApiToken}\"", + Action = (e) => + { + context.API.ChangeQuery("db apiToken "); + return false; + }, + IcoPath = ico }); list.Add(new Result { Title = "Set Git Folder", - SubTitle = $"Currently: \"{settings.gitFolder}\"", - Action = (e) => - { - context.API.ChangeQuery("db gitFolder "); - return false; - }, - IcoPath = ico + SubTitle = $"Currently: \"{settings.GitFolder}\"", + Action = (e) => + { + context.API.ChangeQuery("db gitFolder "); + return false; + }, + IcoPath = ico + }); + list.Add(new Result + { + Title = "Set WSL Git Folder", + SubTitle = $"Currently: \"{settings.WslGitFolder}\"", + Action = (e) => + { + context.API.ChangeQuery("db wslGitFolder "); + return false; + }, + IcoPath = ico }); list.Add(new Result { Title = "Set WSL Distro Name", - SubTitle = $"Causes vscode shortcut to use WSL - Currently: \"{settings.wslName}\"", - Action = (e) => - { - context.API.ChangeQuery("db wslName "); - return false; - }, - IcoPath = ico + SubTitle = $"Currently: \"{settings.WslDistroName}\"", + Action = (e) => + { + context.API.ChangeQuery("db wslDistroName "); + return false; + }, + IcoPath = ico }); // Other sub-plugins @@ -55,44 +64,44 @@ public static List Query(Query query, SettingsModel settings, PluginInit list.Add(new Result { Title = "Open a Repo in Gitub", - SubTitle = "gh", - Action = (e) => - { - context.API.ChangeQuery("gh "); - return false; - }, - IcoPath = "github.png" + SubTitle = "gh", + Action = (e) => + { + context.API.ChangeQuery("gh "); + return false; + }, + IcoPath = "github.png" }); list.Add(new Result { Title = "Open a Repo in VSCode", - SubTitle = "c", - Action = (e) => - { - context.API.ChangeQuery("c "); - return false; - }, - IcoPath = "vscode.png" + SubTitle = "c", + Action = (e) => + { + context.API.ChangeQuery("c "); + return false; + }, + IcoPath = "vscode.png" }); list.Add(new Result { Title = "Search Ember imports and copy to clipboard", - SubTitle = "ember", - Action = (e) => - { - context.API.ChangeQuery("ember "); - return false; - }, - IcoPath = "ember.png" + SubTitle = "ember", + Action = (e) => + { + context.API.ChangeQuery("ember "); + return false; + }, + IcoPath = "ember.png" }); return list; } - String[] searchStrings = query.Search.Split(' '); + string[] searchStrings = query.Search.Split(' '); if ("apiToken".Equals(searchStrings[0])) { - String apiToken = ""; + string apiToken = ""; if (searchStrings.Length > 1) { apiToken = searchStrings[1]; @@ -100,21 +109,21 @@ public static List Query(Query query, SettingsModel settings, PluginInit list.Add(new Result { Title = $"Set Github API Token to \"{apiToken}\"", - SubTitle = $"Currently: \"{settings.apiToken}\"", - Action = (e) => - { - settings.apiToken = apiToken; - storage.Save(); - return true; - }, - IcoPath = ico + SubTitle = $"Currently: \"{settings.ApiToken}\"", + Action = (e) => + { + settings.ApiToken = apiToken; + storage.Save(); + return true; + }, + IcoPath = ico }); return list; } if ("gitFolder".Equals(searchStrings[0])) { - String gitFolder = ""; + string gitFolder = ""; if (searchStrings.Length > 1) { gitFolder = searchStrings[1]; @@ -122,36 +131,58 @@ public static List Query(Query query, SettingsModel settings, PluginInit list.Add(new Result { Title = $"Set git folder to \"{gitFolder}\"", - SubTitle = $"Currently: \"{settings.gitFolder}\"", - Action = (e) => - { - settings.gitFolder = gitFolder; - storage.Save(); - return true; - }, - IcoPath = ico + SubTitle = $"Currently: \"{settings.GitFolder}\"", + Action = (e) => + { + settings.GitFolder = gitFolder; + storage.Save(); + return true; + }, + IcoPath = ico + }); + return list; + } + + if ("wslGitFolder".Equals(searchStrings[0])) + { + string gitFolder = ""; + if (searchStrings.Length > 1) + { + gitFolder = searchStrings[1]; + } + list.Add(new Result + { + Title = $"Set WSL git folder to \"{gitFolder}\"", + SubTitle = $"Currently: \"{settings.WslGitFolder}\"", + Action = (e) => + { + settings.WslGitFolder = gitFolder; + storage.Save(); + return true; + }, + IcoPath = ico }); return list; } - if ("wslName".Equals(searchStrings[0])) + if ("wslDistroName".Equals(searchStrings[0])) { - String wslName = ""; + string distroName = ""; if (searchStrings.Length > 1) { - wslName = searchStrings[1]; + distroName = searchStrings[1]; } list.Add(new Result { - Title = $"Set WSL2 distro to \"{wslName}\"", - SubTitle = $"Currently: \"{settings.wslName}\"", - Action = (e) => - { - settings.wslName = wslName; - storage.Save(); - return true; - }, - IcoPath = ico + Title = $"Set WSL Distro Name to \"{distroName}\"", + SubTitle = $"Currently: \"{settings.WslDistroName}\"", + Action = (e) => + { + settings.WslDistroName = distroName; + storage.Save(); + return true; + }, + IcoPath = ico }); return list; } diff --git a/Plugins/VSCode.cs b/Plugins/VSCode.cs index d1c7642..ef84d54 100644 --- a/Plugins/VSCode.cs +++ b/Plugins/VSCode.cs @@ -10,17 +10,15 @@ static class VSCode { private static readonly string ico = "Prompt.png"; - public static void openResultInVSCode(string result, SettingsModel settings) + public static void OpenVSCode(string folder, Boolean useWsl, SettingsModel settings) { - openVSCode(result, settings); - } - public static void openVSCode(String folder, SettingsModel settings) - { - String command = $"code {folder}"; - if (!string.IsNullOrEmpty(settings.wslName) && !string.IsNullOrEmpty(folder)) + string command = $"code {folder}"; + if (useWsl) { - command = $"code --folder-uri vscode-remote://wsl+{settings.wslName}{folder}"; + string wslFolder = folder.Replace($"\\\\wsl$\\{settings.WslDistroName}", ""); + wslFolder = wslFolder.Replace("\\", "/"); + command = $"code --folder-uri vscode-remote://wsl+{settings.WslDistroName}{wslFolder}"; } ProcessStartInfo info; @@ -45,43 +43,57 @@ public static List Query(Query query, SettingsModel settings, PluginInit list.Add(new Result { Title = "Open VSCode", - SubTitle = "...or keep typing to search for repositories", - Action = (e) => - { - openVSCode("", settings); - return true; - }, - IcoPath = ico + SubTitle = "...or keep typing to search for repositories", + Action = (e) => + { + OpenVSCode("", false, settings); + return true; + }, + IcoPath = ico }); return list; } - - string searchFolder = settings.gitFolder; - if (!string.IsNullOrEmpty(settings.wslName)) + var searchString = query.Search; + string[] splitQuery = searchString.Split(' '); + if (splitQuery.Length > 1) + { + searchString = string.Join("*", splitQuery); + } + string[] wslResults = new string[0]; + if (!string.IsNullOrEmpty(settings.WslDistroName)) { - searchFolder = $"\\\\wsl$\\{settings.wslName}{settings.gitFolder}"; + wslResults = Directory.GetDirectories($"\\\\wsl$\\{settings.WslDistroName}{settings.WslGitFolder}", $"*{searchString}*", SearchOption.TopDirectoryOnly); } - string[] results = Directory.GetDirectories(searchFolder, $"*{query.Search}*", SearchOption.TopDirectoryOnly); + string[] localResults = Directory.GetDirectories(settings.GitFolder, $"*{searchString}*", SearchOption.TopDirectoryOnly); - if (results.Length > 0) + if (wslResults.Length > 0 || localResults.Length > 0) { - foreach (string result in results) + foreach (string result in wslResults) { list.Add(new Result { Title = Path.GetFileName(result), - IcoPath = ico, - Action = (e) => - { - string folderName = result; - if (!string.IsNullOrEmpty(settings.wslName)) - { - folderName = folderName.Replace($"\\\\wsl$\\{settings.wslName}", ""); - folderName = folderName.Replace("\\", "/"); - } - openResultInVSCode(folderName, settings); - return true; - } + SubTitle = "WSL", + IcoPath = ico, + Action = (e) => + { + OpenVSCode(result, true, settings); + return true; + } + }); + } + foreach (string result in localResults) + { + list.Add(new Result + { + Title = Path.GetFileName(result), + SubTitle = "Windows", + IcoPath = ico, + Action = (e) => + { + OpenVSCode(result, false, settings); + return true; + } }); } } @@ -90,7 +102,7 @@ public static List Query(Query query, SettingsModel settings, PluginInit list.Add(new Result { Title = "No Results Found", - IcoPath = ico + IcoPath = ico }); }