/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. using System;
  19. using System.Collections.Generic;
  20. using NuGet;
  21. namespace ICSharpCode.PackageManagement.Scripting
  22. {
  23. public class PackageInitializationScripts : IPackageInitializationScripts
  24. {
  25. ISolutionPackageRepository solutionPackageRepository;
  26. IPackageScriptFactory scriptFactory;
  27. List<IPackageScript> scripts;
  28. public PackageInitializationScripts(
  29. ISolutionPackageRepository solutionPackageRepository,
  30. IPackageScriptFactory scriptFactory)
  31. {
  32. this.solutionPackageRepository = solutionPackageRepository;
  33. this.scriptFactory = scriptFactory;
  34. }
  35. public void Run(IPackageScriptSession session)
  36. {
  37. foreach (IPackageScript script in GetScripts()) {
  38. script.Run(session);
  39. }
  40. }
  41. List<IPackageScript> GetScripts()
  42. {
  43. if (scripts == null) {
  44. CreatePackageInitializationScripts();
  45. }
  46. return scripts;
  47. }
  48. void CreatePackageInitializationScripts()
  49. {
  50. scripts = new List<IPackageScript>();
  51. foreach (IPackage package in GetPackages()) {
  52. IPackageScript script = CreateInitializeScript(package);
  53. if (script.Exists()) {
  54. scripts.Add(script);
  55. }
  56. }
  57. }
  58. IEnumerable<IPackage> GetPackages()
  59. {
  60. return solutionPackageRepository.GetPackagesByDependencyOrder();
  61. }
  62. IPackageScript CreateInitializeScript(IPackage package)
  63. {
  64. string packageInstallDirectory = solutionPackageRepository.GetInstallPath(package);
  65. return scriptFactory.CreatePackageInitializeScript(package, packageInstallDirectory);
  66. }
  67. public bool Any()
  68. {
  69. List<IPackageScript> scripts = GetScripts();
  70. return scripts.Count > 0;
  71. }
  72. }
  73. }