|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Xml.Linq; |
| 4 | + |
| 5 | +namespace BaGet.Core |
| 6 | +{ |
| 7 | + public class V2Builder : IV2Builder |
| 8 | + { |
| 9 | + private readonly IUrlGenerator _url; |
| 10 | + |
| 11 | + public V2Builder(IUrlGenerator url) |
| 12 | + { |
| 13 | + _url = url; |
| 14 | + } |
| 15 | + |
| 16 | + public XElement BuildIndex() |
| 17 | + { |
| 18 | + var serviceIndex = _url.GetServiceIndexV2Url(); |
| 19 | + |
| 20 | + return XElement.Parse($@" |
| 21 | +<service xmlns=""http://www.w3.org/2007/app"" xmlns:atom=""http://www.w3.org/2005/Atom"" xml:base=""{serviceIndex}""> |
| 22 | + <workspace> |
| 23 | + <atom:title type=""text"">Default</atom:title> |
| 24 | + <collection href=""Packages""> |
| 25 | + <atom:title type=""text"">Packages</atom:title> |
| 26 | + </collection> |
| 27 | + </workspace> |
| 28 | +</service>"); |
| 29 | + } |
| 30 | + |
| 31 | + public XElement BuildPackages(IReadOnlyList<Package> packages) |
| 32 | + { |
| 33 | + // See: https://joelverhagen.github.io/NuGetUndocs/#endpoint-find-packages-by-id |
| 34 | + var serviceIndex = _url.GetServiceIndexV2Url(); |
| 35 | + |
| 36 | + // TODO: Add <?xml version="1.0" encoding="utf-8"?> to top |
| 37 | + return new XElement( |
| 38 | + N.feed, |
| 39 | + new XAttribute(N.baze, XNamespace.Get(serviceIndex)), |
| 40 | + new XAttribute(N.m, NS.m), |
| 41 | + new XAttribute(N.d, NS.d), |
| 42 | + new XAttribute(N.georss, NS.georss), |
| 43 | + new XAttribute(N.gml, NS.gml), |
| 44 | + new XElement(N.m_count, packages.Count), |
| 45 | + |
| 46 | + packages.Select(package => |
| 47 | + { |
| 48 | + var packageV2Url = _url.GetPackageVersionV2Url(package); |
| 49 | + var downloadUrl = _url.GetPackageDownloadUrl(package); |
| 50 | + |
| 51 | + return new XElement( |
| 52 | + N.entry, |
| 53 | + new XElement(N.id, packageV2Url), |
| 54 | + new XElement(N.title, package.Title), |
| 55 | + new XElement( |
| 56 | + N.content, |
| 57 | + new XAttribute("type", "application/zip"), |
| 58 | + new XAttribute("src", downloadUrl) |
| 59 | + ), |
| 60 | + |
| 61 | + BuildAuthor(package), |
| 62 | + BuildProperties(package) |
| 63 | + ); |
| 64 | + }) |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + public XElement BuildPackage(Package package) |
| 69 | + { |
| 70 | + // See: https://joelverhagen.github.io/NuGetUndocs/#endpoint-get-a-single-package |
| 71 | + var serviceIndex = _url.GetServiceIndexV2Url(); |
| 72 | + var packageV2Url = _url.GetPackageVersionV2Url(package); |
| 73 | + var downloadUrl = _url.GetPackageDownloadUrl(package); |
| 74 | + |
| 75 | + return new XElement( |
| 76 | + N.entry, |
| 77 | + new XAttribute(N.baze, XNamespace.Get(serviceIndex)), |
| 78 | + new XAttribute(N.m, NS.m), |
| 79 | + new XAttribute(N.d, NS.d), |
| 80 | + new XAttribute(N.georss, NS.georss), |
| 81 | + new XAttribute(N.gml, NS.gml), |
| 82 | + new XElement(N.id, packageV2Url), |
| 83 | + new XElement(N.title, package.Title), |
| 84 | + |
| 85 | + new XElement( |
| 86 | + N.content, |
| 87 | + new XAttribute("type", "application/zip"), |
| 88 | + new XAttribute("src", downloadUrl) |
| 89 | + ), |
| 90 | + |
| 91 | + BuildAuthor(package), |
| 92 | + BuildProperties(package) |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + private XElement BuildProperties(Package package) |
| 97 | + { |
| 98 | + // See: https://joelverhagen.github.io/NuGetUndocs/#package-entity |
| 99 | + return new XElement( |
| 100 | + N.m_properties, |
| 101 | + new XElement(N.d_Id, package.Id), |
| 102 | + new XElement(N.d_Title, package.Title), |
| 103 | + new XElement(N.d_Version, package.OriginalVersionString), |
| 104 | + new XElement(N.d_NormalizedVersion, package.NormalizedVersionString), |
| 105 | + new XElement(N.d_Authors, string.Join(", ", package.Authors)), |
| 106 | + new XElement(N.d_Copyright, ""), // TODO |
| 107 | + new XElement(N.d_Description, package.Description), |
| 108 | + new XElement( |
| 109 | + N.d_DownloadCount, |
| 110 | + new XAttribute(N.m_type, "Edm.Int32"), |
| 111 | + package.Downloads), |
| 112 | + new XElement(N.d_LastEdited, package.Published), |
| 113 | + new XElement(N.d_Published, package.Published), |
| 114 | + new XElement(N.d_PackageHash, ""), |
| 115 | + new XElement(N.d_PackageHashAlgorithm, ""), |
| 116 | + new XElement(N.d_PackageSize, 0), |
| 117 | + new XElement(N.d_ProjectUrl, package.ProjectUrl), |
| 118 | + new XElement(N.d_IconUrl, package.IconUrl), // TODO, URL logic |
| 119 | + new XElement(N.d_LicenseUrl, package.LicenseUrl), // TODO |
| 120 | + new XElement(N.d_Tags, string.Join(", ", package.Tags)), |
| 121 | + new XElement(N.d_RequireLicenseAcceptance, package.RequireLicenseAcceptance), |
| 122 | + |
| 123 | + BuildDependencies(package) |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + private XElement BuildAuthor(Package package) |
| 128 | + { |
| 129 | + // TODO: No authors? |
| 130 | + return new XElement( |
| 131 | + N.author, |
| 132 | + package.Authors.Select(author => new XElement(N.name, author)) |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + private XElement BuildDependencies(Package package) |
| 137 | + { |
| 138 | + var flattenedDependencies = new List<string>(); |
| 139 | + |
| 140 | + flattenedDependencies.AddRange( |
| 141 | + package |
| 142 | + .Dependencies |
| 143 | + .Where(IsFrameworkDependency) |
| 144 | + .Select(dependency => dependency.TargetFramework) |
| 145 | + .Distinct() |
| 146 | + .Select(targetFramework => $"::{targetFramework}")); |
| 147 | + |
| 148 | + flattenedDependencies.AddRange( |
| 149 | + package |
| 150 | + .Dependencies |
| 151 | + .Where(dependency => !IsFrameworkDependency(dependency)) |
| 152 | + .Select(dependency => $"{dependency.Id}:{dependency.VersionRange}:{dependency.TargetFramework}")); |
| 153 | + |
| 154 | + var result = string.Join("|", flattenedDependencies); |
| 155 | + |
| 156 | + return new XElement(N.d_Dependencies, result); |
| 157 | + } |
| 158 | + |
| 159 | + private bool IsFrameworkDependency(PackageDependency dependency) |
| 160 | + { |
| 161 | + return dependency.Id == null && dependency.VersionRange == null; |
| 162 | + } |
| 163 | + |
| 164 | + private static class NS |
| 165 | + { |
| 166 | + public static readonly XNamespace xmlns = "http://www.w3.org/2005/Atom"; |
| 167 | + //public static readonly XNamespace baze = "https://www.nuget.org/api/v2/curated-feeds/microsoftdotnet"; |
| 168 | + public static readonly XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"; |
| 169 | + public static readonly XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"; |
| 170 | + public static readonly XNamespace georss = "http://www.georss.org/georss"; |
| 171 | + public static readonly XNamespace gml = "http://www.opengis.net/gml"; |
| 172 | + } |
| 173 | + |
| 174 | + private static class N |
| 175 | + { |
| 176 | + public static readonly XName feed = NS.xmlns + "feed"; |
| 177 | + public static readonly XName entry = NS.xmlns + "entry"; |
| 178 | + public static readonly XName title = NS.xmlns + "title"; |
| 179 | + public static readonly XName author = NS.xmlns + "author"; |
| 180 | + public static readonly XName name = NS.xmlns + "name"; |
| 181 | + public static readonly XName link = NS.xmlns + "link"; |
| 182 | + public static readonly XName id = NS.xmlns + "id"; |
| 183 | + public static readonly XName content = NS.xmlns + "content"; |
| 184 | + |
| 185 | + public static readonly XName m_count = NS.m + "count"; |
| 186 | + public static readonly XName m_properties = NS.m + "properties"; |
| 187 | + public static readonly XName m_type = NS.m + "type"; |
| 188 | + |
| 189 | + public static readonly XName d_Id = NS.d + "Id"; |
| 190 | + public static readonly XName d_Title = NS.d + "Title"; |
| 191 | + public static readonly XName d_Version = NS.d + "Version"; |
| 192 | + public static readonly XName d_NormalizedVersion = NS.d + "NormalizedVersion"; |
| 193 | + public static readonly XName d_Authors = NS.d + "Authors"; |
| 194 | + public static readonly XName d_Copyright = NS.d + "Copyright"; |
| 195 | + public static readonly XName d_Dependencies = NS.d + "Dependencies"; |
| 196 | + public static readonly XName d_Description = NS.d + "Description"; |
| 197 | + public static readonly XName d_IconUrl = NS.d + "IconUrl"; |
| 198 | + public static readonly XName d_LicenseUrl = NS.d + "LicenseUrl"; |
| 199 | + public static readonly XName d_ProjectUrl = NS.d + "ProjectUrl"; |
| 200 | + public static readonly XName d_Tags = NS.d + "Tags"; |
| 201 | + public static readonly XName d_ReportAbuseUrl = NS.d + "ReportAbuseUrl"; |
| 202 | + public static readonly XName d_RequireLicenseAcceptance = NS.d + "RequireLicenseAcceptance"; |
| 203 | + public static readonly XName d_DownloadCount = NS.d + "DownloadCount"; |
| 204 | + public static readonly XName d_Created = NS.d + "Created"; |
| 205 | + public static readonly XName d_LastEdited = NS.d + "LastEdited"; |
| 206 | + public static readonly XName d_Published = NS.d + "Published"; |
| 207 | + public static readonly XName d_PackageHash = NS.d + "PackageHash"; |
| 208 | + public static readonly XName d_PackageHashAlgorithm = NS.d + "PackageHashAlgorithm"; |
| 209 | + public static readonly XName d_MinClientVersion = NS.d + "MinClientVersion"; |
| 210 | + public static readonly XName d_PackageSize = NS.d + "PackageSize"; |
| 211 | + |
| 212 | + public static readonly XName baze = XNamespace.Xmlns + "base"; |
| 213 | + public static readonly XName m = XNamespace.Xmlns + "m"; |
| 214 | + public static readonly XName d = XNamespace.Xmlns + "d"; |
| 215 | + public static readonly XName georss = XNamespace.Xmlns + "georss"; |
| 216 | + public static readonly XName gml = XNamespace.Xmlns + "gml"; |
| 217 | + } |
| 218 | + } |
| 219 | +} |
0 commit comments