Merge branch 'nuget-native-plugin' into trigger
[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       IEnumerable<IPackage> package = GetAllPackages(req.Source, req.Packages.Select(x => x.Id)).ToList();\r
21       var hash = new Dictionary<string, Func<IPackage, bool>>(\r
22         req\r
23           .Packages\r
24           .Where(x => x.LoadVersionSpec() != null)\r
25           .ToDictionary(x => x.Id, x => x.LoadVersionSpec())\r
26         );\r
27 \r
28       package = package.Where(x =>\r
29                                 {\r
30                                   Func<IPackage, bool> r;\r
31                                   if (!hash.TryGetValue(x.Id, out r))\r
32                                     return true;\r
33                                   return r(x);\r
34                                 });\r
35 \r
36       foreach (var pkg in package)\r
37       {\r
38         PrintPackageInfo(pkg);\r
39       }\r
40     }\r
41 \r
42     private NuGetRequests LoadRequests()\r
43     {\r
44       if (!File.Exists(Request))\r
45         throw new CommandLineException("Failed to find file at {0}", Request);\r
46 \r
47       using (var file = File.OpenRead(Request))\r
48       {\r
49         var parser = new XmlSerializerFactory().CreateSerializer(typeof (NuGetRequests));\r
50         return (NuGetRequests) parser.Deserialize(file);\r
51       }\r
52     }\r
53   }\r
54 \r
55   [Serializable]\r
56   [XmlRoot("Request")]\r
57   public class NuGetCheckRequest\r
58   {\r
59     private Func<IPackage, bool> myVersionSpec;\r
60 \r
61     [XmlAttribute("Id")]\r
62     public string Id { get; set; }\r
63 \r
64     [XmlAttribute("Versions")]\r
65     public string VersionSpec { get; set; }\r
66 \r
67     public Func<IPackage, bool> LoadVersionSpec()\r
68     {\r
69       return string.IsNullOrWhiteSpace(VersionSpec)\r
70                ? null\r
71                : myVersionSpec ?? (myVersionSpec = VersionUtility.ParseVersionSpec(VersionSpec).ToDelegate());\r
72     }\r
73   }\r
74 \r
75   [Serializable]\r
76   [XmlRoot("NuGet-Request")]\r
77   public class NuGetRequests\r
78   {\r
79     [XmlAttribute("Source")]\r
80     public string Source { get; set; }\r
81 \r
82     [XmlArray("Requests")]\r
83     [XmlArrayItem("Request")]\r
84     public NuGetCheckRequest[] Packages { get; set; }\r
85   }\r
86 }