.NET Core 3.1 - 运行 Python 3.7 post 以独立于平台的方式构建脚本
.NET Core 3.1 - Run Python 3.7 post build script in platform independent way
我想达到的目标
运行 Python post 上的 3.7 脚本 - 在 .NET Core 3.1 中构建,因此它在 Linux 和 Windows 上开箱即用.
我的假设
- 将在其上构建项目的机器我们将至少安装 2 个版本的 Python,即 2.7 和 3.6+。
- 两个 Python 版本都在 PATH 中。
- 我想避免任何操作,例如重命名二进制文件或编辑 PATH 等。这应该开箱即用,没有任何黑客攻击。
附加问题
为了访问脚本,我使用了 MSBuild 宏,例如 $(SolutionDir)
,因此路径脚本将 OS 相关,因为 /
和 \
我试过的
我的理解是:并行安装 Python 2.x 和 3.x 确保脚本将使用 Python [=67 执行的最简单方法=] 是在 Windows 上使用 py -3
,在 Linux 上使用 python3
。因为调用 python
将影响使用 Python 2.x.
执行脚本
我试图以至少 3 种不同的方式强制 MSBuild 运行 不同的 post 构建脚本:
(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) 中的条件。
但是 none 如果这些有效。我错过了什么?或者我做错了什么?也许还有其他方法可以达到同样的效果?
非常感谢您的帮助! :)
Microsoft 关于在项目文件中声明目标:https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-targets?view=vs-2019#declare-targets-in-the-project-file
这意味着由于您的两个 post-build 事件的名称相同,因此第二个 post-build 将隐藏第一个,这意味着唯一的 post-build当您在 Linux 机器上 运行 时,运行 可能是 Linux 脚本。
要使其正常工作,请将您的代码更改为如下内容:
<Target Name="PostBuildWindows" AfterTargets="PostBuildEvent" Condition="'$(OS)' == 'Windows_NT'">
<Target Name="PostBuildLinux" AfterTargets="PostBuildEvent" Condition="'$(OS)' == 'Unix'">
您可以将名称值更改为任何您喜欢的值,但请确保它解释了它是什么。
我想达到的目标
运行 Python post 上的 3.7 脚本 - 在 .NET Core 3.1 中构建,因此它在 Linux 和 Windows 上开箱即用.
我的假设
- 将在其上构建项目的机器我们将至少安装 2 个版本的 Python,即 2.7 和 3.6+。
- 两个 Python 版本都在 PATH 中。
- 我想避免任何操作,例如重命名二进制文件或编辑 PATH 等。这应该开箱即用,没有任何黑客攻击。
附加问题
为了访问脚本,我使用了 MSBuild 宏,例如 $(SolutionDir)
,因此路径脚本将 OS 相关,因为 /
和 \
我试过的
我的理解是:并行安装 Python 2.x 和 3.x 确保脚本将使用 Python [=67 执行的最简单方法=] 是在 Windows 上使用 py -3
,在 Linux 上使用 python3
。因为调用 python
将影响使用 Python 2.x.
我试图以至少 3 种不同的方式强制 MSBuild 运行 不同的 post 构建脚本:
(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) 中的条件。
但是 none 如果这些有效。我错过了什么?或者我做错了什么?也许还有其他方法可以达到同样的效果?
非常感谢您的帮助! :)
Microsoft 关于在项目文件中声明目标:https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-targets?view=vs-2019#declare-targets-in-the-project-file
这意味着由于您的两个 post-build 事件的名称相同,因此第二个 post-build 将隐藏第一个,这意味着唯一的 post-build当您在 Linux 机器上 运行 时,运行 可能是 Linux 脚本。
要使其正常工作,请将您的代码更改为如下内容:
<Target Name="PostBuildWindows" AfterTargets="PostBuildEvent" Condition="'$(OS)' == 'Windows_NT'">
<Target Name="PostBuildLinux" AfterTargets="PostBuildEvent" Condition="'$(OS)' == 'Unix'">
您可以将名称值更改为任何您喜欢的值,但请确保它解释了它是什么。