コード例 #1
0
        private void ChangePackage(CommandEventArgs e, string packageId, string version)
        {
            IEnumerable <string> errors = Enumerable.Empty <string>();

            switch (e.CommandName.ToLower())
            {
            case "uninstall":
            {
                var package = NuGetService.SourceRepository.FindPackage(packageId, (version != null) ? SemanticVersion.Parse(version) : null, false, false);
                errors = NuGetService.UninstallPackage(package, true);
            }
            break;

            case "install":
            {
                var package = NuGetService.SourceRepository.FindPackage(packageId, (version != null) ? SemanticVersion.Parse(version) : null, false, false);
                if (package != null)
                {
                    errors = NuGetService.InstallPackage(package);
                    //IEnumerable<IPackage> packagesRequiringLicenseAcceptance = NuGetService.GetPackagesRequiringLicenseAcceptance(package);
                }
                break;
            }

            case "update":
            {
                var installed = NuGetService.GetInstalledPackage(packageId);
                try
                {
                    var update = NuGetService.GetUpdate(installed);
                    errors = NuGetService.UpdatePackage(update);
                }
                catch (InvalidOperationException ex)
                {
                    errors.Concat(new[] { string.Format("There is a problem with {0}: {1}", installed.Title, ex.Message) });
                }
            }
            break;
            }

            if (errors != null && errors.Count() > 0)
            {
                nbMessage.Visible = true;
                nbMessage.Text    = errors.Aggregate(new StringBuilder("<ul>"), (sb, s) => sb.AppendFormat("<li>{0}</li>", s)).Append("</ul>").ToString();
            }
        }
コード例 #2
0
        /// <summary>
        /// Used to populate each item in the PackageList
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gPackageList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            IPackage package = e.Row.DataItem as IPackage;

            if (package != null)
            {
                Boolean isPackageInstalled = NuGetService.IsPackageInstalled(package, anyVersion: true);

                LinkButton lbCommand           = e.Row.FindControl("lbCommand") as LinkButton;
                LinkButton lbUpdate            = e.Row.FindControl("lbUpdate") as LinkButton;
                LinkButton lbView              = e.Row.FindControl("lbView") as LinkButton;
                HtmlAnchor link                = e.Row.FindControl("lProjectUrl") as HtmlAnchor;
                Literal    lblAuthors          = e.Row.FindControl("lblAuthors") as Literal;
                Literal    lblVersion          = e.Row.FindControl("lblVersion") as Literal;
                Literal    lblLatestVersion    = e.Row.FindControl("lblLatestVersion") as Literal;
                Literal    lblInstalledVersion = e.Row.FindControl("lblInstalledVersion") as Literal;

                if (package.IconUrl != null)
                {
                    Image imgIconUrl = e.Row.FindControl("imgIconUrl") as Image;
                    imgIconUrl.ImageUrl = package.IconUrl.ToString();;
                }

                lblAuthors.Text = string.Join(",", package.Authors);

                if (package.ProjectUrl != null)
                {
                    link.Visible = true;
                    link.HRef    = package.ProjectUrl.ToString();
                }
                else
                {
                    link.Visible = false;
                }

                lbUpdate.Visible = false;

                // If this package (not necessarily this version) is installed
                // show an uninstall button and/or an update button if a later version exists
                if (isPackageInstalled)
                {
                    IPackage theInstalledPackage = NuGetService.GetInstalledPackage(package.Id);
                    if (theInstalledPackage != null)
                    {
                        lblInstalledVersion.Visible = true;
                        lblInstalledVersion.Text   += theInstalledPackage.Version;

                        try
                        {
                            // Checking "IsLatestVersion" does not work because of what's discussed here:
                            // http://nuget.codeplex.com/discussions/279837
                            // if ( !installedPackage.IsLatestVersion )...
                            var latestPackage = NuGetService.GetUpdate(package);
                            if (latestPackage != null)
                            {
                                lbUpdate.Visible         = true;
                                lblLatestVersion.Visible = true;
                                lblLatestVersion.Text   += latestPackage.Version;
                            }
                        }
                        catch (InvalidOperationException ex)
                        {
                            Literal lblItemError = e.Row.FindControl("lblItemError") as Literal;
                            lblItemError.Text = string.Format("<p class='text-error'>We're having a problem... {0}</p>", ex.Message);
                        }
                    }

                    lbCommand.CommandName = "uninstall";
                    lbCommand.Text        = "<i class='fa fa-times'></i> &nbsp; Uninstall";
                    lbCommand.AddCssClass("btn-warning");
                }
                else
                {
                    lblVersion.Visible    = true;
                    lblVersion.Text      += package.Version;
                    lbCommand.CommandName = "Install";
                    lbCommand.Text        = "<i class='fa fa-download'></i> &nbsp; Install";
                }

                lbCommand.CommandArgument = lbUpdate.CommandArgument = lbView.CommandArgument = e.Row.RowIndex.ToString();
            }
        }