Skip to content

Commit d253daf

Browse files
committed
Unify more build and publish logic
1 parent 4f3ec61 commit d253daf

File tree

7 files changed

+143
-102
lines changed

7 files changed

+143
-102
lines changed

src/Tasks/Common/MetadataKeys.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ internal static class MetadataKeys
6565
public const string NuGetSourceType = "NuGetSourceType";
6666
public const string PathInPackage = "PathInPackage";
6767
public const string PackageDirectory = "PackageDirectory";
68+
public const string Publish = "Publish";
6869

6970
// References
7071
public const string ExternallyResolved = "ExternallyResolved";

src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/Mocks/MockTaskItem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public string GetMetadata(string metadataName)
5858
string metadataValue = null;
5959
if (_metadata.TryGetValue(metadataName, out metadataValue))
6060
{
61-
return metadataValue;
61+
return metadataValue ?? string.Empty;
6262
}
6363

64-
return null;
64+
return string.Empty;
6565
}
6666

6767
public void RemoveMetadata(string metadataName)

src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageAssets.cs

Lines changed: 94 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public sealed class ResolvePackageAssets : TaskBase
142142
/// </summary>
143143
public ITaskItem[] ShimRuntimeIdentifiers { get; set; }
144144

145-
public ITaskItem[] ExcludeFromOutputPackageReferences { get; set; }
145+
public ITaskItem[] PackageReferences { get; set; }
146146

147147
/// <summary>
148148
/// The file name of Apphost asset.
@@ -385,12 +385,13 @@ internal byte[] HashSettings()
385385
writer.Write(EmitAssetsLogMessages);
386386
writer.Write(EnsureRuntimePackageDependencies);
387387
writer.Write(MarkPackageReferencesAsExternallyResolved);
388-
if (ExcludeFromOutputPackageReferences != null)
388+
if (PackageReferences != null)
389389
{
390-
foreach (var excludedPackage in ExcludeFromOutputPackageReferences)
390+
foreach (var packageReference in PackageReferences)
391391
{
392-
writer.Write(excludedPackage.ItemSpec ?? "");
393-
writer.Write(excludedPackage.GetMetadata(MetadataKeys.Version) ?? "");
392+
writer.Write(packageReference.ItemSpec ?? "");
393+
writer.Write(packageReference.GetMetadata(MetadataKeys.Version));
394+
writer.Write(packageReference.GetMetadata(MetadataKeys.Publish));
394395
}
395396
}
396397
if (ExpectedPlatformPackages != null)
@@ -628,6 +629,7 @@ private sealed class CacheWriter : IDisposable
628629
private List<string> _metadataStrings;
629630
private List<int> _bufferedMetadata;
630631
private HashSet<string> _copyLocalPackageExclusions;
632+
private HashSet<string> _publishPackageExclusions;
631633
private Placeholder _metadataStringTablePosition;
632634
private NuGetFramework _targetFramework;
633635
private int _itemCount;
@@ -679,7 +681,7 @@ public CacheWriter(ResolvePackageAssets task)
679681
_stringTable = new Dictionary<string, int>(InitialStringTableCapacity, StringComparer.Ordinal);
680682
_metadataStrings = new List<string>(InitialStringTableCapacity);
681683
_bufferedMetadata = new List<int>();
682-
_copyLocalPackageExclusions = GetCopyLocalPackageExclusions();
684+
ComputePackageExclusions();
683685
}
684686

685687
public void WriteToCacheFile()
@@ -1022,10 +1024,7 @@ private void WriteNativeLibraries()
10221024
writeMetadata: (package, asset) =>
10231025
{
10241026
WriteMetadata(MetadataKeys.AssetType, "native");
1025-
if (ShouldCopyLocalPackageAssets(package))
1026-
{
1027-
WriteCopyLocalMetadata(package, Path.GetFileName(asset.Path));
1028-
}
1027+
WriteCopyLocalMetadataIfNeeded(package, Path.GetFileName(asset.Path));
10291028
});
10301029
}
10311030

