E M 87bba240b2
feat: ignore whitespace in openapi hash
Previously, whitespace deviations in openapi.yaml spec would cause a hash mismatch between the openapi.yaml in the Logos Storage image and the openapi.yaml in the project. This update removes whitespaces from the file contents before SHA256 hashing the contents.

In addition, there were two independent Hash functions that were copies of each other to produce the same hash. This has been unified into a single FileHash utility.
2026-06-26 16:43:04 +10:00

74 lines
2.8 KiB
C#

using Utils;
public static class Program
{
private const string Search = "<INSERT-OPENAPI-YAML-HASH>";
private const string StoragePluginFolderName = "StoragePlugin";
private const string ProjectPluginsFolderName = "ProjectPlugins";
public static void Main(string[] args)
{
Console.WriteLine("Injecting hash of 'openapi.yaml'...");
var pluginRoot = FindStoragePluginFolder();
var clientRoot = FindLogosStorageClientFolder();
Console.WriteLine("Located StoragePlugin: " + pluginRoot);
var openApiFile = Path.Combine(clientRoot, "openapi.yaml");
var clientFile = Path.Combine(clientRoot, "obj", "openapiClient.cs");
var targetFile = Path.Combine(pluginRoot, "ApiChecker.cs");
// Force client rebuild by deleting previous artifact.
File.Delete(clientFile);
var hash = FileHash.Hash(openApiFile);
// This hash is used to verify that the Codex docker image being used is compatible
// with the openapi.yaml being used by the Codex plugin.
// If the openapi.yaml files don't match, an exception is thrown.
SearchAndInject(hash, targetFile);
// This program runs as the pre-build trigger for "StoragePlugin".
// You might be wondering why this work isn't done by a shell script.
// This is because this project is being run on many different platforms.
// (Mac, Unix, Win, but also desktop/cloud containers.)
// In order to not go insane trying to make a shell script that works in all possible cases,
// instead we use the one tool that's definitely installed in all platforms and locations
// when you're trying to run this plugin.
Console.WriteLine("Done!");
}
private static string FindStoragePluginFolder()
{
var folder = Path.Combine(PluginPathUtils.ProjectPluginsDir, "StoragePlugin");
if (!Directory.Exists(folder)) throw new Exception("StoragePlugin folder not found. Expected: " + folder);
return folder;
}
private static string FindLogosStorageClientFolder()
{
var folder = Path.Combine(PluginPathUtils.ProjectPluginsDir, "LogosStorageClient");
if (!Directory.Exists(folder)) throw new Exception("LogosStorageClient folder not found. Expected: " + folder);
return folder;
}
private static void SearchAndInject(string hash, string targetFile)
{
var lines = File.ReadAllLines(targetFile);
Inject(lines, hash);
File.WriteAllLines(targetFile, lines);
}
private static void Inject(string[] lines, string hash)
{
for (var i = 0; i < lines.Length; i++)
{
if (lines[i].Contains(Search))
{
lines[i + 1] = $" private const string OpenApiYamlHash = \"{hash}\";";
return;
}
}
}
}