cs-codex-dist-tests/ProjectPlugins/CodexPluginPrebuild/Program.cs

65 lines
2.3 KiB
C#
Raw Normal View History

using System.Security.Cryptography;
2024-03-26 15:10:50 +00:00
using System.Text;
public static class Program
{
private const string OpenApiFile = "../CodexPlugin/openapi.yaml";
private const string ClientFile = "../CodexPlugin/obj/openapiClient.cs";
2024-03-26 13:42:47 +00:00
private const string Search = "<INSERT-OPENAPI-YAML-HASH>";
private const string TargetFile = "ApiChecker.cs";
public static void Main(string[] args)
{
Console.WriteLine("Injecting hash of 'openapi.yaml'...");
// Force client rebuild by deleting previous artifact.
File.Delete(ClientFile);
2024-03-26 13:42:47 +00:00
var hash = CreateHash();
// 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);
// This program runs as the pre-build trigger for "CodexPlugin".
// 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 CreateHash()
{
2024-03-26 15:10:50 +00:00
var file = File.ReadAllText(OpenApiFile);
var fileBytes = Encoding.ASCII.GetBytes(file
.Replace(Environment.NewLine, ""));
var sha = SHA256.Create();
var hash = sha.ComputeHash(fileBytes);
return BitConverter.ToString(hash);
}
2024-03-26 13:42:47 +00:00
private static void SearchAndInject(string hash)
{
var lines = File.ReadAllLines(TargetFile);
2024-03-26 13:42:47 +00:00
Inject(lines, hash);
File.WriteAllLines(TargetFile, lines);
}
2024-03-26 13:42:47 +00:00
private static void Inject(string[] lines, string hash)
{
2024-03-26 13:42:47 +00:00
for (var i = 0; i < lines.Length; i++)
{
if (lines[i].Contains(Search))
{
lines[i + 1] = $" private const string OpenApiYamlHash = \"{hash}\";";
return;
}
}
}
}