Beispiel #1
0
        public override async Task ExecuteCommandAsync()
        {
            // Arguments[0] will not be null at this point.
            // Because, this command has MinArgs set to 1.
            var packagePath = Arguments[0];

            if (string.IsNullOrEmpty(Source))
            {
                throw new CommandLineException(
                          LocalizedResourceManager.GetString(nameof(NuGetResources.AddCommand_SourceNotProvided)));
            }

            OfflineFeedUtility.ThrowIfInvalidOrNotFound(
                packagePath,
                isDirectory: false,
                resourceString: LocalizedResourceManager.GetString(nameof(NuGetResources.NupkgPath_NotFound)));

            // If the Source Feed Folder does not exist, it will be created.
            OfflineFeedUtility.ThrowIfInvalid(Source);

            var signedPackageVerifier = new PackageSignatureVerifier(SignatureVerificationProviderFactory.GetSignatureVerificationProviders());

            var packageExtractionContext = new PackageExtractionContext(
                Expand ? PackageSaveMode.Defaultv3 : PackageSaveMode.Nuspec | PackageSaveMode.Nupkg,
                PackageExtractionBehavior.XmlDocFileSaveMode,
                Console,
                signedPackageVerifier,
                SignedPackageVerifierSettings.GetClientPolicy(Settings, Console));

            var offlineFeedAddContext = new OfflineFeedAddContext(
                packagePath,
                Source,
                Console, // IConsole is an ILogger
                throwIfSourcePackageIsInvalid: true,
                throwIfPackageExistsAndInvalid: true,
                throwIfPackageExists: false,
                extractionContext: packageExtractionContext);

            await OfflineFeedUtility.AddPackageToSource(offlineFeedAddContext, CancellationToken.None);
        }
