PageRenderTime 30ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

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

https://github.com/ajadex/SharpDevelop
C# | 90 lines | 64 code | 9 blank | 17 comment | 4 complexity | d6964c0d8287da4f53f0108daaf59512 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 ICSharpCode.SharpDevelop.Project;
22using NuGet;
23
24namespace ICSharpCode.PackageManagement
25{
26	public class PackageReferencesForProject : IPackageReferencesForProject
27	{
28		MSBuildBasedProject project;
29		IPackageReferenceFileFactory packageReferenceFileFactory;
30		IPackageReferenceInstaller packageReferenceInstaller;
31		IPackageReferenceFile packageReferenceFile;
32		List<PackageReference> packageReferences;
33
34		public PackageReferencesForProject(
35			MSBuildBasedProject project,
36			IPackageRepositoryCache packageRepositoryCache)
37			: this(
38				project,
39				new PackageReferenceInstaller(packageRepositoryCache),
40				new PackageReferenceFileFactory())
41		{
42		}
43		
44		public PackageReferencesForProject(
45			MSBuildBasedProject project,
46			IPackageReferenceInstaller packageReferenceInstaller,
47			IPackageReferenceFileFactory packageReferenceFileFactory)
48		{
49			this.project = project;
50			this.packageReferenceFileFactory = packageReferenceFileFactory;
51			this.packageReferenceInstaller = packageReferenceInstaller;
52		}
53		
54		public void RemovePackageReferences()
55		{
56			GetPackageReferences();
57			IPackageReferenceFile file = PackageReferenceFile;
58			file.Delete();
59		}
60		
61		void GetPackageReferences()
62		{
63			if (packageReferences == null) {
64				IEnumerable<PackageReference> packageReferencesInFile = PackageReferenceFile.GetPackageReferences();
65				packageReferences = new List<PackageReference>(packageReferencesInFile);
66			}
67		}
68		
69		IPackageReferenceFile PackageReferenceFile {
70			get {
71				if (packageReferenceFile == null) {
72					packageReferenceFile = GetPackageReferenceFile();
73				}
74				return packageReferenceFile;
75			}
76		}
77		
78		IPackageReferenceFile GetPackageReferenceFile()
79		{
80			var fileName = new PackageReferenceFileNameForProject(project);
81			return packageReferenceFileFactory.CreatePackageReferenceFile(fileName.ToString());
82		}
83		
84		public void InstallPackages()
85		{
86			GetPackageReferences();
87			packageReferenceInstaller.InstallPackages(packageReferences, project);
88		}
89	}
90}