3
3
4
4
public static class Program
5
5
{
6
- private const string OpenApiFile = "../CodexPlugin/openapi.yaml" ;
7
- private const string ClientFile = "../CodexPlugin/obj/openapiClient.cs" ;
8
6
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" ;
10
9
11
10
public static void Main ( string [ ] args )
12
11
{
13
12
Console . WriteLine ( "Injecting hash of 'openapi.yaml'..." ) ;
14
13
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
+
15
20
// Force client rebuild by deleting previous artifact.
16
- File . Delete ( ClientFile ) ;
21
+ File . Delete ( clientFile ) ;
17
22
18
- var hash = CreateHash ( ) ;
23
+ var hash = CreateHash ( openApiFile ) ;
19
24
// This hash is used to verify that the Codex docker image being used is compatible
20
25
// with the openapi.yaml being used by the Codex plugin.
21
26
// If the openapi.yaml files don't match, an exception is thrown.
22
27
23
- SearchAndInject ( hash ) ;
28
+ SearchAndInject ( hash , targetFile ) ;
24
29
25
30
// This program runs as the pre-build trigger for "CodexPlugin".
26
31
// You might be wondering why this work isn't done by a shell script.
@@ -33,9 +38,39 @@ public static void Main(string[] args)
33
38
Console . WriteLine ( "Done!" ) ;
34
39
}
35
40
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 )
37
72
{
38
- var file = File . ReadAllText ( OpenApiFile ) ;
73
+ var file = File . ReadAllText ( openApiFile ) ;
39
74
var fileBytes = Encoding . ASCII . GetBytes ( file
40
75
. Replace ( Environment . NewLine , "" ) ) ;
41
76
@@ -44,11 +79,11 @@ private static string CreateHash()
44
79
return BitConverter . ToString ( hash ) ;
45
80
}
46
81
47
- private static void SearchAndInject ( string hash )
82
+ private static void SearchAndInject ( string hash , string targetFile )
48
83
{
49
- var lines = File . ReadAllLines ( TargetFile ) ;
84
+ var lines = File . ReadAllLines ( targetFile ) ;
50
85
Inject ( lines , hash ) ;
51
- File . WriteAllLines ( TargetFile , lines ) ;
86
+ File . WriteAllLines ( targetFile , lines ) ;
52
87
}
53
88
54
89
private static void Inject ( string [ ] lines , string hash )
0 commit comments