在预构建中生成代码的问题

问题描述 投票:1回答:1

对于我的项目,我需要为GRPC自动生成代码。我使用.bat / .sh文件生成此代码。

但是当我在预构建中生成代码时,代码会因为包含错误而失败。

然后所有错误显示op,但它们很快就消失了,因为现在它找到生成的代码并且没有错误。

我尝试使用脚本名称作为prebuild命令运行脚本,其中构建确定脚本的扩展名。

我也尝试从python运行命令,甚至在生成完成后添加一个睡眠,但遗憾的是没有任何效果。

visual studio中的预构建脚本:

python generate.py

python脚本:

import sys
import os
import time
print("Working dir: " + os.getcwd())
if("win" in sys.platform):
    os.system('generate.bat')
else:
    os.system("bash \"" + os.path.dirname(os.path.realpath(__file__)) + '/generate.sh' + "\"")
print("done installing")
time.sleep(5)
print("done finalising")

构建输出:

1>done installing
1>done finalising
1>CSharp\TimelineEvents.cs(18,32,18,48): error CS0246: The type or namespace name 'PayloadOneofCase' could not be found (are you missing a using directive or an assembly reference?)
1>CSharp\FrontendEndpointGrpc.cs(28,31,28,51): error CS0246: The type or namespace name 'RegisterFileResponse' could not be found (are you missing a using directive or an assembly reference?)
1>Done building project "Protocol.csproj" -- FAILED.

我希望构建等待预构建,但似乎并非如此,因为当你第一次运行生成时构建失败,但是当我再次构建它时,它运行得很好

c# python
1个回答
1
投票

我通过更改项目文件解决了这个问题:

  <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="python generate.py" />

对此:

  <Target Name="Generate" BeforeTargets="BeforeBuild">
    <Message Text="Generating GRPC code" />
    <Exec Command="python generate.py" />
    <ItemGroup>
      <Compile Include="CSharp\**\*.cs" />
    </ItemGroup>

这解决了我的问题

© www.soinside.com 2019 - 2024. All rights reserved.