Skip to content

Commit

Permalink
Adding drop down to select a source control provider
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoehna committed Sep 30, 2024
1 parent fed4755 commit fa22fce
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using DevHome.Common.Services;
using DevHome.Database.Services;
using DevHome.RepositoryManagement.Services;
using DevHome.RepositoryManagement.ViewModels;
using DevHome.SetupFlow.Services;
using Microsoft.UI.Xaml;
Expand All @@ -20,14 +21,22 @@ public class RepositoryManagementItemViewModelFactory

private readonly ConfigurationFileBuilder _configurationFileBuilder;

private readonly IExtensionService _extensionService;

private readonly RepositoryEnhancerService _repositoryEnhancerService;

public RepositoryManagementItemViewModelFactory(
Window window,
RepositoryManagementDataAccessService dataAccess,
ConfigurationFileBuilder configurationFileBuilder)
ConfigurationFileBuilder configurationFileBuilder,
IExtensionService extensionService,
RepositoryEnhancerService repositoryEnhancerService)
{
_window = window;
_dataAccessService = dataAccess;
_configurationFileBuilder = configurationFileBuilder;
_extensionService = extensionService;
_repositoryEnhancerService = repositoryEnhancerService;
}

public RepositoryManagementItemViewModel MakeViewModel(string repositoryName, string cloneLocation, bool isHidden)
Expand All @@ -49,7 +58,14 @@ public RepositoryManagementItemViewModel MakeViewModel(string repositoryName, st
localIsHidden = true;
}

var newViewModel = new RepositoryManagementItemViewModel(_window, _dataAccessService, _configurationFileBuilder, localRepositoryName, localCloneLocation);
var newViewModel = new RepositoryManagementItemViewModel(
_window,
_dataAccessService,
_configurationFileBuilder,
_extensionService,
_repositoryEnhancerService,
localRepositoryName,
localCloneLocation);

newViewModel.IsHiddenFromPage = localIsHidden;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Threading.Tasks;
using DevHome.Common.Services;
using DevHome.Customization.Helpers;
using DevHome.Customization.ViewModels;
using DevHome.Telemetry;
using FileExplorerSourceControlIntegration;
Expand Down Expand Up @@ -45,16 +46,20 @@ public RepositoryEnhancerService(
_extensionService = extensionService;
}

public List<IExtensionWrapper> GetAllSourceControlProviders()
{
return _extensionService.GetInstalledExtensionsAsync(ProviderType.LocalRepository).Result.ToList();
}

/// <summary>
/// associates a source control provider with a local repository.
/// Associates a source control provider with a local repository.
/// </summary>
/// <param name="repositoryLocation">The full path to the repositories root.</param>
/// <returns>The guid of the class. Otherwise Guid.Empty.</returns>
public async Task<Guid> MakeRepositoryEnhanced(string repositoryLocation)
/// <returns>True if the association is made. False otherwise</returns>
public async Task<bool> MakeRepositoryEnhanced(string repositoryLocation, IExtensionWrapper sourceControlId)
{
_sourceControlRegistrar.AddRepositoryAlreadyOnMachine(repositoryLocation);
var sourceControlId = await AssignSourceControlToPath(repositoryLocation);
return sourceControlId;
return await AssignSourceControlToPath(repositoryLocation, sourceControlId);
}

public string GetLocalBranchName(string repositoryLocation)
Expand Down Expand Up @@ -102,25 +107,22 @@ public IPropertySet GetProperties(string[] propertiesToReturn, string repository
return provider.GetProperties(propertiesToReturn, string.Empty);
}

private async Task<Guid> AssignSourceControlToPath(string maybeRepositoryPath)
public async Task<SourceControlValidationResult> ReAssignSourceControl(string repositoryPath, IExtensionWrapper extensionWrapper)
{
return await _sourceControlRegistrar.AssignSourceControlProviderToRepository(extensionWrapper, repositoryPath);
}

