Cleanup and NullReferenceException fix (#26956)

This commit is contained in:
Davide Giacometti 2023-06-22 14:28:13 +02:00 committed by GitHub
parent 26bf4193f7
commit 08215a8a77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,9 +15,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper
{ {
public static class VSCodeInstances public static class VSCodeInstances
{ {
private static List<string> _paths = new List<string>(); private static readonly string _userAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
private static string _userAppDataPath = Environment.GetEnvironmentVariable("AppData");
public static List<VSCodeInstance> Instances { get; set; } = new List<VSCodeInstance>(); public static List<VSCodeInstance> Instances { get; set; } = new List<VSCodeInstance>();
@ -44,7 +42,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper
int bitmap1Width = bitmap1.Width; int bitmap1Width = bitmap1.Width;
int bitmap1Height = bitmap1.Height; int bitmap1Height = bitmap1.Height;
Bitmap overlayBitmapResized = new Bitmap(overlayBitmap, new System.Drawing.Size(bitmap1Width / 2, bitmap1Height / 2)); using Bitmap overlayBitmapResized = new Bitmap(overlayBitmap, new System.Drawing.Size(bitmap1Width / 2, bitmap1Height / 2));
float marginLeft = (float)((bitmap1Width * 0.7) - (overlayBitmapResized.Width * 0.5)); float marginLeft = (float)((bitmap1Width * 0.7) - (overlayBitmapResized.Width * 0.5));
float marginTop = (float)((bitmap1Height * 0.7) - (overlayBitmapResized.Height * 0.5)); float marginTop = (float)((bitmap1Height * 0.7) - (overlayBitmapResized.Height * 0.5));
@ -62,84 +60,80 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper
// Gets the executablePath and AppData foreach instance of VSCode // Gets the executablePath and AppData foreach instance of VSCode
public static void LoadVSCodeInstances() public static void LoadVSCodeInstances()
{ {
var environmentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User); var environmentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User) ?? string.Empty;
environmentPath += (environmentPath.Length > 0 && environmentPath.EndsWith(';') ? ";" : string.Empty) + Environment.GetEnvironmentVariable("PATH"); environmentPath += (environmentPath.Length > 0 && environmentPath.EndsWith(';') ? string.Empty : ";") + Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
var paths = environmentPath.Split(";").ToList(); var paths = environmentPath
paths = paths.Distinct().ToList(); .Split(';')
.Distinct()
.Where(x => x.Contains("VS Code", StringComparison.OrdinalIgnoreCase)
|| x.Contains("VSCodium", StringComparison.OrdinalIgnoreCase)
|| x.Contains("vscode", StringComparison.OrdinalIgnoreCase)).ToArray();
var deletedItems = paths.Except(_paths).Any(); foreach (var path in paths)
var newItems = _paths.Except(paths).Any();
if (newItems || deletedItems)
{ {
Instances = new List<VSCodeInstance>(); if (Directory.Exists(path))
paths = paths.Where(x =>
x.Contains("VS Code", StringComparison.OrdinalIgnoreCase) ||
x.Contains("VSCodium", StringComparison.OrdinalIgnoreCase) ||
x.Contains("vscode", StringComparison.OrdinalIgnoreCase)).ToList();
foreach (var path in paths)
{ {
if (Directory.Exists(path)) var files = Directory.GetFiles(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)
{ {
var files = Directory.GetFiles(path); var file = files[0];
var iconPath = Path.GetDirectoryName(path); var version = string.Empty;
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 instance = new VSCodeInstance
{ {
var file = files[0]; ExecutablePath = file,
var version = string.Empty; };
var instance = new VSCodeInstance if (file.EndsWith("code", StringComparison.OrdinalIgnoreCase))
{ {
ExecutablePath = file, version = "Code";
}; instance.VSCodeVersion = VSCodeVersion.Stable;
}
else if (file.EndsWith("code-insiders", StringComparison.OrdinalIgnoreCase))
{
version = "Code - Insiders";
instance.VSCodeVersion = VSCodeVersion.Insiders;
}
else if (file.EndsWith("code-exploration", StringComparison.OrdinalIgnoreCase))
{
version = "Code - Exploration";
instance.VSCodeVersion = VSCodeVersion.Exploration;
}
else if (file.EndsWith("VSCodium", StringComparison.OrdinalIgnoreCase))
{
version = "VSCodium";
instance.VSCodeVersion = VSCodeVersion.Stable; // ?
}
if (file.EndsWith("code", StringComparison.OrdinalIgnoreCase)) if (version != string.Empty)
{ {
version = "Code"; var portableData = Path.Join(iconPath, "data");
instance.VSCodeVersion = VSCodeVersion.Stable; instance.AppData = Directory.Exists(portableData) ? Path.Join(portableData, "user-data") : Path.Combine(_userAppDataPath, version);
}
else if (file.EndsWith("code-insiders", StringComparison.OrdinalIgnoreCase))
{
version = "Code - Insiders";
instance.VSCodeVersion = VSCodeVersion.Insiders;
}
else if (file.EndsWith("code-exploration", StringComparison.OrdinalIgnoreCase))
{
version = "Code - Exploration";
instance.VSCodeVersion = VSCodeVersion.Exploration;
}
else if (file.EndsWith("VSCodium", StringComparison.OrdinalIgnoreCase))
{
version = "VSCodium";
instance.VSCodeVersion = VSCodeVersion.Stable; // ?
}
if (version != string.Empty) var vsCodeIconPath = Path.Join(iconPath, $"{version}.exe");
var vsCodeIcon = Icon.ExtractAssociatedIcon(vsCodeIconPath);
if (vsCodeIcon != null)
{ {
var portableData = Path.Join(iconPath, "data"); using var vsCodeIconBitmap = vsCodeIcon.ToBitmap();
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 // workspace
var folderIcon = (Bitmap)System.Drawing.Image.FromFile(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "//Images//folder.png"); using var folderIcon = (Bitmap)Image.FromFile(Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Images//folder.png"));
instance.WorkspaceIconBitMap = Bitmap2BitmapImage(BitmapOverlayToCenter(folderIcon, bitmapIconVscode)); using var bitmapFolderIcon = BitmapOverlayToCenter(folderIcon, vsCodeIconBitmap);
instance.WorkspaceIconBitMap = Bitmap2BitmapImage(bitmapFolderIcon);
// remote // remote
var monitorIcon = (Bitmap)System.Drawing.Image.FromFile(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "//Images//monitor.png"); using var monitorIcon = (Bitmap)Image.FromFile(Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Images//monitor.png"));
using var bitmapMonitorIcon = BitmapOverlayToCenter(monitorIcon, vsCodeIconBitmap);
instance.RemoteIconBitMap = Bitmap2BitmapImage(BitmapOverlayToCenter(monitorIcon, bitmapIconVscode)); instance.RemoteIconBitMap = Bitmap2BitmapImage(bitmapMonitorIcon);
Instances.Add(instance);
} }
Instances.Add(instance);
} }
} }
} }