@@ -1098,14 +1097,11 @@ private void WriteResourceAssemblies()
10981097
{
10991098
WriteMetadata(MetadataKeys.AssetType, "resources");
11001099
string locale = asset.Properties["locale"];
1101-
if (ShouldCopyLocalPackageAssets(package))
1102-
{
1103-
WriteCopyLocalMetadata(
1100+
bool wroteCopyLocalMetadata = WriteCopyLocalMetadataIfNeeded(
11041101
package,
11051102
Path.GetFileName(asset.Path),
11061103
destinationSubDirectory: locale + Path.DirectorySeparatorChar);
1107-
}
1108-
else
1104+
if (!wroteCopyLocalMetadata)
11091105
{
11101106
WriteMetadata(MetadataKeys.DestinationSubDirectory, locale + Path.DirectorySeparatorChar);
11111107
}
@@ -1121,10 +1117,7 @@ private void WriteRuntimeAssemblies()
11211117
writeMetadata: (package, asset) =>
11221118
{
11231119
WriteMetadata(MetadataKeys.AssetType, "runtime");
1124-
if (ShouldCopyLocalPackageAssets(package))
1125-
{
1126-
WriteCopyLocalMetadata(package, Path.GetFileName(asset.Path));
1127-
}
1120+
WriteCopyLocalMetadataIfNeeded(package, Path.GetFileName(asset.Path));
11281121
});
11291122
}
11301123

@@ -1136,14 +1129,15 @@ private void WriteRuntimeTargets()
11361129
writeMetadata: (package, asset) =>
11371130
{
11381131
WriteMetadata(MetadataKeys.AssetType, asset.AssetType.ToLowerInvariant());
1139-
if (_task.CopyLocalRuntimeTargetAssets && ShouldCopyLocalPackageAssets(package))
1132+
bool wroteCopyLocalMetadata = false;
1133+
if (_task.CopyLocalRuntimeTargetAssets)
11401134
{
1141-
WriteCopyLocalMetadata(
1135+
wroteCopyLocalMetadata = WriteCopyLocalMetadataIfNeeded(
11421136
package,
11431137
Path.GetFileName(asset.Path),
11441138
destinationSubDirectory: Path.GetDirectoryName(asset.Path) + Path.DirectorySeparatorChar);
11451139
}
1146-
else
1140+
if (!wroteCopyLocalMetadata)
11471141
{
11481142
WriteMetadata(MetadataKeys.DestinationSubDirectory, Path.GetDirectoryName(asset.Path) + Path.DirectorySeparatorChar);
11491143
}
@@ -1260,9 +1254,32 @@ private void WriteMetadata(string key, string value)
12601254
}
12611255
}
12621256

