Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 70 additions & 26 deletions src/wix/WixToolset.Core.ExtensionCache/ExtensionCacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,39 @@ public Task<bool> RemoveAsync(bool global, string extension, CancellationToken c
return Task.FromResult(false);
}


public async Task<bool> 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<CachedExtension> 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<IEnumerable<CachedExtension>> ListAsync(bool global, string extension, CancellationToken cancellationToken)
{
var found = new List<CachedExtension>();
Expand All @@ -99,39 +132,50 @@ public Task<IEnumerable<CachedExtension>> ListAsync(bool global, string extensio
}
else // looking for all versions of an extension or all versions of all extensions.
{
IEnumerable<string> 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<string>();
}
found.AddRange(foundInFolder);
}
}

foreach (var foundExtensionId in foundExtensionIds)
{
var extensionFolder = Path.Combine(cacheFolder, foundExtensionId);
return Task.FromResult((IEnumerable<CachedExtension>)found);
}

foreach (var foundExtensionVersionFolder in Directory.GetDirectories(extensionFolder))
{
cancellationToken.ThrowIfCancellationRequested();
private Task<IEnumerable<CachedExtension>> ListInFolderAsync(string cacheFolder, string extensionId, CancellationToken cancellationToken)
{
var found = new List<CachedExtension>();

var foundExtensionVersion = Path.GetFileName(foundExtensionVersionFolder);
IEnumerable<string> 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<string>();
}

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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ internal class ExtensionCacheManagerCommand : BaseCommandLineCommand
private enum CacheSubcommand
{
Add,

Update,

Remove,
List
}
Expand Down Expand Up @@ -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."),
},
Expand All @@ -76,6 +80,10 @@ public override async Task<int> 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;
Expand Down Expand Up @@ -134,6 +142,19 @@ private async Task<bool> AddExtensions(ExtensionCacheManager cacheManager, Cance
return success;
}

private async Task<bool> 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<bool> RemoveExtensions(ExtensionCacheManager cacheManager, CancellationToken cancellationToken)
{
var success = false;
Expand Down
Loading