Skip to content

Commit

Permalink
Fix MSBuild project evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
matkoch committed Jul 9, 2020
1 parent 145b96e commit 118af17
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build/_build.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\source\Nuke.Common\Nuke.Common.props" />

Expand Down
9 changes: 5 additions & 4 deletions source/Nuke.Common/Nuke.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
<PackageReference Include="Glob" Version="1.1.5" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Microsoft.Azure.KeyVault" Version="3.0.4" />
<PackageReference Include="Microsoft.Build" Version="16.3.0" />
<PackageReference Include="Microsoft.Build.Framework" Version="16.3.0" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="16.3.0" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="16.3.0" />
<PackageReference Include="Microsoft.Build" Version="16.3.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Framework" Version="16.3.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.2.6" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="16.3.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="16.3.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="5.2.4" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NuGet.Packaging" Version="5.3.1" />
Expand Down
76 changes: 44 additions & 32 deletions source/Nuke.Common/ProjectModel/ProjectModelTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using JetBrains.Annotations;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Locator;
using Nuke.Common.IO;
using Nuke.Common.Tooling;
using Nuke.Common.Utilities;
Expand All @@ -17,6 +18,27 @@ namespace Nuke.Common.ProjectModel
[PublicAPI]
public static class ProjectModelTasks
{
static ProjectModelTasks()
{
try
{
MSBuildLocator.RegisterDefaults();
}
catch (Exception exception)
{
Logger.Warn(exception);

var dotnet = ToolPathResolver.TryGetEnvironmentExecutable("DOTNET_EXE") ??
ToolPathResolver.GetPathExecutable("dotnet");
var output = ProcessTasks.StartProcess(dotnet, "--info", logOutput: false).AssertZeroExitCode().Output;
var basePath = (AbsolutePath) output
.Select(x => x.Text.Trim())
.Single(x => x.StartsWith("Base Path:"))
.TrimStart("Base Path:").Trim();
MSBuildLocator.RegisterMSBuildPath(basePath);
}
}

public static Solution CreateSolution(string fileName = null, params Solution[] solutions)
{
return CreateSolution(fileName, solutions, folderNameProvider: null);
Expand Down Expand Up @@ -84,48 +106,38 @@ public static Microsoft.Build.Evaluation.Project ParseProject(
string configuration = null,
string targetFramework = null)
{
var msbuildPath = Environment.GetEnvironmentVariable("MSBUILD_EXE_PATH");

try
{
if (string.IsNullOrEmpty(msbuildPath))
Environment.SetEnvironmentVariable("MSBUILD_EXE_PATH", s_msbuildPathResolver.Value);

var projectCollection = new ProjectCollection();
var projectRoot = Microsoft.Build.Construction.ProjectRootElement.Open(projectFile, projectCollection, preserveFormatting: true);
var msbuildProject = Microsoft.Build.Evaluation.Project.FromProjectRootElement(projectRoot, new Microsoft.Build.Definition.ProjectOptions
var projectCollection = new ProjectCollection();
var projectRoot = Microsoft.Build.Construction.ProjectRootElement.Open(projectFile, projectCollection, preserveFormatting: true);
var msbuildProject = Microsoft.Build.Evaluation.Project.FromProjectRootElement(
projectRoot,
new Microsoft.Build.Definition.ProjectOptions
{
GlobalProperties = GetProperties(configuration, targetFramework),
ToolsVersion = projectCollection.DefaultToolsVersion,
ProjectCollection = projectCollection
});

var targetFrameworks = msbuildProject.AllEvaluatedItems
.Where(x => x.ItemType == "_TargetFrameworks")
.Select(x => x.EvaluatedInclude)
.OrderBy(x => x).ToList();

if (targetFramework == null && targetFrameworks.Count > 1)
{
projectCollection.UnloadProject(msbuildProject);
targetFramework = targetFrameworks.First();
var targetFrameworks = msbuildProject.AllEvaluatedItems
.Where(x => x.ItemType == "_TargetFrameworks")
.Select(x => x.EvaluatedInclude)
.OrderBy(x => x).ToList();

Logger.Warn($"Project '{projectFile}' has multiple target frameworks ({targetFrameworks.JoinComma()}).");
Logger.Warn($"Evaluating using '{targetFramework}'...");
if (targetFramework == null && targetFrameworks.Count > 1)
{
projectCollection.UnloadProject(msbuildProject);
targetFramework = targetFrameworks.First();

msbuildProject = new Microsoft.Build.Evaluation.Project(
projectFile,
GetProperties(configuration, targetFramework),
projectCollection.DefaultToolsVersion,
projectCollection);
}
Logger.Warn($"Project '{projectFile}' has multiple target frameworks ({targetFrameworks.JoinComma()}).");
Logger.Warn($"Evaluating using '{targetFramework}'...");

return msbuildProject;
}
finally
{
Environment.SetEnvironmentVariable("MSBUILD_EXE_PATH", msbuildPath);
msbuildProject = new Microsoft.Build.Evaluation.Project(
projectFile,
GetProperties(configuration, targetFramework),
projectCollection.DefaultToolsVersion,
projectCollection);
}

return msbuildProject;
}

private static Dictionary<string, string> GetProperties([CanBeNull] string configuration, [CanBeNull] string targetFramework)
Expand Down

0 comments on commit 118af17

Please sign in to comment.