JD2022-TU1/main/tools/framework/JD.Perforce/P4Manager.cs

156 lines
No EOL
5.8 KiB
C#

using System;
using JD.Core.ProjectManaging;
using Plume.Core.Logging;
using Plume.Perforce.Service.Api;
using Plume.Perforce.Service.Implementation;
using Ubisoft.Perforce;
using Ubisoft.Perforce.Data;
namespace JD.Perforce
{
/// <summary>
/// Singleton class to encapsulate Ubisoft.PerforceWrapper from Alexandrie.
/// Accessor is GetInstance property.
/// This class manage connection disconnection, and prompt a special P4V like password dialog.
/// </summary>
public class P4Manager : IDisposable
{
public const string DefaultP4JdData = "p4-jd-data-mrc.ubisoft.org:1808";
/// <summary>
/// Get instance of PA4manager object
/// </summary>
public static P4Manager Instance { get; } = new P4Manager();
public IPerforceService PerforceService { get; set; }
public bool Connected => PerforceWrapper != null;
public PerforceWrapper PerforceWrapper => PerforceService.Connection;
public string WorkspaceRootPath
{
get
{
if (PerforceService.Connection != null)
{
ClientInfo clientInfo = PerforceService.Connection.GetClientInfo();
return clientInfo.Root;
}
return string.Empty;
}
}
private ILogger _logger;
private bool _isInitialized;
private P4Manager()
{
PerforceService = new PerforceService();
_isInitialized = false;
}
public void Dispose()
{
PerforceService.Dispose();
}
public void Initialize(ILogger logger)
{
if (_isInitialized)
{
throw new Exception($"{nameof(P4Manager)} is already initialized.");
}
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
FakeServiceContext serviceContext = new FakeServiceContext
{
Logger = logger
};
PerforceService.Initialize(serviceContext);
_isInitialized = true;
}
/// <summary>
/// Connect to perforce automatically
/// </summary>
public bool Connect()
{
return Connect(DataPath.Value);
}
/// <summary>
/// Connect to perforce with the arguments supplied
/// </summary>
/// <param name="perforcePort">adress of the server</param>
/// <param name="perforceUser">user name</param>
/// <param name="perforceClient">name of the client (also called workspace)</param>
/// <returns></returns>
public bool Connect(string perforcePort, string perforceUser, string perforceClient)
{
ThrowIfNotIntialized();
_logger.Info($"Trying to connect on the server '{perforcePort}' with user '{perforceUser}' on client '{perforceClient}'.");
if (!PerforceService.Connect(perforcePort, perforceUser, perforceClient))
{
_logger.Error($"Unable to connect on the following server '{perforcePort}' with user '{perforceUser}' on client '{perforceClient}'.");
return false;
}
_logger.Info($"Connection established on the server '{PerforceService.Connection.PerforcePort}' with user '{PerforceService.Connection.PerforceUser}' on client '{PerforceService.Connection.PerforceClient}'.");
return true;
}
/// <summary>
/// Connect to perforce automatically by using the path supplied in parameters.
/// </summary>
/// <param name="dataRoot"></param>
public bool Connect(string dataRoot)
{
ThrowIfNotIntialized();
try
{
_logger.Info($"Trying to find automatically a valid client on the following server '{DefaultP4JdData}' with user '{Environment.UserName}'.");
var temp = new PerforceWrapper("", Environment.UserName, "", DefaultP4JdData, true);
if (!temp.AutoConnect(dataRoot))
{
_logger.Error($"No valid client has been found automatically on the following server '{DefaultP4JdData}' with user '{Environment.UserName}'.");
return false;
}
if(!PerforceService.Connect(temp.PerforcePort, temp.PerforceUser, temp.PerforceClient))
{
_logger.Error($"Unable to connect on the following server '{temp.PerforcePort}' with user '{temp.PerforceUser}' on client '{temp.PerforceClient}'.");
return false;
}
_logger.Info($"Connection established on the server '{PerforceService.Connection.PerforcePort}' with user '{PerforceService.Connection.PerforceUser}' on client '{PerforceService.Connection.PerforceClient}'.");
return true;
}
catch (Exception exception)
{
_logger.Error($"Error connecting to '{DefaultP4JdData}' with user '{Environment.UserName}'.", exception);
return false;
}
}
private void ThrowIfNotIntialized()
{
if (!_isInitialized)
{
throw new Exception($"{nameof(P4Manager)} is not initialized. Please ensure to call first the method {nameof(Initialize)}().");
}
}
/// <summary>
/// Disconnection function
/// </summary>
public void Disconnect()
{
_logger.Info($"Disconnection from '{PerforceService.Connection?.PerforcePort}' of ''{PerforceService.Connection?.PerforceUser}:{PerforceService.Connection?.PerforceClient}' has been requested.");
PerforceService.Disconnect();
}
}
}