support multiple package checks
[teamcity/dotNetPackagesSupport.git] / nuget-extensions / nuget-commands / src / ListCommandBase.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.ComponentModel.Composition;\r
4 using System.Linq;\r
5 using System.Linq.Expressions;\r
6 using JetBrains.TeamCity.NuGet.ExtendedCommands.Util;\r
7 using NuGet;\r
8 using NuGet.Commands;\r
9 \r
10 namespace JetBrains.TeamCity.NuGet.ExtendedCommands\r
11 {\r
12   public abstract class ListCommandBase : Command\r
13   {\r
14     [Import]\r
15     public IPackageRepositoryFactory RepositoryFactory { get; set; }\r
16 \r
17     [Import]\r
18     public IPackageSourceProvider SourceProvider { get; set; }\r
19 \r
20     protected IEnumerable<IPackage> GetAllPackages(string source, IEnumerable<string> ids)\r
21     {\r
22       IPackageRepository packageRepository = RepositoryFactory.CreateRepository(source);\r
23 \r
24       var param = Expression.Parameter(typeof (IPackageMetadata));\r
25 \r
26       Expression result = Expression.Constant(true);\r
27       foreach (var id in ids)\r
28       {\r
29         result = Expression.Equal(Expression.Property(param, "Id"), Expression.Constant(id));\r
30       }\r
31 \r
32       return packageRepository.GetPackages().Where(Expression.Lambda<Func<IPackage, bool>>(result, param));\r
33     }\r
34 \r
35     protected static void PrintPackageInfo(IPackage p)\r
36     {\r
37       var msg = ServiceMessageFormatter.FormatMessage(\r
38         "nuget-package",\r
39         new ServiceMessageProperty("Id", p.Id),\r
40         new ServiceMessageProperty("Version", p.Version.ToString())\r
41         );\r
42 \r
43       System.Console.Out.WriteLine(msg);\r
44     }\r
45 \r
46   }\r
47 }