using System.Collections.Generic; using System.IO; using System.Linq; namespace Semmle.Extraction.CSharp.DependencyFetching { /// /// Container class for dependencies found in the assets file. /// internal class DependencyContainer { /// /// Paths to dependencies required for compilation. /// public HashSet Paths { get; } = []; /// /// Packages that are used as a part of the required dependencies. /// public HashSet Packages { get; } = []; /// /// If the path specifically adds a .dll we use that, otherwise we as a fallback /// add the entire directory. /// private static string ParseFilePath(string path) { if (path.EndsWith(".dll")) { return path; } return Path.GetDirectoryName(path) ?? path; } private static string GetPackageName(string package) => package.Split(Path.DirectorySeparatorChar)[0]; /// /// Add a dependency inside a package. /// public void Add(string package, string dependency) { var p = package.Replace('/', Path.DirectorySeparatorChar); var d = dependency.Replace('/', Path.DirectorySeparatorChar); // In most cases paths in assets files point to dll's or the empty _._ file. // That is, for _._ we don't need to add anything. if (Path.GetFileName(d) == "_._") { return; } var path = Path.Combine(p, ParseFilePath(d)); Paths.Add(path); Packages.Add(GetPackageName(p)); } /// /// Add a dependency to an entire framework package. /// public void AddFramework(string framework) { var p = framework.Replace('/', Path.DirectorySeparatorChar); Paths.Add(p); Packages.Add(GetPackageName(p)); } } internal static class DependencyContainerExtensions { /// /// Flatten a list of containers into a single container. /// public static DependencyContainer Flatten(this IEnumerable containers, DependencyContainer init) => containers.Aggregate(init, (acc, container) => { acc.Paths.UnionWith(container.Paths); acc.Packages.UnionWith(container.Packages); return acc; }); } }