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