From e165a33048e2dcb0a122bfddc7f2082133889e72 Mon Sep 17 00:00:00 2001 From: "Kraemer, Benjamin" Date: Tue, 16 Jun 2020 14:13:44 +0200 Subject: [PATCH] Add support for Protobuf_ProtoRoot --- .../Grpc.Tools.Tests/CSharpGeneratorTest.cs | 4 ++-- src/csharp/Grpc.Tools/GeneratorServices.cs | 19 +++++++++++-------- src/csharp/Grpc.Tools/ProtoCompilerOutputs.cs | 9 ++++----- .../_protobuf/Google.Protobuf.Tools.targets | 1 + 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/csharp/Grpc.Tools.Tests/CSharpGeneratorTest.cs b/src/csharp/Grpc.Tools.Tests/CSharpGeneratorTest.cs index 642ebf92cef..c53f0bdf8d4 100644 --- a/src/csharp/Grpc.Tools.Tests/CSharpGeneratorTest.cs +++ b/src/csharp/Grpc.Tools.Tests/CSharpGeneratorTest.cs @@ -89,8 +89,8 @@ namespace Grpc.Tools.Tests public void OutputDirPatched() { var item = Utils.MakeItem("sub/foo.proto", "OutputDir", "out"); - _generator.PatchOutputDirectory(item); - var poss = _generator.GetPossibleOutputs(item); + var output = _generator.PatchOutputDirectory(item); + var poss = _generator.GetPossibleOutputs(output); Assert.AreEqual(1, poss.Length); Assert.That(poss[0], Is.EqualTo("out/sub/Foo.cs") | Is.EqualTo("out\\sub\\Foo.cs")); } diff --git a/src/csharp/Grpc.Tools/GeneratorServices.cs b/src/csharp/Grpc.Tools/GeneratorServices.cs index efa67e02ab0..0608bb9d54d 100644 --- a/src/csharp/Grpc.Tools/GeneratorServices.cs +++ b/src/csharp/Grpc.Tools/GeneratorServices.cs @@ -57,9 +57,10 @@ namespace Grpc.Tools // Update OutputDir and GrpcOutputDir for the item and all subsequent // targets using this item. This should only be done if the real // output directories for protoc should be modified. - public virtual void PatchOutputDirectory(ITaskItem protoItem) + public virtual ITaskItem PatchOutputDirectory(ITaskItem protoItem) { // Nothing to do + return protoItem; } public abstract string[] GetPossibleOutputs(ITaskItem protoItem); @@ -116,23 +117,25 @@ namespace Grpc.Tools { public CSharpGeneratorServices(TaskLoggingHelper log) : base(log) { } - public override void PatchOutputDirectory(ITaskItem protoItem) + public override ITaskItem PatchOutputDirectory(ITaskItem protoItem) { - string root = protoItem.GetMetadata(Metadata.ProtoRoot); - string proto = protoItem.ItemSpec; + var outputItem = new TaskItem(protoItem); + string root = outputItem.GetMetadata(Metadata.ProtoRoot); + string proto = outputItem.ItemSpec; string relative = GetRelativeDir(root, proto, Log); - string outdir = protoItem.GetMetadata(Metadata.OutputDir); + string outdir = outputItem.GetMetadata(Metadata.OutputDir); string pathStem = Path.Combine(outdir, relative); - protoItem.SetMetadata(Metadata.OutputDir, pathStem); + outputItem.SetMetadata(Metadata.OutputDir, pathStem); // Override outdir if GrpcOutputDir present, default to proto output. - string grpcdir = protoItem.GetMetadata(Metadata.GrpcOutputDir); + string grpcdir = outputItem.GetMetadata(Metadata.GrpcOutputDir); if (grpcdir != "") { pathStem = Path.Combine(grpcdir, relative); } - protoItem.SetMetadata(Metadata.GrpcOutputDir, pathStem); + outputItem.SetMetadata(Metadata.GrpcOutputDir, pathStem); + return outputItem; } public override string[] GetPossibleOutputs(ITaskItem protoItem) diff --git a/src/csharp/Grpc.Tools/ProtoCompilerOutputs.cs b/src/csharp/Grpc.Tools/ProtoCompilerOutputs.cs index 3300b3ef0c7..0bd9c86a224 100644 --- a/src/csharp/Grpc.Tools/ProtoCompilerOutputs.cs +++ b/src/csharp/Grpc.Tools/ProtoCompilerOutputs.cs @@ -79,15 +79,14 @@ namespace Grpc.Tools var patched = new List(); foreach (var proto in Protobuf) { - // This operates on a local copy and has no effect on the MSBuild instance! - generator.PatchOutputDirectory(proto); - patched.Add(proto); + var patchedProto = generator.PatchOutputDirectory(proto); + patched.Add(patchedProto); - var outputs = generator.GetPossibleOutputs(proto); + var outputs = generator.GetPossibleOutputs(patchedProto); foreach (string output in outputs) { var ti = new TaskItem(output); - ti.SetMetadata(Metadata.Source, proto.ItemSpec); + ti.SetMetadata(Metadata.Source, patchedProto.ItemSpec); possible.Add(ti); } } diff --git a/src/csharp/Grpc.Tools/build/_protobuf/Google.Protobuf.Tools.targets b/src/csharp/Grpc.Tools/build/_protobuf/Google.Protobuf.Tools.targets index db299b29ff0..fa1cf8f9673 100644 --- a/src/csharp/Grpc.Tools/build/_protobuf/Google.Protobuf.Tools.targets +++ b/src/csharp/Grpc.Tools/build/_protobuf/Google.Protobuf.Tools.targets @@ -135,6 +135,7 @@ %(RelativeDir) + $(Protobuf_ProtoRoot)