| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Core.ExtensionCache |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Linq; |
| 8 | using System.Threading; |
| 9 | using System.Threading.Tasks; |
| 10 | using WixToolset.Data; |
| 11 | using WixToolset.Extensibility; |
| 12 | using WixToolset.Extensibility.Data; |
| 13 | using WixToolset.Extensibility.Services; |
| 14 | |
| 15 | /// <summary> |
| 16 | /// Extension cache manager command. |
| 17 | /// </summary> |
| 18 | internal class ExtensionCacheManagerCommand : BaseCommandLineCommand |
| 19 | { |
| 20 | private enum CacheSubcommand |
| 21 | { |
| 22 | Add, |
| 23 | Remove, |
| 24 | List |
| 25 | } |
| 26 | |
| 27 | public ExtensionCacheManagerCommand(IServiceProvider serviceProvider) |
| 28 | { |
| 29 | this.Messaging = serviceProvider.GetService<IMessaging>(); |
| 30 | this.ExtensionManager = serviceProvider.GetService<IExtensionManager>(); |
| 31 | this.ExtensionReferences = new List<string>(); |
| 32 | } |
| 33 | |
| 34 | private IMessaging Messaging { get; } |
| 35 | |
| 36 | private IExtensionManager ExtensionManager { get; } |
| 37 | |
| 38 | private bool Global { get; set; } |
| 39 | |
| 40 | private CacheSubcommand? Subcommand { get; set; } |
| 41 | |
| 42 | private List<string> ExtensionReferences { get; } |
| 43 | |
| 44 | public override CommandLineHelp GetCommandLineHelp() |
| 45 | { |
| 46 | return new CommandLineHelp("Manage the extension cache.", "extension add|remove|list [options] [extensionRef]") |
| 47 | { |
| 48 | Switches = new[] |
| 49 | { |
| 50 | new CommandLineHelpSwitch("--global", "-g", "Add/remove the extension for the current user."), |
| 51 | }, |
| 52 | Commands = new[] |
| 53 | { |
| 54 | new CommandLineHelpCommand("add", "Add extension to the cache."), |
| 55 | new CommandLineHelpCommand("list", "List extensions in the cache."), |
| 56 | new CommandLineHelpCommand("remove", "Remove extension from the cache."), |
| 57 | }, |
| 58 | Notes = " extensionRef format: extensionId/version (the version is optional)" |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | public override async Task<int> ExecuteAsync(CancellationToken cancellationToken) |
| 63 | { |
| 64 | if (!this.Subcommand.HasValue) |
| 65 | { |
| 66 | this.Messaging.Write(ErrorMessages.CommandLineCommandRequired("extension")); |
| 67 | return this.Messaging.LastErrorNumber; |
| 68 | } |
| 69 | |
| 70 | var success = false; |
| 71 | var cacheManager = new ExtensionCacheManager(this.Messaging, this.ExtensionManager); |
| 72 | |
| 73 | switch (this.Subcommand) |
| 74 | { |
| 75 | case CacheSubcommand.Add: |
| 76 | success = await this.AddExtensions(cacheManager, cancellationToken); |
| 77 | break; |
| 78 | |
| 79 | case CacheSubcommand.Remove: |
| 80 | success = await this.RemoveExtensions(cacheManager, cancellationToken); |
| 81 | break; |
| 82 | |
| 83 | case CacheSubcommand.List: |
| 84 | success = await this.ListExtensions(cacheManager, cancellationToken); |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | return success ? 0 : 2; |
| 89 | } |
| 90 | |
| 91 | public override bool TryParseArgument(ICommandLineParser parser, string argument) |
| 92 | { |
| 93 | if (!parser.IsSwitch(argument)) |
| 94 | { |
| 95 | if (!this.Subcommand.HasValue) |
| 96 | { |
| 97 | if (!Enum.TryParse(argument, true, out CacheSubcommand subcommand)) |
| 98 | { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | this.Subcommand = subcommand; |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | this.ExtensionReferences.Add(argument); |
| 107 | } |
| 108 | |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | var parameter = argument.Substring(1); |
| 113 | switch (parameter.ToLowerInvariant()) |
| 114 | { |
| 115 | case "g": |
| 116 | case "-global": |
| 117 | this.Global = true; |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | private async Task<bool> AddExtensions(ExtensionCacheManager cacheManager, CancellationToken cancellationToken) |
| 125 | { |
| 126 | var success = false; |
| 127 | |
| 128 | foreach (var extensionRef in this.ExtensionReferences) |
| 129 | { |
| 130 | var added = await cacheManager.AddAsync(this.Global, extensionRef, cancellationToken); |
| 131 | success |= added; |
| 132 | } |
| 133 | |
| 134 | return success; |
| 135 | } |
| 136 | |
| 137 | private async Task<bool> RemoveExtensions(ExtensionCacheManager cacheManager, CancellationToken cancellationToken) |
| 138 | { |
| 139 | var success = false; |
| 140 | |
| 141 | foreach (var extensionRef in this.ExtensionReferences) |
| 142 | { |
| 143 | var removed = await cacheManager.RemoveAsync(this.Global, extensionRef, cancellationToken); |
| 144 | success |= removed; |
| 145 | } |
| 146 | |
| 147 | return success; |
| 148 | } |
| 149 | |
| 150 | private async Task<bool> ListExtensions(ExtensionCacheManager cacheManager, CancellationToken cancellationToken) |
| 151 | { |
| 152 | var found = false; |
| 153 | var extensionRef = this.ExtensionReferences.FirstOrDefault(); |
| 154 | |
| 155 | var extensions = await cacheManager.ListAsync(this.Global, extensionRef, cancellationToken); |
| 156 | |
| 157 | foreach (var extension in extensions) |
| 158 | { |
| 159 | this.Messaging.Write($"{extension.Id} {extension.Version}{(extension.Damaged ? " (damaged)" : String.Empty)}"); |
| 160 | found = true; |
| 161 | } |
| 162 | |
| 163 | return found; |
| 164 | } |
| 165 | } |
| 166 | } |