public async Task LocalPackageSearchResource_FileSource()
        {
            using (var root = TestDirectory.Create())
            {
                // Arrange
                var testLogger = new TestLogger();

                var nuspec = XDocument.Parse($@"<?xml version=""1.0"" encoding=""utf-8""?>
                        <package>
                        <metadata>
                            <id>myPackage</id>
                            <version>1.0.0-alpha.1.2+5</version>
                            <description>package description</description>
                            <tags>a b c</tags>
                        </metadata>
                        </package>");

                var packageA = new SimpleTestPackageContext()
                {
                    Id      = "myPackage",
                    Version = "1.0.0-alpha.1.2+5",
                    Nuspec  = nuspec
                };

                var nuspec2 = XDocument.Parse($@"<?xml version=""1.0"" encoding=""utf-8""?>
                        <package>
                        <metadata>
                            <id>myOtherPackage</id>
                            <version>1.0.0-alpha.1.3+5</version>
                            <description>package description</description>
                            <tags>a b c</tags>
                        </metadata>
                        </package>");

                var packageA2 = new SimpleTestPackageContext()
                {
                    Id      = "myOtherPackage",
                    Version = "1.0.0-alpha.1.3+5",
                    Nuspec  = nuspec2
                };

                var packageContexts = new SimpleTestPackageContext[]
                {
                    packageA,
                    packageA2
                };

                var fileUrl = "file://" + root.Path.Replace(@"\", @"/");

                SimpleTestPackageUtility.CreatePackages(root, packageContexts);

                var localResource = new FindLocalPackagesResourceV2(fileUrl);
                var resource      = new LocalPackageSearchResource(localResource);

                var filter = new SearchFilter(includePrerelease: true);

                // Act
                var packages = (await resource.SearchAsync(
                                    "mypackage",
                                    filter,
                                    skip: 0,
                                    take: 30,
                                    log: testLogger,
                                    token: CancellationToken.None))
                               .OrderBy(p => p.Identity.Id)
                               .ToList();

                var package = packages.First();

                // Assert
                Assert.Equal(1, packages.Count);
                Assert.Equal("myPackage", package.Identity.Id);
                Assert.Equal("1.0.0-alpha.1.2+5", package.Identity.Version.ToFullString());
            }
        }
Beispiel #2
0
        private List <PSResourceInfo> InstallPackage(
            IEnumerable <PSResourceInfo> pkgsToInstall, // those found to be required to be installed (includes Dependency packages as well)
            string repoUrl,
            PSCredential credential,
            bool isLocalRepo)
        {
            List <PSResourceInfo> pkgsSuccessfullyInstalled = new List <PSResourceInfo>();
            int totalPkgs = pkgsToInstall.Count();

            // Counters for tracking current package out of total
            int totalInstalledPkgCount = 0;

            foreach (PSResourceInfo pkg in pkgsToInstall)
            {
                totalInstalledPkgCount++;
                var tempInstallPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                try
                {
                    // Create a temp directory to install to
                    var dir = Directory.CreateDirectory(tempInstallPath);  // should check it gets created properly
                                                                           // To delete file attributes from the existing ones get the current file attributes first and use AND (&) operator
                                                                           // with a mask (bitwise complement of desired attributes combination).
                                                                           // TODO: check the attributes and if it's read only then set it
                                                                           // attribute may be inherited from the parent
                                                                           // TODO:  are there Linux accommodations we need to consider here?
                    dir.Attributes &= ~FileAttributes.ReadOnly;

                    _cmdletPassedIn.WriteVerbose(string.Format("Begin installing package: '{0}'", pkg.Name));

                    if (!_quiet)
                    {
                        int    activityId        = 0;
                        int    percentComplete   = ((totalInstalledPkgCount * 100) / totalPkgs);
                        string activity          = string.Format("Installing {0}...", pkg.Name);
                        string statusDescription = string.Format("{0}% Complete", percentComplete);
                        _cmdletPassedIn.WriteProgress(
                            new ProgressRecord(activityId, activity, statusDescription));
                    }

                    // Create PackageIdentity in order to download
                    string createFullVersion = pkg.Version.ToString();
                    if (pkg.IsPrerelease)
                    {
                        createFullVersion = pkg.Version.ToString() + "-" + pkg.PrereleaseLabel;
                    }

                    if (!NuGetVersion.TryParse(createFullVersion, out NuGetVersion pkgVersion))
                    {
                        var message = String.Format("{0} package could not be installed with error: could not parse package '{0}' version '{1} into a NuGetVersion",
                                                    pkg.Name,
                                                    pkg.Version.ToString());
                        var ex = new ArgumentException(message);
                        var packageIdentityVersionParseError = new ErrorRecord(ex, "psdataFileNotExistError", ErrorCategory.ReadError, null);
                        _cmdletPassedIn.WriteError(packageIdentityVersionParseError);
                        _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase));
                        continue;
                    }

                    var pkgIdentity  = new PackageIdentity(pkg.Name, pkgVersion);
                    var cacheContext = new SourceCacheContext();

                    if (isLocalRepo)
                    {
                        /* Download from a local repository -- this is slightly different process than from a server */
                        var localResource = new FindLocalPackagesResourceV2(repoUrl);
                        var resource      = new LocalDownloadResource(repoUrl, localResource);

                        // Actually downloading the .nupkg from a local repo
                        var result = resource.GetDownloadResourceResultAsync(
                            identity: pkgIdentity,
                            downloadContext: new PackageDownloadContext(cacheContext),
                            globalPackagesFolder: tempInstallPath,
                            logger: NullLogger.Instance,
                            token: _cancellationToken).GetAwaiter().GetResult();

                        // Create the package extraction context
                        PackageExtractionContext packageExtractionContext = new PackageExtractionContext(
                            packageSaveMode: PackageSaveMode.Nupkg,
                            xmlDocFileSaveMode: PackageExtractionBehavior.XmlDocFileSaveMode,
                            clientPolicyContext: null,
                            logger: NullLogger.Instance);

                        // Extracting from .nupkg and placing files into tempInstallPath
                        result.PackageReader.CopyFiles(
                            destination: tempInstallPath,
                            packageFiles: result.PackageReader.GetFiles(),
                            extractFile: new PackageFileExtractor(
                                result.PackageReader.GetFiles(),
                                packageExtractionContext.XmlDocFileSaveMode).ExtractPackageFile,
                            logger: NullLogger.Instance,
                            token: _cancellationToken);
                        result.Dispose();
                    }
                    else
                    {
                        /* Download from a non-local repository */
                        // Set up NuGet API resource for download
                        PackageSource source = new PackageSource(repoUrl);
                        if (credential != null)
                        {
                            string password = new NetworkCredential(string.Empty, credential.Password).Password;
                            source.Credentials = PackageSourceCredential.FromUserInput(repoUrl, credential.UserName, password, true, null);
                        }
                        var provider = FactoryExtensionsV3.GetCoreV3(NuGet.Protocol.Core.Types.Repository.Provider);
                        SourceRepository repository = new SourceRepository(source, provider);

                        /* Download from a non-local repository -- ie server */
                        var downloadResource          = repository.GetResourceAsync <DownloadResource>().GetAwaiter().GetResult();
                        DownloadResourceResult result = null;
                        try
                        {
                            result = downloadResource.GetDownloadResourceResultAsync(
                                identity: pkgIdentity,
                                downloadContext: new PackageDownloadContext(cacheContext),
                                globalPackagesFolder: tempInstallPath,
                                logger: NullLogger.Instance,
                                token: _cancellationToken).GetAwaiter().GetResult();
                        }
                        catch (Exception e)
                        {
                            _cmdletPassedIn.WriteVerbose(string.Format("Error attempting download: '{0}'", e.Message));
                        }
                        finally
                        {
                            // Need to close the .nupkg
                            if (result != null)
                            {
                                result.Dispose();
                            }
                        }
                    }

                    _cmdletPassedIn.WriteVerbose(string.Format("Successfully able to download package from source to: '{0}'", tempInstallPath));

                    // pkgIdentity.Version.Version gets the version without metadata or release labels.
                    string newVersion = pkgIdentity.Version.ToNormalizedString();
                    string normalizedVersionNoPrereleaseLabel = newVersion;
                    if (pkgIdentity.Version.IsPrerelease)
                    {
                        // eg: 2.0.2
                        normalizedVersionNoPrereleaseLabel = pkgIdentity.Version.ToNormalizedString().Substring(0, pkgIdentity.Version.ToNormalizedString().IndexOf('-'));
                    }

                    string tempDirNameVersion        = isLocalRepo ? tempInstallPath : Path.Combine(tempInstallPath, pkgIdentity.Id.ToLower(), newVersion);
                    var    version4digitNoPrerelease = pkgIdentity.Version.Version.ToString();
                    string moduleManifestVersion     = string.Empty;
                    var    scriptPath = Path.Combine(tempDirNameVersion, pkg.Name + ".ps1");
                    var    modulePath = Path.Combine(tempDirNameVersion, pkg.Name + ".psd1");
                    // Check if the package is a module or a script
                    var isModule = File.Exists(modulePath);

                    string installPath;
                    if (_savePkg)
                    {
                        // For save the installation path is what is passed in via -Path
                        installPath = _pathsToInstallPkg.FirstOrDefault();

                        // If saving as nupkg simply copy the nupkg and move onto next iteration of loop
                        // asNupkg functionality only applies to Save-PSResource
                        if (_asNupkg)
                        {
                            var nupkgFile = pkgIdentity.ToString().ToLower() + ".nupkg";
                            File.Copy(Path.Combine(tempDirNameVersion, nupkgFile), Path.Combine(installPath, nupkgFile));

                            _cmdletPassedIn.WriteVerbose(string.Format("'{0}' moved into file path '{1}'", nupkgFile, installPath));
                            pkgsSuccessfullyInstalled.Add(pkg);

                            continue;
                        }
                    }
                    else
                    {
                        // PSModules:
                        /// ./Modules
                        /// ./Scripts
                        /// _pathsToInstallPkg is sorted by desirability, Find will pick the pick the first Script or Modules path found in the list
                        installPath = isModule ? _pathsToInstallPkg.Find(path => path.EndsWith("Modules", StringComparison.InvariantCultureIgnoreCase))
                                : _pathsToInstallPkg.Find(path => path.EndsWith("Scripts", StringComparison.InvariantCultureIgnoreCase));
                    }

                    if (isModule)
                    {
                        var moduleManifest = Path.Combine(tempDirNameVersion, pkgIdentity.Id + ".psd1");
                        if (!File.Exists(moduleManifest))
                        {
                            var message = String.Format("{0} package could not be installed with error: Module manifest file: {1} does not exist. This is not a valid PowerShell module.", pkgIdentity.Id, moduleManifest);

                            var ex = new ArgumentException(message);
                            var psdataFileDoesNotExistError = new ErrorRecord(ex, "psdataFileNotExistError", ErrorCategory.ReadError, null);
                            _cmdletPassedIn.WriteError(psdataFileDoesNotExistError);
                            _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase));
                            continue;
                        }

                        if (!Utils.TryParseModuleManifest(moduleManifest, _cmdletPassedIn, out Hashtable parsedMetadataHashtable))
                        {
                            // Ran into errors parsing the module manifest file which was found in Utils.ParseModuleManifest() and written.
                            continue;
                        }

                        moduleManifestVersion = parsedMetadataHashtable["ModuleVersion"] as string;

                        // Accept License verification
                        if (!_savePkg && !CallAcceptLicense(pkg, moduleManifest, tempInstallPath, newVersion))
                        {
                            continue;
                        }

                        // If NoClobber is specified, ensure command clobbering does not happen
                        if (_noClobber && !DetectClobber(pkg.Name, parsedMetadataHashtable))
                        {
                            continue;
                        }
                    }

                    // Delete the extra nupkg related files that are not needed and not part of the module/script
                    DeleteExtraneousFiles(pkgIdentity, tempDirNameVersion);

                    if (_includeXML)
                    {
                        CreateMetadataXMLFile(tempDirNameVersion, installPath, pkg, isModule);
                    }

                    MoveFilesIntoInstallPath(
                        pkg,
                        isModule,
                        isLocalRepo,
                        tempDirNameVersion,
                        tempInstallPath,
                        installPath,
                        newVersion,
                        moduleManifestVersion,
                        scriptPath);

                    _cmdletPassedIn.WriteVerbose(String.Format("Successfully installed package '{0}' to location '{1}'", pkg.Name, installPath));
                    pkgsSuccessfullyInstalled.Add(pkg);
                }
                catch (Exception e)
                {
                    _cmdletPassedIn.WriteError(
                        new ErrorRecord(
                            new PSInvalidOperationException(
                                message: $"Unable to successfully install package '{pkg.Name}': '{e.Message}'",
                                innerException: e),
                            "InstallPackageFailed",
                            ErrorCategory.InvalidOperation,
                            _cmdletPassedIn));
                    _pkgNamesToInstall.RemoveAll(x => x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase));
                }
                finally
                {
                    // Delete the temp directory and all its contents
                    _cmdletPassedIn.WriteVerbose(string.Format("Attempting to delete '{0}'", tempInstallPath));

                    if (Directory.Exists(tempInstallPath))
                    {
                        if (!TryDeleteDirectory(tempInstallPath, out ErrorRecord errorMsg))
                        {
                            _cmdletPassedIn.WriteError(errorMsg);
                        }
                        else
                        {
                            _cmdletPassedIn.WriteVerbose(String.Format("Successfully deleted '{0}'", tempInstallPath));
                        }
                    }
                }
            }

            return(pkgsSuccessfullyInstalled);
        }
