Пример #1
0
        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);
        }
Пример #2
0
        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);
        }