【发布时间】:2018-09-14 21:54:31
【问题描述】:
我正在尝试使用 dotnet core 2.1 在 dotnet 构建过程中运行 protobuf 的 protoc(这似乎或多或少适用于 2.0,但我们希望利用 2.1 的功能)。 .csproj 文件有一个被盗的 sn-p 来运行它:
<!-- https://github.com/protocolbuffers/protobuf/issues/3820#issuecomment-399538660 -->
<PropertyGroup>
<protoc_linux64>tools\linux_x64\protoc</protoc_linux64>
<protoc_linux86>tools\linux_x86\protoc</protoc_linux86>
<protoc_macosx64>tools\macosx_x64\protoc</protoc_macosx64>
<protoc_macosx86>tools\macosx_x86\protoc</protoc_macosx86>
<protoc_windows64>tools\windows_x64\protoc.exe</protoc_windows64>
<protoc_windows86>tools\windows_x86\protoc.exe</protoc_windows86>
</PropertyGroup>
<PropertyGroup Condition=" '$(MSBuildRuntimeType)' == 'Core' or
 ('$(MSBuildVersion)' != '' and
 $([System.Version]::Parse($(MSBuildVersion))) >= $([System.Version]::Parse(15.3))) ">
<protoc Condition="'$([MSBuild]::IsOsPlatform(Linux))' And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X64'">$(protoc_linux64)</protoc>
<protoc Condition="'$([MSBuild]::IsOsPlatform(Linux))' And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X86'">$(protoc_linux86)</protoc>
<protoc Condition="'$([MSBuild]::IsOsPlatform(OSX))' And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X64'">$(protoc_macosx64)</protoc>
<protoc Condition="'$([MSBuild]::IsOsPlatform(OSX))' And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X86'">$(protoc_macosx86)</protoc>
<protoc Condition="'$([MSBuild]::IsOsPlatform(Windows))' And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X64'">$(protoc_windows64)</protoc>
<protoc Condition="'$([MSBuild]::IsOsPlatform(Windows))' And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X86'">$(protoc_windows86)</protoc>
</PropertyGroup>
<Target Name="BuildProto" BeforeTargets="Compile;Restore">
<ItemGroup>
<Protos Include="$(ProjectDir)Protobuf\Protos\*.proto" />
</ItemGroup>
<Message Importance="high" Text="protobuffing" />
<Exec Command="$(NuGetPackageRoot)google.protobuf.tools\3.6.1\$(protoc) --proto_path=$(ProjectDir) --csharp_out=$(ProjectDir)Protobuf\ --csharp_opt=file_extension=.g.cs @(Protos, ' ')" />
</Target>
当它运行时,它会起作用。生成了.g.cs 文件,一切都很好。但是,如果我从一个干净的构建开始,然后针对解决方案运行构建,则不会生成 .g.cs 文件。之后构建显然会失败,因为无法找到生成的所有内容。
git clean -dxf < /dev/null ; dotnet build MySolution.sln -m:1
当我在输出中搜索 <Message.. 文本(“protobuffing”)时,它不存在。但是,如果我再次运行dotnet build,那么它会在所有“恢复完成”输出之后立即运行“protobuffing”,然后一切正常。 (< /dev/null 的原因是我在 Windows 上运行它,有时 Visual Studio 会在 .vs 目录中锁定一些东西,这在这里应该是无害的,所以它被忽略了。)
我想我可以将 CI 构建服务器设置为运行 dotnet build 两次,但这对我来说似乎是一个 hack。我只是在寻找一种方法,让dotnet build 在 net core 2.1 下运行这个目标,然后再为这个程序集构建 C# 代码。
【问题讨论】: