【问题标题】:.NET Core 3.1 - Run Python 3.7 post build script in platform independent way.NET Core 3.1 - 以独立于平台的方式运行 Python 3.7 后期构建脚本
【发布时间】: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


    【解决方案1】:

    微软关于在项目文件中声明目标:https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-targets?view=vs-2019#declare-targets-in-the-project-file

    这意味着由于您的两个构建后事件的名称相同,因此第二个构建后事件将隐藏第一个,这意味着当您在 Linux 上运行时,唯一可以运行的构建后是 Linux 脚本机器。

    要使其正常工作,请将您的代码更改为以下内容:

    <Target Name="PostBuildWindows" AfterTargets="PostBuildEvent" Condition="'$(OS)' == 'Windows_NT'">
    

    <Target Name="PostBuildLinux" AfterTargets="PostBuildEvent" Condition="'$(OS)' == 'Unix'">
    

    您可以将名称值更改为您喜欢的任何值,但请确保它解释了它是什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      相关资源
      最近更新 更多