コード例 #1
0
        /// <summary>
        /// Removes the dependency from the project
        /// </summary>
        public static async Task RemovePackageDependency(this DotNetProject project, IPackageDependency dependency)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            LoggingService.LogInfo("Removing package dependency '{0}' from project", dependency.DisplayName);

            if (dependency.Status == Status.NotAdded || !project.PackageAdded(dependency))
            {
                LoggingService.LogInfo("Skipped, the package dependency is not added to the project");
                return;
            }

            try {
                var references = new List <string> ();
                references.Add(dependency.PackageId);

                var task = PackageManagementServices.ProjectOperations.UninstallPackagesAsync(project, references, true);

                await task.ConfigureAwait(false);
            } catch (InvalidOperationException) {
                // Nuget throws these and logs them, let's not pollute the log anymore than we need to
                // and assume that it needs to be left in the project
            } catch (Exception ex) {
                LoggingService.LogInternalError("Could not queue package for uninstallation", ex);
                throw;
            }
        }
コード例 #2
0
        /// <summary>
        /// Adds the dependencies to the project
        /// </summary>
        public static async Task <bool> AddPackageDependency(this DotNetProject project, IPackageDependency dependency)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            LoggingService.LogInfo("Adding package dependency '{0}' to project", dependency.DisplayName);

            if (dependency.Status == Status.Added)
            {
                LoggingService.LogInfo("Skipped, the package dependency is already added to the project");
                return(true);
            }

            try {
                var references = new List <PackageManagementPackageReference> ();
                references.Add(new PackageManagementPackageReference(dependency.PackageId, dependency.PackageVersion));

                var task = PackageManagementServices.ProjectOperations.InstallPackagesAsync(project, references);

                await task.ConfigureAwait(false);

                return(true);
            } catch (InvalidOperationException) {
                // Nuget throws these and logs them, let's not pollute the log anymore than we need to
                // and assume that it was already added to the project
                throw;
            } catch (Exception ex) {
                LoggingService.LogInternalError("Could not queue package for installation", ex);
                throw;
            }
        }
コード例 #3
0
 /// <summary>
 /// Determines if the given package dependency has been added to the project or not
 /// </summary>
 public static bool PackageAdded(this DotNetProject project, IPackageDependency dependency)
 {
     return(PackageManagementServices.ProjectOperations.GetInstalledPackages(project).Any(p => p.Id == dependency.PackageId));
 }