1263-
private void WriteCopyLocalMetadata(LockFileTargetLibrary package, string assetsFileName, string destinationSubDirectory = null)
1257+
private bool WriteCopyLocalMetadataIfNeeded(LockFileTargetLibrary package, string assetsFileName, string destinationSubDirectory = null)
12641258
{
1265-
WriteMetadata(MetadataKeys.CopyLocal, "true");
1259+
bool shouldCopyLocal = true;
1260+
if (_copyLocalPackageExclusions != null && _copyLocalPackageExclusions.Contains(package.Name))
1261+
{
1262+
shouldCopyLocal = false;
1263+
}
1264+
bool shouldIncludeInPublish = shouldCopyLocal;
1265+
if (shouldIncludeInPublish && _publishPackageExclusions != null && _publishPackageExclusions.Contains(package.Name))
1266+
{
1267+
shouldIncludeInPublish = false;
1268+
}
1269+
1270+
if (shouldCopyLocal == false && shouldIncludeInPublish == false)
1271+
{
1272+
return false;
1273+
}
1274+
1275+
if (shouldCopyLocal)
1276+
{
1277+
WriteMetadata(MetadataKeys.CopyLocal, "true");
1278+
if (!shouldIncludeInPublish)
1279+
{
1280+
WriteMetadata(MetadataKeys.CopyToPublishDirectory, "false");
1281+
}
1282+
}
12661283
WriteMetadata(
12671284
MetadataKeys.DestinationSubPath,
12681285
string.IsNullOrEmpty(destinationSubDirectory) ?
@@ -1272,6 +1289,8 @@ private void WriteCopyLocalMetadata(LockFileTargetLibrary package, string assets
12721289
{
12731290
WriteMetadata(MetadataKeys.DestinationSubDirectory, destinationSubDirectory);
12741291
}
1292+
1293+
return true;
12751294
}
12761295

12771296
private int GetMetadataIndex(string value)
@@ -1286,14 +1305,9 @@ private int GetMetadataIndex(string value)
12861305
return index;
12871306
}
12881307

1289-
private bool ShouldCopyLocalPackageAssets(LockFileTargetLibrary package)
1290-
{
1291-
return _copyLocalPackageExclusions == null || !_copyLocalPackageExclusions.Contains(package.Name);
1292-
}
1293-
1294-
private HashSet<string> GetCopyLocalPackageExclusions()
1308+
private void ComputePackageExclusions()
12951309
{
1296-
var packageExclusions = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
1310+
var copyLocalPackageExclusions = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
12971311
var libraryLookup = _runtimeTarget.Libraries.ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase);
12981312

12991313
// Only exclude platform packages for framework-dependent applications
@@ -1304,7 +1318,7 @@ private HashSet<string> GetCopyLocalPackageExclusions()
13041318
var platformLibrary = _runtimeTarget.GetLibrary(_task.PlatformLibraryName);
13051319
if (platformLibrary != null)
13061320
{
1307-
packageExclusions.UnionWith(_runtimeTarget.GetPlatformExclusionList(platformLibrary, libraryLookup));
1321+
copyLocalPackageExclusions.UnionWith(_runtimeTarget.GetPlatformExclusionList(platformLibrary, libraryLookup));
13081322

13091323
// If the platform library is not Microsoft.NETCore.App, treat it as an implicit dependency.
13101324
// This makes it so Microsoft.AspNet.* 2.x platforms also exclude Microsoft.NETCore.App files.
@@ -1313,70 +1327,80 @@ private HashSet<string> GetCopyLocalPackageExclusions()
13131327
var library = _runtimeTarget.GetLibrary(NetCorePlatformLibrary);
13141328
if (library != null)
13151329
{
1316-
packageExclusions.UnionWith(_runtimeTarget.GetPlatformExclusionList(library, libraryLookup));
1330+
copyLocalPackageExclusions.UnionWith(_runtimeTarget.GetPlatformExclusionList(library, libraryLookup));
13171331
}
13181332
}
13191333
}
13201334
}
13211335

