csproj 可以检测是否使用 `dotnet` 或 `msbuild`/`devenv` 作为条件吗?

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

我们有一个包含一些 WiX 3 项目的解决方案,当旧式 csproj 尝试导入

dotnet restore
时,这些项目会破坏
dotnet test
Wix.CA.targets
等命令 (https://github.com/wixtoolset/issues/issues/5627 )。
dotnet restore
似乎很重要,因为较新版本的 Paket 使用它,并且我们希望在同一 sln/存储库根目录中包含一些 .NET 8 项目。

我不认为

Wix.CA.targets
对包恢复有任何重要作用,而且它不是单元测试,我们仍在使用 VS 或 MSBuild 进行实际构建。

我们是否可以在

.csproj
文件中使用某些条件来检测
dotnet ...
何时加载该文件并排除导入?

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WixCATargetsPath Condition=" '$(WixCATargetsPath)' == '' ">C:\Program Files (x86)\MSBuild\Microsoft\WiX\v3.x\Wix.CA.targets</WixCATargetsPath>
    ...
  </PropertyGroup>
  ...
  <Import Project="$(WixCATargetsPath)" Condition="NotDotnetCommand" />
</Project>

> dotnet restore demo.sln
C:\Program Files (x86)\MSBuild\Microsoft\WiX\v3.x\Wix.CA.targets(42,5): error MSB4062: The "ReadRegistry" task
could not be loaded from the assembly C:\Program Files (x86)\WiX Toolset v3.11\bin\WixTasks.dll. Could not load
 file or assembly 'Microsoft.Build.Utilities, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
'. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, that the ass
embly and all its dependencies are available, and that the task contains a public class that implements Microso
ft.Build.Framework.ITask. [C:\Demo\Wix\Wix.csproj]
msbuild csproj dotnet-cli
1个回答
0
投票

没有内置属性可以告诉您您的项目正在通过

dotnet
工具运行。

但是,您可以使用

$([System.Environment]::CommandLine)
获得完整的命令行。

<Project>
  <Target Name="CmdLine">
    <Message Text="$([System.Environment]::CommandLine)"/>
  </Target>
</Project>

如果该项目名为 example.proj,您可以使用以下命令测试运行它:

msbuild Example.proj
dotnet build Example.proj /v:n
dotnet msbuild Example.proj /v:n

在 Windows 上的测试中,

msbuild
命令显示
msbuild
作为可执行文件,但
dotnet
命令显示
"C:\Program Files\dotnet\sdk\8.0.101\dotnet.dll"
作为可执行文件。

例如

"C:\Program Files\dotnet\sdk\8.0.101\dotnet.dll" build Example.proj /v:n

不过,您可以根据命令行中的可执行文件是否包含

isDotnetCommand
来初始化
dotnet
属性。

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