PageRenderTime 38ms CodeModel.GetById 11ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

/src/AddIns/Misc/PackageManagement/Project/Src/RecentPackageRepository.cs

https://github.com/ajadex/SharpDevelop
C# | 187 lines | 142 code | 28 blank | 17 comment | 5 complexity | 707781505e565aad946ad9d013c9774f MD5 | raw file
  1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2// 
  3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4// software and associated documentation files (the "Software"), to deal in the Software
  5// without restriction, including without limitation the rights to use, copy, modify, merge,
  6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7// to whom the Software is furnished to do so, subject to the following conditions:
  8// 
  9// The above copyright notice and this permission notice shall be included in all copies or
 10// substantial portions of the Software.
 11// 
 12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 17// DEALINGS IN THE SOFTWARE.
 18
 19using System;
 20using System.Collections.Generic;
 21using System.Linq;
 22using NuGet;
 23
 24namespace ICSharpCode.PackageManagement
 25{
 26	public class RecentPackageRepository : IRecentPackageRepository
 27	{
 28		public const int DefaultMaximumPackagesCount = 20;
 29		
 30		List<IPackage> packages = new List<IPackage>();
 31		int maximumPackagesCount = DefaultMaximumPackagesCount;
 32		IList<RecentPackageInfo> savedRecentPackages;
 33		IPackageRepository aggregateRepository;
 34		IPackageManagementEvents packageManagementEvents;
 35		
 36		public RecentPackageRepository(
 37			IList<RecentPackageInfo> recentPackages,
 38			IPackageRepository aggregateRepository,
 39			IPackageManagementEvents packageManagementEvents)
 40		{
 41			this.savedRecentPackages = recentPackages;
 42			this.aggregateRepository = aggregateRepository;
 43			this.packageManagementEvents = packageManagementEvents;
 44			
 45			this.packageManagementEvents.ParentPackageInstalled += ParentPackageInstalled;
 46		}
 47		
 48		void ParentPackageInstalled(object sender, ParentPackageOperationEventArgs e)
 49		{
 50			AddPackage(e.Package);
 51		}
 52		
 53		public string Source {
 54			get { return "RecentPackages"; }
 55		}
 56		
 57		public void AddPackage(IPackage package)
 58		{
 59			RemovePackageIfAlreadyAdded(package);
 60			AddPackageAtBeginning(package);
 61			RemoveLastPackageIfCurrentPackageCountExceedsMaximum();
 62			UpdateRecentPackagesInOptions();
 63		}
 64		
 65		void RemovePackageIfAlreadyAdded(IPackage package)
 66		{
 67			int index = FindPackage(package);
 68			if (index >= 0) {
 69				packages.RemoveAt(index);
 70			}
 71		}
 72		
 73		int FindPackage(IPackage package)
 74		{
 75			return packages.FindIndex(p => PackageEqualityComparer.IdAndVersion.Equals(package, p));
 76		}
 77		
 78		void AddPackageAtBeginning(IPackage package)
 79		{
 80			packages.Insert(0, package);
 81		}
 82		
 83		void RemoveLastPackageIfCurrentPackageCountExceedsMaximum()
 84		{
 85			if (packages.Count > maximumPackagesCount) {
 86				RemoveLastPackage();
 87			}
 88		}
 89
 90		void RemoveLastPackage()
 91		{
 92			packages.RemoveAt(packages.Count - 1);
 93		}
 94		
 95		void UpdateRecentPackagesInOptions()
 96		{
 97			savedRecentPackages.Clear();
 98			savedRecentPackages.AddRange(GetRecentPackagesInfo());
 99		}
100		
101		List<RecentPackageInfo> GetRecentPackagesInfo()
102		{
103			List<RecentPackageInfo> allRecentPackages = new List<RecentPackageInfo>();
104			foreach (IPackage package in packages) {
105				var recentPackageInfo = new RecentPackageInfo(package);
106				allRecentPackages.Add(recentPackageInfo);
107			}
108			return allRecentPackages;
109		}
110		
111		public void RemovePackage(IPackage package)
112		{
113		}
114		
115		public IQueryable<IPackage> GetPackages()
116		{
117			UpdatePackages();
118			return packages.AsQueryable();
119		}
120		
121		void UpdatePackages()
122		{
123			if (!HasRecentPackagesBeenRead() && HasRecentPackages) {
124				IEnumerable<IPackage> recentPackages = GetRecentPackages();
125				packages.AddRange(recentPackages);
126			}
127		}
128		
129		bool HasRecentPackagesBeenRead()
130		{
131			return packages.Count > 0;
132		}
133		
134		public bool HasRecentPackages {
135			get { return savedRecentPackages.Count > 0; }
136		}
137		
138		IEnumerable<IPackage> GetRecentPackages()
139		{
140			IEnumerable<IPackage> recentPackages = GetRecentPackagesFilteredById();
141			return GetRecentPackagesFilteredByVersion(recentPackages);
142		}
143		
144		IEnumerable<IPackage> GetRecentPackagesFilteredById()
145		{
146			IEnumerable<string> recentPackageIds = GetRecentPackageIds();
147			return aggregateRepository.FindPackages(recentPackageIds);
148		}
149				
150		IEnumerable<string> GetRecentPackageIds()
151		{
152			foreach (RecentPackageInfo recentPackageInfo in savedRecentPackages) {
153				yield return recentPackageInfo.Id;
154			}
155		}
156		
157		IEnumerable<IPackage> GetRecentPackagesFilteredByVersion(IEnumerable<IPackage> recentPackages)
158		{
159			List<IPackage> filteredRecentPackages = new List<IPackage>();
160			foreach (IPackage recentPackage in recentPackages) {
161				foreach (RecentPackageInfo savedRecentPackageInfo in savedRecentPackages) {
162					if (savedRecentPackageInfo.IsMatch(recentPackage)) {
163						filteredRecentPackages.Add(recentPackage);
164					}
165				}
166			}
167			return filteredRecentPackages;
168		}
169		
170		public int MaximumPackagesCount {
171			get { return maximumPackagesCount; }
172			set { maximumPackagesCount = value; }
173		}
174		
175		public void Clear()
176		{
177			packages.Clear();
178			UpdateRecentPackagesInOptions();
179		}
180		
181		public bool SupportsPrereleasePackages {
182			get { return false; }
183		}
184		
185		public PackageSaveModes PackageSaveMode { get; set; }
186	}
187}