Beispiel #3
0
        public async Task LocalDependencyInfoResource_BasicAsync()
        {
            using (var root = TestDirectory.Create())
            {
                // Arrange
                var testLogger = new TestLogger();

                var packageX = new SimpleTestPackageContext()
                {
                    Id      = "x",
                    Version = "1.0.0"
                };

                var packageD = new SimpleTestPackageContext()
                {
                    Id      = "d",
                    Version = "1.0.0"
                };

                var packageD2 = new SimpleTestPackageContext()
                {
                    Id      = "d",
                    Version = "2.0.0"
                };

                var packageD3 = new SimpleTestPackageContext()
                {
                    Id      = "d",
                    Version = "0.1.0"
                };

                var packageC = new SimpleTestPackageContext()
                {
                    Id           = "c",
                    Version      = "1.0.0",
                    Dependencies = new List <SimpleTestPackageContext>()
                    {
                        packageD
                    }
                };

                var packageB = new SimpleTestPackageContext()
                {
                    Id           = "b",
                    Version      = "1.0.0",
                    Dependencies = new List <SimpleTestPackageContext>()
                    {
                        packageD2
                    }
                };

                var packageA = new SimpleTestPackageContext()
                {
                    Id           = "a",
                    Version      = "1.0.0",
                    Dependencies = new List <SimpleTestPackageContext>()
                    {
                        packageC, packageB
                    }
                };

                var packageContexts = new SimpleTestPackageContext[]
                {
                    packageA,
                    packageB,
                    packageC,
                    packageD,
                    packageD2,
                    packageD3,
                    packageX
                };

                await SimpleTestPackageUtility.CreatePackagesAsync(root, packageContexts);

                var source        = Repository.Factory.GetCoreV3(root);
                var localResource = new FindLocalPackagesResourceV2(root);
                var resource      = new LocalDependencyInfoResource(localResource, source);

                // Act
                var resultsA = (await resource.ResolvePackages("a", NuGetFramework.Parse("net46"), NullSourceCacheContext.Instance, testLogger, CancellationToken.None)).ToList();
                var resultsX = (await resource.ResolvePackages("x", NuGetFramework.Parse("net46"), NullSourceCacheContext.Instance, testLogger, CancellationToken.None)).ToList();
                var resultsY = (await resource.ResolvePackages("y", NuGetFramework.Parse("net46"), NullSourceCacheContext.Instance, testLogger, CancellationToken.None)).ToList();

                // Assert
                Assert.Equal(1, resultsA.Count);
                Assert.Equal(packageA.Identity, resultsA[0], PackageIdentity.Comparer);
                Assert.True(resultsA[0].Listed);
                Assert.Contains("a.1.0.0.nupkg", resultsA[0].DownloadUri.LocalPath);
                Assert.Equal(2, resultsA[0].Dependencies.Count());
                Assert.Equal("c", resultsA[0].Dependencies.First().Id);
                Assert.Equal("[1.0.0, )", resultsA[0].Dependencies.First().VersionRange.ToNormalizedString());
                Assert.Equal("b", resultsA[0].Dependencies.Skip(1).First().Id);
                Assert.Equal("[1.0.0, )", resultsA[0].Dependencies.Skip(1).First().VersionRange.ToNormalizedString());

                // no dependencies
                Assert.Equal(1, resultsX.Count);
                Assert.Equal(packageX.Identity, resultsX[0], PackageIdentity.Comparer);
                Assert.True(resultsX[0].Listed);
                Assert.Contains("x.1.0.0.nupkg", resultsX[0].DownloadUri.LocalPath);
                Assert.Equal(0, resultsX[0].Dependencies.Count());

                // not found
                Assert.Equal(0, resultsY.Count);
            }
        }
