Skip to content

Commit

Permalink
refactor codes and resolves duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
taooceros committed Dec 30, 2022
1 parent 56f50d7 commit b2eedf3
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 136 deletions.
20 changes: 10 additions & 10 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public class Main : IPlugin, IPluginI18n, ISettingProvider

public string Description => GetTranslatedPluginDescription();

private VSCodeInstance defaultInstalce = null;
private VSCodeInstance defaultInstalce;

private readonly VSCodeWorkspacesApi _workspacesApi = new VSCodeWorkspacesApi();
private readonly VSCodeWorkspacesApi _workspacesApi = new();

private readonly VSCodeRemoteMachinesApi _machinesApi = new VSCodeRemoteMachinesApi();
private readonly VSCodeRemoteMachinesApi _machinesApi = new();

public List<Result> Query(Query query)
{
Expand All @@ -51,9 +51,9 @@ public List<Result> Query(Query query)
}

// Simple de-duplication
results
.AddRange(workspaces.GroupBy(x => new { Path = Uri.UnescapeDataString(x.Path).ToLower(), x.VSCodeInstance })
.Select(y => CreateWorkspaceResult(y.First())));
results.AddRange(workspaces.Distinct()
.Select(CreateWorkspaceResult)
);

// Search opened remote machines
if (_settings.DiscoverMachines)
Expand All @@ -62,7 +62,7 @@ public List<Result> Query(Query query)
{
var title = $"{a.Host}";

if (a.User != null && a.User != string.Empty && a.HostName != null && a.HostName != string.Empty)
if (!string.IsNullOrEmpty(a.User) && !string.IsNullOrEmpty(a.HostName))
{
title += $" [{a.User}@{a.HostName}]";
}
Expand Down Expand Up @@ -119,7 +119,7 @@ public List<Result> Query(Query query)
return results;
}

public Result CreateWorkspaceResult(VSCodeWorkspace ws)
private Result CreateWorkspaceResult(VSCodeWorkspace ws)
{
var title = $"{ws.FolderName}";
var typeWorkspace = ws.WorkspaceTypeToString();
Expand Down Expand Up @@ -156,7 +156,7 @@ public Result CreateWorkspaceResult(VSCodeWorkspace ws)
catch (Win32Exception)
{
var name = $"Plugin: {_context.CurrentPluginMetadata.Name}";
var msg = "Can't Open this file";
const string msg = "Can't Open this file";
_context.API.ShowMsg(name, msg, string.Empty);
}
return false;
Expand All @@ -174,7 +174,7 @@ public void Init(PluginInitContext context)

// Prefer stable version, or the first one we got
defaultInstalce = VSCodeInstances.Instances.Find(e => e.VSCodeVersion == VSCodeVersion.Stable) ??
VSCodeInstances.Instances.FirstOrDefault();
VSCodeInstances.Instances.FirstOrDefault();
}

public Control CreateSettingPanel() => new SettingsView(_context, _settings);
Expand Down
25 changes: 24 additions & 1 deletion RemoteMachinesHelper/VSCodeRemoteMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Flow.Plugin.VSCodeWorkspaces.VSCodeHelper;

namespace Flow.Plugin.VSCodeWorkspaces.RemoteMachinesHelper
{
public class VSCodeRemoteMachine
public class VSCodeRemoteMachine : IEquatable<VSCodeRemoteMachine>
{
public string Host { get; set; }

Expand All @@ -15,5 +16,27 @@ public class VSCodeRemoteMachine
public string HostName { get; set; }

public VSCodeInstance VSCodeInstance { get; set; }
public bool Equals(VSCodeRemoteMachine other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return Host == other.Host && User == other.User && HostName == other.HostName && Equals(VSCodeInstance, other.VSCodeInstance);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj.GetType() != this.GetType())
return false;
return Equals((VSCodeRemoteMachine)obj);
}
public override int GetHashCode()
{
return HashCode.Combine(Host, User, HostName, VSCodeInstance);
}
}
}
28 changes: 27 additions & 1 deletion VSCodeHelper/VSCodeInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Windows.Media;
using System.Windows.Media.Imaging;

Expand All @@ -14,7 +15,7 @@ public enum VSCodeVersion
Exploration = 3,
}

