Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 437f161

Browse files
committed
Factor sync submodules into VS command
Extract the sync submodules action into a VS command called GitHub.SyncSubmodules.
1 parent 36e680e commit 437f161

File tree

8 files changed

+146
-17
lines changed

8 files changed

+146
-17
lines changed

src/GitHub.App/ViewModels/GitHubPane/PullRequestDetailViewModel.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
using System.Reactive.Linq;
88
using System.Reactive.Threading.Tasks;
99
using System.Threading.Tasks;
10-
using System.Globalization;
1110
using GitHub.App;
11+
using GitHub.Commands;
1212
using GitHub.Extensions;
1313
using GitHub.Factories;
1414
using GitHub.Helpers;
@@ -36,7 +36,7 @@ public sealed class PullRequestDetailViewModel : PanePageViewModelBase, IPullReq
3636
readonly IPullRequestSessionManager sessionManager;
3737
readonly IUsageTracker usageTracker;
3838
readonly ITeamExplorerContext teamExplorerContext;
39-
readonly IStatusBarNotificationService statusBarNotificationService;
39+
readonly ISyncSubmodulesCommand syncSubmodulesCommand;
4040
IModelService modelService;
4141
IPullRequestModel model;
4242
string sourceBranchDisplayName;
@@ -71,22 +71,22 @@ public PullRequestDetailViewModel(
7171
IModelServiceFactory modelServiceFactory,
7272
IUsageTracker usageTracker,
7373
ITeamExplorerContext teamExplorerContext,
74-
IStatusBarNotificationService statusBarNotificationService,
75-
IPullRequestFilesViewModel files)
74+
IPullRequestFilesViewModel files,
75+
ISyncSubmodulesCommand syncSubmodulesCommand)
7676
{
7777
Guard.ArgumentNotNull(pullRequestsService, nameof(pullRequestsService));
7878
Guard.ArgumentNotNull(sessionManager, nameof(sessionManager));
7979
Guard.ArgumentNotNull(modelServiceFactory, nameof(modelServiceFactory));
8080
Guard.ArgumentNotNull(usageTracker, nameof(usageTracker));
8181
Guard.ArgumentNotNull(teamExplorerContext, nameof(teamExplorerContext));
82-
Guard.ArgumentNotNull(statusBarNotificationService, nameof(statusBarNotificationService));
82+
Guard.ArgumentNotNull(syncSubmodulesCommand, nameof(syncSubmodulesCommand));
8383

8484
this.pullRequestsService = pullRequestsService;
8585
this.sessionManager = sessionManager;
8686
this.modelServiceFactory = modelServiceFactory;
8787
this.usageTracker = usageTracker;
8888
this.teamExplorerContext = teamExplorerContext;
89-
this.statusBarNotificationService = statusBarNotificationService;
89+
this.syncSubmodulesCommand = syncSubmodulesCommand;
9090
Files = files;
9191

9292
Checkout = ReactiveCommand.CreateAsyncObservable(
@@ -606,21 +606,17 @@ async Task DoSyncSubmodules(object unused)
606606
IsBusy = true;
607607
usageTracker.IncrementCounter(x => x.NumberOfSyncSubmodules).Forget();
608608

609-
var writer = new StringWriter(CultureInfo.CurrentCulture);
610-
var complete = await pullRequestsService.SyncSubmodules(LocalRepository, line =>
611-
{
612-
writer.WriteLine(line);
613-
statusBarNotificationService.ShowMessage(line);
614-
});
609+
var result = await syncSubmodulesCommand.SyncSubmodules();
610+
var complete = result.Item1;
611+
var summary = result.Item2;
615612
if (!complete)
616613
{
617-
throw new ApplicationException(writer.ToString());
614+
throw new ApplicationException(summary);
618615
}
619616
}
620617
finally
621618
{
622619
IsBusy = false;
623-
statusBarNotificationService.ShowMessage(string.Empty);
624620
}
625621
}
626622

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace GitHub.Commands
5+
{
6+
/// <summary>
7+
/// Sync submodules in local repository.
8+
/// </summary>
9+
public interface ISyncSubmodulesCommand : IVsCommand
10+
{
11+
/// <summary>
12+
/// Sync submodules in local repository.
13+
/// </summary>
14+
/// <returns>Tuple with bool that is true if command completed successfully and string with
15+
/// output from sync submodules Git command.</returns>
16+
Task<Tuple<bool, string>> SyncSubmodules();
17+
}
18+
}