Beispiel #4
0
        public async Task LocalDependencyInfoResource_ResolvePackagesNearestDependencyGroupAsync()
        {
            using (var root = TestDirectory.Create())
            {
                // Arrange
                var testLogger = new TestLogger();

                var packageA = new SimpleTestPackageContext()
                {
                    Id      = "a",
                    Version = "1.0",
                    Nuspec  = XDocument.Parse($@"<?xml version=""1.0"" encoding=""utf-8""?>
                        <package>
                        <metadata>
                            <id>a</id>
                            <version>1.0</version>
                            <title />
                            <dependencies>
                                <group>
                                    <dependency id=""b"" version=""1.0"" />
                                </group>
                                <group targetFramework=""net46"">
                                    <dependency id=""x"" />
                                </group>
                                <group targetFramework=""net461"">
                                </group>
                            </dependencies>
                        </metadata>
                        </package>")
                };

                var packageContexts = new SimpleTestPackageContext[]
                {
                    packageA
                };

                await SimpleTestPackageUtility.CreatePackagesAsync(root, packageContexts);

                var source        = Repository.Factory.GetCoreV3(root);
                var localResource = new FindLocalPackagesResourceV2(root);
                var resource      = new LocalDependencyInfoResource(localResource, source);

                // Act
                var resultNet462 = (await resource.ResolvePackages(
                                        "a",
                                        NuGetFramework.Parse("net462"),
                                        NullSourceCacheContext.Instance,
                                        testLogger,
                                        CancellationToken.None)).Single();

                var resultNet46 = (await resource.ResolvePackages(
                                       "a",
                                       NuGetFramework.Parse("net46"),
                                       NullSourceCacheContext.Instance,
                                       testLogger,
                                       CancellationToken.None)).Single();

                var resultWin8 = (await resource.ResolvePackages(
                                      "a",
                                      NuGetFramework.Parse("win8"),
                                      NullSourceCacheContext.Instance,
                                      testLogger,
                                      CancellationToken.None)).Single();

                // Assert
                Assert.Equal(0, resultNet462.Dependencies.Count());
                Assert.Equal(1, resultNet46.Dependencies.Count());
                Assert.Equal(1, resultWin8.Dependencies.Count());

                Assert.Equal("x", resultNet46.Dependencies.Single().Id);
                Assert.Equal(VersionRange.All, resultNet46.Dependencies.Single().VersionRange);
                Assert.Equal("b", resultWin8.Dependencies.Single().Id);
                Assert.Equal(VersionRange.Parse("1.0"), resultWin8.Dependencies.Single().VersionRange);
            }
        }
Beispiel #5
0
        public async Task LocalPackageMetadataResourceTests_GetMetadataPrereleaseAsync()
        {
            using (var root = TestDirectory.Create())
            {
                // Arrange
                var testLogger = new TestLogger();

                var packageX = new SimpleTestPackageContext()
                {
                    Id      = "x",
                    Version = "1.0.0"
                };

                var packageA1 = new SimpleTestPackageContext()
                {
                    Id           = "a",
                    Version      = "1.0.0",
                    Dependencies = new List <SimpleTestPackageContext>()
                    {
                        packageX
                    }
                };

                var packageA2 = new SimpleTestPackageContext()
                {
                    Id           = "a",
                    Version      = "2.0.0-preview.12.4+server.a",
                    Dependencies = new List <SimpleTestPackageContext>()
                    {
                        packageX
                    }
                };

                var packageContexts = new SimpleTestPackageContext[]
                {
                    packageA1,
                    packageA2,
                    packageX
                };

                await SimpleTestPackageUtility.CreatePackagesAsync(root, packageContexts);

                var localResource = new FindLocalPackagesResourceV2(root);
                var resource      = new LocalPackageMetadataResource(localResource);

                // Act
                var results = (await resource.GetMetadataAsync(
                                   "A",
                                   includePrerelease: true,
                                   includeUnlisted: false,
                                   sourceCacheContext: NullSourceCacheContext.Instance,
                                   log: testLogger,
                                   token: CancellationToken.None))
                              .ToList();

                var package = results.OrderByDescending(p => p.Identity.Version).First();

                // Assert
                Assert.Equal("a", package.Identity.Id);
                Assert.Equal("2.0.0-preview.12.4+server.a", package.Identity.Version.ToFullString());
            }
        }
Beispiel #6
0
        public async Task LocalDownloadResource_PackageIsReturnedNonNormalizedAsync()
        {
            using (var root = TestDirectory.Create())
            {
                // Arrange
                var testLogger = new TestLogger();

                var packageA1 = new SimpleTestPackageContext()
                {
                    Id      = "a",
                    Version = "1.0"
                };

                var packageA2 = new SimpleTestPackageContext()
                {
                    Id      = "a",
                    Version = "1.0.0"
                };

                var packageA3 = new SimpleTestPackageContext()
                {
                    Id      = "a",
                    Version = "1.0.0.0"
                };

                var packageContexts = new SimpleTestPackageContext[]
                {
                    packageA1,
                    packageA2,
                    packageA3
                };

                await SimpleTestPackageUtility.CreatePackagesAsync(root, packageContexts);

                string packagesFolder = null; // This is unused by the implementation.

                var localResource = new FindLocalPackagesResourceV2(root);
                var resource      = new LocalDownloadResource(localResource);

                // Act
                using (var cacheContext = new SourceCacheContext())
                {
                    var downloadContext = new PackageDownloadContext(cacheContext);

                    var result1 = await resource.GetDownloadResourceResultAsync(
                        packageA1.Identity,
                        downloadContext,
                        packagesFolder,
                        testLogger,
                        CancellationToken.None);

                    var result2 = await resource.GetDownloadResourceResultAsync(
                        packageA2.Identity,
                        downloadContext,
                        packagesFolder,
                        testLogger,
                        CancellationToken.None);

                    var result3 = await resource.GetDownloadResourceResultAsync(
                        packageA3.Identity,
                        downloadContext,
                        packagesFolder,
                        testLogger,
                        CancellationToken.None);

                    using (var reader1 = result1.PackageReader)
                        using (var reader2 = result2.PackageReader)
                            using (var reader3 = result3.PackageReader)
                            {
                                // Assert
                                Assert.Equal("1.0", reader1.GetIdentity().Version.ToString());
                                Assert.Equal("1.0.0", reader2.GetIdentity().Version.ToString());
                                Assert.Equal("1.0.0.0", reader3.GetIdentity().Version.ToString());
                            }
                }
            }
        }
        private List <string> InstallPackage(IEnumerable <PSResourceInfo> pkgsToInstall, string repoName, string repoUrl, PSCredential credential, bool isLocalRepo)
        {
            List <string> pkgsSuccessfullyInstalled = new List <string>();

            foreach (PSResourceInfo p in pkgsToInstall)
            {
                var tempInstallPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                try
                {
                    // Create a temp directory to install to
                    var dir = Directory.CreateDirectory(tempInstallPath);  // should check it gets created properly
                                                                           // To delete file attributes from the existing ones get the current file attributes first and use AND (&) operator
                                                                           // with a mask (bitwise complement of desired attributes combination).
                                                                           // TODO: check the attributes and if it's read only then set it
                                                                           // attribute may be inherited from the parent
                                                                           // TODO:  are there Linux accommodations we need to consider here?
                    dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;

                    _cmdletPassedIn.WriteVerbose(string.Format("Begin installing package: '{0}'", p.Name));

                    // TODO: add progress bar here

                    // Create PackageIdentity in order to download
                    string createFullVersion = p.Version.ToString();
                    if (p.IsPrerelease)
                    {
                        createFullVersion = p.Version.ToString() + "-" + p.PrereleaseLabel;
                    }

                    if (!NuGetVersion.TryParse(createFullVersion, out NuGetVersion pkgVersion))
                    {
                        _cmdletPassedIn.WriteDebug(string.Format("Error parsing package '{0}' version '{1}' into a NuGetVersion", p.Name, p.Version.ToString()));
                        continue;
                    }
                    var pkgIdentity  = new PackageIdentity(p.Name, pkgVersion);
                    var cacheContext = new SourceCacheContext();

                    if (isLocalRepo)
                    {
                        /* Download from a local repository -- this is slightly different process than from a server */
                        var localResource = new FindLocalPackagesResourceV2(repoUrl);
                        var resource      = new LocalDownloadResource(repoUrl, localResource);

                        // Actually downloading the .nupkg from a local repo
                        var result = resource.GetDownloadResourceResultAsync(
                            identity: pkgIdentity,
                            downloadContext: new PackageDownloadContext(cacheContext),
                            globalPackagesFolder: tempInstallPath,
                            logger: NullLogger.Instance,
                            token: _cancellationToken).GetAwaiter().GetResult();

                        if (_asNupkg) // this is Save functionality
                        {
                            DirectoryInfo nupkgPath = new DirectoryInfo(((System.IO.FileStream)result.PackageStream).Name);
                            File.Copy(nupkgPath.FullName, Path.Combine(tempInstallPath, pkgIdentity.Id + pkgIdentity.Version + ".nupkg"));

                            continue;
                        }

                        // Create the package extraction context
                        PackageExtractionContext packageExtractionContext = new PackageExtractionContext(
                            packageSaveMode: PackageSaveMode.Nupkg,
                            xmlDocFileSaveMode: PackageExtractionBehavior.XmlDocFileSaveMode,
                            clientPolicyContext: null,
                            logger: NullLogger.Instance);

                        // Extracting from .nupkg and placing files into tempInstallPath
                        result.PackageReader.CopyFiles(
                            destination: tempInstallPath,
                            packageFiles: result.PackageReader.GetFiles(),
                            extractFile: (new PackageFileExtractor(result.PackageReader.GetFiles(), packageExtractionContext.XmlDocFileSaveMode)).ExtractPackageFile,
                            logger: NullLogger.Instance,
                            token: _cancellationToken);
                        result.Dispose();
                    }
                    else
                    {
                        /* Download from a non-local repository */
                        // Set up NuGet API resource for download
                        PackageSource source = new PackageSource(repoUrl);
                        if (credential != null)
                        {
                            string password = new NetworkCredential(string.Empty, credential.Password).Password;
                            source.Credentials = PackageSourceCredential.FromUserInput(repoUrl, credential.UserName, password, true, null);
                        }
                        var provider = FactoryExtensionsV3.GetCoreV3(NuGet.Protocol.Core.Types.Repository.Provider);
                        SourceRepository repository = new SourceRepository(source, provider);

                        /* Download from a non-local repository -- ie server */
                        var downloadResource          = repository.GetResourceAsync <DownloadResource>().GetAwaiter().GetResult();
                        DownloadResourceResult result = null;
                        try
                        {
                            result = downloadResource.GetDownloadResourceResultAsync(
                                identity: pkgIdentity,
                                downloadContext: new PackageDownloadContext(cacheContext),
                                globalPackagesFolder: tempInstallPath,
                                logger: NullLogger.Instance,
                                token: _cancellationToken).GetAwaiter().GetResult();
                        }
                        catch (Exception e)
                        {
                            _cmdletPassedIn.WriteDebug(string.Format("Error attempting download: '{0}'", e.Message));
                        }
                        finally
                        {
                            // Need to close the .nupkg
                            if (result != null)
                            {
                                result.Dispose();
                            }
                        }
                    }

                    _cmdletPassedIn.WriteDebug(string.Format("Successfully able to download package from source to: '{0}'", tempInstallPath));

                    // Prompt if module requires license acceptance (need to read info license acceptance info from the module manifest)
                    // pkgIdentity.Version.Version gets the version without metadata or release labels.


                    string newVersion = pkgIdentity.Version.ToNormalizedString();

                    //
                    string normalizedVersionNoPrereleaseLabel = newVersion;
                    if (pkgIdentity.Version.IsPrerelease)
                    {
                        // 2.0.2
                        normalizedVersionNoPrereleaseLabel = pkgIdentity.Version.ToNormalizedString().Substring(0, pkgIdentity.Version.ToNormalizedString().IndexOf('-'));
                    }
                    //p.Version = new System.Version(normalizedVersionNoPrereleaseLabel);

                    string tempDirNameVersion        = isLocalRepo ? tempInstallPath : Path.Combine(tempInstallPath, pkgIdentity.Id.ToLower(), newVersion);
                    var    version4digitNoPrerelease = pkgIdentity.Version.Version.ToString();
                    string moduleManifestVersion     = string.Empty;
                    var    scriptPath = Path.Combine(tempDirNameVersion, (p.Name + ".ps1"));
                    var    isScript   = File.Exists(scriptPath) ? true : false;

                    if (!isScript)
                    {
                        var moduleManifest = Path.Combine(tempDirNameVersion, pkgIdentity.Id + ".psd1");

                        var parsedMetadataHashtable = Utils.ParseModuleManifest(moduleManifest, this);
                        moduleManifestVersion = parsedMetadataHashtable["ModuleVersion"] as string;

                        // Accept License verification
                        if (!_savePkg && !CallAcceptLicense(p, moduleManifest, tempInstallPath, newVersion))
                        {
                            continue;
                        }
                    }

                    // Delete the extra nupkg related files that are not needed and not part of the module/script
                    DeleteExtraneousFiles(tempInstallPath, pkgIdentity, tempDirNameVersion);

                    string installPath;
                    if (_savePkg)
                    {
                        // For save the installation path is what is passed in via -Path
                        installPath = _pathsToInstallPkg.FirstOrDefault();
                    }
                    else
                    {
                        // PSModules:
                        /// ./Modules
                        /// ./Scripts
                        /// _pathsToInstallPkg is sorted by desirability, Find will pick the pick the first Script or Modules path found in the list
                        installPath = isScript ? _pathsToInstallPkg.Find(path => path.EndsWith("Scripts", StringComparison.InvariantCultureIgnoreCase))
                                : _pathsToInstallPkg.Find(path => path.EndsWith("Modules", StringComparison.InvariantCultureIgnoreCase));
                    }

                    if (_includeXML)
                    {
                        CreateMetadataXMLFile(tempDirNameVersion, installPath, repoName, p, isScript);
                    }

                    MoveFilesIntoInstallPath(p, isScript, isLocalRepo, tempDirNameVersion, tempInstallPath, installPath, newVersion, moduleManifestVersion, normalizedVersionNoPrereleaseLabel, version4digitNoPrerelease, scriptPath);

                    _cmdletPassedIn.WriteVerbose(String.Format("Successfully installed package '{0}' to location '{1}'", p.Name, installPath));
                    pkgsSuccessfullyInstalled.Add(p.Name);
                }
                catch (Exception e)
                {
                    _cmdletPassedIn.WriteDebug(string.Format("Unable to successfully install package '{0}': '{1}'", p.Name, e.Message));
                }
                finally
                {
                    // Delete the temp directory and all its contents
                    _cmdletPassedIn.WriteDebug(string.Format("Attempting to delete '{0}'", tempInstallPath));
                    if (Directory.Exists(tempInstallPath))
                    {
                        Directory.Delete(tempInstallPath, true);
                    }
                }
            }

            return(pkgsSuccessfullyInstalled);
        }
Beispiel #8
0
        public async Task LocalPackageMetadataResourceTests_VerifyAllFieldsAsync()
        {
            using (var root = TestDirectory.Create())
            {
                // Arrange
                var testLogger = new TestLogger();

                var nuspec = XDocument.Parse($@"<?xml version=""1.0"" encoding=""utf-8""?>
                        <package>
                        <metadata>
                            <id>a</id>
                            <version>1.0.0-alpha.1.2+5</version>
                            <title>myTitle</title>
                            <authors>a,b,c</authors>
                            <owners>a,b</owners>
                            <description>package description</description>
                            <releaseNotes>notes</releaseNotes>
                            <summary>sum</summary>
                            <language>en-us</language>
                            <projectUrl>http://nuget.org/</projectUrl>
                            <iconUrl>http://nuget.org/nuget.jpg</iconUrl>
                            <licenseUrl>http://nuget.org/license.txt</licenseUrl>
                            <requireLicenseAcceptance>true</requireLicenseAcceptance>
                            <copyright>MIT</copyright>
                            <tags>a b c</tags>
                            <developmentDependency>true</developmentDependency>
                            <dependencies>
                                <group>
                                 <dependency id=""b"" version=""1.0"" />
                                </group>
                                 <group targetFramework=""net461"">
                                </group>
                            </dependencies>
                        </metadata>
                        </package>");

                var packageA = new SimpleTestPackageContext()
                {
                    Id      = "a",
                    Version = "1.0.0-alpha.1.2+5",
                    Nuspec  = nuspec
                };

                var packageA2 = new SimpleTestPackageContext()
                {
                    Id      = "a",
                    Version = "1.0.0-alpha.1.1"
                };

                var packageContexts = new SimpleTestPackageContext[]
                {
                    packageA,
                    packageA2
                };

                await SimpleTestPackageUtility.CreatePackagesAsync(root, packageContexts);

                var localResource = new FindLocalPackagesResourceV2(root);
                var resource      = new LocalPackageMetadataResource(localResource);

                // Act
                var packages = (await resource.GetMetadataAsync(
                                    "A",
                                    includePrerelease: true,
                                    includeUnlisted: false,
                                    sourceCacheContext: NullSourceCacheContext.Instance,
                                    log: testLogger,
                                    token: CancellationToken.None))
                               .OrderByDescending(p => p.Identity.Version)
                               .ToList();

                var package = packages.First();

                // Assert
                Assert.Equal(2, packages.Count);
                Assert.Equal("a,b,c", package.Authors);
                Assert.Equal(2, package.DependencySets.Count());
                Assert.Equal("package description", package.Description);
                Assert.Equal(0, package.DownloadCount);
                Assert.Equal(new Uri("http://nuget.org/nuget.jpg"), package.IconUrl);
                Assert.Equal("1.0.0-alpha.1.2+5", package.Identity.Version.ToFullString());
                Assert.Equal(new Uri("http://nuget.org/license.txt"), package.LicenseUrl);
                Assert.Equal("a,b", package.Owners);
                Assert.Equal(new Uri("http://nuget.org/"), package.ProjectUrl);
                Assert.NotNull(package.Published);
                Assert.Null(package.ReportAbuseUrl);
                Assert.True(package.RequireLicenseAcceptance);
                Assert.Equal("sum", package.Summary);
                Assert.Equal("a b c", package.Tags);
                Assert.Equal("myTitle", package.Title);
            }
        }
Beispiel #9
0
        public IEnumerable <PSResourceInfo> SearchFromRepository(
            string repositoryName,
            Uri repositoryUrl)
        {
            PackageSearchResource   resourceSearch;
            PackageMetadataResource resourceMetadata;
            SearchFilter            filter;
            SourceCacheContext      context;

            // file based Uri scheme
            if (repositoryUrl.Scheme == Uri.UriSchemeFile)
            {
                FindLocalPackagesResourceV2 localResource = new FindLocalPackagesResourceV2(repositoryUrl.ToString());
                resourceSearch   = new LocalPackageSearchResource(localResource);
                resourceMetadata = new LocalPackageMetadataResource(localResource);
                filter           = new SearchFilter(_prerelease);
                context          = new SourceCacheContext();

                foreach (PSResourceInfo pkg in SearchAcrossNamesInRepository(
                             repositoryName: repositoryName,
                             pkgSearchResource: resourceSearch,
                             pkgMetadataResource: resourceMetadata,
                             searchFilter: filter,
                             sourceContext: context))
                {
                    yield return(pkg);
                }
                yield break;
            }

            // check if ADOFeed- for which searching for Name with wildcard has a different logic flow
            if (repositoryUrl.ToString().Contains("pkgs.visualstudio.com"))
            {
                _isADOFeedRepository = true;
            }

            // HTTP, HTTPS, FTP Uri schemes (only other Uri schemes allowed by RepositorySettings.Read() API)
            PackageSource source = new PackageSource(repositoryUrl.ToString());

            if (_credential != null)
            {
                string password = new NetworkCredential(string.Empty, _credential.Password).Password;
                source.Credentials = PackageSourceCredential.FromUserInput(repositoryUrl.ToString(), _credential.UserName, password, true, null);
                _cmdletPassedIn.WriteVerbose("credential successfully set for repository: " + repositoryName);
            }

            // GetCoreV3() API is able to handle V2 and V3 repository endpoints
            var provider = FactoryExtensionsV3.GetCoreV3(NuGet.Protocol.Core.Types.Repository.Provider);
            SourceRepository repository = new SourceRepository(source, provider);

            resourceSearch   = null;
            resourceMetadata = null;

            try
            {
                resourceSearch   = repository.GetResourceAsync <PackageSearchResource>().GetAwaiter().GetResult();
                resourceMetadata = repository.GetResourceAsync <PackageMetadataResource>().GetAwaiter().GetResult();
            }
            catch (Exception e) {
                Utils.WriteVerboseOnCmdlet(_cmdletPassedIn, "Error retrieving resource from repository: " + e.Message);
            }

            if (resourceSearch == null || resourceMetadata == null)
            {
                yield break;
            }

            filter  = new SearchFilter(_prerelease);
            context = new SourceCacheContext();

            foreach (PSResourceInfo pkg in SearchAcrossNamesInRepository(
                         repositoryName: repositoryName,
                         pkgSearchResource: resourceSearch,
                         pkgMetadataResource: resourceMetadata,
                         searchFilter: filter,
                         sourceContext: context))
            {
                yield return(pkg);
            }
        }
Beispiel #10
0
        private IEnumerable <PSResourceInfo> SearchFromRepository(
            string repositoryName,
            Uri repositoryUri,
            PSCredentialInfo repositoryCredentialInfo)
        {
            PackageSearchResource   resourceSearch;
            PackageMetadataResource resourceMetadata;
            SearchFilter            filter;
            SourceCacheContext      context;

            // File based Uri scheme.
            if (repositoryUri.Scheme == Uri.UriSchemeFile)
            {
                FindLocalPackagesResourceV2 localResource = new FindLocalPackagesResourceV2(repositoryUri.ToString());
                resourceSearch   = new LocalPackageSearchResource(localResource);
                resourceMetadata = new LocalPackageMetadataResource(localResource);
                filter           = new SearchFilter(_prerelease);
                context          = new SourceCacheContext();

                foreach (PSResourceInfo pkg in SearchAcrossNamesInRepository(
                             repositoryName: repositoryName,
                             pkgSearchResource: resourceSearch,
                             pkgMetadataResource: resourceMetadata,
                             searchFilter: filter,
                             sourceContext: context))
                {
                    yield return(pkg);
                }
                yield break;
            }

            // Check if ADOFeed- for which searching for Name with wildcard has a different logic flow.
            if (repositoryUri.ToString().Contains("pkgs."))
            {
                _isADOFeedRepository = true;
            }

            // HTTP, HTTPS, FTP Uri schemes (only other Uri schemes allowed by RepositorySettings.Read() API).
            PackageSource source = new PackageSource(repositoryUri.ToString());

            // Explicitly passed in Credential takes precedence over repository CredentialInfo.
            if (_credential != null)
            {
                string password = new NetworkCredential(string.Empty, _credential.Password).Password;
                source.Credentials = PackageSourceCredential.FromUserInput(repositoryUri.ToString(), _credential.UserName, password, true, null);
                _cmdletPassedIn.WriteVerbose("credential successfully set for repository: " + repositoryName);
            }
            else if (repositoryCredentialInfo != null)
            {
                PSCredential repoCredential = Utils.GetRepositoryCredentialFromSecretManagement(
                    repositoryName,
                    repositoryCredentialInfo,
                    _cmdletPassedIn);

                string password = new NetworkCredential(string.Empty, repoCredential.Password).Password;
                source.Credentials = PackageSourceCredential.FromUserInput(repositoryUri.ToString(), repoCredential.UserName, password, true, null);
                _cmdletPassedIn.WriteVerbose("credential successfully read from vault and set for repository: " + repositoryName);
            }

            // GetCoreV3() API is able to handle V2 and V3 repository endpoints.
            var provider = FactoryExtensionsV3.GetCoreV3(NuGet.Protocol.Core.Types.Repository.Provider);
            SourceRepository repository = new SourceRepository(source, provider);

            resourceSearch   = null;
            resourceMetadata = null;

            try
            {
                resourceSearch   = repository.GetResourceAsync <PackageSearchResource>().GetAwaiter().GetResult();
                resourceMetadata = repository.GetResourceAsync <PackageMetadataResource>().GetAwaiter().GetResult();
            }
            catch (Exception e)
            {
                Utils.WriteVerboseOnCmdlet(_cmdletPassedIn, "Error retrieving resource from repository: " + e.Message);
            }

            if (resourceSearch == null || resourceMetadata == null)
            {
                yield break;
            }

            filter  = new SearchFilter(_prerelease);
            context = new SourceCacheContext();

            foreach (PSResourceInfo pkg in SearchAcrossNamesInRepository(
                         repositoryName: String.Equals(repositoryUri.AbsoluteUri, _psGalleryUri, StringComparison.InvariantCultureIgnoreCase) ? _psGalleryRepoName :
                         (String.Equals(repositoryUri.AbsoluteUri, _poshTestGalleryUri, StringComparison.InvariantCultureIgnoreCase) ? _poshTestGalleryRepoName : repositoryName),
                         pkgSearchResource: resourceSearch,
                         pkgMetadataResource: resourceMetadata,
                         searchFilter: filter,
                         sourceContext: context))
            {
                yield return(pkg);
            }
        }
        public async Task LocalPackageMetadataResourceTests_GetMetadataStable()
        {
            using (var root = TestFileSystemUtility.CreateRandomTestFolder())
            {
                // Arrange
                var testLogger = new TestLogger();

                var packageX = new SimpleTestPackageContext()
                {
                    Id      = "x",
                    Version = "1.0.0"
                };

                var packageA1 = new SimpleTestPackageContext()
                {
                    Id           = "a",
                    Version      = "1.0.0",
                    Dependencies = new List <SimpleTestPackageContext>()
                    {
                        packageX
                    }
                };

                var packageA2 = new SimpleTestPackageContext()
                {
                    Id           = "a",
                    Version      = "2.0.0-preview.12.4+server.a",
                    Dependencies = new List <SimpleTestPackageContext>()
                    {
                        packageX
                    }
                };

                var packageContexts = new SimpleTestPackageContext[]
                {
                    packageA1,
                    packageA2,
                    packageX
                };

                SimpleTestPackageUtility.CreatePackages(root, packageContexts);

                var localResource = new FindLocalPackagesResourceV2(root);
                var resource      = new LocalPackageMetadataResource(localResource);

                // Act
                var results = (await resource.GetMetadataAsync(
                                   "A",
                                   includePrerelease: false,
                                   includeUnlisted: false,
                                   log: testLogger,
                                   token: CancellationToken.None))
                              .ToList();

                var package = results.Single();

                // Assert
                Assert.Equal("a", package.Identity.Id);
                Assert.Equal("1.0.0", package.Identity.Version.ToFullString());
            }
        }
        public async Task LocalDownloadResource_PackageIsReturnedNonNormalized()
        {
            using (var root = TestFileSystemUtility.CreateRandomTestFolder())
            {
                // Arrange
                var testLogger = new TestLogger();

                var packageA1 = new SimpleTestPackageContext()
                {
                    Id      = "a",
                    Version = "1.0"
                };

                var packageA2 = new SimpleTestPackageContext()
                {
                    Id      = "a",
                    Version = "1.0.0"
                };

                var packageA3 = new SimpleTestPackageContext()
                {
                    Id      = "a",
                    Version = "1.0.0.0"
                };

                var packageContexts = new SimpleTestPackageContext[]
                {
                    packageA1,
                    packageA2,
                    packageA3
                };

                SimpleTestPackageUtility.CreatePackages(root, packageContexts);

                var localResource = new FindLocalPackagesResourceV2(root);
                var resource      = new LocalDownloadResource(localResource);

                // Act
                var result1 = await resource.GetDownloadResourceResultAsync(
                    packageA1.Identity,
                    NullSettings.Instance,
                    testLogger,
                    CancellationToken.None);

                var result2 = await resource.GetDownloadResourceResultAsync(
                    packageA2.Identity,
                    NullSettings.Instance,
                    testLogger,
                    CancellationToken.None);

                var result3 = await resource.GetDownloadResourceResultAsync(
                    packageA3.Identity,
                    NullSettings.Instance,
                    testLogger,
                    CancellationToken.None);

                using (var reader1 = result1.PackageReader)
                    using (var reader2 = result2.PackageReader)
                        using (var reader3 = result3.PackageReader)
                        {
                            // Assert
                            Assert.Equal("1.0", reader1.GetIdentity().Version.ToString());
                            Assert.Equal("1.0.0", reader2.GetIdentity().Version.ToString());
                            Assert.Equal("1.0.0.0", reader3.GetIdentity().Version.ToString());
                        }
            }
        }
        public async Task <IPackageToPush> FindPackageToPushAsync(string nugetFeedId, IFolder packageFolderWithBinaries, IFolder repositoryFolder, string solutionFileFullName, IErrorsAndInfos errorsAndInfos)
        {
            IPackageToPush packageToPush = new PackageToPush();

            errorsAndInfos.Infos.Add(Properties.Resources.CheckingProjectVsSolution);
            var projectFileFullName = solutionFileFullName.Replace(".sln", ".csproj");

            if (!File.Exists(projectFileFullName))
            {
                errorsAndInfos.Errors.Add(string.Format(Properties.Resources.ProjectFileNotFound, projectFileFullName));
                return(packageToPush);
            }

            errorsAndInfos.Infos.Add(Properties.Resources.LoadingProject);
            var project = ProjectFactory.Load(solutionFileFullName, projectFileFullName, errorsAndInfos);

            if (errorsAndInfos.Errors.Any())
            {
                return(packageToPush);
            }

            errorsAndInfos.Infos.Add(Properties.Resources.LoadingNugetFeeds);
            var developerSettingsSecret = new DeveloperSettingsSecret();
            var developerSettings       = await SecretRepository.GetAsync(developerSettingsSecret, errorsAndInfos);

            if (errorsAndInfos.Errors.Any())
            {
                return(packageToPush);
            }

            if (developerSettings == null)
            {
                errorsAndInfos.Errors.Add(string.Format(Properties.Resources.MissingDeveloperSettings, developerSettingsSecret.Guid + ".xml"));
                return(packageToPush);
            }

            var nugetFeedsSecret = new SecretNugetFeeds();
            var nugetFeeds       = await SecretRepository.GetAsync(nugetFeedsSecret, errorsAndInfos);

            if (errorsAndInfos.AnyErrors())
            {
                return(packageToPush);
            }

            errorsAndInfos.Infos.Add(Properties.Resources.IdentifyingNugetFeed);
            var nugetFeed = nugetFeeds.FirstOrDefault(f => f.Id == nugetFeedId);

            if (nugetFeed == null)
            {
                errorsAndInfos.Errors.Add(string.Format(Properties.Resources.UnknownNugetFeed, nugetFeedId, nugetFeedsSecret.Guid + ".xml"));
                return(packageToPush);
            }

            if (!nugetFeed.IsAFolderToResolve())
            {
                var nugetConfigFileFullName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\NuGet\" + "nuget.config";
                packageToPush.ApiKey = NugetConfigReader.GetApiKey(nugetConfigFileFullName, nugetFeed.Id, errorsAndInfos);
                if (errorsAndInfos.Errors.Any())
                {
                    return(packageToPush);
                }
            }

            errorsAndInfos.Infos.Add(Properties.Resources.IdentifyingFeedUrl);
            var source = await nugetFeed.UrlOrResolvedFolderAsync(FolderResolver, errorsAndInfos);

            if (errorsAndInfos.AnyErrors())
            {
                return(packageToPush);
            }

            packageToPush.FeedUrl = source;
            if (string.IsNullOrEmpty(packageToPush.FeedUrl))
            {
                errorsAndInfos.Errors.Add(string.Format(Properties.Resources.IncompleteDeveloperSettings, developerSettingsSecret.Guid + ".xml"));
                return(packageToPush);
            }

            errorsAndInfos.Infos.Add(Properties.Resources.SearchingLocalPackage);
            var localPackageRepository = new FindLocalPackagesResourceV2(packageFolderWithBinaries.FullName);
            var localPackages          = localPackageRepository.GetPackages(new NullLogger(), CancellationToken.None).Where(p => !p.Identity.Version.IsPrerelease).ToList();

            if (!localPackages.Any())
            {
                errorsAndInfos.Errors.Add(string.Format(Properties.Resources.NoPackageFilesFound, packageFolderWithBinaries.FullName));
                return(packageToPush);
            }

            var latestLocalPackageVersion = localPackages.Max(p => p.Identity.Version.Version);

            errorsAndInfos.Infos.Add(string.Format(Properties.Resources.FoundLocalPackage, latestLocalPackageVersion));

            errorsAndInfos.Infos.Add(Properties.Resources.SearchingRemotePackage);
            var packageId      = string.IsNullOrWhiteSpace(project.PackageId) ? project.RootNamespace : project.PackageId;
            var remotePackages = await NugetFeedLister.ListReleasedPackagesAsync(nugetFeedId, packageId, errorsAndInfos);

            if (errorsAndInfos.Errors.Any())
            {
                return(packageToPush);
            }
            if (!remotePackages.Any())
            {
                errorsAndInfos.Errors.Add(string.Format(Properties.Resources.NoRemotePackageFilesFound, packageToPush.FeedUrl, packageId));
                return(packageToPush);
            }

            errorsAndInfos.Infos.Add(Properties.Resources.LoadingPushedHeadTipShas);
            var pushedHeadTipShas = await PushedHeadTipShaRepository.GetAsync(nugetFeedId, errorsAndInfos);

            if (errorsAndInfos.AnyErrors())
            {
                return(packageToPush);
            }

            var headTipIdSha = repositoryFolder == null ? "" : GitUtilities.HeadTipIdSha(repositoryFolder);

            if (!string.IsNullOrWhiteSpace(headTipIdSha) && pushedHeadTipShas.Contains(headTipIdSha))
            {
                errorsAndInfos.Infos.Add(string.Format(Properties.Resources.HeadTipShaHasAlreadyBeenPushed, headTipIdSha, nugetFeedId));
                return(packageToPush);
            }

            var latestRemotePackageVersion = remotePackages.Max(p => p.Identity.Version.Version);

            errorsAndInfos.Infos.Add(string.Format(Properties.Resources.FoundRemotePackage, latestRemotePackageVersion));
            if (latestRemotePackageVersion >= latestLocalPackageVersion)
            {
                errorsAndInfos.Infos.Add(string.Format(Properties.Resources.RemotePackageHasHigherOrEqualVersion, headTipIdSha));
                return(packageToPush);
            }

            errorsAndInfos.Infos.Add(Properties.Resources.CheckingRemotePackageTag);
            var remotePackage = remotePackages.First(p => p.Identity.Version.Version == latestRemotePackageVersion);

            if (!string.IsNullOrEmpty(remotePackage.Tags) && !string.IsNullOrWhiteSpace(headTipIdSha))
            {
                errorsAndInfos.Infos.Add(string.Format(Properties.Resources.TagsAre, remotePackage.Tags));
                var tags = remotePackage.Tags.Split(' ').ToList();
                if (tags.Contains(headTipIdSha))
                {
                    errorsAndInfos.Infos.Add(string.Format(Properties.Resources.PackageAlreadyTaggedWithHeadTipSha, headTipIdSha));
                    return(packageToPush);
                }

                if (tags.Count != 1)
                {
                    errorsAndInfos.Errors.Add(string.Format(Properties.Resources.RemotePackageContainsSeveralTags, tags));
                    return(packageToPush);
                }

                var tag = tags[0];
                errorsAndInfos.Infos.Add(string.Format(Properties.Resources.CheckingIfThereAreChangedBinaries, headTipIdSha, tag));
                var listerErrorsAndInfos = new ErrorsAndInfos();
                var changedBinaries      = ChangedBinariesLister.ListChangedBinaries(packageId, headTipIdSha, tag, listerErrorsAndInfos);
                if (listerErrorsAndInfos.AnyErrors())
                {
                    errorsAndInfos.Infos.AddRange(listerErrorsAndInfos.Infos);
                    errorsAndInfos.Errors.AddRange(listerErrorsAndInfos.Errors);
                    return(packageToPush);
                }
                if (!changedBinaries.Any())
                {
                    errorsAndInfos.Infos.Add(string.Format(Properties.Resources.NoBinariesHaveChanged));
                    return(packageToPush);
                }
            }

            errorsAndInfos.Infos.Add(Properties.Resources.PackageNeedsToBePushed);
            packageToPush.PackageFileFullName = packageFolderWithBinaries.FullName + @"\" + packageId + "." + latestLocalPackageVersion + ".nupkg";
            packageToPush.Id      = packageId;
            packageToPush.Version = latestLocalPackageVersion?.ToString();
            if (File.Exists(packageToPush.PackageFileFullName))
            {
                return(packageToPush);
            }

            errorsAndInfos.Errors.Add(string.Format(Properties.Resources.FileNotFound, packageToPush.PackageFileFullName));
            return(packageToPush);
        }