Example #1
0
        // Check if any of the pkg versions are already installed, if they are we'll remove them from the list of packages to install
        private List <PSResourceInfo> FilterByInstalledPkgs(List <PSResourceInfo> packages)
        {
            // Package install paths.
            // _pathsToInstallPkg will only contain the paths specified within the -Scope param (if applicable).
            // _pathsToSearch will contain all resource package subdirectories within _pathsToInstallPkg path locations.
            // e.g.:
            // ./InstallPackagePath1/PackageA
            // ./InstallPackagePath1/PackageB
            // ./InstallPackagePath2/PackageC
            // ./InstallPackagePath3/PackageD

            // Get currently installed packages.
            var getHelper             = new GetHelper(_cmdletPassedIn);
            var installedPackageNames = new HashSet <string>(StringComparer.CurrentCultureIgnoreCase);

            foreach (var installedPkg in getHelper.GetInstalledPackages(
                         pkgs: packages,
                         pathsToSearch: _pathsToSearch))
            {
                installedPackageNames.Add(installedPkg.Name);
            }

            if (installedPackageNames.Count is 0)
            {
                return(packages);
            }

            // Return only packages that are not already installed.
            var filteredPackages = new List <PSResourceInfo>();

            foreach (var pkg in packages)
            {
                if (!installedPackageNames.Contains(pkg.Name))
                {
                    // Add packages that still need to be installed.
                    filteredPackages.Add(pkg);
                }
                else
                {
                    // Remove from tracking list of packages to install.
                    _cmdletPassedIn.WriteWarning(
                        string.Format("Resource '{0}' with version '{1}' is already installed.  If you would like to reinstall, please run the cmdlet again with the -Reinstall parameter",
                                      pkg.Name,
                                      pkg.Version));

                    _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase));
                }
            }

            return(filteredPackages);
        }