private async Task <(InstallerErrorCode, string)> EnsureInstallPrerequisites(List <TemplatePackageData> packagesInSettings, string identifier, string?version, IInstaller installer, CancellationToken cancellationToken, bool update = false, bool forceUpdate = false) { var packages = await GetAllTemplatePackagesAsync(cancellationToken).ConfigureAwait(false); //check if the package with same identifier is already installed if (packages.OfType <IManagedTemplatePackage>().FirstOrDefault(s => s.Identifier == identifier && s.Installer == installer) is IManagedTemplatePackage packageToBeUpdated) { //if same version is already installed - return if (!forceUpdate && packageToBeUpdated.Version == version) { return(InstallerErrorCode.AlreadyInstalled, string.Format(LocalizableStrings.GlobalSettingsTemplatePackageProvider_InstallResult_Error_PackageAlreadyInstalled, packageToBeUpdated.DisplayName)); } if (!update) { _logger.LogInformation( string.Format( LocalizableStrings.GlobalSettingsTemplatePackagesProvider_Info_PackageAlreadyInstalled, string.IsNullOrWhiteSpace(packageToBeUpdated.Version) ? packageToBeUpdated.Identifier : $"{packageToBeUpdated.Identifier} ({string.Format(LocalizableStrings.Generic_Version, packageToBeUpdated.Version)})", string.IsNullOrWhiteSpace(version) ? LocalizableStrings.Generic_LatestVersion : string.Format(LocalizableStrings.Generic_Version, version))); } //uninstall previous version first UninstallResult uninstallResult = await installer.UninstallAsync(packageToBeUpdated, this, cancellationToken).ConfigureAwait(false); if (!uninstallResult.Success) { if (uninstallResult.ErrorMessage is null) { throw new InvalidOperationException($"{nameof(uninstallResult.ErrorMessage)} cannot be null when {nameof(uninstallResult.Success)} is 'true'"); } return(InstallerErrorCode.UpdateUninstallFailed, uninstallResult.ErrorMessage); } _logger.LogInformation( string.Format( LocalizableStrings.GlobalSettingsTemplatePackagesProvider_Info_PackageUninstalled, packageToBeUpdated.DisplayName)); lock (packagesInSettings) { packagesInSettings.RemoveAll(p => p.MountPointUri == packageToBeUpdated.MountPointUri); } } return(InstallerErrorCode.Success, string.Empty); }
private async Task <(InstallerErrorCode, string)> EnsureInstallPrerequisites(List <TemplatePackageData> packagesInSettings, string identifier, string version, IInstaller installer, CancellationToken cancellationToken, bool update = false) { var packages = await GetAllTemplatePackagesAsync(cancellationToken).ConfigureAwait(false); //check if the package with same identifier is already installed if (packages.OfType <IManagedTemplatePackage>().FirstOrDefault(s => s.Identifier == identifier && s.Installer == installer) is IManagedTemplatePackage packageToBeUpdated) { //if same version is already installed - return if (packageToBeUpdated.Version == version) { return(InstallerErrorCode.AlreadyInstalled, $"{packageToBeUpdated.DisplayName} is already installed."); } if (!update) { _environmentSettings.Host.LogMessage( string.Format( LocalizableStrings.GlobalSettingsTemplatePackagesProvider_Info_PackageAlreadyInstalled, packageToBeUpdated.Identifier, packageToBeUpdated.Version, string.IsNullOrWhiteSpace(identifier) ? LocalizableStrings.Generic_LatestVersion : string.Format(LocalizableStrings.Generic_Version, version))); } //if different version is installed - uninstall previous version first UninstallResult uninstallResult = await installer.UninstallAsync(packageToBeUpdated, this, cancellationToken).ConfigureAwait(false); if (!uninstallResult.Success) { return(InstallerErrorCode.UpdateUninstallFailed, uninstallResult.ErrorMessage); } _environmentSettings.Host.LogMessage( string.Format( LocalizableStrings.GlobalSettingsTemplatePackagesProvider_Info_PackageUninstalled, packageToBeUpdated.DisplayName)); lock (packagesInSettings) { packagesInSettings.RemoveAll(p => p.MountPointUri == packageToBeUpdated.MountPointUri); } } return(InstallerErrorCode.Success, string.Empty); }