PageRenderTime 23ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageInitializationScripts.cs

https://github.com/ajadex/SharpDevelop
C# | 83 lines | 56 code | 10 blank | 17 comment | 3 complexity | 018cb550460c5e4e3e5ad32100797dc6 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;
21
22using NuGet;
23
24namespace ICSharpCode.PackageManagement.Scripting
25{
26	public class PackageInitializationScripts : IPackageInitializationScripts
27	{
28		ISolutionPackageRepository solutionPackageRepository;
29		IPackageScriptFactory scriptFactory;
30		List<IPackageScript> scripts;
31		
32		public PackageInitializationScripts(
33			ISolutionPackageRepository solutionPackageRepository,
34			IPackageScriptFactory scriptFactory)
35		{
36			this.solutionPackageRepository = solutionPackageRepository;
37			this.scriptFactory = scriptFactory;
38		}
39		
40		public void Run(IPackageScriptSession session)
41		{
42			foreach (IPackageScript script in GetScripts()) {
43				script.Run(session);
44			}
45		}
46		
47		List<IPackageScript> GetScripts()
48		{
49			if (scripts == null) {
50				CreatePackageInitializationScripts();
51			}
52			return scripts;
53		}
54		
55		void CreatePackageInitializationScripts()
56		{
57			scripts = new List<IPackageScript>();
58			foreach (IPackage package in GetPackages()) {
59				IPackageScript script = CreateInitializeScript(package);
60				if (script.Exists()) {
61					scripts.Add(script);
62				}
63			}
64		}
65		
66		IEnumerable<IPackage> GetPackages()
67		{
68			return solutionPackageRepository.GetPackagesByDependencyOrder();
69		}
70		
71		IPackageScript CreateInitializeScript(IPackage package)
72		{
73			string packageInstallDirectory = solutionPackageRepository.GetInstallPath(package);
74			return scriptFactory.CreatePackageInitializeScript(package, packageInstallDirectory);
75		}
76		
77		public bool Any()
78		{
79			List<IPackageScript> scripts = GetScripts();
80			return scripts.Count > 0;
81		}
82	}
83}