-
Notifications
You must be signed in to change notification settings - Fork 7.1k
[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
moooyo
wants to merge
39
commits into
main
Choose a base branch
from
yuleng/cmdpal/bookmarks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+437
−197
Open
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
e5f9d4b
init
311b8ac
Change structure
e6abca9
Fix icon issues
9f8a238
Fix cmd issue
923e33a
Fix i18n issue
84727c8
Remove unused field.
038ace0
Folder not web need directoryPage and OpenInShellCommand
681d9a2
Add subtitle for shell command
3db59a3
Fix typo issue
d220051
fix
c39bda9
Add error msg as return
8d2b3f2
Merge main
df252db
Ok, fine. We can detect the bookmark type by ourself. No need to expl…
0efb642
Fix typo
a0827b0
Fix typo
d7dbe0d
Fix typo
dc69a83
Merge branch 'main' into yuleng/cmdpal/bookmarks
9b25427
merge main and add settings
962cb43
store
9ee9824
Merge branch 'main' into yuleng/cmdpal/bookmarks
ae0da4e
simplify the logic. Just call them directly.
242dff4
Remove setting page
b825f7a
Remove unused todo
48184d6
Remove unused resource
a51b222
Merge folder and file
0aeeebc
Fetch icon
dfea4c2
Remove unused string
4a2cb80
fix typo
b17d53c
Merge urlCommand into shellCommand
56c8b57
Add folder context menu back
425b8c2
update comments
44b677b
Merge branch 'main' into yuleng/cmdpal/bookmarks
897a71e
Remove unused code
7bbe1f6
Remove unused code
b631615
Fix bug
d5e01fc
merge main
5323dea
Remove unused code
553c7eb
Remove unused code
5164a1f
Improve robust
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Bookmark/Command/ShellCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// 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 System.Data; | ||
using Microsoft.CmdPal.Ext.Bookmarks.Helpers; | ||
using Microsoft.CmdPal.Ext.Bookmarks.Models; | ||
using Microsoft.CmdPal.Ext.Bookmarks.Properties; | ||
using Microsoft.CommandPalette.Extensions.Toolkit; | ||
|
||
namespace Microsoft.CmdPal.Ext.Bookmarks.Command; | ||
|
||
public sealed partial class ShellCommand : InvokableCommand | ||
{ | ||
public BookmarkData BookmarkData { get; } | ||
|
||
public ShellCommand(BookmarkData data) | ||
{ | ||
BookmarkData = data; | ||
Name = data.Name; | ||
Icon = IconHelper.CreateIcon(data.Bookmark, data.Type, false); | ||
} | ||
|
||
public override CommandResult Invoke() | ||
{ | ||
return ShellCommand.Invoke(BookmarkData.Type, BookmarkData.Bookmark); | ||
} | ||
|
||
public static CommandResult Invoke(BookmarkType bookmarkType, string bookmarkValue) | ||
{ | ||
if (bookmarkType == BookmarkType.Web) | ||
{ | ||
var uri = OpenInShellHelper.GetUri(bookmarkValue); | ||
if (uri != null) | ||
{ | ||
if (!OpenInShellHelper.OpenInShell(uri.ToString(), null, null, OpenInShellHelper.ShellRunAsType.None, false, out var errMsg)) | ||
{ | ||
ExtensionHost.LogMessage($"Failed to open {bookmarkValue} in shell. Ex: {errMsg}"); | ||
return CommandResult.ShowToast(new ToastArgs() { Message = Resources.bookmarks_command_invoke_failed_message }); | ||
} | ||
|
||
return CommandResult.Dismiss(); | ||
} | ||
else | ||
{ | ||
return CommandResult.ShowToast(new ToastArgs() { Message = Resources.bookmarks_command_invoke_failed_message }); | ||
} | ||
} | ||
|
||
// if it's a file or folder bookmark, call them directly. | ||
if (bookmarkType == BookmarkType.File || bookmarkType == BookmarkType.Folder) | ||
{ | ||
if (!OpenInShellHelper.OpenInShell(bookmarkValue, null, null, OpenInShellHelper.ShellRunAsType.None, false, out var errMsg)) | ||
{ | ||
ExtensionHost.LogMessage($"Failed to open {bookmarkValue} in shell. Ex: {errMsg}"); | ||
return CommandResult.ShowToast(new ToastArgs() { Message = Resources.bookmarks_command_invoke_failed_message }); | ||
} | ||
|
||
return CommandResult.Dismiss(); | ||
} | ||
|
||
// We assume all command bookmarks will follow the same format. | ||
// For example: "python test.py" or "pwsh test.ps1" | ||
// So, we can split the command and get the first part as the command name. | ||
var splittedBookmarkValue = bookmarkValue.Split(" "); | ||
if (splittedBookmarkValue.Length == 0) | ||
{ | ||
ExtensionHost.LogMessage($"Failed to open {bookmarkValue} in shell. Empty bookmark value."); | ||
return CommandResult.ShowToast(new ToastArgs() { Message = Resources.bookmarks_command_invoke_failed_message }); | ||
} | ||
|
||
if (splittedBookmarkValue.Length == 1) | ||
{ | ||
// directly call. Because it maybe a command with no args. eg: haproxy.exe or cmd.exe | ||
if (!OpenInShellHelper.OpenInShell(splittedBookmarkValue[0], null, null, OpenInShellHelper.ShellRunAsType.None, false, out var errMsg)) | ||
{ | ||
ExtensionHost.LogMessage($"Failed to open {bookmarkValue} in shell. Ex: {errMsg}"); | ||
return CommandResult.ShowToast(new ToastArgs() { Message = Resources.bookmarks_command_invoke_failed_message }); | ||
} | ||
|
||
return CommandResult.Dismiss(); | ||
} | ||
|
||
// args = without the first part and join with space | ||
var args = splittedBookmarkValue[1..]; | ||
|
||
if (!OpenInShellHelper.OpenInShell(splittedBookmarkValue[0], string.Join(" ", 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 = Resources.bookmarks_command_invoke_failed_message }); | ||
} | ||
|
||
return CommandResult.Dismiss(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Bookmark/Helpers/BookmarkTypeHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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 it's a valid uri, we assume it's a Web Link. | ||
* Otherwise, we check if it's a existing folder or file. | ||
* By default, we assume it's a command type. | ||
*/ | ||
|
||
public static BookmarkType GetBookmarkTypeFromValue(string bookmark) | ||
{ | ||
// judge if the bookmark is a url | ||
var uri = OpenInShellHelper.GetUri(bookmark); | ||
if (uri?.Scheme == Uri.UriSchemeHttp || uri?.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)) | ||
{ | ||
return BookmarkType.File; | ||
} | ||
|
||
// by default. we assume it's a command type | ||
return BookmarkType.Command; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.