diff --git a/src/wix/WixToolset.Core.ExtensionCache/ExtensionCacheManager.cs b/src/wix/WixToolset.Core.ExtensionCache/ExtensionCacheManager.cs index cf40b2825..5b4961870 100644 --- a/src/wix/WixToolset.Core.ExtensionCache/ExtensionCacheManager.cs +++ b/src/wix/WixToolset.Core.ExtensionCache/ExtensionCacheManager.cs @@ -77,6 +77,39 @@ public Task RemoveAsync(bool global, string extension, CancellationToken c return Task.FromResult(false); } + + public async Task UpdateAsync(bool global, string extension, CancellationToken cancellationToken) + { + if (String.IsNullOrEmpty(extension)) + { + throw new ArgumentNullException(nameof(extension)); + } + + (var extensionId, var extensionVersion) = ParseExtensionReference(extension); + + var cacheFolder = this.GetCacheFolder(global); + + var extensionNugetVersion = NuGetVersion.Parse(extensionVersion); + + // Get installed extension versions and remove old versions: + IEnumerable installedExtensions = this.ListInFolderAsync(cacheFolder, extensionId, cancellationToken).Result; + foreach (var installedExtension in installedExtensions) + { + if (!NuGetVersion.TryParse(installedExtension.Version, out var installedExtensionNugetVersion)) + { + throw new ArgumentException($"Invalid extension version in {cacheFolder}: {installedExtension.Version}"); + } + + if (installedExtensionNugetVersion.Version < extensionNugetVersion.Version) + { + // Remove old version: + await this.RemoveAsync(global, installedExtension.Id + "/" + installedExtension.Version, cancellationToken); + } + } + // Now update extension: + return await this.AddAsync(global, extension, cancellationToken); + } + public Task> ListAsync(bool global, string extension, CancellationToken cancellationToken) { var found = new List(); @@ -99,39 +132,50 @@ public Task> ListAsync(bool global, string extensio } else // looking for all versions of an extension or all versions of all extensions. { - IEnumerable foundExtensionIds; + var foundInFolder = this.ListInFolderAsync(cacheFolder, extensionId, cancellationToken).Result; - if (String.IsNullOrEmpty(extensionId)) - { - // Looking for all versions of all extensions. - foundExtensionIds = Directory.GetDirectories(cacheFolder).Select(folder => Path.GetFileName(folder)).ToList(); - } - else - { - // Looking for all versions of a single extension. - var extensionFolder = Path.Combine(cacheFolder, extensionId); - foundExtensionIds = Directory.Exists(extensionFolder) ? new[] { extensionId } : Array.Empty(); - } + found.AddRange(foundInFolder); + } + } - foreach (var foundExtensionId in foundExtensionIds) - { - var extensionFolder = Path.Combine(cacheFolder, foundExtensionId); + return Task.FromResult((IEnumerable)found); + } - foreach (var foundExtensionVersionFolder in Directory.GetDirectories(extensionFolder)) - { - cancellationToken.ThrowIfCancellationRequested(); + private Task> ListInFolderAsync(string cacheFolder, string extensionId, CancellationToken cancellationToken) + { + var found = new List(); - var foundExtensionVersion = Path.GetFileName(foundExtensionVersionFolder); + IEnumerable foundExtensionIds; - if (!NuGetVersion.TryParse(foundExtensionVersion, out _)) - { - continue; - } + if (String.IsNullOrEmpty(extensionId)) + { + // Looking for all versions of all extensions. + foundExtensionIds = Directory.GetDirectories(cacheFolder).Select(folder => Path.GetFileName(folder)).ToList(); + } + else + { + // Looking for all versions of a single extension. + var extensionFolder = Path.Combine(cacheFolder, extensionId); + foundExtensionIds = Directory.Exists(extensionFolder) ? new[] { extensionId } : Array.Empty(); + } - var present = this.ExtensionFileExists(cacheFolder, foundExtensionId, foundExtensionVersion); - found.Add(new CachedExtension(foundExtensionId, foundExtensionVersion, !present)); - } + foreach (var foundExtensionId in foundExtensionIds) + { + var extensionFolder = Path.Combine(cacheFolder, foundExtensionId); + + foreach (var foundExtensionVersionFolder in Directory.GetDirectories(extensionFolder)) + { + cancellationToken.ThrowIfCancellationRequested(); + + var foundExtensionVersion = Path.GetFileName(foundExtensionVersionFolder); + + if (!NuGetVersion.TryParse(foundExtensionVersion, out _)) + { + continue; } + + var present = this.ExtensionFileExists(cacheFolder, foundExtensionId, foundExtensionVersion); + found.Add(new CachedExtension(foundExtensionId, foundExtensionVersion, !present)); } } diff --git a/src/wix/WixToolset.Core.ExtensionCache/ExtensionCacheManagerCommand.cs b/src/wix/WixToolset.Core.ExtensionCache/ExtensionCacheManagerCommand.cs index a1059e834..f1d9838ea 100644 --- a/src/wix/WixToolset.Core.ExtensionCache/ExtensionCacheManagerCommand.cs +++ b/src/wix/WixToolset.Core.ExtensionCache/ExtensionCacheManagerCommand.cs @@ -20,6 +20,9 @@ internal class ExtensionCacheManagerCommand : BaseCommandLineCommand private enum CacheSubcommand { Add, + + Update, + Remove, List } @@ -52,6 +55,7 @@ public override CommandLineHelp GetCommandLineHelp() Commands = new[] { new CommandLineHelpCommand("add", "Add extension to the cache."), + new CommandLineHelpCommand("update", "Update extension in the cache. If the extension is not installed, it is the same as 'add'."), new CommandLineHelpCommand("list", "List extensions in the cache."), new CommandLineHelpCommand("remove", "Remove extension from the cache."), }, @@ -76,6 +80,10 @@ public override async Task ExecuteAsync(CancellationToken cancellationToken success = await this.AddExtensions(cacheManager, cancellationToken); break; + case CacheSubcommand.Update: + success = await this.UpdateExtensions(cacheManager, cancellationToken); + break; + case CacheSubcommand.Remove: success = await this.RemoveExtensions(cacheManager, cancellationToken); break; @@ -134,6 +142,19 @@ private async Task AddExtensions(ExtensionCacheManager cacheManager, Cance return success; } + private async Task UpdateExtensions(ExtensionCacheManager cacheManager, CancellationToken cancellationToken) + { + var success = false; + + foreach (var extensionRef in this.ExtensionReferences) + { + var added = await cacheManager.UpdateAsync(this.Global, extensionRef, cancellationToken); + success |= added; + } + + return success; + } + private async Task RemoveExtensions(ExtensionCacheManager cacheManager, CancellationToken cancellationToken) { var success = false;