public override bool Execute() { log = new MSBuildLog(Log); // Build the package graph var packages = GetPackagesFromPackageDependencies(); // Build file graph var files = GetFiles(packages); trimmable = new Trimmable(TrimmablePackages?.Select(i => i.ItemSpec), TrimmableFiles?.Select(i => GetFileNameFromItem(i))); Queue <NuGetPackageNode> packageRoots = GetPackageRoots(packages, trimmable); Queue <FileNode> fileRoots = GetFileRoots(files, trimmable); while (packageRoots.Count > 0 || fileRoots.Count > 0) { while (fileRoots.Count > 0) { var fileNode = fileRoots.Dequeue(); foreach (var file in fileNode.Dependencies.Where(f => !trimmable.IsFileTrimmable(f.Name))) { IncludeNode(fileRoots, file); } if (fileNode.Package != null && !IsPackageTrimmable(fileNode.Package)) { IncludeNode(packageRoots, fileNode.Package); } } while (packageRoots.Count > 0) { var packageNode = packageRoots.Dequeue(); foreach (var dependency in packageNode.Dependencies.Where(d => !IsPackageTrimmable(d))) { IncludeNode(packageRoots, dependency); } foreach (var file in packageNode.Files.Where(f => !trimmable.IsFileTrimmable(f.Name))) { IncludeNode(fileRoots, file); } } } var excludedItems = files.Values.Where(f => !f.IsIncluded).Select(f => f.OriginalItem); TrimmedItems = excludedItems.ToArray(); RuntimeItemsAfterTrimming = RuntimeItems.Except(excludedItems).ToArray(); LogResults(files.Values); return(!Log.HasLoggedErrors); }
Queue <FileNode> GetFileRoots(IDictionary <string, FileNode> files, Trimmable trimmable) { var fileRootQueue = new Queue <FileNode>(); var trimmedRootFilenames = RootFiles.Select(i => GetFileNameFromItem(i)).Where(f => !trimmable.IsFileTrimmable(f)); foreach (var rootFilename in trimmedRootFilenames) { FileNode rootFile; if (files.TryGetValue(rootFilename, out rootFile)) { IncludeNode(fileRootQueue, rootFile); } else { Log.LogMessage($"Root file {rootFilename} was specified but was not found in the file index."); } } return(fileRootQueue); }
Queue <NuGetPackageNode> GetPackageRoots(IDictionary <string, NuGetPackageNode> packages, Trimmable trimmable) { var packageRootQueue = new Queue <NuGetPackageNode>(); if (RootPackages != null) { var rootPackageIds = RootPackages.Select(i => GetPackageIdFromItemSpec(i.ItemSpec)); foreach (var rootPackageId in rootPackageIds) { NuGetPackageNode rootPackage; if (!packages.TryGetValue(rootPackageId, out rootPackage)) { throw new Exception($"Root package {rootPackageId} was specified but was not found in PackageDependencies"); } if (!IsPackageTrimmable(rootPackage)) { IncludeNode(packageRootQueue, rootPackage); } } } return(packageRootQueue); }
Queue <NuGetPackageNode> GetPackageRoots(IDictionary <string, NuGetPackageNode> packages, Trimmable trimmable) { var packageRootQueue = new Queue <NuGetPackageNode>(); if (RootPackages != null) { var rootPackageIds = RootPackages.Select(i => GetPackageIdFromItemSpec(i.ItemSpec)); var trimmedRootPackageIds = rootPackageIds.Where(p => !trimmable.IsPackageTrimmable(p)); foreach (var rootPackageId in trimmedRootPackageIds) { NuGetPackageNode rootPackage; if (!packages.TryGetValue(rootPackageId, out rootPackage)) { throw new Exception($"Root package {rootPackageId} was specified but was not found in {AssetsFilePath}"); } IncludeNode(packageRootQueue, rootPackage); } } return(packageRootQueue); }
public override bool Execute() { log = new MSBuildLog(Log); // Build the package graph var packages = GetPackagesFromPackageDependencies(); if (packages.Count == 0) { packages = GetPackagesFromAssetsFile(); } // Build file graph var files = GetFiles(packages); trimmable = new Trimmable(TrimmablePackages?.Select(i => i.ItemSpec), TrimmableFiles?.Select(i => GetFileNameFromItem(i))); Queue <NuGetPackageNode> packageRoots = GetPackageRoots(packages, trimmable); Queue <FileNode> fileRoots = GetFileRoots(files, trimmable); var originalPackageRoots = packageRoots.ToArray(); var originalFileRoots = fileRoots.ToArray(); while (packageRoots.Count > 0 || fileRoots.Count > 0) { while (fileRoots.Count > 0) { var fileNode = fileRoots.Dequeue(); if (fileNode.IsMissing) { Log.LogWarning($"File {fileNode.SourceFile} was included through references or roots but the file was missing. It's dependencies will also be missing from the trimmed output."); } foreach (var file in fileNode.Dependencies.Where(f => !trimmable.IsFileTrimmable(f.Name))) { IncludeNode(fileRoots, file); } if (IncludeRelatedFiles) { foreach (var file in fileNode.RelatedFiles.Where(f => !trimmable.IsFileTrimmable(f.Name))) { IncludeNode(fileRoots, file); } } if (fileNode.Package != null && !IsPackageTrimmable(fileNode.Package)) { IncludeNode(packageRoots, fileNode.Package); } } while (packageRoots.Count > 0) { var packageNode = packageRoots.Dequeue(); foreach (var dependency in packageNode.Dependencies.Where(d => !IsPackageTrimmable(d))) { IncludeNode(packageRoots, dependency); } foreach (var file in packageNode.Files.Where(f => !trimmable.IsFileTrimmable(f.Name))) { IncludeNode(fileRoots, file); } } } var excludedItems = files.Values.Where(f => !f.IsIncluded).Select(f => f.OriginalItem); TrimmedItems = excludedItems.ToArray(); RuntimeItemsAfterTrimming = RuntimeItems.Except(excludedItems).ToArray(); LogResults(files.Values); if (!string.IsNullOrEmpty(DirectedGraphFile)) { DirectedGraphWriter.WriteGraph(DirectedGraphFile, files.Values, packages.Values, originalFileRoots, originalPackageRoots); } return(!Log.HasLoggedErrors); }