public class VSCodeInstance
public class VSCodeInstance : IEquatable<VSCodeInstance>
{
public VSCodeVersion VSCodeVersion { get; set; }

Expand All @@ -29,5 +30,30 @@ public class VSCodeInstance
public BitmapImage WorkspaceIconBitMap { get; set; }

public BitmapImage RemoteIconBitMap { get; set; }
public bool Equals(VSCodeInstance other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return VSCodeVersion == other.VSCodeVersion
&& string.Equals(ExecutablePath, other.ExecutablePath, StringComparison.InvariantCultureIgnoreCase)
&& string.Equals(AppData, other.AppData, StringComparison.InvariantCultureIgnoreCase);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;

return obj is VSCodeInstance instance && Equals(instance);
}
public override int GetHashCode()
{
return HashCode.Combine((int)VSCodeVersion,
ExecutablePath.GetHashCode(StringComparison.InvariantCultureIgnoreCase),
AppData.GetHashCode(StringComparison.InvariantCultureIgnoreCase));
}
}
}
139 changes: 70 additions & 69 deletions VSCodeHelper/VSCodeInstances.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public static class VSCodeInstances
{
private static string _systemPath = string.Empty;

private static string _userAppDataPath = Environment.GetEnvironmentVariable("AppData");
private static readonly string _userAppDataPath = Environment.GetEnvironmentVariable("AppData");

public static List<VSCodeInstance> Instances { get; set; } = new List<VSCodeInstance>();
public static List<VSCodeInstance> Instances { get; set; } = new();

private static BitmapImage Bitmap2BitmapImage(Bitmap bitmap)
{
Expand All @@ -39,7 +39,7 @@ private static BitmapImage Bitmap2BitmapImage(Bitmap bitmap)
}
}

public static Bitmap BitmapOverlayToCenter(Bitmap bitmap1, Bitmap overlayBitmap)
private static Bitmap BitmapOverlayToCenter(Bitmap bitmap1, Bitmap overlayBitmap)
{
int bitmap1Width = bitmap1.Width;
int bitmap1Height = bitmap1.Height;
Expand All @@ -62,75 +62,76 @@ public static Bitmap BitmapOverlayToCenter(Bitmap bitmap1, Bitmap overlayBitmap)
// Gets the executablePath and AppData foreach instance of VSCode
public static void LoadVSCodeInstances()
{
if (_systemPath != Environment.GetEnvironmentVariable("PATH"))
if (_systemPath == Environment.GetEnvironmentVariable("PATH"))
return;


Instances = new List<VSCodeInstance>();

_systemPath = Environment.GetEnvironmentVariable("PATH") ?? "";
var paths = _systemPath.Split(";").Where(x =>
x.Contains("VS Code", StringComparison.OrdinalIgnoreCase) ||
x.Contains("VSCodium", StringComparison.OrdinalIgnoreCase) ||
x.Contains("vscode", StringComparison.OrdinalIgnoreCase));
foreach (var path in paths)
{
Instances = new List<VSCodeInstance>();

_systemPath = Environment.GetEnvironmentVariable("PATH") ?? "";
var paths = _systemPath.Split(";").Where(x =>
x.Contains("VS Code", StringComparison.OrdinalIgnoreCase) ||
x.Contains("VSCodium", StringComparison.OrdinalIgnoreCase) ||
x.Contains("vscode", StringComparison.OrdinalIgnoreCase));
foreach (var path in paths)
if (!Directory.Exists(path))
continue;

var files = Directory.EnumerateFiles(path).Where(x =>
(x.Contains("code", StringComparison.OrdinalIgnoreCase) ||
x.Contains("VSCodium", StringComparison.OrdinalIgnoreCase))
&& !x.EndsWith(".cmd", StringComparison.OrdinalIgnoreCase)).ToArray();

var iconPath = Path.GetDirectoryName(path);

if (files.Length <= 0)
continue;

var file = files[0];
var version = string.Empty;

var instance = new VSCodeInstance
{
if (Directory.Exists(path))
{
var files = Directory.GetFiles(path);
var iconPath = Path.GetDirectoryName(path);
files = files.Where(x =>
(x.Contains("code", StringComparison.OrdinalIgnoreCase) ||
x.Contains("VSCodium", StringComparison.OrdinalIgnoreCase))
&& !x.EndsWith(".cmd", StringComparison.OrdinalIgnoreCase)).ToArray();


if (files.Length > 0)
{
var file = files[0];
var version = string.Empty;

var instance = new VSCodeInstance
{
ExecutablePath = file,
};

if (file.EndsWith("code"))
{
version = "Code";
instance.VSCodeVersion = VSCodeVersion.Stable;
}
else if (file.EndsWith("code-insiders"))
{
version = "Code - Insiders";
instance.VSCodeVersion = VSCodeVersion.Insiders;
}
else if (file.EndsWith("code-exploration"))
{
version = "Code - Exploration";
instance.VSCodeVersion = VSCodeVersion.Exploration;
}

if (version != string.Empty)
{
var portableData = Path.Join(iconPath, "data");
instance.AppData = Directory.Exists(portableData) ? Path.Join(portableData, "user-data") : Path.Combine(_userAppDataPath, version);
var iconVSCode = Path.Join(iconPath, $"{version}.exe");

var bitmapIconVscode = Icon.ExtractAssociatedIcon(iconVSCode)?.ToBitmap();

// workspace
var folderIcon = (Bitmap)Image.FromFile(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "//Images//folder.png");
instance.WorkspaceIconBitMap = Bitmap2BitmapImage(BitmapOverlayToCenter(folderIcon, bitmapIconVscode));

// remote
var monitorIcon = (Bitmap)Image.FromFile(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "//Images//monitor.png");

instance.RemoteIconBitMap = Bitmap2BitmapImage(BitmapOverlayToCenter(monitorIcon, bitmapIconVscode));

Instances.Add(instance);
}
}
}
ExecutablePath = file,
};

if (file.EndsWith("code"))
{
version = "Code";
instance.VSCodeVersion = VSCodeVersion.Stable;
}
else if (file.EndsWith("code-insiders"))
{
version = "Code - Insiders";
instance.VSCodeVersion = VSCodeVersion.Insiders;
}
else if (file.EndsWith("code-exploration"))
{
version = "Code - Exploration";
instance.VSCodeVersion = VSCodeVersion.Exploration;
}

if (version == string.Empty)
continue;


var portableData = Path.Join(iconPath, "data");
instance.AppData = Directory.Exists(portableData) ? Path.Join(portableData, "user-data") : Path.Combine(_userAppDataPath, version);
var iconVSCode = Path.Join(iconPath, $"{version}.exe");

var bitmapIconVscode = Icon.ExtractAssociatedIcon(iconVSCode)?.ToBitmap();

// workspace
var folderIcon = (Bitmap)Image.FromFile(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "//Images//folder.png");
instance.WorkspaceIconBitMap = Bitmap2BitmapImage(BitmapOverlayToCenter(folderIcon, bitmapIconVscode));

// remote
var monitorIcon = (Bitmap)Image.FromFile(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "//Images//monitor.png");

instance.RemoteIconBitMap = Bitmap2BitmapImage(BitmapOverlayToCenter(monitorIcon, bitmapIconVscode));

Instances.Add(instance);
}
}
}
Expand Down
35 changes: 18 additions & 17 deletions WorkspacesHelper/VSCodeWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,39 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Flow.Plugin.VSCodeWorkspaces.Properties;
using Flow.Plugin.VSCodeWorkspaces.VSCodeHelper;

