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

/BlogEngine/DotNetSlave.BusinessLogic/Packaging/Installer.cs

#
C# | 125 lines | 89 code | 19 blank | 17 comment | 13 complexity | cf1c345f5b2a62e70e8e7090ab76d8b5 MD5 | raw file
  1using System;
  2using System.Linq;
  3using System.Web;
  4using System.Collections.Generic;
  5using BlogEngine.Core.Json;
  6using BlogEngine.Core.Providers;
  7using NuGet;
  8
  9namespace BlogEngine.Core.Packaging
 10{
 11    /// <summary>
 12    /// Responsible for install/uninstall operations
 13    /// </summary>
 14    public static class Installer
 15    {
 16        private static IPackageRepository _repository
 17        {
 18            get
 19            {
 20                return PackageRepositoryFactory.Default.CreateRepository(
 21                    new PackageSource(BlogSettings.Instance.GalleryFeedUrl, "Default"));
 22            }
 23        }
 24
 25        /// <summary>
 26        /// Install package
 27        /// </summary>
 28        /// <param name="pkgId"></param>
 29        public static JsonResponse InstallPackage(string pkgId)
 30        {
 31            try
 32            {
 33                if(BlogService.InstalledFromGalleryPackages() != null && 
 34                    BlogService.InstalledFromGalleryPackages().Find(p => p.PackageId == pkgId) != null)
 35                    UninstallPackage(pkgId);
 36
 37                var packageManager = new PackageManager(
 38                    _repository,
 39                    new DefaultPackagePathResolver(BlogSettings.Instance.GalleryFeedUrl),
 40                    new PhysicalFileSystem(HttpContext.Current.Server.MapPath(Utils.ApplicationRelativeWebRoot + "App_Data/packages"))
 41                );
 42
 43                var package = _repository.FindPackage(pkgId);
 44
 45                packageManager.InstallPackage(package, false);
 46
 47                var iPkg = new InstalledPackage { PackageId = package.Id, Version = package.Version.ToString() };
 48                BlogService.InsertPackage(iPkg);
 49
 50                var packageFiles = FileSystem.InstallPackage(package.Id, package.Version.ToString());
 51                BlogService.InsertPackageFiles(packageFiles);
 52
 53                Blog.CurrentInstance.Cache.Remove(Constants.CacheKey);
 54            }
 55            catch (Exception ex)
 56            {
 57                Utils.Log("BlogEngine.Core.Packaging.Installer.InstallPackage", ex);
 58                return new JsonResponse { Success = false, Message = "Error installing package, see logs for details" };
 59            }
 60
 61            return new JsonResponse { Success = true, Message = "Package successfully installed" };
 62        }
 63
 64        /// <summary>
 65        /// Uninstall package
 66        /// </summary>
 67        /// <param name="pkgId"></param>
 68        /// <returns></returns>
 69        public static JsonResponse UninstallPackage(string pkgId)
 70        {
 71            try
 72            {
 73                var packageManager = new PackageManager(
 74                    _repository,
 75                    new DefaultPackagePathResolver(BlogSettings.Instance.GalleryFeedUrl),
 76                    new PhysicalFileSystem(HttpContext.Current.Server.MapPath(Utils.ApplicationRelativeWebRoot + "App_Data/packages"))
 77                );
 78
 79                var package = _repository.FindPackage(pkgId);
 80
 81                if (package == null)
 82                    return new JsonResponse { Success = false, Message = "Package " + pkgId + " not found" };
 83
 84                packageManager.UninstallPackage(package, true);
 85
 86                FileSystem.UninstallPackage(package.Id);
 87
 88                BlogService.DeletePackage(pkgId);
 89
 90                // reset cache
 91                Blog.CurrentInstance.Cache.Remove(Constants.CacheKey);
 92            }
 93            catch (Exception ex)
 94            {
 95                Utils.Log("PackageManager.UninstallPackage", ex);
 96                return new JsonResponse { Success = false, Message = "Error uninstalling package, see logs for details" };
 97            }
 98
 99            return new JsonResponse { Success = true, Message = "Package successfully uninstalled" };
100        }
101
102        /// <summary>
103        /// Load installed packages
104        /// </summary>
105        /// <param name="packages"></param>
106        public static void MarkAsInstalled(List<JsonPackage> packages)
107        {
108            var installed = BlogService.InstalledFromGalleryPackages();
109
110            foreach (var pkg in packages)
111            {
112                if (pkg == null) continue;
113                var p = pkg;
114                if(installed != null && installed.Count > 0)
115                {
116                    foreach (var inst in installed.Where(inst => p.Id == inst.PackageId))
117                    {
118                        pkg.Location = "I";
119                        pkg.LocalVersion = inst.Version;
120                    }
121                }
122            }
123        }
124    }
125}