public IManagedTemplatePackage Deserialize(IManagedTemplatePackageProvider provider, TemplatePackageData data)
 {
     _ = provider ?? throw new ArgumentNullException(nameof(provider));
     if (data.InstallerId != Factory.Id)
     {
         throw new ArgumentException($"{nameof(NuGetInstaller)} can only deserialize packages with {nameof(data.InstallerId)} {Factory.Id}", nameof(data));
     }
     return(NuGetManagedTemplatePackage.Deserialize(_environmentSettings, this, provider, data.MountPointUri, data.Details));
 }
Exemple #2
0
        public TemplatePackageData Serialize(IManagedTemplatePackage templatePackage)
        {
            _ = templatePackage ?? throw new ArgumentNullException(nameof(templatePackage));
            NuGetManagedTemplatePackage nuGetTemplatePackage = templatePackage as NuGetManagedTemplatePackage
                                                               ?? throw new ArgumentException($"{nameof(templatePackage)} should be of type {nameof(NuGetManagedTemplatePackage)}", nameof(templatePackage));

            return(new TemplatePackageData(
                       Factory.Id,
                       nuGetTemplatePackage.MountPointUri,
                       nuGetTemplatePackage.LastChangeTime,
                       nuGetTemplatePackage.Details));
        }
Exemple #3
0
        public async Task <InstallResult> InstallAsync(InstallRequest installRequest, IManagedTemplatePackageProvider provider, CancellationToken cancellationToken)
        {
            _ = installRequest ?? throw new ArgumentNullException(nameof(installRequest));
            _ = provider ?? throw new ArgumentNullException(nameof(provider));

            if (!await CanInstallAsync(installRequest, cancellationToken).ConfigureAwait(false))
            {
                return(InstallResult.CreateFailure(
                           installRequest,
                           InstallerErrorCode.UnsupportedRequest,
                           string.Format(LocalizableStrings.NuGetInstaller_InstallResut_Error_PackageNotSupported, installRequest.DisplayName, Factory.Name)));
            }

            try
            {
                bool             isLocalPackage = IsLocalPackage(installRequest);
                NuGetPackageInfo nuGetPackageInfo;
                if (isLocalPackage)
                {
                    nuGetPackageInfo = InstallLocalPackage(installRequest);
                }
                else
                {
                    string[] additionalNuGetSources = Array.Empty <string>();
                    if (installRequest.Details != null && installRequest.Details.TryGetValue(InstallerConstants.NuGetSourcesKey, out string nugetSources))
                    {
                        additionalNuGetSources = nugetSources.Split(InstallerConstants.NuGetSourcesSeparator);
                    }

                    nuGetPackageInfo = await _packageDownloader.DownloadPackageAsync(
                        _installPath,
                        installRequest.PackageIdentifier,
                        installRequest.Version,
                        additionalNuGetSources,
                        force : installRequest.Force,
                        cancellationToken)
                                       .ConfigureAwait(false);
                }

                NuGetManagedTemplatePackage package = new NuGetManagedTemplatePackage(
                    _environmentSettings,
                    installer: this,
                    provider,
                    nuGetPackageInfo.FullPath,
                    nuGetPackageInfo.PackageIdentifier)
                {
                    Author         = nuGetPackageInfo.Author,
                    NuGetSource    = nuGetPackageInfo.NuGetSource,
                    Version        = nuGetPackageInfo.PackageVersion.ToString(),
                    IsLocalPackage = isLocalPackage
                };

                return(InstallResult.CreateSuccess(installRequest, package));
            }
            catch (DownloadException e)
            {
                string?packageLocation = e.SourcesList == null
                    ? e.PackageLocation
                    : string.Join(", ", e.SourcesList);

                return(InstallResult.CreateFailure(
                           installRequest,
                           InstallerErrorCode.DownloadFailed,
                           string.Format(LocalizableStrings.NuGetInstaller_InstallResut_Error_DownloadFailed, installRequest.DisplayName, packageLocation)));
            }
            catch (PackageNotFoundException e)
            {
                return(InstallResult.CreateFailure(
                           installRequest,
                           InstallerErrorCode.PackageNotFound,
                           string.Format(LocalizableStrings.NuGetInstaller_Error_FailedToReadPackage, e.PackageIdentifier, string.Join(", ", e.SourcesList))));
            }
            catch (InvalidNuGetSourceException e)
            {
                string message = e.SourcesList == null || !e.SourcesList.Any()
                    ? LocalizableStrings.NuGetInstaller_InstallResut_Error_InvalidSources_None
                    : string.Format(LocalizableStrings.NuGetInstaller_InstallResut_Error_InvalidSources, string.Join(", ", e.SourcesList));

                return(InstallResult.CreateFailure(
                           installRequest,
                           InstallerErrorCode.InvalidSource,
                           message));
            }
            catch (InvalidNuGetPackageException e)
            {
                return(InstallResult.CreateFailure(
                           installRequest,
                           InstallerErrorCode.InvalidPackage,
                           string.Format(LocalizableStrings.NuGetInstaller_InstallResut_Error_InvalidPackage, e.PackageLocation)));
            }
            catch (OperationCanceledException)
            {
                return(InstallResult.CreateFailure(
                           installRequest,
                           InstallerErrorCode.GenericError,
                           LocalizableStrings.NuGetInstaller_InstallResut_Error_OperationCancelled));
            }
            catch (Exception e)
            {
                _logger.LogDebug($"Installing {installRequest.DisplayName} failed. Details:{e.ToString()}");
                return(InstallResult.CreateFailure(
                           installRequest,
                           InstallerErrorCode.GenericError,
                           string.Format(LocalizableStrings.NuGetInstaller_InstallResut_Error_InstallGeneric, installRequest.DisplayName, e.Message)));
            }
        }
        public async Task <InstallResult> InstallAsync(InstallRequest installRequest, IManagedTemplatePackageProvider provider, CancellationToken cancellationToken)
        {
            _ = installRequest ?? throw new ArgumentNullException(nameof(installRequest));
            _ = provider ?? throw new ArgumentNullException(nameof(provider));

            if (!await CanInstallAsync(installRequest, cancellationToken).ConfigureAwait(false))
            {
                return(InstallResult.CreateFailure(installRequest, InstallerErrorCode.UnsupportedRequest, $"The install request {installRequest} cannot be processed by installer {Factory.Name}"));
            }

            try
            {
                bool             isLocalPackage = IsLocalPackage(installRequest);
                NuGetPackageInfo nuGetPackageInfo;
                if (isLocalPackage)
                {
                    nuGetPackageInfo = InstallLocalPackage(installRequest);
                }
                else
                {
                    string[] additionalNuGetSources = Array.Empty <string>();
                    if (installRequest.Details != null && installRequest.Details.TryGetValue(InstallerConstants.NuGetSourcesKey, out string nugetSources))
                    {
                        additionalNuGetSources = nugetSources.Split(InstallerConstants.NuGetSourcesSeparator);
                    }

                    nuGetPackageInfo = await _packageDownloader.DownloadPackageAsync(
                        _installPath,
                        installRequest.PackageIdentifier,
                        installRequest.Version,
                        additionalNuGetSources,
                        cancellationToken)
                                       .ConfigureAwait(false);
                }

                NuGetManagedTemplatePackage package = new NuGetManagedTemplatePackage(
                    _environmentSettings,
                    installer: this,
                    provider,
                    nuGetPackageInfo.FullPath,
                    nuGetPackageInfo.PackageIdentifier)
                {
                    Author       = nuGetPackageInfo.Author,
                    NuGetSource  = nuGetPackageInfo.NuGetSource,
                    Version      = nuGetPackageInfo.PackageVersion.ToString(),
                    LocalPackage = isLocalPackage
                };

                return(InstallResult.CreateSuccess(installRequest, package));
            }
            catch (DownloadException e)
            {
                return(InstallResult.CreateFailure(installRequest, InstallerErrorCode.DownloadFailed, e.Message));
            }
            catch (PackageNotFoundException e)
            {
                return(InstallResult.CreateFailure(installRequest, InstallerErrorCode.PackageNotFound, e.Message));
            }
            catch (InvalidNuGetSourceException e)
            {
                return(InstallResult.CreateFailure(installRequest, InstallerErrorCode.InvalidSource, e.Message));
            }
            catch (InvalidNuGetPackageException e)
            {
                return(InstallResult.CreateFailure(installRequest, InstallerErrorCode.InvalidPackage, e.Message));
            }
            catch (Exception e)
            {
                _environmentSettings.Host.LogDiagnosticMessage($"Installing {installRequest.DisplayName} failed.", DebugLogCategory);
                _environmentSettings.Host.LogDiagnosticMessage($"Details:{e.ToString()}", DebugLogCategory);
                return(InstallResult.CreateFailure(installRequest, InstallerErrorCode.GenericError, $"Failed to install the package {installRequest.DisplayName}, reason: {e.Message}"));
            }
        }