JD2022-TU1/main/tools/apps/TeaBox/Script/Build/Code/PrebuildMacros.cs

186 lines
8.5 KiB
C#

using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using TeaBox.Macros;
using TeaBox.SourceControl;
using System.Collections.Generic;
namespace TeaBox.Code
{
public static partial class BuildMacros
{
private static readonly string DeveloperPrefsFile = Path.Combine(RootFolder.Value, "src", "core", "developer_prefs.h");
private static readonly string BloombergIniFile = Path.Combine(RootFolder.Value, "tools", "framework",
"JD.Bloomberg", "bloomberg_settings_tools.ini");
private static readonly string ResourceFile = Path.Combine(RootFolder.Value, "src", "appFramework", "Win32",
"itf_main_win32.rc");
private static readonly string MacrosFile = Path.Combine(RootFolder.Value, "src", "core", "Macros.h");
[DisplayName("Pre-build engine as robot")]
[Category(@"Robot\Code")]
[Macro(ThreadAffinity.ExecuteAsync, Icon = "robot.png")]
public static void PrebuildEngineAsRobot(IExecutionContext context, string engineChangelist)
{
// if no change list provided
if (!string.IsNullOrEmpty(engineChangelist))
{
Environment.SetEnvironmentVariable("P4_CHANGELIST", engineChangelist);
}
engineChangelist = GetEngineChangelist();
if (!string.IsNullOrEmpty(engineChangelist))
{
context.LogMessage("Engine changelist = {0}", engineChangelist);
}
else
{
context.LogError("Unable to load engine changelist value, exiting...");
return;
}
// Define ROBOT_COMPILATION
context.AddStep("Defining ROBOT_COMPILATION", step =>
{
const string pattern = "#define _DEVELOPER_PREFS_H_";
var newLine = string.Format("{0}\n#define ROBOT_COMPILATION\n#define VERSION_ENGINE {1}\n", pattern,
engineChangelist);
// Start update
UpdateFileContent(context, engineChangelist, DeveloperPrefsFile, pattern, newLine);
});
// Define version number for Bloomberg
context.AddStep("Defining Bloomberg version", step =>
{
const string pattern = "Version=42";
var newLine = string.Format("Version={0}\n", engineChangelist);
// Start update
UpdateFileContent(context, engineChangelist, BloombergIniFile, pattern, newLine);
});
// Updating engine resource file
context.AddStep("Updating engine resource file", step =>
{
var leftWord = Int32.Parse(engineChangelist) / 65536;
var rightWord = Int32.Parse(engineChangelist) % 65536;
const string pattern = "FILEVERSION 0,0,0,0";
var newLine = string.Format("FILEVERSION 0,0,{0},{1}\n", leftWord, rightWord);
// Start update
UpdateFileContent(context, engineChangelist, ResourceFile, pattern, newLine);
});
}
[DisplayName("Pre-build engine with Uplay DEV")]
[Category(@"Robot\Code")]
[Macro(ThreadAffinity.ExecuteAsync, Icon = "robot.png")]
public static void EnableUplaydev(IExecutionContext context, string engineChangelist)
{
context.AddStep("Defining UPLAY_DEV", step =>
{
const string pattern = "#define _DEVELOPER_PREFS_H_";
var newLine = $"{pattern} \n#define UPLAY_DEV";
// Start update
UpdateFileContent(context, engineChangelist, DeveloperPrefsFile, pattern, newLine, false);
});
}
[DisplayName("Pre-build engine with Uplay and Steam deactivated")]
[Category(@"Robot\Code")]
[Macro(ThreadAffinity.ExecuteAsync, Icon = "robot.png")]
public static void DisableUplaySteamFeatures(IExecutionContext context, string engineChangelist)
{
context.AddStep("Deactivating UPLAY_STEAM", step =>
{
const string pattern = "//#define ITF_PC_NO_UPLAY_NO_STEAM";
var newLine = "#define ITF_PC_NO_UPLAY_NO_STEAM // to enable no UPlay / no Steam special version for China";
// Start update
UpdateFileContent(context, engineChangelist, MacrosFile, pattern, newLine, false);
});
}
[DisplayName("Engine as robot")]
[Category(@"Robot/Code")]
[Macro(ThreadAffinity.ExecuteAsync, Icon = "robot.png")]
public static void BuildEngineAsRobot(IExecutionContext context, string engineChangelist, [DefaultValue("")] string unshelvedChangelist, [DefaultValue(DevEnvBuildVariant.Release)] DevEnvBuildVariant buildVariants, [DefaultValue(global::Platform.win64)] global::Platform platforms)
{
Clean_Output(context, forceAll: true);
if (!string.IsNullOrEmpty(unshelvedChangelist))
{
Source_ControlMacros.ApplyShelve(context, unshelvedChangelist, string.Empty, SourceControlConnections.ConnectionType.Code);
}
Generate_solutions(context);
PrebuildEngineAsRobot(context, engineChangelist);
foreach (var buildVariant in buildVariants.FlagsDefined())
foreach (var platform in platforms.FlagsDefined())
{
if (!(buildVariant == DevEnvBuildVariant.Final && platform == global::Platform.win64))
Build_engine_solution(context, buildVariant, platform);
}
Source_ControlMacros.RollbackShelve(context, string.Empty, SourceControlConnections.ConnectionType.Code);
}
private static void UpdateFileContent(IExecutionContext context, string engineChangelist, string fileToUpdate, string searchedPattern, string newLineContent, bool forceSync = true)
{
context.LogMessage("Add engine changelist {0} into {1}", engineChangelist, fileToUpdate);
//force get latest version of the file
if (forceSync)
SourceControlService.Instance.Code().GetRevision(GetRevisionOptions.Force, new List<PathEntry> { PathEntry.FromFile(fileToUpdate) });
// read all lines in file, and update the line matching the searche pattern
if (searchedPattern != null)
{
var regex = new Regex(searchedPattern);
if (fileToUpdate == null) return;
var allLines = System.IO.File.ReadAllLines(fileToUpdate);
TeaBox.File.RemoveReadOnly(fileToUpdate);
using (var writer = new StreamWriter(fileToUpdate))
{
try
{
// for each line into the file, replace it by newLineContent if it matches the searched pattern
foreach (var newLine in from line in allLines
let match = regex.Match(line)
select
match.Success
? line.Replace(line, newValue: newLineContent)
: line)
{
writer.WriteLine(newLine);
}
context.LogMessage("Update successfully");
writer.Close();
}
catch (Exception ex)
{
context.LogException(ex, "Exception caught during update of {0};\n Exception : {1}",
fileToUpdate);
}
}
}
}
private static string GetEngineChangelist()
{
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("P4_CHANGELIST"))
? Environment.GetEnvironmentVariable("P4_CHANGELIST")
: (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CODE_CHANGELSIT"))
? Environment.GetEnvironmentVariable("CODE_CHANGELIST")
: string.Empty);
}
}
}