Skip to content

Commit

Permalink
CustomBuildTool out of date tools detection
Browse files Browse the repository at this point in the history
  • Loading branch information
jxy-s committed Jan 26, 2025
1 parent 4d98e1f commit d25a259
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build/build_tools.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ if exist "%VSINSTALLPATH%\VC\Auxiliary\Build\vcvarsall.bat" (
goto end
)

set BUILD_TOOL="tools\CustomBuildTool\bin\Release\%PROCESSOR_ARCHITECTURE%\CustomBuildTool.exe"
start /B /W "" %BUILD_TOOL% "-write-tools-id"

:end
pause
59 changes: 59 additions & 0 deletions tools/CustomBuildTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public static void Main(string[] args)

ProgramArgs = Utils.ParseArgs(args);

if (ProgramArgs.ContainsKey("-write-tools-id"))
{
WriteToolsId();
return;
}

CheckForOutOfDateTools();

if (ProgramArgs.ContainsKey("-cleanup"))
{
Build.CleanupBuildEnvironment();
Expand Down Expand Up @@ -325,5 +333,56 @@ public static void PrintColorMessage(LogInterpolatedStringHandler builder, Conso
Console.Write(builder.GetFormattedText());
Console.ResetColor();
}

private static void CheckForOutOfDateTools()
{
#if RELEASE
string currentId = GetToolsId();
string previousId = string.Empty;
if (File.Exists("tools\\CustomBuildTool\\bin\\Release\\ToolsId.txt"))
previousId = File.ReadAllText("tools\\CustomBuildTool\\bin\\Release\\ToolsId.txt");
if (previousId != currentId)
PrintColorMessage($"[WARN] Build tools are out of date!", ConsoleColor.Yellow);
#endif
}

private static void WriteToolsId()
{
string currentHash = GetToolsId();
File.WriteAllText("tools\\CustomBuildTool\\bin\\Release\\ToolsId.txt", currentHash);
}

private static string GetToolsId()
{
int bufferSize = 0x1000;
string[] directories =
{
"tools\\CustomBuildTool",
"tools\\CustomBuildTool\\AzureSignTool",
};

using (var sha256 = SHA256.Create())
{
foreach (var directory in directories)
foreach (string source in Directory.EnumerateFiles(directory, "*.cs", SearchOption.TopDirectoryOnly))
{
byte[] buffer = new byte[bufferSize];
int bytesRead;

using (var filestream = File.OpenRead(source))
using (var bufferedStream = new BufferedStream(filestream, bufferSize))
{
while ((bytesRead = bufferedStream.Read(buffer, 0, buffer.Length)) > 0)
{
sha256.TransformBlock(buffer, 0, bytesRead, buffer, 0);
}
}
}

sha256.TransformFinalBlock(Array.Empty<byte>(), 0, 0);

return BitConverter.ToString(sha256.Hash).Replace("-", "").ToLowerInvariant();
}
}
}
}

0 comments on commit d25a259

Please sign in to comment.