2016-05-02 19:51:12 -07:00
using System.Collections.Generic ;
2016-04-29 08:11:59 -07:00
using System.Linq ;
using Microsoft.DotNet.ProjectModel.Compilation ;
using Microsoft.DotNet.ProjectModel.Graph ;
namespace Microsoft.DotNet.ProjectModel
{
public static class ProjectModelPlatformExtensions
{
2016-05-02 19:51:12 -07:00
public static HashSet < string > GetPlatformExclusionList ( this ProjectContext context , IDictionary < string , LibraryExport > exports )
2016-04-29 08:11:59 -07:00
{
var exclusionList = new HashSet < string > ( ) ;
var redistPackage = context . PlatformLibrary ;
if ( redistPackage = = null )
{
return exclusionList ;
}
var redistExport = exports [ redistPackage . Identity . Name ] ;
exclusionList . Add ( redistExport . Library . Identity . Name ) ;
CollectDependencies ( exports , redistExport . Library . Dependencies , exclusionList ) ;
return exclusionList ;
}
2016-05-02 19:51:12 -07:00
private static void CollectDependencies ( IDictionary < string , LibraryExport > exports , IEnumerable < LibraryRange > dependencies , HashSet < string > exclusionList )
2016-04-29 08:11:59 -07:00
{
foreach ( var dependency in dependencies )
{
var export = exports [ dependency . Name ] ;
if ( export . Library . Identity . Version . Equals ( dependency . VersionRange . MinVersion ) )
{
exclusionList . Add ( export . Library . Identity . Name ) ;
CollectDependencies ( exports , export . Library . Dependencies , exclusionList ) ;
}
}
}
2016-05-02 19:51:12 -07:00
public static HashSet < string > GetTypeBuildExclusionList ( this ProjectContext context , IDictionary < string , LibraryExport > exports )
{
var acceptedExports = new HashSet < string > ( ) ;
// Accept the root project, obviously :)
acceptedExports . Add ( context . RootProject . Identity . Name ) ;
// Walk all dependencies, tagging exports. But don't walk through Build dependencies.
CollectNonBuildDependencies ( exports , context . RootProject . Dependencies , acceptedExports ) ;
// Whatever is left in exports was brought in ONLY by a build dependency
var exclusionList = new HashSet < string > ( exports . Keys ) ;
exclusionList . ExceptWith ( acceptedExports ) ;
return exclusionList ;
}
private static void CollectNonBuildDependencies ( IDictionary < string , LibraryExport > exports , IEnumerable < LibraryRange > dependencies , HashSet < string > acceptedExports )
{
foreach ( var dependency in dependencies )
{
var export = exports [ dependency . Name ] ;
if ( ! dependency . Type . Equals ( LibraryDependencyType . Build ) )
{
acceptedExports . Add ( export . Library . Identity . Name ) ;
CollectNonBuildDependencies ( exports , export . Library . Dependencies , acceptedExports ) ;
}
}
}
2016-04-29 08:11:59 -07:00
}
}