PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/InterceptNuGet/InterceptNuGet/InterceptFormatting.cs

https://github.com/johnataylor/InterceptNuGet
C# | 221 lines | 163 code | 46 blank | 12 comment | 11 complexity | 72decdc133ae578b34b34908152c53fa MD5 | raw file
  1. using Newtonsoft.Json.Linq;
  2. using NuGet.Versioning;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Xml.Linq;
  8. namespace InterceptNuGet
  9. {
  10. class InterceptFormatting
  11. {
  12. public static XElement MakeFeed(string feedBaseAddress, string method, IEnumerable<JToken> packages, string id)
  13. {
  14. return MakeFeed(feedBaseAddress, method, packages, Enumerable.Repeat(id, packages.Count()).ToArray());
  15. }
  16. public static XElement MakeFeed(string feedBaseAddress, string method, IEnumerable<JToken> packages, string[] id)
  17. {
  18. XNamespace atom = XNamespace.Get(@"http://www.w3.org/2005/Atom");
  19. XElement feed = new XElement(atom + "feed");
  20. feed.Add(new XElement(atom + "id", string.Format("{0}/api/v2/{1}", feedBaseAddress, method)));
  21. feed.Add(new XElement(atom + "title", method));
  22. int i = 0;
  23. foreach (JToken package in packages)
  24. {
  25. feed.Add(MakeEntry(feedBaseAddress, id[i++], package));
  26. }
  27. return feed;
  28. }
  29. static XElement MakeEntry(string feedBaseAddress, string id, JToken package)
  30. {
  31. XNamespace atom = XNamespace.Get(@"http://www.w3.org/2005/Atom");
  32. XNamespace d = XNamespace.Get(@"http://schemas.microsoft.com/ado/2007/08/dataservices");
  33. XNamespace m = XNamespace.Get(@"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
  34. XElement entry = new XElement(atom + "entry");
  35. entry.Add(new XElement(atom + "id", string.Format("{0}/api/v2/Packages(Id='{1}',Version='{2}')", feedBaseAddress, id, package["version"])));
  36. entry.Add(new XElement(atom + "title", id));
  37. entry.Add(new XElement(atom + "author", new XElement(atom + "name", "SHIM")));
  38. // the content URL should come from the json
  39. entry.Add(new XElement(atom + "content",
  40. new XAttribute("type", "application/zip"),
  41. new XAttribute("src", string.Format("http://www.nuget.org/api/v2/package/{0}/{1}", id, package["version"]))));
  42. XElement properties = new XElement(m + "properties");
  43. entry.Add(properties);
  44. properties.Add(new XElement(d + "Version", package["version"].ToString()));
  45. // the following fields should come from the json
  46. properties.Add(new XElement(d + "Description", "SHIM"));
  47. properties.Add(new XElement(d + "IsLatestVersion", new XAttribute(m + "type", "Edm.Boolean"), "true"));
  48. properties.Add(new XElement(d + "IsAbsoluteLatestVersion", new XAttribute(m + "type", "Edm.Boolean"), "true"));
  49. properties.Add(new XElement(d + "IsPrerelease", new XAttribute(m + "type", "Edm.Boolean"), "false"));
  50. JToken dependencies;
  51. if (((JObject)package).TryGetValue("dependencies", out dependencies))
  52. {
  53. StringBuilder sb = new StringBuilder();
  54. foreach (JToken group in dependencies["group"])
  55. {
  56. string targetFramework = string.Empty;
  57. JToken tf;
  58. if (((JObject)group).TryGetValue("targetFramework", out tf))
  59. {
  60. targetFramework = tf.ToString();
  61. }
  62. foreach (JToken dependency in group["dependency"])
  63. {
  64. sb.AppendFormat("{0}:{1}:{2}|", dependency["id"].ToString().ToLowerInvariant(), dependency["range"], targetFramework);
  65. }
  66. if (sb.Length > 0)
  67. {
  68. sb.Remove(sb.Length - 1, 1);
  69. }
  70. }
  71. properties.Add(new XElement(d + "Dependencies", sb.ToString()));
  72. }
  73. // license information should come from the json
  74. bool license = false;
  75. properties.Add(new XElement(d + "RequireLicenseAcceptance", new XAttribute(m + "type", "Edm.Boolean"), license.ToString().ToLowerInvariant()));
  76. if (license)
  77. {
  78. properties.Add(new XElement(d + "LicenseUrl", "http://shim/test"));
  79. }
  80. // the following properties required for GetUpdates (from the UI)
  81. // the following properties should come from the json
  82. bool iconUrl = false;
  83. if (iconUrl)
  84. {
  85. properties.Add(new XElement(d + "IconUrl", "http://tempuri.org/"));
  86. }
  87. properties.Add(new XElement(d + "DownloadCount", new XAttribute(m + "type", "Edm.Int32"), 123456));
  88. properties.Add(new XElement(d + "GalleryDetailsUrl", "http://tempuri.org/"));
  89. properties.Add(new XElement(d + "Published", new XAttribute(m + "type", "Edm.DateTime"), "2014-02-25T02:04:38.407"));
  90. properties.Add(new XElement(d + "Tags", "SHIM.Tags"));
  91. // title is optional, if it is not there the UI uses the Id
  92. //properties.Add(new XElement(d + "Title", "SHIM.Title"));
  93. properties.Add(new XElement(d + "ReleaseNotes", "SHIM.ReleaseNotes"));
  94. return entry;
  95. }
  96. // The search service currently returns a slightly different JSON format (this will be fixed)
  97. public static XElement MakeFeedFromSearch(string feedBaseAddress, string method, IEnumerable<JToken> packages, string id)
  98. {
  99. XNamespace atom = XNamespace.Get(@"http://www.w3.org/2005/Atom");
  100. XElement feed = new XElement(atom + "feed");
  101. feed.Add(new XElement(atom + "id", string.Format("{0}/api/v2/{1}", feedBaseAddress, method)));
  102. feed.Add(new XElement(atom + "title", method));
  103. foreach (JToken package in packages)
  104. {
  105. feed.Add(MakeEntrySearch(feedBaseAddress, "", package));
  106. }
  107. return feed;
  108. }
  109. static XElement MakeEntrySearch(string feedBaseAddress, string id, JToken package)
  110. {
  111. XNamespace atom = XNamespace.Get(@"http://www.w3.org/2005/Atom");
  112. XNamespace d = XNamespace.Get(@"http://schemas.microsoft.com/ado/2007/08/dataservices");
  113. XNamespace m = XNamespace.Get(@"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
  114. XElement entry = new XElement(atom + "entry");
  115. string registrationId = package["PackageRegistration"]["Id"].ToString();
  116. string version = package["Version"].ToString();
  117. entry.Add(new XElement(atom + "id", string.Format("{0}/api/v2/Packages(Id='{1}',Version='{2}')", feedBaseAddress, registrationId, version)));
  118. entry.Add(new XElement(atom + "title", registrationId));
  119. entry.Add(new XElement(atom + "author", new XElement(atom + "name", package["Authors"].ToString())));
  120. // the content URL should come from the json
  121. entry.Add(new XElement(atom + "content",
  122. new XAttribute("type", "application/zip"),
  123. new XAttribute("src", string.Format("http://www.nuget.org/api/v2/package/{0}/{1}", registrationId, version))));
  124. XElement properties = new XElement(m + "properties");
  125. entry.Add(properties);
  126. properties.Add(new XElement(d + "Version", package["Version"].ToString()));
  127. NuGetVersion nugetVersion = NuGetVersion.Parse(version);
  128. // the following fields should come from the json
  129. properties.Add(new XElement(d + "Description", package["Description"].ToString()));
  130. properties.Add(new XElement(d + "IsLatestVersion", new XAttribute(m + "type", "Edm.Boolean"), package["IsLatestStable"].ToString().ToLowerInvariant()));
  131. properties.Add(new XElement(d + "IsAbsoluteLatestVersion", new XAttribute(m + "type", "Edm.Boolean"), package["IsLatest"].ToString().ToLowerInvariant()));
  132. properties.Add(new XElement(d + "IsPrerelease", new XAttribute(m + "type", "Edm.Boolean"), nugetVersion.IsPrerelease.ToString().ToLowerInvariant()));
  133. JToken flattenedDependencies;
  134. if (((JObject)package).TryGetValue("FlattenedDependencies", out flattenedDependencies))
  135. {
  136. properties.Add(new XElement(d + "Dependencies", flattenedDependencies.ToString()));
  137. }
  138. // license information should come from the json
  139. bool license = false;
  140. bool.TryParse(package["RequiresLicenseAcceptance"].ToString().ToLowerInvariant(), out license);
  141. properties.Add(new XElement(d + "RequireLicenseAcceptance", new XAttribute(m + "type", "Edm.Boolean"), license.ToString().ToLowerInvariant()));
  142. if (license)
  143. {
  144. properties.Add(new XElement(d + "LicenseUrl", package["LicenseUrl"].ToString()));
  145. }
  146. JToken iconUrl;
  147. if (((JObject)package).TryGetValue("IconUrl", out iconUrl))
  148. {
  149. properties.Add(new XElement(d + "IconUrl", iconUrl.ToString()));
  150. }
  151. string downloadCount = package["PackageRegistration"]["DownloadCount"].ToString();
  152. DateTime published = DateTime.Parse(package["Published"].ToString());
  153. properties.Add(new XElement(d + "DownloadCount", new XAttribute(m + "type", "Edm.Int32"), downloadCount));
  154. properties.Add(new XElement(d + "GalleryDetailsUrl", "http://tempuri.org/"));
  155. properties.Add(new XElement(d + "Published", new XAttribute(m + "type", "Edm.DateTime"), published.ToString("O")));
  156. properties.Add(new XElement(d + "Tags", package["Tags"].ToString()));
  157. // title is optional, if it is not there the UI uses the Id
  158. JToken title;
  159. if (((JObject)package).TryGetValue("Title", out title))
  160. {
  161. properties.Add(new XElement(d + "Title", title.ToString()));
  162. }
  163. string releaseNotes = package["ReleaseNotes"].ToString();
  164. if (releaseNotes == "null")
  165. {
  166. properties.Add(new XElement(d + "ReleaseNotes", string.Empty));
  167. }
  168. else
  169. {
  170. properties.Add(new XElement(d + "ReleaseNotes", releaseNotes));
  171. }
  172. return entry;
  173. }
  174. }
  175. }