UninstallPackage() public method

public UninstallPackage ( IPackage package ) : void
package IPackage
return void
Beispiel #1
0
        public void InstallPackage(PackageManager packageManager, Engine engine, bool updatePackages)
        {
            using (engine.Trace.WithIndent().Verbose("Installing package {0}{1} from {2}",
                _packageId, _versionSpec == null ? string.Empty : " " + _versionSpec, packageManager.SourceRepository.Source))
            {
                // Find the local package
                IPackage localPackage = packageManager.LocalRepository.FindPackage(_packageId);

                // Check if we're up to date
                if (localPackage != null && _versionSpec.Satisfies(localPackage.Version) && !updatePackages)
                {
                    engine.Trace.Verbose("Package {0}{1} is satisfied by version {2}, skipping",
                        _packageId, _versionSpec == null ? string.Empty : " " + _versionSpec, localPackage.Version);
                    return;
                }

                // Find the source package
                IPackage sourcePackage = packageManager.SourceRepository
                    .FindPackage(_packageId, _versionSpec, _allowPrereleaseVersions, _allowUnlisted);
                if (sourcePackage == null)
                {
                    engine.Trace.Warning("Package {0} {1} could not be found at {2}",
                        _packageId, _versionSpec == null ? string.Empty : " " + _versionSpec, packageManager.SourceRepository.Source);
                    return;
                }

                // Check if we're up to date
                if (localPackage != null && localPackage.Version >= sourcePackage.Version)
                {
                    engine.Trace.Verbose("Package {0}{1} is up to date with version {2}, skipping",
                        _packageId, _versionSpec == null ? string.Empty : " " + _versionSpec, localPackage.Version);
                    return;
                }

                // Uninstall the old package (removing any content files)
                if (localPackage != null)
                {
                    packageManager.UninstallPackage(localPackage, true);
                    engine.Trace.Verbose("Uninstalled package {0} {1}", localPackage.Id, localPackage.Version);
                }
            
                // Install it
                packageManager.InstallPackage(sourcePackage, false, _allowPrereleaseVersions);
                engine.Trace.Verbose("Installed package {0} {1}", sourcePackage.Id, sourcePackage.Version);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Uninstall <paramref name="package"/>, while still keeping the downloaded file in the cache.
        /// </summary>
        /// <remarks>It is safe to call it concurrently be cause we operations are done using the FileLock.</remarks>
        /// <param name="package">Package to uninstall.</param>
        public void UninstallPackage(NugetPackage package, ProgressReport progress)
        {
            using (GetLocalRepositoryLock())
            {
                currentProgressReport = progress;
                try
                {
                    manager.UninstallPackage(package.IPackage);

                    // Every time a new package is installed, we are updating the common targets
                    UpdateTargetsHelper();
                }
                finally
                {
                    currentProgressReport = null;
                }
            }
        }
        private void EnsureMultiplePackagesRespected(IReadOnlyCollection <IPackage> currentPackages, NuGet.PackageManager packageManager, string packageId, SemanticVersion packageSemanticVersion, Guid corrId)
        {
            if (this.allowMultipleVersionOfPackagesInCache)
            {
                return;
            }
            const string disallowMultipleVersion = "(Multiple version in cache disabled)";

            var packagesToUninstall = currentPackages.Where(p => p.Version != packageSemanticVersion).ToList();

            if (!packagesToUninstall.Any())
            {
                return;
            }

            foreach (var existingPackage in packagesToUninstall)
            {
                this.LogPackageOperation(existingPackage.Id, existingPackage.Version, $"Uninstall from cache {disallowMultipleVersion}", corrId);
                packageManager.UninstallPackage(existingPackage);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Uninstall package
        /// </summary>
        /// <param name="pkgId"></param>
        /// <returns></returns>
        public static JsonResponse UninstallPackage(string pkgId)
        {
            try
            {
                var packageManager = new NuGet.PackageManager(
                    _repository,
                    new DefaultPackagePathResolver(_feedUrl),
                    new PhysicalFileSystem(HttpContext.Current.Server.MapPath(Utils.ApplicationRelativeWebRoot + "App_Data/packages"))
                    );

                var package = _repository.FindPackage(pkgId);

                if (package == null)
                {
                    return new JsonResponse {
                               Success = false, Message = "Package " + pkgId + " not found"
                    }
                }
                ;

                packageManager.UninstallPackage(package, true);

                FileSystem.RemovePackageFiles(package.Id, package.Version.ToString());

                // reset cache
                Blog.CurrentInstance.Cache.Remove("Installed-Themes");
            }
            catch (Exception ex)
            {
                Utils.Log("PackageManager.UninstallPackage", ex);
                return(new JsonResponse {
                    Success = false, Message = "Error uninstalling package, see logs for details"
                });
            }

            return(new JsonResponse {
                Success = true, Message = "Package successfully uninstalled"
            });
        }
Beispiel #5
0
        public static async Task Uninstall(this IPackage package, string source, string dest)
        {
            IPackageRepository packageRepository = new LocalPackageRepository(source);
            IPackageManager packageManager = new PackageManager(packageRepository, dest);

            var tcs = new TaskCompletionSource<string>();

            var handler = new EventHandler<PackageOperationEventArgs>((sender, args) => tcs.SetResult("Ok"));

            packageManager.PackageUninstalled += handler;

            packageManager.UninstallPackage(package, true, true);

            packageManager.PackageUninstalled -= handler;

            if (await Task.WhenAny(tcs.Task, Task.Delay(5000)) == tcs.Task)
            {
                await tcs.Task;
            }
            else
            {
                throw new TimeoutException("Package uninstall timeout");
            }    
        }
Beispiel #6
0
        /// <summary>
        /// Uninstalls a package.
        /// </summary>
        /// <param name="packageId">The package identifier for the package to be uninstalled.</param>
        /// <param name="applicationPath">The application path.</param>
        public void Uninstall(string packageId, string applicationPath)
        {
            string solutionPath;
            string extensionFullPath = string.Empty;

            if (packageId.StartsWith(PackagingSourceManager.GetExtensionPrefix(DefaultExtensionTypes.Theme)))
            {
                extensionFullPath = _virtualPathProvider.MapPath("~/Themes/" + packageId.Substring(PackagingSourceManager.GetExtensionPrefix(DefaultExtensionTypes.Theme).Length));
            }
            else if (packageId.StartsWith(PackagingSourceManager.GetExtensionPrefix(DefaultExtensionTypes.Module)))
            {
                extensionFullPath = _virtualPathProvider.MapPath("~/Modules/" + packageId.Substring(PackagingSourceManager.GetExtensionPrefix(DefaultExtensionTypes.Module).Length));
            }

            if (string.IsNullOrEmpty(extensionFullPath) ||
                !Directory.Exists(extensionFullPath))
            {
                throw new OrchardException(T("Package not found: {0}", packageId));
            }

            // if we can access the parent directory, and the solution is inside, NuGet-uninstall the package here
            if (TryGetSolutionPath(applicationPath, out solutionPath))
            {
                // this logger is used to render NuGet's log on the notifier
                var logger = new NugetLogger(_notifier);

                var installedPackagesPath   = Path.Combine(solutionPath, PackagesPath);
                var sourcePackageRepository = new LocalPackageRepository(installedPackagesPath);

                try {
                    var project = new FileBasedProjectSystem(applicationPath)
                    {
                        Logger = logger
                    };
                    var projectManager = new ProjectManager(
                        sourcePackageRepository,
                        new DefaultPackagePathResolver(installedPackagesPath),
                        project,
                        new ExtensionReferenceRepository(project, sourcePackageRepository, _extensionManager)
                        )
                    {
                        Logger = logger
                    };

                    // add the package to the project
                    projectManager.RemovePackageReference(packageId);
                }
                catch {
                    // Uninstalling the package at the solution level failed
                }

                try {
                    var packageManager = new NuGetPackageManager(
                        sourcePackageRepository,
                        new DefaultPackagePathResolver(applicationPath),
                        new PhysicalFileSystem(installedPackagesPath)
                    {
                        Logger = logger
                    }
                        )
                    {
                        Logger = logger
                    };

                    packageManager.UninstallPackage(packageId);
                }
                catch {
                    // Package doesnt exist anymore
                }
            }

            // If the package was not installed through nuget we still need to try to uninstall it by removing its directory
            if (Directory.Exists(extensionFullPath))
            {
                Directory.Delete(extensionFullPath, true);
            }
        }
        public override bool UninstallPackage(string packageId)
        {
            var installedRepository = GetInstalledRepository();

            var package = installedRepository.FindPackage(packageId);
            if (package == null)
            {
                return false;
            }

            var packageManager = new PackageManager(installedRepository, InstalledRepository);

            packageManager.UninstallPackage(package, true);

            return true;
        }
        public override bool InstallOrUpdatePackage(string packageId)
        {
            var installableRepository = GetInstallableRepository();
            var installablePackage = installableRepository.FindPackage(packageId);
            if (installablePackage == null)
            {
                return false;
            }

            var installedRepository = GetInstalledRepository();
            var installedPackages = installedRepository.GetPackages().ToList();

            var packageManager = new PackageManager(installableRepository, InstalledRepository);
            packageManager.PackageInstalled += InstallPackage;

            var installedPackage = installedPackages.FirstOrDefault(i => string.Equals(i.Id, packageId, StringComparison.OrdinalIgnoreCase));
            if (installedPackage != null)
            {
                if (installedPackage.Version == installablePackage.Version)
                {
                    packageManager.UninstallPackage(installedPackage, true);
                    packageManager.InstallPackage(installablePackage, false, true);
                }
                else
                {
                    packageManager.UpdatePackage(installablePackage, false, true);
                }
            }
            else
            {
                packageManager.InstallPackage(installablePackage, false, true);
            }

            return true;
        }
Beispiel #9
0
        public static IPackageManager GetPackageManager(ChocolateyConfiguration configuration, ILogger nugetLogger, Action<PackageOperationEventArgs> installSuccessAction, Action<PackageOperationEventArgs> uninstallSuccessAction, bool addUninstallHandler)
        {
            IFileSystem nugetPackagesFileSystem = GetNuGetFileSystem(configuration, nugetLogger);
            IPackagePathResolver pathResolver = GetPathResolver(configuration, nugetPackagesFileSystem);
            var packageManager = new PackageManager(GetRemoteRepository(configuration, nugetLogger), pathResolver, nugetPackagesFileSystem, GetLocalRepository(pathResolver, nugetPackagesFileSystem))
                {
                    DependencyVersion = DependencyVersion.Highest,
                };

            if (configuration.Debug)
            {
                packageManager.Logger = nugetLogger;
            }

            //NOTE DO NOT EVER use this method - packageManager.PackageInstalling += (s, e) =>

            packageManager.PackageInstalled += (s, e) =>
                {
                    var pkg = e.Package;
                    "chocolatey".Log().Info(ChocolateyLoggers.Important, "{0}{1} v{2}{3}".format_with(Environment.NewLine, pkg.Id, pkg.Version.to_string(), configuration.Force ? " (forced)" : string.Empty));

                    if (installSuccessAction != null) installSuccessAction.Invoke(e);
                };

            if (addUninstallHandler)
            {
                // NOTE DO NOT EVER use this method, or endless loop - packageManager.PackageUninstalling += (s, e) =>


                packageManager.PackageUninstalled += (s, e) =>
                    {
                        IPackage pkg = packageManager.LocalRepository.FindPackage(e.Package.Id, e.Package.Version);
                        if (pkg != null)
                        {
                            // install not actually removed, let's clean it up. This is a bug with nuget, where it reports it removed some package and did NOTHING
                            // this is what happens when you are switching from AllowMultiple to just one and back
                            var chocoPathResolver = packageManager.PathResolver as ChocolateyPackagePathResolver;
                            if (chocoPathResolver != null)
                            {
                                chocoPathResolver.UseSideBySidePaths = !chocoPathResolver.UseSideBySidePaths;

                                // an unfound package folder can cause an endless loop.
                                // look for it and ignore it if doesn't line up with versioning
                                if (nugetPackagesFileSystem.DirectoryExists(chocoPathResolver.GetInstallPath(pkg)))
                                {
                                    //todo: This causes an issue with upgrades.
                                    // this causes this to be called again, which should then call the uninstallSuccessAction below
                                    packageManager.UninstallPackage(pkg, forceRemove: configuration.Force, removeDependencies: false);
                                }

                                chocoPathResolver.UseSideBySidePaths = configuration.AllowMultipleVersions;
                            }
                        }
                        else
                        {
                            if (uninstallSuccessAction != null) uninstallSuccessAction.Invoke(e);
                        }
                    };
            }

            return packageManager;
        }
Beispiel #10
0
        /// <summary>
        /// Uninstalls a package.
        /// </summary>
        /// <param name="packageId">The package identifier for the package to be uninstalled.</param>
        /// <param name="applicationPath">The application path.</param>
        public void Uninstall(string packageId, string applicationPath) {
            string solutionPath;
            string extensionFullPath = string.Empty;

            if (packageId.StartsWith(PackagingSourceManager.GetExtensionPrefix(DefaultExtensionTypes.Theme))) {
                extensionFullPath = _virtualPathProvider.MapPath("~/Themes/" + packageId.Substring(PackagingSourceManager.GetExtensionPrefix(DefaultExtensionTypes.Theme).Length));
            } else if (packageId.StartsWith(PackagingSourceManager.GetExtensionPrefix(DefaultExtensionTypes.Module))) {
                extensionFullPath = _virtualPathProvider.MapPath("~/Modules/" + packageId.Substring(PackagingSourceManager.GetExtensionPrefix(DefaultExtensionTypes.Module).Length));
            }

            if (string.IsNullOrEmpty(extensionFullPath) ||
                !Directory.Exists(extensionFullPath)) {

                throw new OrchardException(T("Package not found: {0}", packageId));
            }

            // if we can access the parent directory, and the solution is inside, NuGet-uninstall the package here
            if (TryGetSolutionPath(applicationPath, out solutionPath)) {

                // this logger is used to render NuGet's log on the notifier
                var logger = new NugetLogger(_notifier);

                var installedPackagesPath = Path.Combine(solutionPath, PackagesPath);
                var sourcePackageRepository = new LocalPackageRepository(installedPackagesPath);

                try {
                    var project = new FileBasedProjectSystem(applicationPath) {Logger = logger};
                    var projectManager = new ProjectManager(
                        sourcePackageRepository,
                        new DefaultPackagePathResolver(installedPackagesPath),
                        project,
                        new ExtensionReferenceRepository(project, sourcePackageRepository, _extensionManager)
                        ) {Logger = logger};

                    // add the package to the project
                    projectManager.RemovePackageReference(packageId);
                }
                catch {
                    // Uninstalling the package at the solution level failed
                }

                try {
                    var packageManager = new NuGetPackageManager(
                        sourcePackageRepository,
                        new DefaultPackagePathResolver(applicationPath),
                        new PhysicalFileSystem(installedPackagesPath) {Logger = logger}
                        ) {Logger = logger};

                    packageManager.UninstallPackage(packageId);
                }
                catch {
                    // Package doesnt exist anymore
                }
            }

            // If the package was not installed through nuget we still need to try to uninstall it by removing its directory
            if (Directory.Exists(extensionFullPath)) {
                Directory.Delete(extensionFullPath, true);
            }
        }
        public virtual void UninstallPackage([NotNull] string packageId)
        {
            var installedRepository = GetInstalledRepository();

            var package = installedRepository.FindPackage(packageId);
            if (package == null)
            {
                throw new InvalidOperationException("Package not found: " + packageId);
            }

            var packageManager = new PackageManager(installedRepository, InstalledRepository);

            packageManager.UninstallPackage(package, true);
        }
        public virtual void InstallOrUpdatePackage([NotNull] string packageId)
        {
            var availableRepository = GetAvailableRepository();
            var availablePackage = availableRepository.FindPackage(packageId);
            if (availablePackage == null)
            {
                throw new InvalidOperationException("Package not found: " + packageId);
            }

            var installedRepository = GetInstalledRepository();
            var installedPackages = installedRepository.GetPackages().ToList();

            var packageManager = new PackageManager(availableRepository, InstalledRepository);
            packageManager.PackageInstalled += InstallPackage;

            var installedPackage = installedPackages.FirstOrDefault(i => string.Equals(i.Id, packageId, StringComparison.OrdinalIgnoreCase));
            if (installedPackage != null)
            {
                if (installedPackage.Version == availablePackage.Version)
                {
                    packageManager.UninstallPackage(installedPackage, true);
                    packageManager.InstallPackage(availablePackage, false, true);
                }
                else
                {
                    packageManager.UpdatePackage(availablePackage, false, true);
                }
            }
            else
            {
                packageManager.InstallPackage(availablePackage, false, true);
            }
        }
Beispiel #13
0
        static void Uninstall(string pkgName, SemanticVersion version)
        {
            var packageManager = new PackageManager(Repository, Path);

            packageManager.UninstallPackage(pkgName, version);
        }
        /// <summary>
        /// Uninstall package
        /// </summary>
        /// <param name="pkgId"></param>
        /// <returns></returns>
        public static JsonResponse UninstallPackage(string pkgId)
        {
            try
            {
                var packageManager = new NuGet.PackageManager(
                    _repository,
                    new DefaultPackagePathResolver(_feedUrl),
                    new PhysicalFileSystem(HttpContext.Current.Server.MapPath(Utils.ApplicationRelativeWebRoot + "App_Data/packages"))
                );

                var package = _repository.FindPackage(pkgId);

                if(package == null)
                    return new JsonResponse { Success = false, Message = "Package " + pkgId + " not found" };

                packageManager.UninstallPackage(package, true);

                FileSystem.RemovePackageFiles(package.Id, package.Version.ToString());

                // reset cache
                Blog.CurrentInstance.Cache.Remove("Installed-Themes");
            }
            catch (Exception ex)
            {
                Utils.Log("PackageManager.UninstallPackage", ex);
                return new JsonResponse { Success = false, Message = "Error uninstalling package, see logs for details" };
            }

            return new JsonResponse { Success = true, Message = "Package successfully uninstalled" };
        }