-
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.
Open
Changes from 16 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
80 changes: 80 additions & 0 deletions
80
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,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}\""; | ||
moooyo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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}" }); | ||
} | ||
|
||
moooyo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return CommandResult.Dismiss(); | ||
} | ||
} |
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
94 changes: 94 additions & 0 deletions
94
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,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) | ||
moooyo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
// 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 _)) | ||
moooyo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return BookmarkType.PWSH; | ||
} | ||
|
||
return BookmarkType.PowerShell; | ||
} | ||
|
||
// judge if the bookmark is a url | ||
if (Uri.TryCreate(bookmark, UriKind.Absolute, out var uriResult)) | ||
moooyo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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; | ||
} | ||
} |
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.