예제 #1
0
 public NuGetPackageInstaller(
     NuGetDownloadClient?nugetDownloadClient     = null,
     NuGetCliSettings?nugetCliSettings           = null,
     NuGetDownloadSettings?nugetDownloadSettings = null,
     ILogger?logger = null)
 {
     _nugetCliSettings      = nugetCliSettings ?? NuGetCliSettings.Default;
     _nugetDownloadClient   = nugetDownloadClient ?? new NuGetDownloadClient();
     _nugetDownloadSettings = nugetDownloadSettings ?? NuGetDownloadSettings.Default;
     _logger = logger ?? Logger.None;
 }
        public Task <NuGetDownloadResult> DownloadNuGetAsync(
            NuGetDownloadSettings nuGetDownloadSettings,
            [NotNull] ILogger logger,
            HttpClient?httpClient = null,
            CancellationToken cancellationToken = default)
        {
            if (nuGetDownloadSettings == null)
            {
                throw new ArgumentNullException(nameof(nuGetDownloadSettings));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            return(DownloadNuGetInternalAsync(nuGetDownloadSettings, logger, httpClient, cancellationToken));
        }
        public async Task <NuGetDownloadResult> DownloadNuGetInternalAsync(
            NuGetDownloadSettings nuGetDownloadSettings,
            [NotNull] ILogger logger,
            HttpClient?httpClient = null,
            CancellationToken cancellationToken = default)
        {
            if (!nuGetDownloadSettings.NugetDownloadEnabled)
            {
                return(NuGetDownloadResult.Disabled);
            }

            if (nuGetDownloadSettings.NugetDownloadUriFormat is null)
            {
                return(NuGetDownloadResult.MissingNuGetDownloadUriFormat);
            }

            if (string.IsNullOrWhiteSpace(nuGetDownloadSettings.NugetExeVersion))
            {
                return(NuGetDownloadResult.MissingNuGetExeVersion);
            }

            bool ownsClient = httpClient is null;

            httpClient ??= new HttpClient();

            try
            {
                string downloadDirectoryPath = nuGetDownloadSettings.DownloadDirectory.WithDefault(
                    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                                 "Arbor.Tooler",
                                 "tools",
                                 "nuget",
                                 nuGetDownloadSettings.NugetExeVersion)) !;

                var downloadDirectory = new DirectoryInfo(downloadDirectoryPath);

                try
                {
                    if (!downloadDirectory.Exists)
                    {
                        downloadDirectory.Create();
                    }
                }
                catch (Exception ex)
                {
                    return(NuGetDownloadResult.FromException(ex));
                }

                var targetFile = new FileInfo(Path.Combine(downloadDirectory.FullName, "nuget.exe"));

                string targetFileTempPath = Path.Combine(downloadDirectory.FullName,
                                                         $"nuget.exe-{DateTime.UtcNow.Ticks}.tmp");

                if (targetFile.Exists && !nuGetDownloadSettings.Force)
                {
                    logger.Debug("Found existing nuget.exe at {FilePath}, skipping download", targetFile);

                    if (nuGetDownloadSettings.UpdateEnabled)
                    {
                        NuGetDownloadResult?nuGetDownloadResult = await EnsureLatestAsync(targetFile,
                                                                                          targetFileTempPath,
                                                                                          logger,
                                                                                          httpClient,
                                                                                          cancellationToken).ConfigureAwait(false);

                        if (nuGetDownloadResult?.Succeeded == true)
                        {
                            return(nuGetDownloadResult);
                        }
                    }

                    return(NuGetDownloadResult.Success(targetFile.FullName));
                }

                string downloadUriFormat =
                    nuGetDownloadSettings.NugetDownloadUriFormat.WithDefault(NuGetDownloadSettings
                                                                             .DefaultNuGetExeDownloadUriFormat) !;

                string downloadUri = downloadUriFormat.Contains("{0}", StringComparison.OrdinalIgnoreCase)
                    ? string.Format(downloadUriFormat, nuGetDownloadSettings.NugetExeVersion)
                    : downloadUriFormat;

                if (!Uri.TryCreate(downloadUri, UriKind.Absolute, out Uri? nugetExeUri) ||
                    !nugetExeUri.IsHttpOrHttps())
                {
                    return(NuGetDownloadResult.InvalidDownloadUri(downloadUri));
                }

                NuGetDownloadResult result = await DownloadAsync(logger,
                                                                 nugetExeUri,
                                                                 targetFile,
                                                                 targetFileTempPath,
                                                                 httpClient,
                                                                 cancellationToken).ConfigureAwait(false);

                if (result.Succeeded && nuGetDownloadSettings.UpdateEnabled)
                {
                    NuGetDownloadResult?nuGetDownloadResult = await EnsureLatestAsync(targetFile,
                                                                                      targetFileTempPath,
                                                                                      logger,
                                                                                      httpClient,
                                                                                      cancellationToken).ConfigureAwait(false);

                    if (nuGetDownloadResult?.Succeeded == true)
                    {
                        return(nuGetDownloadResult);
                    }
                }

                return(result);
            }
            finally
            {
                if (ownsClient)
                {
                    httpClient.Dispose();
                }
            }
        }