Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add way to LoadProject with caches ect #11234

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions src/Build/Definition/ProjectCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Collections;
using Microsoft.Build.Construction;
using Microsoft.Build.Definition;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Internal;
Expand Down Expand Up @@ -1156,15 +1157,30 @@ public Project LoadProject(string fileName, string toolsVersion)
/// <param name="toolsVersion">The tools version. May be null.</param>
/// <returns>A loaded project.</returns>
public Project LoadProject(string fileName, IDictionary<string, string> globalProperties, string toolsVersion)
{
return LoadProjectWithOptions(fileName, new ProjectOptions { GlobalProperties = globalProperties, ToolsVersion = toolsVersion });
}

/// <summary>
/// Loads a project with the specified filename, tools version and global properties.
/// If a matching project is already loaded, it will be returned, otherwise a new project will be loaded.
/// </summary>
/// <param name="fileName">The project file to load</param>
/// <param name="projectOptions">The <see cref="ProjectOptions"/> to use.</param>
/// <returns>A loaded project.</returns>
public Project LoadProjectWithOptions(string fileName, ProjectOptions projectOptions)
{
ErrorUtilities.VerifyThrowArgumentLength(fileName);
fileName = FileUtilities.NormalizePath(fileName);

using (_locker.EnterDisposableWriteLock())
{
if (globalProperties == null)
// We can only work on this Collection.
projectOptions.ProjectCollection = this;

if (projectOptions.GlobalProperties == null)
{
globalProperties = GlobalProperties;
projectOptions.GlobalProperties = GlobalProperties;
}
else
{
Expand All @@ -1174,9 +1190,9 @@ public Project LoadProject(string fileName, IDictionary<string, string> globalPr
// BUT remember that project global properties win -- don't override a property that already exists.
foreach (KeyValuePair<string, string> globalProperty in GlobalProperties)
{
if (!globalProperties.ContainsKey(globalProperty.Key))
if (!projectOptions.GlobalProperties.ContainsKey(globalProperty.Key))
{
globalProperties.Add(globalProperty);
projectOptions.GlobalProperties.Add(globalProperty);
}
}
}
Expand All @@ -1185,7 +1201,7 @@ public Project LoadProject(string fileName, IDictionary<string, string> globalPr
// passed a relative path, the caller assumes we will prepend the current directory.
string toolsVersionFromProject = null;

if (toolsVersion == null)
if (projectOptions.ToolsVersion == null)
{
// Load the project XML to get any ToolsVersion attribute.
// If there isn't already an equivalent project loaded, the real load we'll do will be satisfied from the cache.
Expand All @@ -1194,7 +1210,7 @@ public Project LoadProject(string fileName, IDictionary<string, string> globalPr
// Either way, no time wasted.
try
{
ProjectRootElement xml = ProjectRootElement.OpenProjectOrSolution(fileName, globalProperties, toolsVersion, ProjectRootElementCache, true /*explicitlyloaded*/);
ProjectRootElement xml = ProjectRootElement.OpenProjectOrSolution(fileName, projectOptions.GlobalProperties, projectOptions.ToolsVersion, ProjectRootElementCache, true /*explicitlyloaded*/);
toolsVersionFromProject = (xml.ToolsVersion.Length > 0) ? xml.ToolsVersion : DefaultToolsVersion;
}
catch (InvalidProjectFileException ex)
Expand All @@ -1205,14 +1221,14 @@ public Project LoadProject(string fileName, IDictionary<string, string> globalPr
}
}

string effectiveToolsVersion = Utilities.GenerateToolsVersionToUse(toolsVersion, toolsVersionFromProject, GetToolset, DefaultToolsVersion, out _);
Project project = _loadedProjects.GetMatchingProjectIfAny(fileName, globalProperties, effectiveToolsVersion);
string effectiveToolsVersion = Utilities.GenerateToolsVersionToUse(projectOptions.ToolsVersion, toolsVersionFromProject, GetToolset, DefaultToolsVersion, out _);
Project project = _loadedProjects.GetMatchingProjectIfAny(fileName, projectOptions.GlobalProperties, effectiveToolsVersion);

if (project == null)
{
// The Project constructor adds itself to our collection,
// it is not done by us
project = new Project(fileName, globalProperties, effectiveToolsVersion, this);
project = Project.FromFile(fileName, projectOptions);
}

return project;
Expand Down
Loading