Skip to content

Commit 3d5c693

Browse files
committed
Add imports path fixer
When bumping source proto files, a single build will change the necessary imports or fail as needed
1 parent e9940c2 commit 3d5c693

File tree

5 files changed

+76
-8
lines changed

5 files changed

+76
-8
lines changed

src/GrokClient/GrokClient.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
<Protobuf Include="*.proto" GrpcServices="Client" />
1515
</ItemGroup>
1616

17-
<Target Name="FixProto" BeforeTargets="Build">
17+
<Target Name="FixProto" BeforeTargets="Protobuf_BeforeCompile">
1818
<Exec Command="dotnet run --file ..\protofix.cs" />
19+
1920
</Target>
2021

2122
</Project>

src/GrokClient/chat.proto

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ syntax = "proto3";
33
package xai_api;
44

55
import "google/protobuf/timestamp.proto";
6-
import "xai/api/v1/deferred.proto";
7-
import "xai/api/v1/image.proto";
8-
import "xai/api/v1/sample.proto";
9-
import "xai/api/v1/usage.proto";
6+
import "deferred.proto";
7+
import "image.proto";
8+
import "sample.proto";
9+
import "usage.proto";
1010

1111
// An API that exposes our language models via a Chat interface.
1212
service Chat {

src/GrokClient/embed.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ syntax = "proto3";
22

33
package xai_api;
44

5-
import "xai/api/v1/image.proto";
6-
import "xai/api/v1/usage.proto";
5+
import "image.proto";
6+
import "usage.proto";
77

88
// An API service for interaction with available embedding models.
99
service Embedder {

src/GrokClient/sample.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ syntax = "proto3";
33
package xai_api;
44

55
import "google/protobuf/timestamp.proto";
6-
import "xai/api/v1/usage.proto";
6+
import "usage.proto";
77

88
// An API service for sampling the responses of available language models.
99
service Sample {

src/protofix.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#:package Spectre.Console@0.*
2+
#:package ConsoleAppFramework@5.*
3+
#:property Nullable=enable
4+
#:property ImportDirectoryBuildProps=false
5+
#:property ImportDirectoryBuildTargets=false
6+
7+
using System.IO;
8+
using System.Linq;
9+
using System.Text.RegularExpressions;
10+
using ConsoleAppFramework;
11+
using Spectre.Console;
12+
13+
ConsoleApp.Run(args, FixProto);
14+
15+
/// <summary>Check and fix imports in .proto files.</summary>
16+
/// <para name="dir">Optional directory, defaults to current directory.</para>
17+
static int FixProto(bool dryRun, [Argument] string? dir = default)
18+
{
19+
dir ??= Directory.GetCurrentDirectory();
20+
var regex = ImportExpr();
21+
var result = 0;
22+
foreach (var file in Directory.EnumerateFiles(dir, "*.proto", SearchOption.AllDirectories))
23+
{
24+
var lines = File.ReadAllLines(file).ToList();
25+
var changed = false;
26+
for (var i = 0; i < lines.Count; i++)
27+
{
28+
var line = lines[i];
29+
if (regex.Match(line) is { Success: true } match)
30+
{
31+
var path = match.Groups[1].Value;
32+
var baseDir = Path.GetDirectoryName(file)!;
33+
if (File.Exists(Path.Combine(baseDir, path)))
34+
{
35+
AnsiConsole.MarkupLine($":check_mark_button: {Path.GetRelativePath(dir, file)} [lime]{path}[/]");
36+
continue;
37+
}
38+
39+
if (File.Exists(Path.Combine(baseDir, Path.GetFileName(path))))
40+
{
41+
AnsiConsole.MarkupLine($":pencil:{(dryRun ? ":ghost:" : "")} {Path.GetRelativePath(dir, file)}: {path} > {Path.GetFileName(path)}");
42+
if (!dryRun)
43+
{
44+
lines[i] = line.Replace(path, Path.GetFileName(path));
45+
changed = true;
46+
}
47+
continue;
48+
}
49+
50+
result = 1;
51+
AnsiConsole.MarkupLine($":cross_mark: {Path.GetRelativePath(dir, file)}: import not found [yellow]{path}[/]");
52+
}
53+
}
54+
if (changed && !dryRun)
55+
{
56+
File.WriteAllLines(file, lines);
57+
}
58+
}
59+
60+
return result;
61+
}
62+
63+
partial class Program
64+
{
65+
[GeneratedRegex(@"^import\s+""([^""]+\.proto)"";$", RegexOptions.Multiline)]
66+
private static partial Regex ImportExpr();
67+
}

0 commit comments

Comments
 (0)