Skip to content

[cmdpal] Support invoke command in Bookmarks extension #39059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
e5f9d4b
init
Apr 23, 2025
311b8ac
Change structure
Apr 23, 2025
e6abca9
Fix icon issues
Apr 23, 2025
9f8a238
Fix cmd issue
Apr 24, 2025
923e33a
Fix i18n issue
Apr 24, 2025
84727c8
Remove unused field.
Apr 24, 2025
038ace0
Folder not web need directoryPage and OpenInShellCommand
Apr 24, 2025
681d9a2
Add subtitle for shell command
Apr 24, 2025
3db59a3
Fix typo issue
Apr 24, 2025
d220051
fix
Apr 24, 2025
c39bda9
Add error msg as return
Apr 25, 2025
8d2b3f2
Merge main
Apr 27, 2025
df252db
Ok, fine. We can detect the bookmark type by ourself. No need to expl…
Apr 27, 2025
0efb642
Fix typo
Apr 27, 2025
a0827b0
Fix typo
Apr 27, 2025
d7dbe0d
Fix typo
Apr 27, 2025
dc69a83
Merge branch 'main' into yuleng/cmdpal/bookmarks
Apr 29, 2025
9b25427
merge main and add settings
Apr 29, 2025
962cb43
store
Apr 30, 2025
9ee9824
Merge branch 'main' into yuleng/cmdpal/bookmarks
May 6, 2025
ae0da4e
simplify the logic. Just call them directly.
May 6, 2025
242dff4
Remove setting page
May 6, 2025
b825f7a
Remove unused todo
May 6, 2025
48184d6
Remove unused resource
May 6, 2025
a51b222
Merge folder and file
May 7, 2025
0aeeebc
Fetch icon
May 7, 2025
dfea4c2
Remove unused string
May 7, 2025
4a2cb80
fix typo
May 7, 2025
b17d53c
Merge urlCommand into shellCommand
May 7, 2025
56c8b57
Add folder context menu back
May 7, 2025
425b8c2
update comments
May 7, 2025
44b677b
Merge branch 'main' into yuleng/cmdpal/bookmarks
May 7, 2025
897a71e
Remove unused code
May 8, 2025
7bbe1f6
Remove unused code
May 8, 2025
b631615
Fix bug
May 8, 2025
d5e01fc
merge main
Jun 5, 2025
5323dea
Remove unused code
Jun 5, 2025
553c7eb
Remove unused code
Jun 5, 2025
5164a1f
Improve robust
Jun 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Collections.Generic;
using System.Text.Json.Serialization;
using Microsoft.CmdPal.Ext.Bookmarks.Models;

namespace Microsoft.CmdPal.Ext.Bookmarks;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Microsoft.CmdPal.Ext.Bookmarks.Models;

namespace Microsoft.CmdPal.Ext.Bookmarks;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.CmdPal.Ext.Bookmarks.Helpers;
using Microsoft.CmdPal.Ext.Bookmarks.Models;
using Microsoft.CmdPal.Ext.Bookmarks.Properties;
using Microsoft.CmdPal.Ext.Indexer;
using Microsoft.CommandPalette.Extensions;
Expand All @@ -22,10 +24,6 @@ public partial class BookmarksCommandProvider : CommandProvider

private Bookmarks? _bookmarks;

public static IconInfo DeleteIcon { get; private set; } = new("\uE74D"); // Delete

public static IconInfo EditIcon { get; private set; } = new("\uE70F"); // Edit

