Esempio n. 1
0
 /// <summary>
 /// Parse package identity from path to .nupkg file, such as https://az320820.vo.msecnd.net/packages/microsoft.aspnet.mvc.4.0.20505.nupkg
 /// </summary>
 /// <param name="sourceUrl"></param>
 /// <returns></returns>
 private PackageIdentity ParsePackageIdentityFromNupkgPath(string path, string divider)
 {
     if (!string.IsNullOrEmpty(path))
     {
         string lastPart = path.Split(new string[] { divider }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
         lastPart = lastPart.Replace(Constants.PackageExtension, "");
         string[]      parts             = lastPart.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
         StringBuilder builderForId      = new StringBuilder();
         StringBuilder builderForVersion = new StringBuilder();
         foreach (string s in parts)
         {
             int  n;
             bool isNumeric = int.TryParse(s, out n);
             // Take pre-release versions such as EntityFramework.6.1.3-beta1 into account.
             if ((!isNumeric || string.IsNullOrEmpty(builderForId.ToString())) && string.IsNullOrEmpty(builderForVersion.ToString()))
             {
                 builderForId.Append(s);
                 builderForId.Append(".");
             }
             else
             {
                 builderForVersion.Append(s);
                 builderForVersion.Append(".");
             }
         }
         NuGetVersion nVersion = PowerShellCmdletsUtility.GetNuGetVersionFromString(builderForVersion.ToString().TrimEnd('.'));
         // Set _allowPrerelease to true if nVersion is prerelease version.
         if (nVersion != null && nVersion.IsPrerelease)
         {
             _allowPrerelease = true;
         }
         return(new PackageIdentity(builderForId.ToString().TrimEnd('.'), nVersion));
     }
     return(null);
 }
Esempio n. 2
0
        /// <summary>
        /// Preview update actions for single package
        /// </summary>
        /// <returns></returns>
        private async Task PreviewAndExecuteUpdateActionsForSinglePackage()
        {
            var actions = Enumerable.Empty <NuGetProjectAction>();

            using (var sourceCacheContext = new SourceCacheContext())
            {
                var resolutionContext = new ResolutionContext(
                    GetDependencyBehavior(),
                    _allowPrerelease,
                    ShouldAllowDelistedPackages(),
                    DetermineVersionConstraints(),
                    new GatherCache(),
                    sourceCacheContext);

                // PackageReference projects don't support `Update-Package -Reinstall`.
                List <NuGetProject> applicableProjects = GetApplicableProjectsAndWarnForRest(Projects);

                // If -Version switch is specified
                if (!string.IsNullOrEmpty(Version))
                {
                    actions = await PackageManager.PreviewUpdatePackagesAsync(
                        new PackageIdentity(Id, PowerShellCmdletsUtility.GetNuGetVersionFromString(Version)),
                        applicableProjects,
                        resolutionContext,
                        this,
                        PrimarySourceRepositories,
                        EnabledSourceRepositories,
                        Token);
                }
                else
                {
                    actions = await PackageManager.PreviewUpdatePackagesAsync(
                        Id,
                        applicableProjects,
                        resolutionContext,
                        this,
                        PrimarySourceRepositories,
                        EnabledSourceRepositories,
                        Token);
                }

                if (!actions.Any())
                {
                    _status = NuGetOperationStatus.NoOp;
                }
                else
                {
                    _packageCount = actions.Select(
                        action => action.PackageIdentity.Id).Distinct(StringComparer.OrdinalIgnoreCase).Count();
                }

                await ExecuteActions(actions, sourceCacheContext);
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Parse user input for -Version switch.
 /// If Version is given as prerelease versions, automatically append -Prerelease
 /// </summary>
 private void ParseUserInputForVersion()
 {
     if (!string.IsNullOrEmpty(Version))
     {
         _nugetVersion = PowerShellCmdletsUtility.GetNuGetVersionFromString(Version);
         if (_nugetVersion.IsPrerelease)
         {
             _versionSpecifiedPrerelease = true;
         }
     }
     _allowPrerelease = IncludePrerelease.IsPresent || _versionSpecifiedPrerelease;
 }
 /// <summary>
 /// Parse user input for -Version switch
 /// </summary>
 private void ParseUserInputForVersion()
 {
     if (!string.IsNullOrEmpty(Version))
     {
         // If Version is prerelease, automatically allow prerelease (i.e. append -Prerelease switch).
         _nugetVersion = PowerShellCmdletsUtility.GetNuGetVersionFromString(Version);
         if (_nugetVersion.IsPrerelease)
         {
             _versionSpecifiedPrerelease = true;
         }
     }
     _allowPrerelease = IncludePrerelease.IsPresent || _versionSpecifiedPrerelease;
 }
        /// <summary>
        /// Preview update actions for single package
        /// </summary>
        /// <returns></returns>
        private async Task PreviewAndExecuteUpdateActionsForSinglePackage()
        {
            var actions = Enumerable.Empty <NuGetProjectAction>();

            using (var sourceCacheContext = new SourceCacheContext())
            {
                var resolutionContext = new ResolutionContext(
                    GetDependencyBehavior(),
                    _allowPrerelease,
                    false,
                    DetermineVersionConstraints(),
                    new GatherCache(),
                    sourceCacheContext);

                // If -Version switch is specified
                if (!string.IsNullOrEmpty(Version))
                {
                    actions = await PackageManager.PreviewUpdatePackagesAsync(
                        new PackageIdentity(Id, PowerShellCmdletsUtility.GetNuGetVersionFromString(Version)),
                        Projects,
                        resolutionContext,
                        this,
                        PrimarySourceRepositories,
                        EnabledSourceRepositories,
                        Token);
                }
                else
                {
                    actions = await PackageManager.PreviewUpdatePackagesAsync(
                        Id,
                        Projects,
                        resolutionContext,
                        this,
                        PrimarySourceRepositories,
                        EnabledSourceRepositories,
                        Token);
                }

                if (!actions.Any())
                {
                    _status = NuGetOperationStatus.NoOp;
                }
                else
                {
                    _packageCount = actions.Select(
                        action => action.PackageIdentity.Id).Distinct().Count();
                }

                await ExecuteActions(actions, sourceCacheContext);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Returns single package identity for resolver when Id is specified
        /// </summary>
        /// <returns></returns>
        private async Task <PackageIdentity> GetPackageIdentity()
        {
            PackageIdentity identity = null;

            if (!string.IsNullOrEmpty(Version))
            {
                NuGetVersion nVersion = PowerShellCmdletsUtility.GetNuGetVersionFromString(Version);
                identity = new PackageIdentity(Id, nVersion);
            }
            else
            {
                identity = (await Project.GetInstalledPackagesAsync(CancellationToken.None))
                           .Where(p => string.Equals(p.PackageIdentity.Id, Id, StringComparison.OrdinalIgnoreCase))
                           .Select(v => v.PackageIdentity).FirstOrDefault();
            }
            return(identity);
        }
        /// <summary>
        /// Preview update actions for single package
        /// </summary>
        /// <returns></returns>
        private async Task PreviewAndExecuteUpdateActionsForSinglePackage()
        {
            var actions = Enumerable.Empty <NuGetProjectAction>();

            // If -Version switch is specified
            if (!string.IsNullOrEmpty(Version))
            {
                actions = await PackageManager.PreviewUpdatePackagesAsync(
                    new PackageIdentity(Id, PowerShellCmdletsUtility.GetNuGetVersionFromString(Version)),
                    Projects,
                    ResolutionContext,
                    this,
                    PrimarySourceRepositories,
                    EnabledSourceRepositories,
                    Token);
            }
            else
            {
                actions = await PackageManager.PreviewUpdatePackagesAsync(
                    Id,
                    Projects,
                    ResolutionContext,
                    this,
                    PrimarySourceRepositories,
                    EnabledSourceRepositories,
                    Token);
            }

            if (!actions.Any())
            {
                _status = NuGetOperationStatus.NoOp;
            }
            else
            {
                _packageCount = actions.Select(
                    action => action.PackageIdentity.Id).Distinct().Count();
            }

            await ExecuteActions(actions);
        }
Esempio n. 8
0
        /// <summary>
        /// Preview update actions for single package
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        private async Task PreviewAndExecuteUpdateActionsforSinglePackage(NuGetProject project)
        {
            var installedPackage = (await project.GetInstalledPackagesAsync(Token))
                                   .FirstOrDefault(p => string.Equals(p.PackageIdentity.Id, Id, StringComparison.OrdinalIgnoreCase));

            if (installedPackage != null)
            {
                // set _installed to true, if package to update is installed.
                _isPackageInstalled = true;

                var actions = Enumerable.Empty <NuGetProjectAction>();

                // If -Version switch is specified
                if (!string.IsNullOrEmpty(Version))
                {
                    actions = await PackageManager.PreviewUpdatePackagesAsync(
                        new PackageIdentity(installedPackage.PackageIdentity.Id, PowerShellCmdletsUtility.GetNuGetVersionFromString(Version)),
                        project,
                        ResolutionContext,
                        this,
                        PrimarySourceRepositories,
                        EnabledSourceRepositories,
                        Token);
                }
                else
                {
                    actions = await PackageManager.PreviewUpdatePackagesAsync(
                        installedPackage.PackageIdentity.Id,
                        project,
                        ResolutionContext,
                        this,
                        PrimarySourceRepositories,
                        EnabledSourceRepositories,
                        Token);
                }

                await ExecuteActions(project, actions);
            }
        }
Esempio n. 9
0
 /// <summary>
 /// Parse user input for -Version switch
 /// </summary>
 private void ParseUserInputForVersion()
 {
     if (!string.IsNullOrEmpty(Version))
     {
         DependencyBehavior updateVersion;
         IsVersionEnum = Enum.TryParse <DependencyBehavior>(Version, true, out updateVersion);
         if (IsVersionEnum)
         {
             _updateVersionEnum = updateVersion;
         }
         // If Version is prerelease, automatically allow prerelease (i.e. append -Prerelease switch).
         else
         {
             _nugetVersion = PowerShellCmdletsUtility.GetNuGetVersionFromString(Version);
             if (_nugetVersion.IsPrerelease)
             {
                 _versionSpecifiedPrerelease = true;
             }
         }
     }
     _allowPrerelease = IncludePrerelease.IsPresent || _versionSpecifiedPrerelease;
 }
Esempio n. 10
0
        /// <summary>
        /// Get update identity for a package that is installed to a project. Used for Update-Package Id -Version.
        /// </summary>
        /// <param name="installedPackage"></param>
        /// <param name="project"></param>
        /// <param name="includePrerelease"></param>
        /// <param name="isSafe"></param>
        /// <param name="version"></param>
        /// <param name="isEnum"></param>
        /// <param name="dependencyEnum"></param>
        /// <returns></returns>
        protected PackageIdentity GetPackageUpdate(PackageReference installedPackage, NuGetProject project,
                                                   bool includePrerelease, bool isSafe, string version = null, bool isEnum = false, DependencyBehavior dependencyEnum = DependencyBehavior.Lowest)
        {
            PackageIdentity identity = null;

            if (isSafe)
            {
                identity = PowerShellCmdletsUtility.GetSafeUpdateForPackageIdentity(ActiveSourceRepository, installedPackage.PackageIdentity, project, includePrerelease, installedPackage.PackageIdentity.Version);
            }
            else if (isEnum)
            {
                identity = PowerShellCmdletsUtility.GetUpdateForPackageByDependencyEnum(ActiveSourceRepository, installedPackage.PackageIdentity, project, dependencyEnum, includePrerelease);
            }
            else
            {
                NuGetVersion nVersion = PowerShellCmdletsUtility.GetNuGetVersionFromString(version);
                identity = new PackageIdentity(installedPackage.PackageIdentity.Id, nVersion);
            }

            // Since package downgrade is allowed, we will not check nVersion > installedPackage.PackageIdentity.Version here.
            return(identity);
        }
        protected override void ProcessRecordCore()
        {
            Preprocess();

            IPackageSearchMetadata package = null;

            try
            {
                var metadata = NuGetUIThreadHelper.JoinableTaskFactory.Run(() => GetPackagesFromRemoteSourceAsync(Id, IncludePrerelease.IsPresent));

                if (!string.IsNullOrEmpty(Version))
                {
                    NuGetVersion nVersion = PowerShellCmdletsUtility.GetNuGetVersionFromString(Version);
                    metadata = metadata.Where(p => p.Identity.Version == nVersion);
                }
                package = metadata
                          .OrderByDescending(v => v.Identity.Version)
                          .FirstOrDefault();
            }
            catch
            {
            }

            if (package != null &&
                package.Identity != null)
            {
                Uri targetUrl = null;
                if (License.IsPresent)
                {
                    targetUrl = package.LicenseUrl;
                }
                else if (ReportAbuse.IsPresent)
                {
                    targetUrl = package.ReportAbuseUrl;
                }
                else
                {
                    targetUrl = package.ProjectUrl;
                }

                if (targetUrl != null)
                {
                    OpenUrl(targetUrl);

                    if (PassThru.IsPresent)
                    {
                        WriteObject(targetUrl);
                    }
                }
                else
                {
                    WriteError(String.Format(CultureInfo.CurrentCulture, Resources.Cmdlet_UrlMissing, package.Identity.Id + " " + package.Identity.Version));
                }
            }
            else
            {
                // show appropriate error message depending on whether Version parameter is set.
                if (string.IsNullOrEmpty(Version))
                {
                    WriteError(String.Format(CultureInfo.CurrentCulture, Resources.Cmdlet_PackageIdNotFound, Id));
                }
                else
                {
                    WriteError(String.Format(CultureInfo.CurrentCulture, Resources.Cmdlet_PackageIdAndVersionNotFound, Id, Version));
                }
            }
        }
Esempio n. 12
0
        protected override void ProcessRecordCore()
        {
            Preprocess();

            UIPackageMetadata package = null;

            try
            {
                UIMetadataResource resource = ActiveSourceRepository.GetResource <UIMetadataResource>();
                Task <IEnumerable <UIPackageMetadata> > task = resource.GetMetadata(Id, IncludePrerelease.IsPresent, false, CancellationToken.None);
                var metadata = task.Result;
                if (!string.IsNullOrEmpty(Version))
                {
                    NuGetVersion nVersion = PowerShellCmdletsUtility.GetNuGetVersionFromString(Version);
                    metadata = metadata.Where(p => p.Identity.Version == nVersion);
                }
                package = metadata.Where(p => string.Equals(p.Identity.Id, Id, StringComparison.OrdinalIgnoreCase))
                          .OrderByDescending(v => v.Identity.Version)
                          .FirstOrDefault();
            }
            catch (Exception)
            {
            }

            if (package != null)
            {
                Uri targetUrl = null;
                if (License.IsPresent)
                {
                    targetUrl = package.LicenseUrl;
                }
                else if (ReportAbuse.IsPresent)
                {
                    targetUrl = package.ReportAbuseUrl;
                }
                else
                {
                    targetUrl = package.ProjectUrl;
                }

                if (targetUrl != null)
                {
                    OpenUrl(targetUrl);

                    if (PassThru.IsPresent)
                    {
                        WriteObject(targetUrl);
                    }
                }
                else
                {
                    WriteError(String.Format(CultureInfo.CurrentCulture, Resources.Cmdlet_UrlMissing, Id + " " + Version));
                }
            }
            else
            {
                // show appropriate error message depending on whether Version parameter is set.
                if (string.IsNullOrEmpty(Version))
                {
                    WriteError(String.Format(CultureInfo.CurrentCulture, Resources.Cmdlet_PackageIdNotFound, Id));
                }
                else
                {
                    WriteError(String.Format(CultureInfo.CurrentCulture, Resources.Cmdlet_PackageIdAndVersionNotFound, Id, Version));
                }
            }
        }
Esempio n. 13
0
        protected override void ProcessRecordCore()
        {
            Preprocess();

            IPackageSearchMetadata package = null;

            try {
                var metadata = Task.Run(() => GetPackagesFromRemoteSourceAsync(Id, IncludePrerelease.IsPresent)).Result;

                if (!string.IsNullOrEmpty(Version))
                {
                    NuGetVersion nVersion = PowerShellCmdletsUtility.GetNuGetVersionFromString(Version);
                    metadata = metadata.Where(p => p.Identity.Version == nVersion);
                }
                package = metadata
                          .OrderByDescending(v => v.Identity.Version)
                          .FirstOrDefault();
            } catch {
            }

            if (package != null &&
                package.Identity != null)
            {
                Uri targetUrl = null;
                if (License.IsPresent)
                {
                    targetUrl = package.LicenseUrl;
                }
                else if (ReportAbuse.IsPresent)
                {
                    targetUrl = package.ReportAbuseUrl;
                }
                else
                {
                    targetUrl = package.ProjectUrl;
                }

                if (targetUrl != null)
                {
                    OpenUrl(targetUrl);

                    if (PassThru.IsPresent)
                    {
                        WriteObject(targetUrl);
                    }
                }
                else
                {
                    WriteError(String.Format(
                                   CultureInfo.CurrentCulture,
                                   "The package '{0}' does not provide the requested URL.",
                                   package.Identity.Id + " " + package.Identity.Version));
                }
            }
            else
            {
                // show appropriate error message depending on whether Version parameter is set.
                if (string.IsNullOrEmpty(Version))
                {
                    WriteError(String.Format(
                                   CultureInfo.CurrentCulture,
                                   "Package with the Id '{0}' is not found in the specified source.",
                                   Id));
                }
                else
                {
                    WriteError(String.Format(
                                   CultureInfo.CurrentCulture,
                                   "Package with the Id '{0}' and version '{1}' is not found in the specified source.",
                                   Id,
                                   Version));
                }
            }
        }