private async Task<bool> AssignSourceControlToPath(string maybeRepositoryPath, IExtensionWrapper extension)
{
Directory.CreateDirectory(maybeRepositoryPath);

foreach (var extension in _extensionService.GetInstalledExtensionsAsync(ProviderType.LocalRepository).Result.ToList())
var assignSourceControlResult = await _sourceControlRegistrar.AssignSourceControlProviderToRepository(extension, maybeRepositoryPath);
if (assignSourceControlResult.Result == Customization.Helpers.ResultType.Success)
{
var assignSourceControlResult = await _sourceControlRegistrar.AssignSourceControlProviderToRepository(extension, maybeRepositoryPath);
if (assignSourceControlResult.Result == Customization.Helpers.ResultType.Success)
{
_log.Information($"Source control {extension.ExtensionDisplayName} is assigned to repository {maybeRepositoryPath}");
if (Guid.TryParse(extension.ExtensionClassId, out Guid id))
{
_log.Information($"Successfully assigned the extension class Id of {extension.ExtensionClassId}");
return id;
}
}
_log.Information($"Source control {extension.ExtensionDisplayName} is assigned to repository {maybeRepositoryPath}");
return true;
}

_log.Information($"Did not find any source extensions for repository {maybeRepositoryPath}");
return Guid.Empty;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,24 @@
<value>More options for repository {0}</value>
<comment>{Locked="{0}"} {0} name of the repository. Automation name for the ... menu</comment>
</data>
<data name="MinuteAbbreviation" xml:space="preserve">
<value>min</value>
<comment>The shorthand name for minute.</comment>
</data>
<data name="PrefixForDevHomeVersion" xml:space="preserve">
<value>Provided by {0}</value>
<comment>Prefix for Dev Home version that appears on the ToolTip for source control extension drop down</comment>
</data>
<data name="UnassignedSourceControlProvider" xml:space="preserve">
<value>unassigned</value>
<comment>Label when a repository does not have an assigned source control provider.</comment>
</data>
<data name="CloseButtonText" xml:space="preserve">
<value>Close</value>
<comment>Button text for the close button.</comment>
</data>
<data name="ErrorAssigningSourceControlProvider" xml:space="preserve">
<value>Error assigning source control provider</value>
<comment>Title of the source control error dialog.</comment>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
using DevHome.Common.Services;
using DevHome.Common.TelemetryEvents.RepositoryManagement;
using DevHome.Common.Windows.FileDialog;
using DevHome.Customization.Helpers;
using DevHome.Database.DatabaseModels.RepositoryManagement;
using DevHome.Database.Services;
using DevHome.RepositoryManagement.Services;
using DevHome.SetupFlow.Services;
using DevHome.Telemetry;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.DevHome.SDK;
using Serilog;

namespace DevHome.RepositoryManagement.ViewModels;
Expand All @@ -37,6 +40,10 @@ public partial class RepositoryManagementItemViewModel : ObservableObject

private readonly ConfigurationFileBuilder _configurationFileBuilder;

private readonly RepositoryEnhancerService _repositoryEnhancerService;

private readonly IExtensionService _extensionService;

/// <summary>
/// Gets the name of the repository.
/// </summary>
Expand Down Expand Up @@ -79,12 +86,61 @@ public string Branch

public string LatestCommitAuthor { get; set; }

public int MinutesSinceLatestCommit { get; set; }
public string MinutesSinceLatestCommit { get; set; }

public bool HasCommitInformation { get; set; }

public string MoreOptionsButtonAutomationName { get; set; }

[ObservableProperty]
private string _sourceControlProviderDisplayName;

[ObservableProperty]
private string _sourceControlProviderPackageDisplayName;

public string SourceControlExtensionClassId { get; set; }

[ObservableProperty]
private MenuFlyout _allSourceControlProviderNames;

[RelayCommand]
public void UpdateSourceControlProviderNames()
{
AllSourceControlProviderNames.Items.Clear();
foreach (var extension in _extensionService.GetInstalledExtensionsAsync(ProviderType.LocalRepository).Result.ToList())
{
var menuItem = new MenuFlyoutItem
{
Text = extension.ExtensionDisplayName,
Tag = extension,
};

menuItem.Command = AssignRepositoryANewSourceControlProviderCommand;
menuItem.CommandParameter = extension;

ToolTipService.SetToolTip(menuItem, _stringResource.GetLocalized("PrefixForDevHomeVersion", extension.PackageDisplayName));
AllSourceControlProviderNames.Items.Add(menuItem);
}
}

[RelayCommand]
public async Task AssignRepositoryANewSourceControlProvider(IExtensionWrapper extensionWrapper)
{
if (!string.Equals(extensionWrapper.ExtensionClassId, SourceControlExtensionClassId, StringComparison.OrdinalIgnoreCase))
{
var result = await _repositoryEnhancerService.ReAssignSourceControl(ClonePath, extensionWrapper);
if (result.Result != ResultType.Success)
{
ShowErrorContentDialog(result);
}
else
{
var repository = GetRepositoryReportIfNull(nameof(AssignRepositoryANewSourceControlProvider));
_dataAccess.SetSourceControlId(repository, Guid.Parse(extensionWrapper.ExtensionClassId));
}
}
}

[RelayCommand]
public async Task OpenInFileExplorer()
{
Expand Down Expand Up @@ -293,6 +349,8 @@ internal RepositoryManagementItemViewModel(
Window window,
RepositoryManagementDataAccessService dataAccess,
ConfigurationFileBuilder configurationFileBuilder,
IExtensionService extensionService,
RepositoryEnhancerService repositoryEnhancerService,
string repositoryName,
string cloneLocation)
{
Expand All @@ -301,6 +359,9 @@ internal RepositoryManagementItemViewModel(
_configurationFileBuilder = configurationFileBuilder;
RepositoryName = repositoryName;
_clonePath = cloneLocation;
_extensionService = extensionService;
_allSourceControlProviderNames = new MenuFlyout();
_repositoryEnhancerService = repositoryEnhancerService;
}

public void RemoveThisRepositoryFromTheList()
Expand Down Expand Up @@ -500,4 +561,16 @@ private async Task CheckCloneLocationNotifyUserIfNotFound()
await ShowCloneLocationNotFoundDialogAsync();
}
}

public async void ShowErrorContentDialog(SourceControlValidationResult result)
{
var errorDialog = new ContentDialog
{
Title = _stringResource.GetLocalized("ErrorAssigningSourceControlProvider"),
Content = result.DisplayMessage,
CloseButtonText = _stringResource.GetLocalized("CloseButtonText"),
XamlRoot = _window.Content.XamlRoot,
};
_ = await errorDialog.ShowAsync();
}
}
Loading

0 comments on commit fa22fce

Please sign in to comment.