Пример #1
0
        public Task <bool> CanInstallAsync(InstallRequest installationRequest, CancellationToken cancellationToken)
        {
            try
            {
                ReadPackageInformation(installationRequest.PackageIdentifier);
            }
            catch (Exception)
            {
                _logger.LogDebug($"{installationRequest.PackageIdentifier} is not a local NuGet package.");

                //check if identifier is a valid package ID
                bool validPackageId = PackageIdValidator.IsValidPackageId(installationRequest.PackageIdentifier);
                //check if version is specified it is correct version
                bool hasValidVersion = NuGetVersionHelper.IsSupportedVersionString(installationRequest.Version);
                if (!validPackageId)
                {
                    _logger.LogDebug($"{installationRequest.PackageIdentifier} is not a valid NuGet package ID.");
                }
                if (!hasValidVersion)
                {
                    _logger.LogDebug($"{installationRequest.Version} is not a valid NuGet package version.");
                }
                if (validPackageId && hasValidVersion)
                {
                    _logger.LogDebug($"{installationRequest.DisplayName} is identified as the downloadable NuGet package.");
                }

                //not a local package file
                return(Task.FromResult(validPackageId && hasValidVersion));
            }
            _logger.LogDebug($"{installationRequest.PackageIdentifier} is identified as the local NuGet package.");
            return(Task.FromResult(true));
        }
Пример #2
0
        public async Task <NuGetPackageInfo> DownloadPackageAsync(string downloadPath, string identifier, string?version = null, IEnumerable <string>?additionalSources = null, bool force = false, CancellationToken cancellationToken = default)
        {
            if (string.IsNullOrWhiteSpace(identifier))
            {
                throw new ArgumentException($"{nameof(identifier)} cannot be null or empty", nameof(identifier));
            }
            if (string.IsNullOrWhiteSpace(identifier))
            {
                throw new ArgumentException($"{nameof(downloadPath)} cannot be null or empty", nameof(downloadPath));
            }

            IEnumerable <PackageSource> packagesSources = LoadNuGetSources(additionalSources?.ToArray() ?? Array.Empty <string>());

            PackageSource          source;
            IPackageSearchMetadata packageMetadata;

            if (NuGetVersionHelper.TryParseFloatRangeEx(version, out FloatRange floatRange))
            {
                (source, packageMetadata) =
                    await GetLatestVersionInternalAsync(
                        identifier,
                        packagesSources,
                        floatRange,
                        cancellationToken)
                    .ConfigureAwait(false);
            }
            else
            {
                NuGetVersion packageVersion = new NuGetVersion(version);
                (source, packageMetadata) = await GetPackageMetadataAsync(identifier, packageVersion, packagesSources, cancellationToken).ConfigureAwait(false);
            }

            FindPackageByIdResource resource;
            SourceRepository        repository = _sourcesCache.GetOrAdd(source, Repository.Factory.GetCoreV3(source));

            try
            {
                resource = await repository.GetResourceAsync <FindPackageByIdResource>(cancellationToken).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                _nugetLogger.LogError(string.Format(LocalizableStrings.NuGetApiPackageManager_Error_FailedToLoadSource, source.Source));
                _nugetLogger.LogDebug($"Details: {e.ToString()}.");
                throw new InvalidNuGetSourceException("Failed to load NuGet source", new[] { source.Source }, e);
            }

            string filePath = Path.Combine(downloadPath, packageMetadata.Identity.Id + "." + packageMetadata.Identity.Version + ".nupkg");

            if (!force && _environmentSettings.Host.FileSystem.FileExists(filePath))
            {
                _nugetLogger.LogError(string.Format(LocalizableStrings.NuGetApiPackageManager_Error_FileAlreadyExists, filePath));
                throw new DownloadException(packageMetadata.Identity.Id, packageMetadata.Identity.Version.ToNormalizedString(), new[] { source.Source });
            }
            try
            {
                using Stream packageStream = _environmentSettings.Host.FileSystem.CreateFile(filePath);
                if (await resource.CopyNupkgToStreamAsync(
                        packageMetadata.Identity.Id,
                        packageMetadata.Identity.Version,
                        packageStream,
                        _cacheSettings,
                        _nugetLogger,
                        cancellationToken).ConfigureAwait(false))
                {
                    return(new NuGetPackageInfo(
                               packageMetadata.Authors,
                               filePath,
                               source.Source,
                               packageMetadata.Identity.Id,
                               packageMetadata.Identity.Version.ToNormalizedString()
                               ));
                }
                else
                {
                    _nugetLogger.LogWarning(
                        string.Format(
                            LocalizableStrings.NuGetApiPackageManager_Warning_FailedToDownload,
                            $"{packageMetadata.Identity.Id}::{packageMetadata.Identity.Version}",
                            source.Source));
                    try
                    {
                        _environmentSettings.Host.FileSystem.FileDelete(filePath);
                    }
                    catch (Exception ex)
                    {
                        _nugetLogger.LogWarning(
                            string.Format(
                                LocalizableStrings.NuGetApiPackageManager_Warning_FailedToDelete,
                                filePath));
                        _nugetLogger.LogDebug($"Details: {ex.ToString()}.");
                    }
                    throw new DownloadException(packageMetadata.Identity.Id, packageMetadata.Identity.Version.ToNormalizedString(), new[] { source.Source });
                }
            }
            catch (Exception e)
            {
                _nugetLogger.LogWarning(
                    string.Format(
                        LocalizableStrings.NuGetApiPackageManager_Warning_FailedToDownload,
                        $"{packageMetadata.Identity.Id}::{packageMetadata.Identity.Version}",
                        source.Source));
                _nugetLogger.LogDebug($"Details: {e.ToString()}.");
                try
                {
                    _environmentSettings.Host.FileSystem.FileDelete(filePath);
                }
                catch (Exception ex)
                {
                    _nugetLogger.LogWarning(
                        string.Format(
                            LocalizableStrings.NuGetApiPackageManager_Warning_FailedToDelete,
                            filePath));
                    _nugetLogger.LogDebug($"Details: {ex.ToString()}.");
                }
                throw new DownloadException(packageMetadata.Identity.Id, packageMetadata.Identity.Version.ToNormalizedString(), new[] { source.Source }, e.InnerException);
            }
        }