make source field in nuget trigger optional. Use default feed if blank
[teamcity/dotNetPackagesSupport.git] / nuget-extensions / nuget-commands / src / NuGetTeamCityListPackagesCommand.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.IO;\r
4 using System.Xml.Serialization;\r
5 using NuGet;\r
6 using System.Linq;\r
7 \r
8 namespace JetBrains.TeamCity.NuGet.ExtendedCommands\r
9 {\r
10   [Command("TeamCity.ListPackages", "Lists packages for given xml list of packages")]\r
11   public class NuGetTeamCityListPackagesCommand : ListCommandBase\r
12   {\r
13     [Option("Path to file containing package-version pairs to check for updates")]\r
14     public string Request { get; set; }\r
15 \r
16     public override void ExecuteCommand()\r
17     {\r
18       var req = LoadRequests();\r
19 \r
20       if (string.IsNullOrWhiteSpace(req.Source))\r
21         req.Source = NuGetConstants.DefaultFeedUrl;\r
22 \r
23       IEnumerable<IPackage> package = GetAllPackages(req.Source, req.Packages.Select(x => x.Id)).ToList();\r
24       var hash = new Dictionary<string, Func<IPackage, bool>>(\r
25         req\r
26           .Packages\r
27           .Where(x => x.LoadVersionSpec() != null)\r
28           .ToDictionary(x => x.Id, x => x.LoadVersionSpec())\r
29         );\r
30 \r
31       package = package.Where(x =>\r
32                                 {\r
33                                   Func<IPackage, bool> r;\r
34                                   if (!hash.TryGetValue(x.Id, out r))\r
35                                     return true;\r
36                                   return r(x);\r
37                                 });\r
38 \r
39       foreach (var pkg in package)\r
40       {\r
41         PrintPackageInfo(pkg);\r
42       }\r
43     }\r
44 \r
45     private NuGetRequests LoadRequests()\r
46     {\r
47       if (!File.Exists(Request))\r
48         throw new CommandLineException("Failed to find file at {0}", Request);\r
49 \r
50       using (var file = File.OpenRead(Request))\r
51       {\r
52         var parser = new XmlSerializerFactory().CreateSerializer(typeof (NuGetRequests));\r
53         return (NuGetRequests) parser.Deserialize(file);\r
54       }\r
55     }\r
56   }\r
57 \r
58   [Serializable]\r
59   [XmlRoot("Request")]\r
60   public class NuGetCheckRequest\r
61   {\r
62     private Func<IPackage, bool> myVersionSpec;\r
63 \r
64     [XmlAttribute("Id")]\r
65     public string Id { get; set; }\r
66 \r
67     [XmlAttribute("Versions")]\r
68     public string VersionSpec { get; set; }\r
69 \r
70     public Func<IPackage, bool> LoadVersionSpec()\r
71     {\r
72       return string.IsNullOrWhiteSpace(VersionSpec)\r
73                ? null\r
74                : myVersionSpec ?? (myVersionSpec = VersionUtility.ParseVersionSpec(VersionSpec).ToDelegate());\r
75     }\r
76   }\r
77 \r
78   [Serializable]\r
79   [XmlRoot("NuGet-Request")]\r
80   public class NuGetRequests\r
81   {\r
82     [XmlAttribute("Source")]\r
83     public string Source { get; set; }\r
84 \r
85     [XmlArray("Requests")]\r
86     [XmlArrayItem("Request")]\r
87     public NuGetCheckRequest[] Packages { get; set; }\r
88   }\r
89 }