This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Couldn't load subscription status.
- Fork 1.2k
Factor sync submodules into VS command #1642
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
437f161
Factor sync submodules into VS command
jcansdale 142f2e2
Merge branch 'master' into refactor/sync-submodules-vs-command
jcansdale feca080
Fix compile error in tests
jcansdale 502bb7b
Merge branch 'master' into refactor/sync-submodules-vs-command
jcansdale b39b7cb
Merge branch 'master' into refactor/sync-submodules-vs-command
jcansdale 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace GitHub.Commands | ||
| { | ||
| /// <summary> | ||
| /// Sync submodules in local repository. | ||
| /// </summary> | ||
| public interface ISyncSubmodulesCommand : IVsCommand | ||
| { | ||
| /// <summary> | ||
| /// Sync submodules in local repository. | ||
| /// </summary> | ||
| /// <returns>Tuple with bool that is true if command completed successfully and string with | ||
| /// output from sync submodules Git command.</returns> | ||
| Task<Tuple<bool, string>> SyncSubmodules(); | ||
| } | ||
| } |
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
100 changes: 100 additions & 0 deletions
100
src/GitHub.VisualStudio/Commands/SyncSubmodulesCommand.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,100 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using System.ComponentModel.Composition; | ||
| using GitHub.Logging; | ||
| using GitHub.Commands; | ||
| using GitHub.Services; | ||
| using GitHub.Services.Vssdk.Commands; | ||
| using Serilog; | ||
| using System.IO; | ||
| using System.Globalization; | ||
| using System.Linq; | ||
|
|
||
| namespace GitHub.VisualStudio.Commands | ||
| { | ||
| /// <summary> | ||
| /// Command to sync submodules in local repository. | ||
| /// </summary> | ||
| [Export(typeof(ISyncSubmodulesCommand))] | ||
| public class SyncSubmodulesCommand : VsCommand, ISyncSubmodulesCommand | ||
| { | ||
| static readonly ILogger log = LogManager.ForContext<SyncSubmodulesCommand>(); | ||
|
|
||
| readonly Lazy<IPullRequestService> lazyPullRequestService; | ||
| readonly Lazy<IStatusBarNotificationService> lazyStatusBarNotificationService; | ||
| readonly Lazy<IVSGitExt> lazyVSGitExt; | ||
|
|
||
| [ImportingConstructor] | ||
| protected SyncSubmodulesCommand( | ||
| Lazy<IPullRequestService> pullRequestService, | ||
| Lazy<IStatusBarNotificationService> statusBarNotificationService, | ||
| Lazy<IVSGitExt> gitExt) | ||
| : base(CommandSet, CommandId) | ||
| { | ||
| lazyPullRequestService = pullRequestService; | ||
| lazyStatusBarNotificationService = statusBarNotificationService; | ||
| lazyVSGitExt = gitExt; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the GUID of the group the command belongs to. | ||
| /// </summary> | ||
| public static readonly Guid CommandSet = Guids.guidGitHubCmdSet; | ||
|
|
||
| /// <summary> | ||
| /// Gets the numeric identifier of the command. | ||
| /// </summary> | ||
| public const int CommandId = PkgCmdIDList.syncSubmodulesCommand; | ||
|
|
||
| /// <summary> | ||
| /// Syncs submodules. | ||
| /// </summary> | ||
| public override async Task Execute() | ||
| { | ||
| try | ||
| { | ||
| var complete = await SyncSubmodules(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| log.Error(ex, "Error syncing submodules"); | ||
| lazyStatusBarNotificationService.Value.ShowMessage("Error syncing submodules"); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sync submodules in local repository. | ||
| /// </summary> | ||
| /// <returns>Tuple with bool that is true if command completed successfully and string with | ||
| /// output from sync submodules Git command.</returns> | ||
| public async Task<Tuple<bool, string>> SyncSubmodules() | ||
| { | ||
| var pullRequestService = lazyPullRequestService.Value; | ||
| var statusBarNotificationService = lazyStatusBarNotificationService.Value; | ||
| var gitExt = lazyVSGitExt.Value; | ||
|
|
||
| var repository = gitExt.ActiveRepositories.FirstOrDefault(); | ||
| if (repository == null) | ||
| { | ||
| statusBarNotificationService.ShowMessage("No local Git repository"); | ||
| return new Tuple<bool, string>(true, "No local Git repository"); | ||
| } | ||
|
|
||
| var writer = new StringWriter(CultureInfo.CurrentCulture); | ||
| var complete = await pullRequestService.SyncSubmodules(repository, line => | ||
| { | ||
| writer.WriteLine(line); | ||
| statusBarNotificationService.ShowMessage(line); | ||
| }); | ||
|
|
||
| if (!complete) | ||
| { | ||
| statusBarNotificationService.ShowMessage("Failed to sync submodules." + Environment.NewLine + writer); | ||
| return new Tuple<bool, string>(false, writer.ToString()); | ||
| } | ||
|
|
||
| statusBarNotificationService.ShowMessage(string.Empty); | ||
| return new Tuple<bool, string>(true, writer.ToString()); | ||
| } | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it normal for VS commands to write directly to the status bar? Is there no concept of "output" for commands, which e.g. shows in the command pane?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the status bar is what commands tend to use as their ambient UI. There isn't really any concept of a native UI or output for a command.
One possibility would be to pass in a progress object that the command could report back to. This could also sidestep the issue of returning results.
I wanted to start off with a simple refactoring and and avoid any extra implementation detail. We can revisit next time this command is touched (if we decide to surface it somewhere else).