【发布时间】:2020-04-03 07:03:18
【问题描述】:
我想要实现的目标
在 .NET Core 3.1 的后期构建中运行 Python 3.7 脚本,这样它就可以在 Linux 和 Windows 上开箱即用。
我的假设
- 将在其上构建项目的机器我们将至少安装 2 个 Python 版本,即 2.7 和 3.6+。
- 两个 Python 版本都在 PATH 中。
- 我希望避免任何操作,例如重命名二进制文件或编辑 PATH 等。这应该是开箱即用的,没有任何黑客攻击。
其他问题
要访问脚本,我使用 MSBuild 宏,例如 $(SolutionDir),因此路径脚本将依赖于操作系统,因为 / 和 \
我尝试了什么
我的理解是:并行安装 Python 2.x 和 3.x 确保脚本将使用 Python 3.x 执行的最简单方法是在 Windows 上使用 py -3 在 Linux 上使用 python3 .因为调用 python 将影响使用 Python 2.x 执行脚本。
我试图强制 MSBuild 以至少 3 种不同的方式运行不同的后期构建脚本:
(1)
<ItemDefinitionGroup>
<PostBuildEvent Condition="'$(OS)' == 'Unix' ">
<Message>Uisng post-build scripts for Unix/Linux
</Message>
<Command>python3 $(SolutionDir)BuildTools\PostBuild.py -s $(SolutionDir) -p $(ProjectPath) -c $(ConfigurationName) -t $(TargetDir) -n $(ProjectName)
</Command>
</PostBuildEvent>
<PostBuildEvent Condition="'$(OS)' == 'Windows_NT' ">
<Message>Using post-build scripts for Windows
</Message>
<Command>py -3 $(SolutionDir)BuildTools/PostBuild.py -s $(SolutionDir) -p $(ProjectPath) -c $(ConfigurationName) -t $(TargetDir) -n $(ProjectName)
</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
(2)
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(OS)' == 'Windows_NT'">
<Exec Command="py -3 $(SolutionDir)BuildTools\PostBuild.py -s $(SolutionDir) -p $(ProjectPath) -c $(ConfigurationName) -t $(TargetDir) -n $(ProjectName) -o $(TargetPath) -f $(TargetFileName)" />
</Target>
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(OS)' == 'Unix'">
<Exec Command="python3 $(SolutionDir)BuildTools/PostBuild.py -s $(SolutionDir) -p $(ProjectPath) -c $(ConfigurationName) -t $(TargetDir) -n $(ProjectName) -o $(TargetPath) -f $(TargetFileName)" />
</Target>
(3)
<PropertyGroup>
(...)
<IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">true</IsWindows>
<IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">true</IsLinux>
</PropertyGroup>
然后将此属性用于 (2) 和 (3) 中的条件。
但如果这些有效,则没有。我错过了什么?或者我做错了什么?也许还有其他方法可以达到同样的效果?
非常感谢您的帮助! :)
【问题讨论】:
标签: python .net python-3.x .net-core msbuild