Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #15 from xamarin/dev/sandy/consolidation
Browse files Browse the repository at this point in the history
Consolidation improvements
  • Loading branch information
sandyarmstrong authored Oct 22, 2021
2 parents f6f0d43 + cb82a0f commit 75f5d9e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "2.1.500"
"version": "2.1.505"
},
"msbuild-sdks": {
"Microsoft.Build.Traversal": "1.0.45"
Expand Down
5 changes: 5 additions & 0 deletions src/Xamarin.MSBuild.Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<ItemGroup>
<Reference Remove="@(Reference)"/>
</ItemGroup>

<ItemGroup>
<PackageReference Remove="@(PackageReference)"/>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Remove="@(EmbeddedResource)"/>
Expand All @@ -49,6 +53,7 @@
<Output TaskParameter="CompileItems" ItemName="Compile"/>
<Output TaskParameter="ProjectReferenceItems" ItemName="ProjectReference"/>
<Output TaskParameter="ReferenceItems" ItemName="Reference"/>
<Output TaskParameter="PackageReferenceItems" ItemName="PackageReference"/>
<Output TaskParameter="EmbeddedResourceItems" ItemName="EmbeddedResource"/>
</PrepareConsolidationProject>
</Target>
Expand Down
74 changes: 56 additions & 18 deletions src/Xamarin.MSBuild.Sdk/Tasks/PrepareConsolidationProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public sealed class PrepareConsolidationProject : Task
[Output]
public TaskItem[] ReferenceItems { get; set; }

[Output]
public TaskItem[] PackageReferenceItems { get; set; }

[Output]
public TaskItem[] EmbeddedResourceItems { get; set; }

Expand All @@ -53,23 +56,49 @@ public override bool Execute ()
var compileItems = new List<TaskItem> ();
var projectReferenceItems = new List<TaskItem> ();
var referenceItems = new List<TaskItem> ();
var packageReferenceItems = new List<TaskItem> ();
var embeddedResourceItems = new List<TaskItem> ();

var projectsToConsolidate = dependencyGraph
.TopologicallySortedProjects
.Where (project => {
if (ResolveFullPath (project.ProjectPath) == projectPath)
return false;
var projectsToConsolidate = new List<ProjectDependencyNode> ();
foreach (var project in dependencyGraph.TopologicallySortedProjects) {
if (ResolveFullPath (project.ProjectPath) == projectPath)
continue;

if (string.IsNullOrEmpty (ConsolidationConditionMetadataName)
|| project.ProjectReferenceItems.Any (
pr => bool.TryParse (
pr.GetMetadataValue (ConsolidationConditionMetadataName),
out var consolidate) && consolidate)) {
projectsToConsolidate.Add (project);
} else {
// Keep top-level ProjectReferences that are not marked for
// consolidation. Otherwise they get lost due to the removal
// step in the PrepareConsolidationProject target.
var projectDirectory = Path.GetDirectoryName (project.ProjectPath);
foreach (var item in project.ProjectReferenceItems) {
var itemSpec = item.EvaluatedInclude;
var itemSpecFullPath = ResolveFullPath (
projectDirectory,
itemSpec);
var itemMetadata = GetItemMetadata (item);

if (itemMetadata.Count == 0)
continue;

if (!File.Exists (itemSpecFullPath))
continue;

if (string.IsNullOrEmpty (ConsolidationConditionMetadataName))
return true;
if (allItemSpecs.Contains (itemSpecFullPath))
continue;

return project.ProjectReferenceItems.Any (
pr => bool.TryParse (
pr.GetMetadataValue (ConsolidationConditionMetadataName),
out var consolidate) && consolidate);
})
.ToList ();
allItemSpecs.Add (itemSpecFullPath);

projectReferenceItems.Add (new TaskItem (
itemSpecFullPath,
itemMetadata));
}
}
}

foreach (var project in projectsToConsolidate) {
var projectDirectory = Path.GetDirectoryName (project.ProjectPath);
Expand All @@ -85,11 +114,7 @@ public override bool Execute ()
var itemSpecFullPath = ResolveFullPath (
projectDirectory,
itemSpec);

var itemMetadata = new Dictionary<string, string> (StringComparer.OrdinalIgnoreCase);

foreach (var metadata in item.Metadata)
itemMetadata.Add (metadata.Name, metadata.EvaluatedValue);
var itemMetadata = GetItemMetadata (item);

switch (item.ItemType.ToLowerInvariant()) {
case "compile":
Expand All @@ -106,8 +131,15 @@ public override bool Execute ()

break;
case "reference":
// Skip netstandard facades
if (itemMetadata.TryGetValue ("NuGetPackageId", out var packageId)
&& packageId == "NETStandard.Library")
continue;
collection = referenceItems;
break;
case "packagereference":
collection = packageReferenceItems;
break;
case "embeddedresource":
collection = embeddedResourceItems;
useItemSpecFullPath = true;
Expand Down Expand Up @@ -150,10 +182,16 @@ public override bool Execute ()
CompileItems = compileItems.ToArray ();
ProjectReferenceItems = projectReferenceItems.ToArray ();
ReferenceItems = referenceItems.ToArray ();
PackageReferenceItems = packageReferenceItems.ToArray ();
EmbeddedResourceItems = embeddedResourceItems.ToArray ();

return true;

Dictionary<string, string> GetItemMetadata (ProjectItem item)
=> item.Metadata.ToDictionary (
i => i.Name,
i => i.EvaluatedValue);

bool ShouldExcludeItem (ProjectItem item)
=> ConsolidateRemoveItemsRegex
?.Where (regexItem => string.Equals (
Expand Down

0 comments on commit 75f5d9e

Please sign in to comment.