|
|
|
|
| using System;
|
| using System.Collections.Generic;
|
| using System.IO;
|
| using System.Linq;
|
| using System.Text;
|
| using System.Threading;
|
| using System.Threading.Tasks;
|
| using AutomationTool;
|
| using EpicGame;
|
| using EpicGames.Core;
|
| using Microsoft.Extensions.Logging;
|
|
|
| using static AutomationTool.CommandUtils;
|
|
|
| namespace LyraTest
|
| {
|
| [Help("Commandlet for checking content to know if passes all the existing UEditorValidators.")]
|
| [Help("Branch=<Name>", "Branch to use")]
|
| [Help("CL=<value>", "Check file changes in the range LastCL,CL")]
|
| [Help("p4shelved", "If specified, treat CL as a shelved file to check the contents")]
|
| [Help("p4opened", "Check currently opened files instead of using CL ranged")]
|
| [Help("MaxPackagesToLoad=<value>", "Maximum number of recent changes to check")]
|
| [Help("LastGoodContentCLPath=<value>", "A directory location to store the 'last good' CL so we can determine CLs between runs.")]
|
| [Help("SkipPrevCLFileExport", "Skip exporting the 'last good' CL.")]
|
| public class LyraContentValidation : BuildCommand
|
| {
|
| public override void ExecuteBuild()
|
| {
|
| int ThisCLInt = 0;
|
| int PrevCL = 0;
|
| bool CheckOpenedFiles = ParseParam("opened");
|
| bool CheckShelvedFiles = ParseParam("shelved");
|
| string CommandletArgs = "";
|
| bool RunCommandlet = false;
|
| string PrevCLFilePath = "";
|
| bool SkipPrevCLFileExport = ParseParam("SkipPrevCLFileExport");
|
|
|
|
|
| string GameProjectDirectory = "Samples/Games/Lyra";
|
| string GameProject = "Lyra.uproject";
|
|
|
| string ExtensionTypeListParam = ParseParamValue("ExtTypeList", ".uasset,.umap,.cpp,.c,.h,.inl,.ini,.uproject,.uplugin,.json");
|
| List<string> ExtensionTypeList = new List<string>();
|
| if (string.IsNullOrEmpty(ExtensionTypeListParam))
|
| {
|
| Logger.LogInformation("No extensions were passed in, defaulting to always run. Set -ExtTypeList to the extension typelist for triggering the commandlet");
|
| RunCommandlet = true;
|
| }
|
| else
|
| {
|
| ExtensionTypeList = ExtensionTypeListParam.Split(',').ToList();
|
| }
|
|
|
|
|
| if (CheckOpenedFiles)
|
| {
|
|
|
|
|
| RunCommandlet = AreFileTypesModifiedInOpenChangelists(ExtensionTypeList);
|
|
|
|
|
| if (!P4Enabled)
|
| {
|
| throw new AutomationException("This script must be executed with -p4");
|
| }
|
| CommandletArgs = "-P4Opened -P4Client=" + P4Env.Client;
|
| }
|
| else
|
| {
|
| string ThisCL = ParseParamValue("CL");
|
| if (string.IsNullOrEmpty(ThisCL))
|
| {
|
| throw new AutomationException("-CL=<num> or -opened must be specified.");
|
| }
|
|
|
| if (!int.TryParse(ThisCL, out ThisCLInt))
|
| {
|
| throw new AutomationException("-CL must be a number.");
|
| }
|
|
|
| if (CheckShelvedFiles)
|
| {
|
|
|
|
|
| RunCommandlet = AreFileTypesModifiedInShelvedChangelist(ThisCL, ExtensionTypeList);
|
|
|
| CommandletArgs = String.Format("-P4Filter=@={0}", ThisCL);
|
| }
|
| else
|
| {
|
| string Branch = ParseParamValue("Branch");
|
| if (string.IsNullOrEmpty(Branch))
|
| {
|
| throw new AutomationException("-Branch must be specified to check a CL range when -opened or -shelved are not present");
|
| }
|
|
|
| string LastGoodContentCLPath = ParseParamValue("LastGoodContentCLPath");
|
| if (string.IsNullOrEmpty(LastGoodContentCLPath))
|
| {
|
|
|
| LastGoodContentCLPath = CombinePaths(CmdEnv.LocalRoot, "Engine", "Saved");
|
| }
|
|
|
| string PrevCLFileName = "PrevCL_" + Branch.Replace("/", "+") + ".txt";
|
| PrevCLFilePath = CombinePaths(LastGoodContentCLPath, PrevCLFileName);
|
| PrevCL = ReadPrevCLFile(PrevCLFilePath);
|
|
|
| if (PrevCL <= 0)
|
| {
|
| Logger.LogInformation("Previous CL file didn't exist. Defaulting to none!");
|
| RunCommandlet = true;
|
| }
|
| else if (PrevCL >= ThisCLInt)
|
| {
|
| Logger.LogInformation("Previous CL file shows a CL equal or newer than the current CL. This content was already checked. Skipping.");
|
| RunCommandlet = false;
|
| }
|
| else
|
| {
|
|
|
| PrevCL++;
|
| CommandletArgs = String.Format("-P4Filter={0}/{1}/...@{2},{3}", Branch, GameProjectDirectory, PrevCL, ThisCL);
|
| Logger.LogInformation("Generated Filter: {CommandletArgs}", CommandletArgs);
|
|
|
| RunCommandlet = WereFileTypesModifiedInChangelistRange(Branch, PrevCL, ThisCL, ExtensionTypeList);
|
| }
|
|
|
| if (!RunCommandlet)
|
| {
|
| Logger.LogInformation("No files in CL Range {PrevCL} -> {ThisCL} contained any files ending with extensions {ExtensionTypeListParam}, or they were already checked in a previous job, skipping commandlet run", PrevCL, ThisCL, ExtensionTypeListParam);
|
| }
|
| }
|
| }
|
|
|
| if (RunCommandlet)
|
| {
|
| string EditorExe = "UnrealEditor.exe";
|
| EditorExe = AutomationTool.HostPlatform.Current.GetUnrealExePath(EditorExe);
|
|
|
| string PerforceToken = P4.GetAuthenticationToken();
|
|
|
| CommandletArgs += " -ini:Engine:[Core.System]:AssetLogShowsDiskPath=True -LogCmds=\"LogHttp Error\" ";
|
|
|
|
|
|
|
| {
|
| CommandletArgs += String.Format(" -SCCProvider={0} -P4Port={1} -P4User={2} -P4Client={3} -P4Passwd={4}", "Perforce", P4Env.ServerAndPort, P4Env.User, P4Env.Client, PerforceToken);
|
|
|
|
|
| }
|
|
|
| string MaxPackagesToLoad = ParseParamValue("MaxPackagesToLoad", "2000");
|
| CommandletArgs += String.Format(" -MaxPackagesToLoad={0}", MaxPackagesToLoad);
|
| CommandUtils.RunCommandlet(new FileReference(CombinePaths(CmdEnv.LocalRoot, GameProjectDirectory, GameProject)), EditorExe, "ContentValidationCommandlet", CommandletArgs);
|
| }
|
|
|
|
|
| if (SkipPrevCLFileExport)
|
| {
|
| Logger.LogInformation("Not writing PrevCLFile as -SkipPrevCLFileExport was specified");
|
| }
|
| else
|
| {
|
| ExportPrevCL(PrevCL, ThisCLInt, PrevCLFilePath);
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| private void ExportPrevCL(int PrevCL, int ThisCL, string PrevCLFilePath)
|
| {
|
| if (ThisCL > 0)
|
| {
|
| if (PrevCL < ThisCL)
|
| {
|
| PrevCL = ReadPrevCLFile(PrevCLFilePath);
|
| }
|
|
|
| if (PrevCL < ThisCL)
|
| {
|
| Logger.LogInformation("Writing PrevCLFile {PrevCLFilePath}...", PrevCLFilePath);
|
| WritePrevCLFile(PrevCLFilePath, ThisCL.ToString());
|
| }
|
| else
|
| {
|
| Logger.LogInformation("Not writing PrevCLFile {PrevCLFilePath}. The current CL was not newer", PrevCLFilePath);
|
| }
|
| }
|
| else
|
| {
|
| Logger.LogInformation("Not writing PrevCLFile {PrevCLFilePath} as the current CL {ThisCL} was invalid", PrevCLFilePath, ThisCL);
|
| }
|
| }
|
|
|
| private int ReadPrevCLFile(string PrevCLFilePath)
|
| {
|
| int RetVal = 0;
|
| if (File.Exists(PrevCLFilePath))
|
| {
|
| string PrevCLString = "";
|
| int RetryCount = 10;
|
| bool bProceed = false;
|
| do
|
| {
|
| try
|
| {
|
| PrevCLString = File.ReadAllText(PrevCLFilePath);
|
| bProceed = true;
|
| }
|
| catch (Exception Ex)
|
| {
|
| if (RetryCount > 0)
|
| {
|
| Logger.LogInformation("Failed to read PrevCLFilePath {PrevCLFilePath}. Retrying in a few seconds. Ex:{Arg1}", PrevCLFilePath, Ex.Message);
|
| RetryCount--;
|
| Thread.Sleep(TimeSpan.FromSeconds(5));
|
| }
|
| else
|
| {
|
| Logger.LogError("Failed to read PrevCLFilePath {PrevCLFilePath}. All Retries exhausted, skipping. Ex:{Arg1}", PrevCLFilePath, Ex.Message);
|
| bProceed = true;
|
| }
|
| }
|
| } while (!bProceed);
|
|
|
| if (int.TryParse(PrevCLString, out RetVal))
|
| {
|
|
|
| }
|
| else
|
| {
|
| Logger.LogWarning("{Text}", "Couldn't parse out the changelist number from the saved PrevCLFilePath file. " + PrevCLFilePath);
|
| }
|
| }
|
|
|
| return RetVal;
|
| }
|
|
|
| private void WritePrevCLFile(string PrevCLFilePath, string ThisCL)
|
| {
|
| int RetryCount = 10;
|
| bool bProceed = false;
|
| do
|
| {
|
| try
|
| {
|
| Directory.CreateDirectory(Path.GetDirectoryName(PrevCLFilePath));
|
| File.WriteAllText(PrevCLFilePath, ThisCL);
|
| bProceed = true;
|
| }
|
| catch (Exception Ex)
|
| {
|
| if (RetryCount > 0)
|
| {
|
| Logger.LogInformation("Failed to write PrevCLFilePath {PrevCLFilePath}. Retrying in a few seconds. Ex:{Arg1}", PrevCLFilePath, Ex.Message);
|
| RetryCount--;
|
| Thread.Sleep(TimeSpan.FromSeconds(5));
|
| }
|
| else
|
| {
|
| Logger.LogError("Failed to write PrevCLFilePath {PrevCLFilePath}. All Retries exhausted, skipping. Ex:{Arg1}", PrevCLFilePath, Ex.Message);
|
| bProceed = true;
|
| }
|
| }
|
| } while (!bProceed);
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| private bool WereFileTypesModifiedInChangelistRange(string Branch, int PrevCL, string ThisCL, List<string> ExtensionTypeList)
|
| {
|
|
|
| if (ExtensionTypeList.Count != 0)
|
| {
|
|
|
| List<P4Connection.ChangeRecord> ChangeRecords;
|
| CommandUtils.P4.Changes(out ChangeRecords, string.Format("{0}/...@{1},{2}", Branch, PrevCL, ThisCL), false);
|
| foreach (P4Connection.ChangeRecord Record in ChangeRecords)
|
| {
|
| P4Connection.DescribeRecord DescribeRecord;
|
| CommandUtils.P4.DescribeChangelist(Record.CL, out DescribeRecord, false);
|
|
|
| foreach (P4Connection.DescribeRecord.DescribeFile File in DescribeRecord.Files)
|
| {
|
|
|
| foreach (string Extension in ExtensionTypeList)
|
| {
|
| if (File.File.EndsWith(Extension))
|
| {
|
| return true;
|
| }
|
| }
|
| }
|
| }
|
| }
|
|
|
| return false;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| private bool AreFileTypesModifiedInShelvedChangelist(string ShelvedChangelist, List<string> ExtensionTypeList)
|
| {
|
|
|
| if (ExtensionTypeList.Count != 0)
|
| {
|
|
|
| IEnumerable<string> FileList = CommandUtils.P4.Files("@=" + ShelvedChangelist);
|
|
|
| return FileList.Any(F => ExtensionTypeList.Contains(Path.GetExtension(F), StringComparer.OrdinalIgnoreCase));
|
| }
|
|
|
| return false;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| private bool AreFileTypesModifiedInOpenChangelists(List<string> ExtensionTypeList)
|
| {
|
|
|
| if (ExtensionTypeList.Count != 0)
|
| {
|
|
|
| IEnumerable<string> FileList = CommandUtils.P4.Opened("");
|
|
|
| return FileList.Any(F => ExtensionTypeList.Contains(Path.GetExtension(F), StringComparer.OrdinalIgnoreCase));
|
| }
|
|
|
| return false;
|
| }
|
| }
|
| }
|
|
|