public BookmarksCommandProvider()
{
Id = "Bookmarks";
Expand Down Expand Up @@ -109,57 +107,27 @@ private void LoadBookmarksFromFile()

private CommandItem BookmarkToCommandItem(BookmarkData bookmark)
{
ICommand command = bookmark.IsPlaceholder ?
new BookmarkPlaceholderPage(bookmark) :
new UrlCommand(bookmark);

var listItem = new CommandItem(command) { Icon = command.Icon };

List<CommandContextItem> contextMenu = [];

// Add commands for folder types
if (command is UrlCommand urlCommand)
var deleteAction = () =>
{
if (urlCommand.Type == "folder")
if (_bookmarks != null)
{
contextMenu.Add(
new CommandContextItem(new DirectoryPage(urlCommand.Url)));

contextMenu.Add(
new CommandContextItem(new OpenInTerminalCommand(urlCommand.Url)));
}
ExtensionHost.LogMessage($"Deleting bookmark ({bookmark.Name},{bookmark.Bookmark})");

listItem.Subtitle = urlCommand.Url;
}
_bookmarks.Data.Remove(bookmark);

var edit = new AddBookmarkPage(bookmark) { Icon = EditIcon };
edit.AddedCommand += Edit_AddedCommand;
contextMenu.Add(new CommandContextItem(edit));

var delete = new CommandContextItem(
title: Resources.bookmarks_delete_title,
name: Resources.bookmarks_delete_name,
action: () =>
{
if (_bookmarks != null)
{
ExtensionHost.LogMessage($"Deleting bookmark ({bookmark.Name},{bookmark.Bookmark})");

_bookmarks.Data.Remove(bookmark);
SaveAndUpdateCommands();
}
};

SaveAndUpdateCommands();
}
},
result: CommandResult.KeepOpen())
if (CommandItemFactory.TryCreateBookmarkCommand(bookmark, Edit_AddedCommand, deleteAction, out var commandItem))
{
IsCritical = true,
Icon = DeleteIcon,
};
contextMenu.Add(delete);
return commandItem;
}

listItem.MoreCommands = contextMenu.ToArray();
ExtensionHost.LogMessage($"Failed to create command for bookmark ({bookmark.Name},{bookmark.Bookmark})");

return listItem;
// TODO: need fix it
return new ListItem(new NoOpCommand());
}

public override ICommandItem[] TopLevelCommands()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using Microsoft.CmdPal.Ext.Bookmarks.Helpers;
using Microsoft.CmdPal.Ext.Bookmarks.Properties;
using Microsoft.CommandPalette.Extensions;
using Microsoft.CommandPalette.Extensions.Toolkit;
Expand All @@ -16,6 +17,7 @@ internal sealed partial class OpenInTerminalCommand : InvokableCommand
public OpenInTerminalCommand(string folder)
{
Name = Resources.bookmarks_open_in_terminal_name;
Icon = IconHelper.PowerShellIcon;
_folder = folder;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) Microsoft Corporation
// 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.Collections.Generic;
using Microsoft.CmdPal.Ext.Bookmarks.Helpers;
using Microsoft.CmdPal.Ext.Bookmarks.Models;
using Microsoft.CommandPalette.Extensions.Toolkit;

namespace Microsoft.CmdPal.Ext.Bookmarks.Command;