namespace Flow.Plugin.VSCodeWorkspaces.WorkspacesHelper
{
public class VSCodeWorkspace
public record VSCodeWorkspace
{
public string Path { get; set; }
public string Path { get; init; }

public string RelativePath { get; set; }
public string RelativePath { get; init; }

public string FolderName { get; set; }
public string FolderName { get; init; }

public string ExtraInfo { get; set; }
public string ExtraInfo { get; init; }

public TypeWorkspace TypeWorkspace { get; set; }
public TypeWorkspace TypeWorkspace { get; init; }

public VSCodeInstance VSCodeInstance { get; set; }
public VSCodeInstance VSCodeInstance { get; init; }

public string WorkspaceTypeToString()
{
switch (TypeWorkspace)
return TypeWorkspace switch
{
case TypeWorkspace.Local: return Resources.TypeWorkspaceLocal;
case TypeWorkspace.Codespaces: return "Codespaces";
case TypeWorkspace.RemoteContainers: return Resources.TypeWorkspaceContainer;
case TypeWorkspace.RemoteSSH: return "SSH";
case TypeWorkspace.RemoteWSL: return "WSL";
case TypeWorkspace.DevContainer: return Resources.TypeWorkspaceDevContainer;
}

return string.Empty;
TypeWorkspace.Local => Resources.TypeWorkspaceLocal,
TypeWorkspace.Codespaces => "Codespaces",
TypeWorkspace.RemoteContainers => Resources.TypeWorkspaceContainer,
TypeWorkspace.RemoteSSH => "SSH",
TypeWorkspace.RemoteWSL => "WSL",
TypeWorkspace.DevContainer => Resources.TypeWorkspaceDevContainer,
_ => string.Empty
};

}
}

Expand Down
Loading

0 comments on commit b2eedf3

Please sign in to comment.