public DependencyResult Process() { try { var tree = new NugetTreeResolver(NugetSearchService); Project proj = new Project(ProjectPath); List <NugetDependency> deps = new List <NugetDependency>(); foreach (ProjectItem reference in proj.GetItemsIgnoringCondition("PackageReference")) { var versionMetaData = reference.Metadata.Where(meta => meta.Name == "Version").FirstOrDefault().EvaluatedValue; NuGet.Versioning.VersionRange version; if (NuGet.Versioning.VersionRange.TryParse(versionMetaData, out version)) { var dep = new NugetDependency(reference.EvaluatedInclude, version); deps.Add(dep); } else { Console.WriteLine("Framework dependency had no version, will not be included: " + reference.EvaluatedInclude); } } foreach (ProjectItem reference in proj.GetItemsIgnoringCondition("Reference")) { if (reference.Xml != null && !String.IsNullOrWhiteSpace(reference.Xml.Include) && reference.Xml.Include.Contains("Version=")) { string packageInfo = reference.Xml.Include; var artifact = packageInfo.Substring(0, packageInfo.IndexOf(",")); string versionKey = "Version="; int versionKeyIndex = packageInfo.IndexOf(versionKey); int versionStartIndex = versionKeyIndex + versionKey.Length; string packageInfoAfterVersionKey = packageInfo.Substring(versionStartIndex); string seapirater = ","; string version; if (packageInfoAfterVersionKey.Contains(seapirater)) { int firstSeapirater = packageInfoAfterVersionKey.IndexOf(seapirater); version = packageInfoAfterVersionKey.Substring(0, firstSeapirater); } else { version = packageInfoAfterVersionKey; } var dep = new NugetDependency(artifact, NuGet.Versioning.VersionRange.Parse(version)); deps.Add(dep); } } ProjectCollection.GlobalProjectCollection.UnloadProject(proj); foreach (var dep in deps) { tree.Add(dep); } var result = new DependencyResult() { Success = true, Packages = tree.GetPackageList(), Dependencies = new List <Inspector.Model.PackageId>() }; foreach (var package in result.Packages) { var anyPackageReferences = result.Packages.Where(pkg => pkg.Dependencies.Contains(package.PackageId)).Any(); if (!anyPackageReferences) { result.Dependencies.Add(package.PackageId); } } return(result); } catch (InvalidProjectFileException e) { return(new DependencyResult() { Success = false }); } }
public DependencyResult Process() { var result = new DependencyResult(); var tree = new NugetTreeResolver(NugetSearchService); // .NET core default version result.ProjectVersion = "1.0.0"; Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); XmlDocument doc = new XmlDocument(); doc.Load(ProjectPath); XmlNodeList versionNodes = doc.GetElementsByTagName("Version"); if (versionNodes != null && versionNodes.Count > 0) { foreach (XmlNode version in versionNodes) { if (version.NodeType != XmlNodeType.Comment) { result.ProjectVersion = version.InnerText; } } } else { string prefix = "1.0.0"; string suffix = ""; XmlNodeList prefixNodes = doc.GetElementsByTagName("VersionPrefix"); if (prefixNodes != null && prefixNodes.Count > 0) { foreach (XmlNode prefixNode in prefixNodes) { if (prefixNode.NodeType != XmlNodeType.Comment) { prefix = prefixNode.InnerText; } } } XmlNodeList suffixNodes = doc.GetElementsByTagName("VersionSuffix"); if (suffixNodes != null && suffixNodes.Count > 0) { foreach (XmlNode suffixNode in suffixNodes) { if (suffixNode.NodeType != XmlNodeType.Comment) { suffix = suffixNode.InnerText; } } } result.ProjectVersion = String.Format("{0}-{1}", prefix, suffix);; } XmlNodeList packagesNodes = doc.GetElementsByTagName("PackageReference"); if (packagesNodes.Count > 0) { foreach (XmlNode package in packagesNodes) { XmlAttributeCollection attributes = package.Attributes; if (attributes != null) { XmlAttribute include = attributes["Include"]; XmlAttribute version = attributes["Version"]; if (include != null && version != null) { var dep = new NugetDependency(include.Value, NuGet.Versioning.VersionRange.Parse(version.Value)); tree.Add(dep); } } } } result.Packages = tree.GetPackageList(); result.Dependencies = new List <PackageId>(); foreach (var package in result.Packages) { var anyPackageReferences = result.Packages.Where(pkg => pkg.Dependencies.Contains(package.PackageId)).Any(); if (!anyPackageReferences) { result.Dependencies.Add(package.PackageId); } } return(result); }