Example #1
0
        /// <summary>
        /// Output installed packages to the project(s)
        /// </summary>
        private void WriteInstalledPackages(Dictionary <NuGetProject, IEnumerable <Packaging.PackageReference> > dictionary)
        {
            // Get the PowerShellPackageWithProjectView
            var view = PowerShellInstalledPackage.GetPowerShellPackageView(dictionary, VsSolutionManager, ConfigSettings);

            if (view.Any())
            {
                WriteObject(view, enumerateCollection: true);
            }
            else
            {
                LogCore(MessageLevel.Info, Resources.Cmdlet_NoPackagesInstalled);
            }
        }
        /// <summary>
        /// Output installed packages to the project(s)
        /// </summary>
        void WriteInstalledPackages(Dictionary <Project, IEnumerable <Packaging.PackageReference> > dictionary)
        {
            // Get the PowerShellPackageWithProjectView
            var view = PowerShellInstalledPackage.GetPowerShellPackageView(dictionary, null);

            if (view.Any())
            {
                WriteObject(view, enumerateCollection: true);
            }
            else
            {
                LogCore(MessageLevel.Info, "No packages installed.");
            }
        }
 /// <summary>
 /// Get the view of installed packages. Use for Get-Package command. 
 /// </summary>
 /// <param name="metadata"></param>
 /// <param name="versionType"></param>
 /// <returns></returns>
 internal static List<PowerShellInstalledPackage> GetPowerShellPackageView(Dictionary<NuGetProject, IEnumerable<PackageReference>> dictionary)
 {
     List<PowerShellInstalledPackage> views = new List<PowerShellInstalledPackage>();
     foreach (KeyValuePair<NuGetProject, IEnumerable<PackageReference>> entry in dictionary)
     {
         // entry.Value is an empty list if no packages are installed
         foreach (PackageReference package in entry.Value)
         {
             PowerShellInstalledPackage view = new PowerShellInstalledPackage();
             view.Id = package.PackageIdentity.Id;
             view.Versions = new List<NuGetVersion>() { package.PackageIdentity.Version };
             LegacyNuGet.SemanticVersion sVersion;
             LegacyNuGet.SemanticVersion.TryParse(package.PackageIdentity.Version.ToNormalizedString(), out sVersion);
             view.Version = sVersion;
             view.ProjectName = entry.Key.GetMetadata<string>(NuGetProjectMetadataKeys.Name);
             views.Add(view);
         }
     }
     return views;
 }
