如何在MSBuild中获取当前的操作系统?

时间:2017-04-19 14:53:02

标签: linux bash msbuild .net-core visual-studio-2017

我有一个使用Visual Studio 2017创建的.NET Core 1.1控制台项目。 构建项目后,我需要运行一个powershell脚本,所以我将以下内容添加到scrollView.wantsLayer = true scrollView.layer?.masksToBounds = true scrollView.contentView.wantsLayer = true scrollView.contentView.layer?.masksToBounds = true 文件中:

MyProject.csproj

现在我需要在Linux环境中构建项目,我需要指示MSBuild在Linux中运行时运行<Target Name="PostcompileScript" AfterTargets="Build"> <Exec Command="execute-tasks.ps1" /> </Target> 而不是execute-tasks.sh

我相信这可以通过execute-tasks.ps1属性实现,但是否有一个包含操作系统名称的MSBuild变量?

3 个答案:

答案 0 :(得分:11)

变量为$(OS),通常根据是否为Windows_NT进行检查:

<Exec Command="./foo.sh" Condition=" '$(OS)' != 'Windows_NT' " />

答案 1 :(得分:1)

如其他答案中所述,使用$(OS)可以区分Windows_NTUnix(包括Linux和macOS),但不能区分不同的类Unix系统。如果您使用的是MSBuild 15.3或更高版本(很有可能),则可能要考虑使用[MSBuild]::IsOsPlatform()

<Exec Command="./foo.sh" Condition="$([MSBuild]::IsOSPlatform('Linux'))" />

该参数可以是任何OsPlatform成员的名称。

答案 2 :(得分:0)

你可以使用这样的目标。似乎操作系统版本不是MSBUILD中的环境变量。

 <Target Name="AfterBuild">
          <Exec Command="foo.exe arg1 arg2" Condition=" '$(OS)' == 'Windows_NT' " />
 </Target>

或另一个例子,这可能很麻烦。

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="OSVersion" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <OsVersion>$(registry:HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion@CurrentVersion).$(registry:HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion@CurrentBuildNumber)</OsVersion>
  </PropertyGroup>
  <Target Name="OSVersion">
    <Message Text="Version: $(OsVersion)" />
  </Target>
</Project>
相关问题