@@ -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
0 commit comments