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

63 lines
2.5 KiB
C#

using System.IO;
using System.Linq;
using Ubisoft.Perforce;
namespace JD.Perforce
{
public static class PerforceExtensions
{
public static string GetOrCreatePendingChangelist(this IPerforceWrapper perforceWrapper, string description)
{
var existingChanglist = perforceWrapper.GetCurrentClientPendingChangelists().FirstOrDefault(x => x.Description.Replace("\n", "") == description);
return existingChanglist != null ? existingChanglist.Change : perforceWrapper.CreatePendingChangelist(description);
}
public static void MoveFolder(this IPerforceWrapper perforceWrapper, string fromPath, string toPath, string changelist)
{
var clientFrom = perforceWrapper.WhereOnClient(fromPath) + "/...";
var clientTo = perforceWrapper.WhereOnClient(toPath) + "/...";
perforceWrapper.OpenForEdit(clientFrom, changelist);
perforceWrapper.Move(clientFrom, clientTo, changelist);
}
public static void DeleteFolder(this IPerforceWrapper perforceWrapper, string folderPath, string changelistId)
{
P4Manager.Instance.PerforceWrapper.Run("delete", "-c", changelistId, "-v", Path.Combine(folderPath, "..."));
}
public static void OpenForAddOrEdit(this IPerforceWrapper perforceWrapper, string path, string changelist = "")
{
if (string.IsNullOrEmpty(changelist))
{
if (File.Exists(path) && P4Manager.Instance.PerforceWrapper.IsSubmittedInDepot(path))
{
perforceWrapper.OpenForEdit(path);
}
else if (File.Exists(path) && !P4Manager.Instance.PerforceWrapper.IsSubmittedInDepot(path))
{
perforceWrapper.OpenForAdd(path);
}
else
{
perforceWrapper.OpenForAdd(path);
}
}
else
{
if (File.Exists(path) && P4Manager.Instance.PerforceWrapper.IsSubmittedInDepot(path))
{
perforceWrapper.OpenForEdit(path, changelist);
}
else if (File.Exists(path) && !P4Manager.Instance.PerforceWrapper.IsSubmittedInDepot(path))
{
perforceWrapper.OpenForAdd(path, changelist);
}
else
{
perforceWrapper.OpenForAdd(path, changelist);
}
}
}
}
}