2024-03-25 12:48:20 +00:00
|
|
|
|
using System.Security.Cryptography;
|
2024-03-26 15:10:50 +00:00
|
|
|
|
using System.Text;
|
2024-10-08 07:29:26 +00:00
|
|
|
|
using Utils;
|
2024-03-25 12:48:20 +00:00
|
|
|
|
|
|
|
|
|
public static class Program
|
|
|
|
|
{
|
2024-03-26 13:42:47 +00:00
|
|
|
|
private const string Search = "<INSERT-OPENAPI-YAML-HASH>";
|
2024-07-23 08:59:07 +00:00
|
|
|
|
private const string CodexPluginFolderName = "CodexPlugin";
|
|
|
|
|
private const string ProjectPluginsFolderName = "ProjectPlugins";
|
2024-03-26 13:42:47 +00:00
|
|
|
|
|
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Injecting hash of 'openapi.yaml'...");
|
|
|
|
|
|
2024-07-23 08:59:07 +00:00
|
|
|
|
var root = FindCodexPluginFolder();
|
|
|
|
|
Console.WriteLine("Located CodexPlugin: " + root);
|
|
|
|
|
var openApiFile = Path.Combine(root, "openapi.yaml");
|
|
|
|
|
var clientFile = Path.Combine(root, "obj", "openapiClient.cs");
|
|
|
|
|
var targetFile = Path.Combine(root, "ApiChecker.cs");
|
|
|
|
|
|
2024-04-01 08:04:32 +00:00
|
|
|
|
// Force client rebuild by deleting previous artifact.
|
2024-07-23 08:59:07 +00:00
|
|
|
|
File.Delete(clientFile);
|
2024-04-01 08:04:32 +00:00
|
|
|
|
|
2024-07-23 08:59:07 +00:00
|
|
|
|
var hash = CreateHash(openApiFile);
|
2024-03-26 13:42:47 +00:00
|
|
|
|
// 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.
|
|
|
|
|
|
2024-07-23 08:59:07 +00:00
|
|
|
|
SearchAndInject(hash, targetFile);
|
2024-03-26 13:42:47 +00:00
|
|
|
|
|
|
|
|
|
// 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!");
|
|
|
|
|
}
|
2024-03-25 12:48:20 +00:00
|
|
|
|
|
2024-07-23 08:59:07 +00:00
|
|
|
|
private static string FindCodexPluginFolder()
|
|
|
|
|
{
|
2024-10-08 07:29:26 +00:00
|
|
|
|
var folder = Path.Combine(PluginPathUtils.ProjectPluginsDir, "CodexPlugin");
|
|
|
|
|
if (!Directory.Exists(folder)) throw new Exception("CodexPlugin folder not found. Expected: " + folder);
|
|
|
|
|
return folder;
|
2024-07-23 08:59:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string CreateHash(string openApiFile)
|
2024-03-25 12:48:20 +00:00
|
|
|
|
{
|
2024-07-23 08:59:07 +00:00
|
|
|
|
var file = File.ReadAllText(openApiFile);
|
2024-03-26 15:10:50 +00:00
|
|
|
|
var fileBytes = Encoding.ASCII.GetBytes(file
|
|
|
|
|
.Replace(Environment.NewLine, ""));
|
|
|
|
|
|
2024-03-25 12:48:20 +00:00
|
|
|
|
var sha = SHA256.Create();
|
|
|
|
|
var hash = sha.ComputeHash(fileBytes);
|
|
|
|
|
return BitConverter.ToString(hash);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 08:59:07 +00:00
|
|
|
|
private static void SearchAndInject(string hash, string targetFile)
|
2024-03-25 12:48:20 +00:00
|
|
|
|
{
|
2024-07-23 08:59:07 +00:00
|
|
|
|
var lines = File.ReadAllLines(targetFile);
|
2024-03-26 13:42:47 +00:00
|
|
|
|
Inject(lines, hash);
|
2024-07-23 08:59:07 +00:00
|
|
|
|
File.WriteAllLines(targetFile, lines);
|
2024-03-25 12:48:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 13:42:47 +00:00
|
|
|
|
private static void Inject(string[] lines, string hash)
|
2024-03-25 12:48:20 +00:00
|
|
|
|
{
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-25 12:48:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|