[Run][VSCode]Fix plugin initialization failing with trailing backslash in path (#29547)

This commit is contained in:
Davide Giacometti 2023-11-03 15:46:14 +01:00 committed by GitHub
parent 4875564a59
commit d5b9c31847
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -71,16 +71,23 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper
foreach (var path in paths) foreach (var path in paths)
{ {
if (Directory.Exists(path)) if (!Directory.Exists(path))
{ {
continue;
}
var files = Directory.GetFiles(path) var files = Directory.GetFiles(path)
.Where(x => (x.Contains("code", StringComparison.OrdinalIgnoreCase) || x.Contains("codium", StringComparison.OrdinalIgnoreCase)) .Where(x => (x.Contains("code", StringComparison.OrdinalIgnoreCase) || x.Contains("codium", StringComparison.OrdinalIgnoreCase))
&& !x.EndsWith(".cmd", StringComparison.OrdinalIgnoreCase)).ToArray(); && !x.EndsWith(".cmd", StringComparison.OrdinalIgnoreCase)).ToArray();
var iconPath = Path.GetDirectoryName(path); // Remove the trailing backslash to always get the correct path
var iconPath = Path.GetDirectoryName(path.TrimEnd('\\'));
if (files.Length > 0) if (files.Length == 0)
{ {
continue;
}
var file = files[0]; var file = files[0];
var version = string.Empty; var version = string.Empty;
@ -115,34 +122,40 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper
instance.VSCodeVersion = VSCodeVersion.Insiders; instance.VSCodeVersion = VSCodeVersion.Insiders;
} }
if (version != string.Empty) if (string.IsNullOrEmpty(version))
{ {
continue;
}
var portableData = Path.Join(iconPath, "data"); var portableData = Path.Join(iconPath, "data");
instance.AppData = Directory.Exists(portableData) ? Path.Join(portableData, "user-data") : Path.Combine(_userAppDataPath, version); instance.AppData = Directory.Exists(portableData) ? Path.Join(portableData, "user-data") : Path.Combine(_userAppDataPath, version);
var vsCodeIconPath = Path.Join(iconPath, $"{version}.exe"); var vsCodeIconPath = Path.Join(iconPath, $"{version}.exe");
if (!File.Exists(vsCodeIconPath))
{
continue;
}
var vsCodeIcon = Icon.ExtractAssociatedIcon(vsCodeIconPath); var vsCodeIcon = Icon.ExtractAssociatedIcon(vsCodeIconPath);
if (vsCodeIcon != null) if (vsCodeIcon == null)
{ {
continue;
}
using var vsCodeIconBitmap = vsCodeIcon.ToBitmap(); using var vsCodeIconBitmap = vsCodeIcon.ToBitmap();
// workspace // Workspace
using var folderIcon = (Bitmap)Image.FromFile(Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Images//folder.png")); using var folderIcon = (Bitmap)Image.FromFile(Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Images//folder.png"));
using var bitmapFolderIcon = BitmapOverlayToCenter(folderIcon, vsCodeIconBitmap); using var bitmapFolderIcon = BitmapOverlayToCenter(folderIcon, vsCodeIconBitmap);
instance.WorkspaceIconBitMap = Bitmap2BitmapImage(bitmapFolderIcon); instance.WorkspaceIconBitMap = Bitmap2BitmapImage(bitmapFolderIcon);
// remote // Remote
using var monitorIcon = (Bitmap)Image.FromFile(Path.Join(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); using var bitmapMonitorIcon = BitmapOverlayToCenter(monitorIcon, vsCodeIconBitmap);
instance.RemoteIconBitMap = Bitmap2BitmapImage(bitmapMonitorIcon); instance.RemoteIconBitMap = Bitmap2BitmapImage(bitmapMonitorIcon);
}
Instances.Add(instance); Instances.Add(instance);
} }
} }
} }
}
}
}
} }