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
{
///
/// Command to sync submodules in local repository.
///
[Export(typeof(ISyncSubmodulesCommand))]
public class SyncSubmodulesCommand : VsCommand, ISyncSubmodulesCommand
{
static readonly ILogger log = LogManager.ForContext();
readonly Lazy lazyPullRequestService;
readonly Lazy lazyStatusBarNotificationService;
readonly Lazy lazyVSGitExt;
[ImportingConstructor]
protected SyncSubmodulesCommand(
Lazy pullRequestService,
Lazy statusBarNotificationService,
Lazy gitExt)
: base(CommandSet, CommandId)
{
lazyPullRequestService = pullRequestService;
lazyStatusBarNotificationService = statusBarNotificationService;
lazyVSGitExt = gitExt;
}
///
/// Gets the GUID of the group the command belongs to.
///
public static readonly Guid CommandSet = Guids.guidGitHubCmdSet;
///
/// Gets the numeric identifier of the command.
///
public const int CommandId = PkgCmdIDList.syncSubmodulesCommand;
///
/// Syncs submodules.
///
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");
}
}
///
/// Sync submodules in local repository.
///
/// Tuple with bool that is true if command completed successfully and string with
/// output from sync submodules Git command.
public async Task> 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(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(false, writer.ToString());
}
statusBarNotificationService.ShowMessage(string.Empty);
return new Tuple(true, writer.ToString());
}
}
}