src/GitHub.Exports/GitHub.Exports.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
<Compile Include="Commands\IOpenLinkCommand.cs" />
149149
<Compile Include="Commands\ICreateGistCommand.cs" />
150150
<Compile Include="Commands\IShowCurrentPullRequestCommand.cs" />
151+
<Compile Include="Commands\ISyncSubmodulesCommand.cs" />
151152
<Compile Include="Commands\IShowGitHubPaneCommand.cs" />
152153
<Compile Include="Commands\IOpenPullRequestsCommand.cs" />
153154
<Compile Include="Commands\IAddConnectionCommand.cs" />

src/GitHub.Exports/Settings/PkgCmdID.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public static class PkgCmdIDList
1010
public const int showGitHubPaneCommand = 0x200;
1111
public const int openPullRequestsCommand = 0x201;
1212
public const int showCurrentPullRequestCommand = 0x202;
13+
public const int syncSubmodulesCommand = 0x203;
1314
public const int backCommand = 0x300;
1415
public const int forwardCommand = 0x301;
1516
public const int refreshCommand = 0x302;
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using System.ComponentModel.Composition;
4+
using GitHub.Logging;
5+
using GitHub.Commands;
6+
using GitHub.Services;
7+
using GitHub.Services.Vssdk.Commands;
8+
using Serilog;
9+
using System.IO;
10+
using System.Globalization;
11+
using System.Linq;
12+
13+
namespace GitHub.VisualStudio.Commands
14+
{
15+
/// <summary>
16+
/// Command to sync submodules in local repository.
17+
/// </summary>
18+
[Export(typeof(ISyncSubmodulesCommand))]
19+
public class SyncSubmodulesCommand : VsCommand, ISyncSubmodulesCommand
20+
{
21+
static readonly ILogger log = LogManager.ForContext<SyncSubmodulesCommand>();
22+
23+
readonly Lazy<IPullRequestService> lazyPullRequestService;
24+
readonly Lazy<IStatusBarNotificationService> lazyStatusBarNotificationService;
25+
readonly Lazy<IVSGitExt> lazyVSGitExt;
26+
27+
[ImportingConstructor]
28+
protected SyncSubmodulesCommand(
29+
Lazy<IPullRequestService> pullRequestService,
30+
Lazy<IStatusBarNotificationService> statusBarNotificationService,
31+
Lazy<IVSGitExt> gitExt)
32+
: base(CommandSet, CommandId)
33+
{
34+
lazyPullRequestService = pullRequestService;
35+
lazyStatusBarNotificationService = statusBarNotificationService;
36+
lazyVSGitExt = gitExt;
37+
}
38+
39+
/// <summary>
40+
/// Gets the GUID of the group the command belongs to.
41+
/// </summary>
42+
public static readonly Guid CommandSet = Guids.guidGitHubCmdSet;
43+
44+
/// <summary>
45+
/// Gets the numeric identifier of the command.
46+
/// </summary>
47+
public const int CommandId = PkgCmdIDList.syncSubmodulesCommand;
48+
49+
/// <summary>
50+
/// Syncs submodules.
51+
/// </summary>
52+
public override async Task Execute()
53+
{
54+
try
55+
{
56+
var complete = await SyncSubmodules();
57+
}
58+
catch (Exception ex)
59+
{
60+
log.Error(ex, "Error syncing submodules");
61+
lazyStatusBarNotificationService.Value.ShowMessage("Error syncing submodules");
62+
}
63+
}
64+
65+
/// <summary>
66+
/// Sync submodules in local repository.
67+
/// </summary>
68+
/// <returns>Tuple with bool that is true if command completed successfully and string with
69+
/// output from sync submodules Git command.</returns>
70+
public async Task<Tuple<bool, string>> SyncSubmodules()
71+
{
72+
var pullRequestService = lazyPullRequestService.Value;
73+
var statusBarNotificationService = lazyStatusBarNotificationService.Value;
74+
var gitExt = lazyVSGitExt.Value;
75+
76+
var repository = gitExt.ActiveRepositories.FirstOrDefault();
77+
if (repository == null)
78+
{
79+
statusBarNotificationService.ShowMessage("No local Git repository");
80+
return new Tuple<bool, string>(true, "No local Git repository");
81+
}
82+
83+
var writer = new StringWriter(CultureInfo.CurrentCulture);
84+
var complete = await pullRequestService.SyncSubmodules(repository, line =>
85+
{
86+
writer.WriteLine(line);
87+
statusBarNotificationService.ShowMessage(line);
88+
});
89+
90+
if (!complete)
91+
{
92+
statusBarNotificationService.ShowMessage("Failed to sync submodules." + Environment.NewLine + writer);
93+
return new Tuple<bool, string>(false, writer.ToString());
94+
}
95+
96+
statusBarNotificationService.ShowMessage(string.Empty);
97+
return new Tuple<bool, string>(true, writer.ToString());
98+
}
99+
}
100+
}

