Add support for Protobuf_ProtoRoot

pull/22869/head
Kraemer, Benjamin 5 years ago
parent 446f72d7a3
commit e165a33048
  1. 4
      src/csharp/Grpc.Tools.Tests/CSharpGeneratorTest.cs
  2. 19
      src/csharp/Grpc.Tools/GeneratorServices.cs
  3. 9
      src/csharp/Grpc.Tools/ProtoCompilerOutputs.cs
  4. 1
      src/csharp/Grpc.Tools/build/_protobuf/Google.Protobuf.Tools.targets

@ -89,8 +89,8 @@ namespace Grpc.Tools.Tests
public void OutputDirPatched() public void OutputDirPatched()
{ {
var item = Utils.MakeItem("sub/foo.proto", "OutputDir", "out"); var item = Utils.MakeItem("sub/foo.proto", "OutputDir", "out");
_generator.PatchOutputDirectory(item); var output = _generator.PatchOutputDirectory(item);
var poss = _generator.GetPossibleOutputs(item); var poss = _generator.GetPossibleOutputs(output);
Assert.AreEqual(1, poss.Length); Assert.AreEqual(1, poss.Length);
Assert.That(poss[0], Is.EqualTo("out/sub/Foo.cs") | Is.EqualTo("out\\sub\\Foo.cs")); Assert.That(poss[0], Is.EqualTo("out/sub/Foo.cs") | Is.EqualTo("out\\sub\\Foo.cs"));
} }

@ -57,9 +57,10 @@ namespace Grpc.Tools
// Update OutputDir and GrpcOutputDir for the item and all subsequent // Update OutputDir and GrpcOutputDir for the item and all subsequent
// targets using this item. This should only be done if the real // targets using this item. This should only be done if the real
// output directories for protoc should be modified. // output directories for protoc should be modified.
public virtual void PatchOutputDirectory(ITaskItem protoItem) public virtual ITaskItem PatchOutputDirectory(ITaskItem protoItem)
{ {
// Nothing to do // Nothing to do
return protoItem;
} }
public abstract string[] GetPossibleOutputs(ITaskItem protoItem); public abstract string[] GetPossibleOutputs(ITaskItem protoItem);
@ -116,23 +117,25 @@ namespace Grpc.Tools
{ {
public CSharpGeneratorServices(TaskLoggingHelper log) : base(log) { } public CSharpGeneratorServices(TaskLoggingHelper log) : base(log) { }
public override void PatchOutputDirectory(ITaskItem protoItem) public override ITaskItem PatchOutputDirectory(ITaskItem protoItem)
{ {
string root = protoItem.GetMetadata(Metadata.ProtoRoot); var outputItem = new TaskItem(protoItem);
string proto = protoItem.ItemSpec; string root = outputItem.GetMetadata(Metadata.ProtoRoot);
string proto = outputItem.ItemSpec;
string relative = GetRelativeDir(root, proto, Log); string relative = GetRelativeDir(root, proto, Log);
string outdir = protoItem.GetMetadata(Metadata.OutputDir); string outdir = outputItem.GetMetadata(Metadata.OutputDir);
string pathStem = Path.Combine(outdir, relative); 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. // Override outdir if GrpcOutputDir present, default to proto output.
string grpcdir = protoItem.GetMetadata(Metadata.GrpcOutputDir); string grpcdir = outputItem.GetMetadata(Metadata.GrpcOutputDir);
if (grpcdir != "") if (grpcdir != "")
{ {
pathStem = Path.Combine(grpcdir, relative); pathStem = Path.Combine(grpcdir, relative);
} }
protoItem.SetMetadata(Metadata.GrpcOutputDir, pathStem); outputItem.SetMetadata(Metadata.GrpcOutputDir, pathStem);
return outputItem;
} }
public override string[] GetPossibleOutputs(ITaskItem protoItem) public override string[] GetPossibleOutputs(ITaskItem protoItem)

@ -79,15 +79,14 @@ namespace Grpc.Tools
var patched = new List<ITaskItem>(); var patched = new List<ITaskItem>();
foreach (var proto in Protobuf) foreach (var proto in Protobuf)
{ {
// This operates on a local copy and has no effect on the MSBuild instance! var patchedProto = generator.PatchOutputDirectory(proto);
generator.PatchOutputDirectory(proto); patched.Add(patchedProto);
patched.Add(proto);
var outputs = generator.GetPossibleOutputs(proto); var outputs = generator.GetPossibleOutputs(patchedProto);
foreach (string output in outputs) foreach (string output in outputs)
{ {
var ti = new TaskItem(output); var ti = new TaskItem(output);
ti.SetMetadata(Metadata.Source, proto.ItemSpec); ti.SetMetadata(Metadata.Source, patchedProto.ItemSpec);
possible.Add(ti); possible.Add(ti);
} }
} }

@ -135,6 +135,7 @@
<!-- Out-of-project files will have respective ProtoRoot='%(RelativeDir)'. --> <!-- Out-of-project files will have respective ProtoRoot='%(RelativeDir)'. -->
<Protobuf_Rooted Include="@(_Protobuf_NoRootElsewhere)"> <Protobuf_Rooted Include="@(_Protobuf_NoRootElsewhere)">
<ProtoRoot>%(RelativeDir)</ProtoRoot> <ProtoRoot>%(RelativeDir)</ProtoRoot>
<ProtoRoot Condition=" '$(Protobuf_ProtoRoot)' != '' ">$(Protobuf_ProtoRoot)</ProtoRoot>
</Protobuf_Rooted> </Protobuf_Rooted>
</ItemGroup> </ItemGroup>
</Target> </Target>

Loading…
Cancel
Save