1322-
if (_task.ExcludeFromOutputPackageReferences != null && _task.ExcludeFromOutputPackageReferences.Any())
1336+
if (_task.PackageReferences != null)
13231337
{
1324-
var topLevelDependencies = ProjectContext.GetTopLevelDependencies(_lockFile, _runtimeTarget);
1338+
var excludeFromPublishPackageReferences = _task.PackageReferences
1339+
.Where(pr => pr.GetBooleanMetadata(MetadataKeys.Publish) == false)
1340+
.ToList();
13251341

1326-
// Exclude transitive dependencies of excluded packages unless they are also dependencies
1327-
// of non-excluded packages
1342+
if (excludeFromPublishPackageReferences.Any())
1343+
{
13281344

1329-
HashSet<string> includedDependencies = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
1330-
HashSet<string> excludeFromOutputPackageIds = new HashSet<string>(
1331-
_task.ExcludeFromOutputPackageReferences.Select(pr => pr.ItemSpec),
1332-
StringComparer.OrdinalIgnoreCase);
1345+
var topLevelDependencies = ProjectContext.GetTopLevelDependencies(_lockFile, _runtimeTarget);
13331346

1334-
Stack<string> dependenciesToWalk = new Stack<string>(
1335-
topLevelDependencies.Except(excludeFromOutputPackageIds, StringComparer.OrdinalIgnoreCase));
1347+
// Exclude transitive dependencies of excluded packages unless they are also dependencies
1348+
// of non-excluded packages
13361349

1337-
while (dependenciesToWalk.Any())
1338-
{
1339-
var dependencyName = dependenciesToWalk.Pop();
1340-
if (!includedDependencies.Contains(dependencyName))
1350+
HashSet<string> includedDependencies = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
1351+
HashSet<string> excludeFromPublishPackageIds = new HashSet<string>(
1352+
excludeFromPublishPackageReferences.Select(pr => pr.ItemSpec),
1353+
StringComparer.OrdinalIgnoreCase);
1354+
1355+
Stack<string> dependenciesToWalk = new Stack<string>(
1356+
topLevelDependencies.Except(excludeFromPublishPackageIds, StringComparer.OrdinalIgnoreCase));
1357+
1358+
while (dependenciesToWalk.Any())
13411359
{
1342-
// There may not be a library in the assets file if a referenced project has
1343-
// PrivateAssets="all" for a package reference, and there is a package in the graph
1344-
// that depends on the same packge.
1345-
if (libraryLookup.TryGetValue(dependencyName, out var library))
1360+
var dependencyName = dependenciesToWalk.Pop();
1361+
if (!includedDependencies.Contains(dependencyName))
13461362
{
1347-
includedDependencies.Add(dependencyName);
1348-
foreach (var newDependency in library.Dependencies)
1363+
// There may not be a library in the assets file if a referenced project has
1364+
// PrivateAssets="all" for a package reference, and there is a package in the graph
1365+
// that depends on the same packge.
1366+
if (libraryLookup.TryGetValue(dependencyName, out var library))
13491367
{
1350-
dependenciesToWalk.Push(newDependency.Id);
1368+
includedDependencies.Add(dependencyName);
1369+
foreach (var newDependency in library.Dependencies)
1370+
{
1371+
dependenciesToWalk.Push(newDependency.Id);
1372+
}
13511373
}
13521374
}
13531375
}
1354-
}
13551376

1356-
foreach (var library in _runtimeTarget.Libraries)
1357-
{
1358-
// Libraries explicitly marked as exclude from publish should be excluded from
1359-
// publish even if there are other transitive dependencies to them
1360-
if (excludeFromOutputPackageIds.Contains(library.Name))
1377+
var publishPackageExclusions = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
1378+
1379+
foreach (var library in _runtimeTarget.Libraries)
13611380
{
1362-
packageExclusions.Add(library.Name);
1381+
// Libraries explicitly marked as exclude from publish should be excluded from
1382+
// publish even if there are other transitive dependencies to them
1383+
if (publishPackageExclusions.Contains(library.Name))
1384+
{
1385+
publishPackageExclusions.Add(library.Name);
1386+
}
1387+
1388+
if (!includedDependencies.Contains(library.Name))
1389+
{
1390+
publishPackageExclusions.Add(library.Name);
1391+
}
13631392
}
13641393

1365-
if (!includedDependencies.Contains(library.Name))
1394+
if (publishPackageExclusions.Any())
13661395
{
1367-
packageExclusions.Add(library.Name);
1396+
_publishPackageExclusions = publishPackageExclusions;
13681397
}
13691398
}
1370-
13711399
}
13721400

1373-
if (packageExclusions.Any())
1374-
{
1375-
return packageExclusions;
1376-
}
1377-
else
1401+
if (copyLocalPackageExclusions.Any())
13781402
{
1379-
return null;
1403+
_copyLocalPackageExclusions = copyLocalPackageExclusions;
13801404
}
13811405
}
13821406

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.CrossGen.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ Copyright (c) .NET Foundation. All rights reserved.
259259
_SetupStageForCrossgen;
260260
ResolveFrameworkReferences;
261261
ApplyImplicitVersions;
262-
_ComputeExcludeFromOutputPackageReferences"
262+
_ComputePackageReferencePublish"
263263
Condition="$(SkipOptimization) != 'true' ">
264264

265265
<ItemGroup>
@@ -274,6 +274,7 @@ Copyright (c) .NET Foundation. All rights reserved.
274274
StorePackageName=$(MicrosoftNETPlatformLibrary);
275275
StorePackageVersion=%(PackageReferenceForCrossGen.Version);"/>
276276

277+
<!-- TODO: Use ResolvePackageAssets instead of ResolveCopyLocalAssets, delete ResolveCopyLocalAssets task -->
277278
<ResolveCopyLocalAssets Condition="'$(_TargetFrameworkVersionWithoutV)' &lt; '3.0'"
278279
AssetsFilePath="$(_CrossProjAssetsFile)"
279280
TargetFramework="$(_TFM)"

0 commit comments

Comments
 (0)