src/GitHub.VisualStudio/GitHub.VisualStudio.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@
319319
<Compile Include="Commands\LinkCommandBase.cs" />
320320
<Compile Include="Commands\CopyLinkCommand.cs" />
321321
<Compile Include="Commands\OpenLinkCommand.cs" />
322+
<Compile Include="Commands\SyncSubmodulesCommand.cs" />
322323
<Compile Include="Commands\ShowCurrentPullRequestCommand.cs" />
323324
<Compile Include="Commands\ShowGitHubPaneCommand.cs" />
324325
<Compile Include="Commands\OpenPullRequestsCommand.cs" />

src/GitHub.VisualStudio/GitHub.VisualStudio.vsct

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@
9898
</Strings>
9999
</Button>
100100

101+
<Button guid="guidGitHubCmdSet" id="syncSubmodulesCommand" type="Button">
102+
<Icon guid="guidImages" id="logo" />
103+
<CommandFlag>IconIsMoniker</CommandFlag>
104+
<Strings>
105+
<ButtonText>Sync Submodules</ButtonText>
106+
<CanonicalName>.GitHub.SyncSubmodules</CanonicalName>
107+
<LocCanonicalName>.GitHub.SyncSubmodules</LocCanonicalName>
108+
</Strings>
109+
</Button>
110+
101111
<!--- Toolbar buttons -->
102112
<Button guid="guidGitHubToolbarCmdSet" id="backCommand" type="Button">
103113
<Icon guid="guidImages" id="arrow_left" />
@@ -191,7 +201,7 @@
191201
<ButtonText>Blame</ButtonText>
192202
</Strings>
193203
</Button>
194-
204+
195205
<Button guid="guidContextMenuSet" id="openFileInSolutionCommand" type="Button">
196206
<Icon guid="guidImages" id="logo" />
197207
<CommandFlag>IconIsMoniker</CommandFlag>
@@ -295,6 +305,7 @@
295305
<IDSymbol name="addConnectionCommand" value="0x110"/>
296306
<IDSymbol name="showGitHubPaneCommand" value="0x200"/>
297307
<IDSymbol name="showCurrentPullRequestCommand" value="0x202"/>
308+
<IDSymbol name="syncSubmodulesCommand" value="0x0203" />
298309
</GuidSymbol>
299310

300311
<!-- This is the Manage Connections menu -->

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
3939
{
4040
LogVersionInformation();
4141
await base.InitializeAsync(cancellationToken, progress);
42-
42+
4343
await InitializeLoggingAsync();
4444
await GetServiceAsync(typeof(IUsageTracker));
4545

@@ -85,7 +85,8 @@ async Task InitializeMenus()
8585
exports.GetExportedValue<IOpenLinkCommand>(),
8686
exports.GetExportedValue<IOpenPullRequestsCommand>(),
8787
exports.GetExportedValue<IShowCurrentPullRequestCommand>(),
88-
exports.GetExportedValue<IShowGitHubPaneCommand>()
88+
exports.GetExportedValue<IShowGitHubPaneCommand>(),
89+
exports.GetExportedValue<ISyncSubmodulesCommand>()
8990
};
9091

9192
await JoinableTaskFactory.SwitchToMainThreadAsync();

0 commit comments

Comments
 (0)