Beispiel #1
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);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Executes a package installation.
        /// </summary>
        /// <param name="package">The package to install.</param>
        /// <param name="packageRepository">The repository for the package.</param>
        /// <param name="sourceLocation">The source location.</param>
        /// <param name="targetPath">The path where to install the package.</param>
        /// <returns>The package information.</returns>
        protected PackageInfo ExecuteInstall(IPackage package, IPackageRepository packageRepository, string sourceLocation, string targetPath) {
            // this logger is used to render NuGet's log on the notifier
            var logger = new NugetLogger(_notifier);

            bool installed = false;

            // if we can access the parent directory, and the solution is inside, NuGet-install the package here
            string solutionPath;
            var installedPackagesPath = String.Empty;
            if (TryGetSolutionPath(targetPath, out solutionPath)) {
                installedPackagesPath = Path.Combine(solutionPath, PackagesPath);
                try {
                    var packageManager = new NuGetPackageManager(
                        packageRepository,
                        new DefaultPackagePathResolver(sourceLocation),
                        new PhysicalFileSystem(installedPackagesPath) {Logger = logger}
                        ) {Logger = logger};

                    packageManager.InstallPackage(package, true);
                    installed = true;
                }
                catch {
                    // installing the package at the solution level failed
                }
            }

            // if the package got installed successfully, use it, otherwise use the previous repository
            var sourceRepository = installed
                ? new LocalPackageRepository(installedPackagesPath)
                : packageRepository;

            var project = new FileBasedProjectSystem(targetPath) { Logger = logger };
            var projectManager = new ProjectManager(
                sourceRepository, // source repository for the package to install
                new DefaultPackagePathResolver(targetPath),
                project,
                new ExtensionReferenceRepository(project, sourceRepository, _extensionManager)
                ) { Logger = logger };

            // add the package to the project
            projectManager.AddPackageReference(package.Id, package.Version);

            return new PackageInfo {
                ExtensionName = package.Title ?? package.Id,
                ExtensionVersion = package.Version.ToString(),
                ExtensionType = package.Id.StartsWith(PackagingSourceManager.GetExtensionPrefix(DefaultExtensionTypes.Theme)) ? DefaultExtensionTypes.Theme : DefaultExtensionTypes.Module,
                ExtensionPath = targetPath
            };
        }