PageRenderTime 28ms CodeModel.GetById 19ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/ajadex/SharpDevelop
C# | 140 lines | 106 code | 17 blank | 17 comment | 6 complexity | c8d885f787cd016fb44807aeadf62fb6 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 ICSharpCode.PackageManagement.Scripting;
 23using NuGet;
 24
 25namespace ICSharpCode.PackageManagement
 26{
 27	public class UpdateSolutionPackagesAction : IUpdatePackagesAction
 28	{
 29		List<IPackageFromRepository> packages = new List<IPackageFromRepository>();
 30		List<PackageOperation> operations = new List<PackageOperation>();
 31		List<IPackageManagementProject> projects;
 32		IPackageManagementEvents packageManagementEvents;
 33		
 34		public UpdateSolutionPackagesAction(
 35			IPackageManagementSolution solution,
 36			IPackageManagementEvents packageManagementEvents)
 37		{
 38			this.Solution = solution;
 39			this.UpdateDependencies = true;
 40			this.packageManagementEvents = packageManagementEvents;
 41		}
 42		
 43		public IPackageManagementSolution Solution { get; private set; }
 44		public IPackageScriptRunner PackageScriptRunner { get; set; }
 45		public bool UpdateDependencies { get; set; }
 46		public bool AllowPrereleaseVersions { get; set; }
 47		public ILogger Logger { get; set; }
 48		
 49		public IEnumerable<PackageOperation> Operations {
 50			get { return operations; }
 51		}
 52		
 53		public IEnumerable<IPackageFromRepository> Packages {
 54			get { return packages; }
 55		}
 56		
 57		public bool HasPackageScriptsToRun()
 58		{
 59			var files = new PackageFilesForOperations(Operations);
 60			return files.HasAnyPackageScripts();
 61		}
 62		
 63		public void AddOperations(IEnumerable<PackageOperation> operations)
 64		{
 65			this.operations.AddRange(operations);
 66		}
 67		
 68		public void AddPackages(IEnumerable<IPackageFromRepository> packages)
 69		{
 70			this.packages.AddRange(packages);
 71		}
 72		
 73		public void Execute()
 74		{
 75			if (PackageScriptRunner != null) {
 76				ExecuteWithScriptRunner();
 77			} else {
 78				ExecuteCore();
 79			}
 80		}
 81		
 82		void ExecuteWithScriptRunner()
 83		{
 84			using (RunAllProjectPackageScriptsAction runScriptsAction = CreateRunPackageScriptsAction()) {
 85				ExecuteCore();
 86			}
 87		}
 88		
 89		RunAllProjectPackageScriptsAction CreateRunPackageScriptsAction()
 90		{
 91			return CreateRunPackageScriptsAction(PackageScriptRunner, GetProjects());
 92		}
 93		
 94		void ExecuteCore()
 95		{
 96			RunPackageOperations();
 97			UpdatePackageReferences();
 98			packageManagementEvents.OnParentPackagesUpdated(Packages);
 99		}
100		
101		void RunPackageOperations()
102		{
103			IPackageManagementProject project = GetProjects().First();
104			project.RunPackageOperations(operations);
105		}
106		
107		IEnumerable<IPackageManagementProject> GetProjects()
108		{
109			if (projects == null) {
110				IPackageFromRepository package = packages.First();
111				projects = Solution
112					.GetProjects(package.Repository)
113					.Select(project => {
114						project.Logger = Logger;
115						return project;
116					})
117					.ToList();
118			}
119			return projects;
120		}
121		
122		void UpdatePackageReferences()
123		{
124			foreach (IPackageManagementProject project in GetProjects()) {
125				foreach (IPackageFromRepository package in packages) {
126					if (project.HasOlderPackageInstalled(package)) {
127						project.UpdatePackageReference(package, this);
128					}
129				}
130			}
131		}
132		
133		protected virtual RunAllProjectPackageScriptsAction CreateRunPackageScriptsAction(
134			IPackageScriptRunner scriptRunner,
135			IEnumerable<IPackageManagementProject> projects)
136		{
137			return new RunAllProjectPackageScriptsAction(scriptRunner, projects);
138		}
139	}
140}