|
| 1 | +// Copyright (c) .NET Foundation and contributors. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Reflection; |
| 7 | +using System.Collections.Generic; |
| 8 | +using Microsoft.DotNet.PlatformAbstractions; |
| 9 | + |
| 10 | +namespace Microsoft.DotNet.Cli.Utils |
| 11 | +{ |
| 12 | + public class DotnetToolsCommandResolver : ICommandResolver |
| 13 | + { |
| 14 | + private string _dotnetToolPath; |
| 15 | + |
| 16 | + public DotnetToolsCommandResolver(string dotnetToolPath = null) |
| 17 | + { |
| 18 | + if (dotnetToolPath == null) |
| 19 | + { |
| 20 | + _dotnetToolPath = Path.Combine(ApplicationEnvironment.ApplicationBasePath, |
| 21 | + "DotnetTools"); |
| 22 | + } |
| 23 | + else |
| 24 | + { |
| 25 | + _dotnetToolPath = dotnetToolPath; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public CommandSpec Resolve(CommandResolverArguments arguments) |
| 30 | + { |
| 31 | + if (string.IsNullOrEmpty(arguments.CommandName)) |
| 32 | + { |
| 33 | + return null; |
| 34 | + } |
| 35 | + |
| 36 | + var packageId = new DirectoryInfo(Path.Combine(_dotnetToolPath, arguments.CommandName)); |
| 37 | + if (!packageId.Exists) |
| 38 | + { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + var version = packageId.GetDirectories()[0]; |
| 43 | + var dll = version.GetDirectories("tools")[0] |
| 44 | + .GetDirectories()[0] // TFM |
| 45 | + .GetDirectories()[0] // RID |
| 46 | + .GetFiles($"{arguments.CommandName}.dll")[0]; |
| 47 | + |
| 48 | + return CreatePackageCommandSpecUsingMuxer( |
| 49 | + dll.FullName, |
| 50 | + arguments.CommandArguments, |
| 51 | + CommandResolutionStrategy.DotnetToolsPackage); |
| 52 | + } |
| 53 | + |
| 54 | + private CommandSpec CreatePackageCommandSpecUsingMuxer( |
| 55 | + string commandPath, |
| 56 | + IEnumerable<string> commandArguments, |
| 57 | + CommandResolutionStrategy commandResolutionStrategy) |
| 58 | + { |
| 59 | + var arguments = new List<string>(); |
| 60 | + |
| 61 | + var muxer = new Muxer(); |
| 62 | + |
| 63 | + var host = muxer.MuxerPath; |
| 64 | + if (host == null) |
| 65 | + { |
| 66 | + throw new Exception(LocalizableStrings.UnableToLocateDotnetMultiplexer); |
| 67 | + } |
| 68 | + |
| 69 | + arguments.Add(commandPath); |
| 70 | + |
| 71 | + if (commandArguments != null) |
| 72 | + { |
| 73 | + arguments.AddRange(commandArguments); |
| 74 | + } |
| 75 | + |
| 76 | + return CreateCommandSpec(host, arguments, commandResolutionStrategy); |
| 77 | + } |
| 78 | + |
| 79 | + private CommandSpec CreateCommandSpec( |
| 80 | + string commandPath, |
| 81 | + IEnumerable<string> commandArguments, |
| 82 | + CommandResolutionStrategy commandResolutionStrategy) |
| 83 | + { |
| 84 | + var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(commandArguments); |
| 85 | + |
| 86 | + return new CommandSpec(commandPath, escapedArgs, commandResolutionStrategy); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments