Skip to content

Commit 8f16699

Browse files
committed
CodexPluginPrebuild directory travel
1 parent 5ba919a commit 8f16699

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

ProjectPlugins/CodexPluginPrebuild/Program.cs

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,29 @@
33

44
public static class Program
55
{
6-
private const string OpenApiFile = "../CodexPlugin/openapi.yaml";
7-
private const string ClientFile = "../CodexPlugin/obj/openapiClient.cs";
86
private const string Search = "<INSERT-OPENAPI-YAML-HASH>";
9-
private const string TargetFile = "ApiChecker.cs";
7+
private const string CodexPluginFolderName = "CodexPlugin";
8+
private const string ProjectPluginsFolderName = "ProjectPlugins";
109

1110
public static void Main(string[] args)
1211
{
1312
Console.WriteLine("Injecting hash of 'openapi.yaml'...");
1413

14+
var root = FindCodexPluginFolder();
15+
Console.WriteLine("Located CodexPlugin: " + root);
16+
var openApiFile = Path.Combine(root, "openapi.yaml");
17+
var clientFile = Path.Combine(root, "obj", "openapiClient.cs");
18+
var targetFile = Path.Combine(root, "ApiChecker.cs");
19+
1520
// Force client rebuild by deleting previous artifact.
16-
File.Delete(ClientFile);
21+
File.Delete(clientFile);
1722

18-
var hash = CreateHash();
23+
var hash = CreateHash(openApiFile);
1924
// This hash is used to verify that the Codex docker image being used is compatible
2025
// with the openapi.yaml being used by the Codex plugin.
2126
// If the openapi.yaml files don't match, an exception is thrown.
2227

23-
SearchAndInject(hash);
28+
SearchAndInject(hash, targetFile);
2429

2530
// This program runs as the pre-build trigger for "CodexPlugin".
2631
// You might be wondering why this work isn't done by a shell script.
@@ -33,9 +38,39 @@ public static void Main(string[] args)
3338
Console.WriteLine("Done!");
3439
}
3540

36-
private static string CreateHash()
41+
private static string FindCodexPluginFolder()
42+
{
43+
var current = Directory.GetCurrentDirectory();
44+
45+
while (true)
46+
{
47+
var localFolders = Directory.GetDirectories(current);
48+
var projectPluginsFolders = localFolders.Where(l => l.EndsWith(ProjectPluginsFolderName)).ToArray();
49+
if (projectPluginsFolders.Length == 1)
50+
{
51+
return Path.Combine(projectPluginsFolders.Single(), CodexPluginFolderName);
52+
}
53+
var codexPluginFolders = localFolders.Where(l => l.EndsWith(CodexPluginFolderName)).ToArray();
54+
if (codexPluginFolders.Length == 1)
55+
{
56+
return codexPluginFolders.Single();
57+
}
58+
59+
var parent = Directory.GetParent(current);
60+
if (parent == null)
61+
{
62+
var msg = $"Unable to locate '{CodexPluginFolderName}' folder. Travelled up from: '{Directory.GetCurrentDirectory()}'";
63+
Console.WriteLine(msg);
64+
throw new Exception(msg);
65+
}
66+
67+
current = parent.FullName;
68+
}
69+
}
70+
71+
private static string CreateHash(string openApiFile)
3772
{
38-
var file = File.ReadAllText(OpenApiFile);
73+
var file = File.ReadAllText(openApiFile);
3974
var fileBytes = Encoding.ASCII.GetBytes(file
4075
.Replace(Environment.NewLine, ""));
4176

@@ -44,11 +79,11 @@ private static string CreateHash()
4479
return BitConverter.ToString(hash);
4580
}
4681

47-
private static void SearchAndInject(string hash)
82+
private static void SearchAndInject(string hash, string targetFile)
4883
{
49-
var lines = File.ReadAllLines(TargetFile);
84+
var lines = File.ReadAllLines(targetFile);
5085
Inject(lines, hash);
51-
File.WriteAllLines(TargetFile, lines);
86+
File.WriteAllLines(targetFile, lines);
5287
}
5388

5489
private static void Inject(string[] lines, string hash)

0 commit comments

Comments
 (0)