Example #4
0
        /// <summary>
        /// Get the view of installed packages. Use for Get-Package command.
        /// </summary>
        /// <param name="metadata"></param>
        /// <param name="versionType"></param>
        /// <returns></returns>
        internal static List <PowerShellInstalledPackage> GetPowerShellPackageView(Dictionary <NuGetProject, IEnumerable <PackageReference> > dictionary)
        {
            List <PowerShellInstalledPackage> views = new List <PowerShellInstalledPackage>();

            foreach (KeyValuePair <NuGetProject, IEnumerable <PackageReference> > entry in dictionary)
            {
                // entry.Value is an empty list if no packages are installed
                foreach (PackageReference package in entry.Value)
                {
                    PowerShellInstalledPackage view = new PowerShellInstalledPackage();
                    view.Id       = package.PackageIdentity.Id;
                    view.Versions = new List <NuGetVersion>()
                    {
                        package.PackageIdentity.Version
                    };
                    LegacyNuGet.SemanticVersion sVersion;
                    LegacyNuGet.SemanticVersion.TryParse(package.PackageIdentity.Version.ToNormalizedString(), out sVersion);
                    view.Version     = sVersion;
                    view.ProjectName = entry.Key.GetMetadata <string>(NuGetProjectMetadataKeys.Name);
                    views.Add(view);
                }
            }
            return(views);
        }
        /// <summary>
        /// Get the view of installed packages. Use for Get-Package command.
        /// </summary>
        internal static List <PowerShellInstalledPackage> GetPowerShellPackageView(
            Dictionary <NuGetProject, IEnumerable <Packaging.PackageReference> > dictionary,
            ISolutionManager solutionManager,
            Configuration.ISettings settings)
        {
            var views = new List <PowerShellInstalledPackage> ();

            foreach (var entry in dictionary)
            {
                var nugetProject = entry.Key;

                string             packageFolder        = null;
                FolderNuGetProject packageFolderProject = null;

                if (nugetProject is BuildIntegratedNuGetProject)
                {
                    packageFolder = SettingsUtility.GetGlobalPackagesFolder(settings);
                }
                else
                {
                    var project = nugetProject as MSBuildNuGetProject;

                    if (project != null)
                    {
                        packageFolderProject = project.FolderNuGetProject;
                    }
                }

                // entry.Value is an empty list if no packages are installed
                foreach (var package in entry.Value)
                {
                    string installPackagePath = null;
                    string licenseUrl         = null;

                    if (packageFolder != null)
                    {
                        var defaultPackagePathResolver = new VersionFolderPathResolver(packageFolder);
                        installPackagePath = defaultPackagePathResolver.GetPackageFilePath(package.PackageIdentity.Id, package.PackageIdentity.Version);
                    }
                    else if (packageFolderProject != null)
                    {
                        installPackagePath = packageFolderProject.GetInstalledPackageFilePath(package.PackageIdentity);
                    }

                    using (var reader = GetPackageReader(installPackagePath)) {
                        var nuspecReader = new NuspecReader(reader.GetNuspec());
                        licenseUrl = nuspecReader.GetLicenseUrl();
                    }

                    var view = new PowerShellInstalledPackage()
                    {
                        Id = package.PackageIdentity.Id,
                        AsyncLazyVersions = new AsyncLazy <IEnumerable <NuGetVersion> > (() => {
                            return(Task.FromResult <IEnumerable <NuGetVersion> > (new [] { package.PackageIdentity.Version }));
                        }),
                        ProjectName = entry.Key.GetMetadata <string> (NuGetProjectMetadataKeys.Name),
                        LicenseUrl  = licenseUrl
                    };

                    views.Add(view);
                }
            }

            return(views);
        }
        /// <summary>
        /// Get the view of installed packages. Use for Get-Package command.
        /// </summary>
        internal static List <PowerShellInstalledPackage> GetPowerShellPackageView(
            Dictionary <NuGetProject, IEnumerable <PackageReference> > dictionary,
            ISolutionManager SolutionManager,
            Configuration.ISettings settings)
        {
            var views = new List <PowerShellInstalledPackage>();

            foreach (var entry in dictionary)
            {
                var nugetProject = entry.Key;
                var projectName  = entry.Key.GetMetadata <string>(NuGetProjectMetadataKeys.Name);

                FolderNuGetProject          packageFolderProject = null;
                FallbackPackagePathResolver fallbackResolver     = null;

                // Build a project-specific strategy for resolving a package .nupkg path.
                if (nugetProject is INuGetIntegratedProject) // This is technically incorrect for DNX projects,
                                                             // however since that experience is deprecated we don't
                                                             // care.
                {
                    var pathContext = NuGetPathContext.Create(settings);
                    fallbackResolver = new FallbackPackagePathResolver(pathContext);
                }
                else
                {
                    var project = nugetProject as MSBuildNuGetProject;

                    if (project != null)
                    {
                        packageFolderProject = project.FolderNuGetProject;
                    }
                }

                // entry.Value is an empty list if no packages are installed.
                foreach (var package in entry.Value)
                {
                    string installPackagePath = null;
                    string licenseUrl         = null;

                    // Try to get the path to the package .nupkg on disk.
                    if (fallbackResolver != null)
                    {
                        var packageInfo = fallbackResolver.GetPackageInfo(
                            package.PackageIdentity.Id,
                            package.PackageIdentity.Version);

                        installPackagePath = packageInfo?.PathResolver.GetPackageFilePath(
                            package.PackageIdentity.Id,
                            package.PackageIdentity.Version);
                    }
                    else if (packageFolderProject != null)
                    {
                        installPackagePath = packageFolderProject.GetInstalledPackageFilePath(package.PackageIdentity);
                    }

                    // Try to read out the license URL.
                    using (var reader = GetPackageReader(installPackagePath))
                        using (var nuspecStream = reader?.GetNuspec())
                        {
                            if (nuspecStream != null)
                            {
                                var nuspecReader = new NuspecReader(nuspecStream);
                                licenseUrl = nuspecReader.GetLicenseUrl();
                            }
                        }

                    var view = new PowerShellInstalledPackage
                    {
                        Id = package.PackageIdentity.Id,
                        AsyncLazyVersions = new AsyncLazy <IEnumerable <NuGetVersion> >(
                            () => Task.FromResult <IEnumerable <NuGetVersion> >(new[] { package.PackageIdentity.Version }),
                            NuGetUIThreadHelper.JoinableTaskFactory),
                        ProjectName = projectName,
                        LicenseUrl  = licenseUrl
                    };

                    views.Add(view);
                }
            }

            return(views);
        }