Beispiel #2
0
 public void OfflineFeedUtility_ThrowIfInvalid_DoesNotThrow(string path)
 {
     // Act & Assert that the following call does not throw
     OfflineFeedUtility.ThrowIfInvalid(path);
 }
        public async Task AddPackageToSource_InstallsPackageAsync()
        {
            using (var testDirectory = TestDirectory.Create())
            {
                var packageIdentity = new PackageIdentity(id: "a", version: NuGetVersion.Parse("1.0.0"));
                var packageContext  = new SimpleTestPackageContext()
                {
                    Id      = packageIdentity.Id,
                    Version = packageIdentity.Version.ToNormalizedString(),
                    Nuspec  = XDocument.Parse($@"<?xml version=""1.0"" encoding=""utf-8""?>
                        <package>
                        <metadata>
                            <id>{packageIdentity.Id}</id>
                            <version>{packageIdentity.Version.ToNormalizedString()}</version>
                            <title />
                            <frameworkAssemblies>
                                <frameworkAssembly assemblyName=""System.Runtime"" />
                            </frameworkAssemblies>
                            <contentFiles>
                                <files include=""lib/net45/{packageIdentity.Id}.dll"" copyToOutput=""true"" flatten=""false"" />
                            </contentFiles>
                        </metadata>
                        </package>")
                };

                packageContext.AddFile($"lib/net45/{packageIdentity.Id}.dll");

                var sourcePackageDirectoryPath = Path.Combine(testDirectory.Path, "source");
                var destinationDirectoryPath   = Path.Combine(testDirectory.Path, "destination");

                Directory.CreateDirectory(destinationDirectoryPath);

                await SimpleTestPackageUtility.CreatePackagesAsync(sourcePackageDirectoryPath, packageContext);

                var sourcePackageFilePath = Path.Combine(
                    sourcePackageDirectoryPath,
                    $"{packageIdentity.Id}.{packageIdentity.Version.ToNormalizedString()}.nupkg");

                var destinationPackageDirectoryPath = Path.Combine(
                    destinationDirectoryPath,
                    packageIdentity.Id,
                    packageIdentity.Version.ToNormalizedString());

                var extractionContext = new PackageExtractionContext(
                    PackageSaveMode.Defaultv3,
                    PackageExtractionBehavior.XmlDocFileSaveMode,
                    clientPolicyContext: null,
                    logger: NullLogger.Instance);

                var context = new OfflineFeedAddContext(
                    sourcePackageFilePath,
                    destinationDirectoryPath,
                    NullLogger.Instance,
                    throwIfSourcePackageIsInvalid: false,
                    throwIfPackageExistsAndInvalid: false,
                    throwIfPackageExists: false,
                    extractionContext: extractionContext);

                await OfflineFeedUtility.AddPackageToSource(context, CancellationToken.None);

                Assert.True(File.Exists(Path.Combine(destinationPackageDirectoryPath, $"{packageIdentity.Id}.{packageIdentity.Version.ToNormalizedString()}.nupkg")));
                Assert.True(File.Exists(Path.Combine(destinationPackageDirectoryPath, $"{packageIdentity.Id}.{packageIdentity.Version.ToNormalizedString()}.nupkg.sha512")));
                Assert.True(File.Exists(Path.Combine(destinationPackageDirectoryPath, $"{packageIdentity.Id}.nuspec")));
                Assert.True(File.Exists(Path.Combine(destinationPackageDirectoryPath, "lib", "net45", $"{packageIdentity.Id}.dll")));
            }
        }
Beispiel #4
0
        private static async Task PerformPushToSingleSourceAsync(
            ISettings settings,
            String packagePath,
            PackageSourceProvider psp,
            AsyncLazy <PackageIdentity> identity,
            Lazy <NuGetv3LocalRepository[]> allRepositories,
            NuGet.Common.ILogger logger,
            PushSourceInfo sourceItem,
            Int32 retryTimeoutForDirectoryDeletionFail,
            CancellationToken token
            )
        {
            var skipOverwrite               = sourceItem.SkipOverwriteLocalFeed.ParseAsBooleanSafe();
            var skipClearRepositories       = sourceItem.SkipClearingLocalRepositories.ParseAsBooleanSafe();
            var skipOfflineFeedOptimization = sourceItem.SkipOfflineFeedOptimization.ParseAsBooleanSafe();
            var apiKey            = sourceItem.ApiKey;
            var symbolSource      = sourceItem.SymbolSource;
            var symbolApiKey      = sourceItem.SymbolApiKey;
            var noServiceEndPoint = sourceItem.NoServiceEndPoint.ParseAsBooleanSafe();

            var source  = sourceItem.ItemSpec;
            var isLocal = IsLocalFeed(psp, source, out var localPath);

            if (isLocal && !skipOverwrite)
            {
                await DeleteDirAsync(retryTimeoutForDirectoryDeletionFail, OfflineFeedUtility.GetPackageDirectory(await identity, localPath), token);
            }

            if (isLocal && !skipOfflineFeedOptimization)
            {
                // The default v2 repo detection algorithm for PushRunner (PackageUpdateResource.IsV2LocalRepository) always returns true for empty folders, so let's use the OfflineFeedUtility here right away (which will assume v3 repository)
                await OfflineFeedUtility.AddPackageToSource(
                    new OfflineFeedAddContext(
                        packagePath,
                        localPath,
                        logger,
                        true,
                        false,
                        false,
                        new PackageExtractionContext(
                            PackageSaveMode.Defaultv3,
                            XmlDocFileSaveMode.None,
                            ClientPolicyContext.GetClientPolicy(settings, logger),
                            logger
                            )
                        ),
                    token
                    );
            }
            else
            {
                var timeoutString = sourceItem.PushTimeout;
                if (String.IsNullOrEmpty(timeoutString) || !Int32.TryParse(timeoutString, out var timeout))
                {
                    timeout = 1000;
                }

                try
                {
                    await PushRunner.Run(
                        settings,
                        psp,
                        packagePath,
                        source,
                        apiKey,
                        symbolSource,
                        symbolApiKey,
                        timeout,
                        false,
                        String.IsNullOrEmpty(symbolSource),
                        noServiceEndPoint,
                        logger
                        );
                }
                catch (HttpRequestException e) when
                    (e.Message.Contains("already exists. The server is configured to not allow overwriting packages that already exist."))
                {
                    // Nuget.Server returns this message when attempting to overwrite a package.
                    await Console.Out.WriteLineAsync($"Package already exists on source {source}, not updated.");
                }
            }

            if (!skipClearRepositories)
            {
                var id = await identity;
                foreach (var repo in allRepositories.Value)
                {
                    await DeleteDirAsync(retryTimeoutForDirectoryDeletionFail, repo.PathResolver.GetInstallPath(id.Id, id.Version), token);
                }
            }
        }