public sealed partial class ShellCommand : InvokableCommand
{
private static readonly Dictionary<BookmarkType, string> ExecutableFileName = new()
{
{ Models.BookmarkType.Cmd, "cmd.exe" },
{ Models.BookmarkType.PWSH, "pwsh.exe" },
{ Models.BookmarkType.PowerShell, "powershell.exe" },
};

private Models.BookmarkType BookmarkType { get; }

private string BookmarkName { get; }

public string BookmarkValue { get; }

public ShellCommand(BookmarkData data)
: this(data.Name, data.Bookmark, data.Type)
{
}

public ShellCommand(string name, string value, BookmarkType type)
{
BookmarkName = name;
BookmarkType = type;
BookmarkValue = value;
Icon = IconHelper.GetIconByType(type);
Name = name;
}

public override CommandResult Invoke()
{
return ShellCommand.Invoke(BookmarkValue, BookmarkType);
}

public static CommandResult Invoke(string bookmarkValue, BookmarkType bookmarkType)
{
var exeFile = ExecutableFileName[bookmarkType];

if (string.IsNullOrEmpty(exeFile))
{
return CommandResult.ShowToast(new ToastArgs() { Message = "invalid bookmark type" });
}

var fullPath = string.Empty;
if (!EnvironmentsCache.Instance.TryGetExecutableFileFullPath(exeFile, out fullPath))
{
return CommandResult.ShowToast(new ToastArgs() { Message = "invalid fullPath" });
}

var args = bookmarkValue;

if (bookmarkType == BookmarkType.Cmd)
{
args = $"/C {bookmarkValue}";
}
else
{
args = $"-Command \"{bookmarkValue}\"";
}

if (!OpenInShellHelper.OpenInShell(fullPath, args, null, OpenInShellHelper.ShellRunAsType.None, false, out var errorMessage))
{
ExtensionHost.LogMessage($"Failed to open {bookmarkValue} in shell. Ex: {errorMessage}");
return CommandResult.ShowToast(new ToastArgs() { Message = $"Open in shell error. Ex: {errorMessage}" });
}

return CommandResult.Dismiss();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
// See the LICENSE file in the project root for more information.

using System;
using Microsoft.CmdPal.Ext.Bookmarks.Helpers;
using Microsoft.CmdPal.Ext.Bookmarks.Models;
using Microsoft.CommandPalette.Extensions.Toolkit;
using Windows.System;

namespace Microsoft.CmdPal.Ext.Bookmarks;

public partial class UrlCommand : InvokableCommand
{
public string Type { get; }
public BookmarkType Type { get; }

public string Url { get; }

Expand All @@ -19,17 +21,22 @@ public UrlCommand(BookmarkData data)
{
}

public UrlCommand(string name, string url, string type)
public UrlCommand(string name, string url, BookmarkType type)
{
Name = name;
Type = type;
Url = url;
Icon = new IconInfo(IconFromUrl(Url, type));
Icon = IconHelper.CreateIcon(url, type);
}

public override CommandResult Invoke()
{
var target = Url;
return UrlCommand.Invoke(Url);
}

public static CommandResult Invoke(string url)
{
var target = url;
try
{
var uri = GetUri(target);
Expand Down Expand Up @@ -63,36 +70,4 @@ public override CommandResult Invoke()

return uri;
}

internal static string IconFromUrl(string url, string type)
{
switch (type)
{
case "file":
return "📄";
case "folder":
return "📁";
case "web":
default:
// Get the base url up to the first placeholder
var placeholderIndex = url.IndexOf('{');
var baseString = placeholderIndex > 0 ? url.Substring(0, placeholderIndex) : url;
try
{
var uri = GetUri(baseString);
if (uri != null)
{
var hostname = uri.Host;
var faviconUrl = $"{uri.Scheme}://{hostname}/favicon.ico";
return faviconUrl;
}
}
catch (UriFormatException)
{
// return "🔗";
}

return "🔗";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) Microsoft Corporation
// 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 Microsoft.CmdPal.Ext.Bookmarks.Models;

namespace Microsoft.CmdPal.Ext.Bookmarks.Helpers;

public static partial class BookmarkTypeHelper
{
/*
* Summary:
* If bookmark has a space, we assume it's a shell command. eg: "python test.py" or "test.ps1 /C /D"
* Ok fine, we can ensure the bookmark don't have spaces now.
* So:
* 1. Check if it's a valid url.
* 2. Check if it's a existing folder.
* 3. if it's a existing file, it also have the possibility to be a shell command file. eg: "test.cmd" or "test.ps1". So, check the extension. If not, assume it's a normal file.
* By default, we assume it's Web Link.
*/

public static BookmarkType GetBookmarkTypeFromValue(string bookmark)
{
var splittedBookmarkValue = bookmark.Split(" ");

if (splittedBookmarkValue.Length > 1)
{
// absolutely it's a shell command
// we don't need to check the file name
var executableFileName = splittedBookmarkValue[0];
var executableExtension = System.IO.Path.GetExtension(executableFileName);

// if it's a cmd
if (executableExtension == ".cmd" || executableExtension == ".bat")
{
return BookmarkType.Cmd;
}

// Otherwise, we assume it's a powershell or pwsh.
// Prefer to use pwsh, but check if pwsh is installed
// if not, we use powershell
if (EnvironmentsCache.Instance.TryGetExecutableFileFullPath("pwsh.exe", out _))
{
return BookmarkType.PWSH;
}

return BookmarkType.PowerShell;
}

// judge if the bookmark is a url
if (Uri.TryCreate(bookmark, UriKind.Absolute, out var uriResult))
{
if (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps)
{
return BookmarkType.Web;
}
}

// judge if the bookmark is a existing folder
if (System.IO.Directory.Exists(bookmark))
{
return BookmarkType.Folder;
}

// ok, fine. Actually, it's also have the possibility to be a shell command.
// Such as 'test.cmd' or 'test.ps1'. Try to catch this case.
if (System.IO.File.Exists(bookmark))
{
// get file name
var extension = System.IO.Path.GetExtension(bookmark);
switch (extension)
{
case ".ps1":
case ".psm1":
// prefer pwsh.exe over powershell.exe
if (EnvironmentsCache.Instance.TryGetExecutableFileFullPath("pwsh.exe", out _))
{
return BookmarkType.PWSH;
}

return BookmarkType.PowerShell;
case ".cmd":
case ".bat":
return BookmarkType.Cmd;
}

return BookmarkType.File;
}

// by default. we assume the bookmark is a Web link
return BookmarkType.Web;
}
}
Loading
Loading