diff --git a/src/Cli/Microsoft.DotNet.Cli.Utils/Constants.cs b/src/Cli/Microsoft.DotNet.Cli.Utils/Constants.cs index 05090b91cec1..8d011659f565 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Utils/Constants.cs +++ b/src/Cli/Microsoft.DotNet.Cli.Utils/Constants.cs @@ -10,12 +10,14 @@ public static class Constants public const string DefaultConfiguration = "Debug"; public static readonly string ProjectFileName = "project.json"; + public static readonly string ToolManifestFileName = "dotnet-tools.json"; public static readonly string DotConfigDirectoryName = ".config"; public static readonly string ExeSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : string.Empty; public static readonly string BinDirectoryName = "bin"; public static readonly string ObjDirectoryName = "obj"; + public static readonly string GitDirectoryName = ".git"; public static readonly string MSBUILD_EXE_PATH = "MSBUILD_EXE_PATH"; public static readonly string MSBuildExtensionsPath = "MSBuildExtensionsPath"; diff --git a/src/Cli/dotnet/ToolManifest/IToolManifestFinder.cs b/src/Cli/dotnet/ToolManifest/IToolManifestFinder.cs index cc0b50a98b8c..fc98c6870022 100644 --- a/src/Cli/dotnet/ToolManifest/IToolManifestFinder.cs +++ b/src/Cli/dotnet/ToolManifest/IToolManifestFinder.cs @@ -10,7 +10,7 @@ namespace Microsoft.DotNet.ToolManifest internal interface IToolManifestFinder { IReadOnlyCollection Find(FilePath? filePath = null); - FilePath FindFirst(); + FilePath FindFirst(bool createManifestFileOption = false); IReadOnlyList FindByPackageId(PackageId packageId); } } diff --git a/src/Cli/dotnet/ToolManifest/LocalizableStrings.resx b/src/Cli/dotnet/ToolManifest/LocalizableStrings.resx index 0b4423604514..c9d74cba328e 100644 --- a/src/Cli/dotnet/ToolManifest/LocalizableStrings.resx +++ b/src/Cli/dotnet/ToolManifest/LocalizableStrings.resx @@ -169,4 +169,4 @@ For a list of locations searched, specify the "-d" option before the tool name.< More than one entry exists for package(s): {0}. - + \ No newline at end of file diff --git a/src/Cli/dotnet/ToolManifest/ToolManifestFinder.cs b/src/Cli/dotnet/ToolManifest/ToolManifestFinder.cs index 64b09f60b61a..0f233abb687a 100644 --- a/src/Cli/dotnet/ToolManifest/ToolManifestFinder.cs +++ b/src/Cli/dotnet/ToolManifest/ToolManifestFinder.cs @@ -3,10 +3,12 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.ToolPackage; using Microsoft.Extensions.EnvironmentAbstractions; +using NuGet.Packaging; namespace Microsoft.DotNet.ToolManifest { @@ -153,7 +155,7 @@ public bool TryFind(ToolCommandName toolCommandName, out ToolManifestPackage too } } - public FilePath FindFirst() + public FilePath FindFirst(bool createIfNotFound = false) { foreach ((FilePath possibleManifest, DirectoryPath _) in EnumerateDefaultAllPossibleManifests()) { @@ -162,12 +164,66 @@ public FilePath FindFirst() return possibleManifest; } } - + if (createIfNotFound) + { + DirectoryPath manifestInsertFolder = GetDirectoryToCreateToolManifest(); + if (manifestInsertFolder.Value != null) + { + return new FilePath(WriteManifestFile(manifestInsertFolder)); + } + } throw new ToolManifestCannotBeFoundException( - LocalizableStrings.CannotFindAManifestFile, - string.Format(LocalizableStrings.ListOfSearched, - string.Join(Environment.NewLine, - EnumerateDefaultAllPossibleManifests().Select(f => "\t" + f.manifestfile.Value)))); + LocalizableStrings.CannotFindAManifestFile, + string.Format(LocalizableStrings.ListOfSearched, + string.Join(Environment.NewLine, + EnumerateDefaultAllPossibleManifests().Select(f => "\t" + f.manifestfile.Value)))); + } + + /* + The --create-manifest-if-needed will use the following priority to choose the folder where the tool manifest goes: + 1. Walk up the directory tree searching for one that has a.git subfolder + 2. Walk up the directory tree searching for one that has a .sln/git file in it + 3. Use the current working directory + */ + private DirectoryPath GetDirectoryToCreateToolManifest() + { + DirectoryPath? currentSearchDirectory = _probeStart; + while (currentSearchDirectory.HasValue && currentSearchDirectory.Value.GetParentPathNullable()!=null) + { + var currentSearchGitDirectory = currentSearchDirectory.Value.WithSubDirectories(Constants.GitDirectoryName); + if (_fileSystem.Directory.Exists(currentSearchGitDirectory.Value)) + { + return currentSearchDirectory.Value; + } + if (currentSearchDirectory.Value.Value != null) + { + if (_fileSystem.Directory.EnumerateFiles(currentSearchDirectory.Value.Value) + .Any(filename => Path.GetExtension(filename).Equals(".sln", StringComparison.OrdinalIgnoreCase)) + || _fileSystem.File.Exists(currentSearchDirectory.Value.WithFile(".git").Value)) + + { + return currentSearchDirectory.Value; + } + } + currentSearchDirectory = currentSearchDirectory.Value.GetParentPathNullable(); + } + return _probeStart; + } + + private string WriteManifestFile(DirectoryPath folderPath) + { + var manifestFileContent = """ + { + "version": 1, + "isRoot": true, + "tools": {} + } + """; + _fileSystem.Directory.CreateDirectory(Path.Combine(folderPath.Value, Constants.DotConfigDirectoryName)); + string manifestFileLocation = Path.Combine(folderPath.Value, Constants.DotConfigDirectoryName, Constants.ToolManifestFileName); + _fileSystem.File.WriteAllText(manifestFileLocation, manifestFileContent); + + return manifestFileLocation; } /// diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/LocalizableStrings.resx b/src/Cli/dotnet/commands/dotnet-tool/install/LocalizableStrings.resx index 29af9c56d2e0..cf87d1bac6c3 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/LocalizableStrings.resx +++ b/src/Cli/dotnet/commands/dotnet-tool/install/LocalizableStrings.resx @@ -232,4 +232,7 @@ If you would like to create a manifest, use `dotnet new tool-manifest`, usually The --prerelease and --version options are not supported in the same command + + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + \ No newline at end of file diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/ToolInstallCommandParser.cs b/src/Cli/dotnet/commands/dotnet-tool/install/ToolInstallCommandParser.cs index 16d68cccd0f9..1443eb5f31f0 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/ToolInstallCommandParser.cs +++ b/src/Cli/dotnet/commands/dotnet-tool/install/ToolInstallCommandParser.cs @@ -38,6 +38,8 @@ internal static class ToolInstallCommandParser ArgumentHelpName = LocalizableStrings.FrameworkOptionName }; + public static readonly Option CreateManifestIfNeededOption = new Option("--create-manifest-if-needed", LocalizableStrings.CreateManifestIfNeededOptionDescription); + public static readonly Option PrereleaseOption = ToolSearchCommandParser.PrereleaseOption; public static readonly Option VerbosityOption = CommonOptions.VerbosityOption; @@ -80,6 +82,7 @@ private static Command ConstructCommand() command.AddOption(ToolCommandRestorePassThroughOptions.InteractiveRestoreOption); command.AddOption(VerbosityOption); command.AddOption(ArchitectureOption); + command.AddOption(CreateManifestIfNeededOption); command.SetHandler((parseResult) => new ToolInstallCommand(parseResult).Execute()); diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/ToolInstallLocalCommand.cs b/src/Cli/dotnet/commands/dotnet-tool/install/ToolInstallLocalCommand.cs index d7ce6283cda9..2a2447b0d6e5 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/ToolInstallLocalCommand.cs +++ b/src/Cli/dotnet/commands/dotnet-tool/install/ToolInstallLocalCommand.cs @@ -11,6 +11,8 @@ using Microsoft.DotNet.ToolPackage; using Microsoft.DotNet.Tools.Tool.Common; using Microsoft.Extensions.EnvironmentAbstractions; +using System.Collections.Generic; +using NuGet.Packaging; namespace Microsoft.DotNet.Tools.Tool.Install { @@ -23,6 +25,7 @@ internal class ToolInstallLocalCommand : CommandBase private readonly IReporter _reporter; private readonly string _explicitManifestFile; + private readonly bool _createManifestIfNeeded; public ToolInstallLocalCommand( ParseResult parseResult, @@ -30,11 +33,14 @@ public ToolInstallLocalCommand( IToolManifestFinder toolManifestFinder = null, IToolManifestEditor toolManifestEditor = null, ILocalToolsResolverCache localToolsResolverCache = null, - IReporter reporter = null) + IReporter reporter = null + ) : base(parseResult) { _explicitManifestFile = parseResult.GetValue(ToolAppliedOption.ToolManifestOption); + _createManifestIfNeeded = parseResult.GetValue(ToolInstallCommandParser.CreateManifestIfNeededOption); + _reporter = (reporter ?? Reporter.Output); _toolManifestFinder = toolManifestFinder ?? @@ -47,7 +53,6 @@ public ToolInstallLocalCommand( public override int Execute() { FilePath manifestFile = GetManifestFilePath(); - return Install(manifestFile); } @@ -82,7 +87,7 @@ private FilePath GetManifestFilePath() try { return string.IsNullOrWhiteSpace(_explicitManifestFile) - ? _toolManifestFinder.FindFirst() + ? _toolManifestFinder.FindFirst(_createManifestIfNeeded) : new FilePath(_explicitManifestFile); } catch (ToolManifestCannotBeFoundException e) diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.cs.xlf b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.cs.xlf index db5de960bf3b..29748190c12e 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.cs.xlf +++ b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.cs.xlf @@ -2,6 +2,11 @@ + + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + + The local option(--local), the global option (--global), the tool path option (--tool-path), can only have one at a time. Specify only one of the options: {0}. Možnost local (--local), možnost global (--global), možnost tool path (--tool-path), v jednu chvíli je možné mít jen jednu. Zadejte jen jednu z možností: {0} diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.de.xlf b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.de.xlf index 8cd539b174a1..33c89760fc53 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.de.xlf +++ b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.de.xlf @@ -2,6 +2,11 @@ + + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + + The local option(--local), the global option (--global), the tool path option (--tool-path), can only have one at a time. Specify only one of the options: {0}. Die lokale Option (--local), die globale Option (--global) und die Toolpfadoption (--tool-path) können nicht zusammen angegeben werden. Geben Sie nur eine der Optionen an: {0}. diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.es.xlf b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.es.xlf index 3cc779b5a9ec..22748bf0d7b1 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.es.xlf +++ b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.es.xlf @@ -2,6 +2,11 @@ + + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + + The local option(--local), the global option (--global), the tool path option (--tool-path), can only have one at a time. Specify only one of the options: {0}. La opción local (--local), la opción global (--global) y la opción de ruta de acceso de herramienta (--tool-path), solo pueden estar una cada vez. Especifique solo una de las opciones: {0}. diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.fr.xlf b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.fr.xlf index 44de3243301a..1ae0b90cb693 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.fr.xlf +++ b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.fr.xlf @@ -2,6 +2,11 @@ + + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + + The local option(--local), the global option (--global), the tool path option (--tool-path), can only have one at a time. Specify only one of the options: {0}. L'option locale (--local), l'option globale (--global) et l'option de chemin d'outil (--tool-path) ne peuvent pas être utilisées simultanément. Spécifiez une seule des options : {0}. diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.it.xlf b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.it.xlf index d34477f2f16f..c74fefda7c6c 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.it.xlf +++ b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.it.xlf @@ -2,6 +2,11 @@ + + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + + The local option(--local), the global option (--global), the tool path option (--tool-path), can only have one at a time. Specify only one of the options: {0}. Le opzioni locale (--local), globale (--global) e del percorso dello strumento (--tool-path) non possono essere specificate contemporaneamente. Specificare una sola di queste opzioni: {0}. diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.ja.xlf b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.ja.xlf index 8f4ad86b0c9c..2103071c6e3f 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.ja.xlf +++ b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.ja.xlf @@ -2,6 +2,11 @@ + + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + + The local option(--local), the global option (--global), the tool path option (--tool-path), can only have one at a time. Specify only one of the options: {0}. ローカル オプション (--local)、グローバル オプション (--global)、ツール パス オプション (--tool-path) は、一度に 1 つだけ指定できます。オプションを 1 つだけ指定します: {0}。 diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.ko.xlf b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.ko.xlf index 3c9ad5c9da16..08ea6b937fb3 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.ko.xlf +++ b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.ko.xlf @@ -2,6 +2,11 @@ + + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + + The local option(--local), the global option (--global), the tool path option (--tool-path), can only have one at a time. Specify only one of the options: {0}. 로컬 옵션(--local), 전역 옵션(--global), 도구 경로 옵션(--tool-path)은 한 번에 하나씩만 사용할 수 있습니다. {0} 옵션 중에서 하나만 지정하세요. diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.pl.xlf b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.pl.xlf index 1311466b5f84..4d85e0f1c3cd 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.pl.xlf +++ b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.pl.xlf @@ -2,6 +2,11 @@ + + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + + The local option(--local), the global option (--global), the tool path option (--tool-path), can only have one at a time. Specify only one of the options: {0}. Można określić tylko jedną opcję jednocześnie: opcję lokalną (--local), opcję globalną (--global) lub opcję ścieżki do narzędzia (--tool-path). Podaj tylko jedną z opcji: {0}. diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.pt-BR.xlf b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.pt-BR.xlf index 9bfcc4746408..ded86eb28994 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.pt-BR.xlf +++ b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.pt-BR.xlf @@ -2,6 +2,11 @@ + + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + + The local option(--local), the global option (--global), the tool path option (--tool-path), can only have one at a time. Specify only one of the options: {0}. As opções local (--local), global (--global) e do caminho da ferramenta (--tool-path) só podem ocorrer uma por vez. Especifique apenas uma das opções: {0}. diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.ru.xlf b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.ru.xlf index 949c3d47bf95..ae39e544f06d 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.ru.xlf +++ b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.ru.xlf @@ -2,6 +2,11 @@ + + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + + The local option(--local), the global option (--global), the tool path option (--tool-path), can only have one at a time. Specify only one of the options: {0}. Локальный параметр (--local), глобальный параметр (--global) и параметр пути к средству (--tool-path) можно использовать только отдельно друг от друга. Укажите только один из этих параметров: {0}. diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.tr.xlf b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.tr.xlf index 10dbd2b76417..5e73b1edf5b9 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.tr.xlf +++ b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.tr.xlf @@ -2,6 +2,11 @@ + + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + + The local option(--local), the global option (--global), the tool path option (--tool-path), can only have one at a time. Specify only one of the options: {0}. Yerel seçeneği (--local), genel seçeneği (--global), araç yolu seçeneği (--tool-path) arasından yalnızca biri seçilebilir. Seçeneklerden yalnızca birini belirtin: {0}. diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.zh-Hans.xlf b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.zh-Hans.xlf index 52f09eda7162..2eb59f087426 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.zh-Hans.xlf +++ b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.zh-Hans.xlf @@ -2,6 +2,11 @@ + + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + + The local option(--local), the global option (--global), the tool path option (--tool-path), can only have one at a time. Specify only one of the options: {0}. 本地选项(--local)、全局选项(--global)和工具路径选项(--tool-path)一次只能有一个。请仅指定其中一个选项: {0}。 diff --git a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.zh-Hant.xlf b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.zh-Hant.xlf index 6455ca572461..ffa42f0d2c82 100644 --- a/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.zh-Hant.xlf +++ b/src/Cli/dotnet/commands/dotnet-tool/install/xlf/LocalizableStrings.zh-Hant.xlf @@ -2,6 +2,11 @@ + + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + Create a tool manifest if one isn't found during tool installation. For information on how manifests are located, see https://aka.ms/dotnet/tools/create-manifest-if-needed + + The local option(--local), the global option (--global), the tool path option (--tool-path), can only have one at a time. Specify only one of the options: {0}. 一次只能有一個本機選項 (--local)、全域選項 (--global)、工具路徑選項 (--tool-path)。請僅指定其中一個選項: {0}。 diff --git a/src/Tests/dotnet.Tests/CommandTests/ToolInstallLocalCommandTests.cs b/src/Tests/dotnet.Tests/CommandTests/ToolInstallLocalCommandTests.cs index 281bc27abe77..04548903d89a 100644 --- a/src/Tests/dotnet.Tests/CommandTests/ToolInstallLocalCommandTests.cs +++ b/src/Tests/dotnet.Tests/CommandTests/ToolInstallLocalCommandTests.cs @@ -22,10 +22,12 @@ using System.CommandLine; using System.CommandLine.Parsing; using Parser = Microsoft.DotNet.Cli.Parser; +using Microsoft.NET.TestFramework; +using Xunit.Abstractions; namespace Microsoft.DotNet.Tests.Commands.Tool { - public class ToolInstallLocalCommandTests + public class ToolInstallLocalCommandTests:SdkTest { private readonly IFileSystem _fileSystem; private readonly IToolPackageStore _toolPackageStore; @@ -42,7 +44,7 @@ public class ToolInstallLocalCommandTests private readonly ToolManifestFinder _toolManifestFinder; private readonly ToolManifestEditor _toolManifestEditor; - public ToolInstallLocalCommandTests() + public ToolInstallLocalCommandTests(ITestOutputHelper log):base(log) { _packageVersionA = NuGetVersion.Parse("1.0.4"); @@ -314,6 +316,75 @@ out RestoredCommand restoredCommand _fileSystem.File.Exists(restoredCommand.Executable.Value); } + [Fact] + public void GivenNoManifestFileAndCreateManifestIfNeededFlagItShouldCreateManifestInGit() + { + _fileSystem.Directory.CreateDirectory(Path.Combine(_temporaryDirectory, ".git")); + _fileSystem.File.Delete(_manifestFilePath); + var currentFolder = Path.Combine(_temporaryDirectory, "subdirectory1", "subdirectory2"); + _fileSystem.Directory.CreateDirectory(currentFolder); + + ParseResult parseResult = + Parser.Instance.Parse( + $"dotnet tool install {_packageIdA.ToString()} --create-manifest-if-needed"); + + var installLocalCommand = new ToolInstallLocalCommand( + parseResult, + _toolPackageInstallerMock, + _toolManifestFinder, + _toolManifestEditor, + _localToolsResolverCache, + _reporter); + + installLocalCommand.Execute().Should().Be(0); + _fileSystem.File.Exists(Path.Combine(_temporaryDirectory, ".config", "dotnet-tools.json")).Should().BeTrue(); + } + + [Fact] + public void GivenNoManifestFileAndCreateManifestIfNeededFlagItShouldCreateManifestInSln() + { + _fileSystem.Directory.CreateDirectory(Path.Combine(_temporaryDirectory, "test1.sln")); + _fileSystem.File.Delete(_manifestFilePath); + var currentFolder = Path.Combine(_temporaryDirectory, "subdirectory1", "subdirectory2"); + _fileSystem.Directory.CreateDirectory(currentFolder); + + ParseResult parseResult = + Parser.Instance.Parse( + $"dotnet tool install {_packageIdA.ToString()} --create-manifest-if-needed"); + + var installLocalCommand = new ToolInstallLocalCommand( + parseResult, + _toolPackageInstallerMock, + _toolManifestFinder, + _toolManifestEditor, + _localToolsResolverCache, + _reporter); + + installLocalCommand.Execute().Should().Be(0); + _fileSystem.File.Exists(Path.Combine(_temporaryDirectory, ".config", "dotnet-tools.json")).Should().BeTrue(); + } + + [Fact] + public void GivenNoManifestFileAndCreateManifestIfNeededFlagItShouldCreateManifestInCurrentFolder() + { + _fileSystem.File.Delete(_manifestFilePath); + + ParseResult parseResult = + Parser.Instance.Parse( + $"dotnet tool install {_packageIdA.ToString()} --create-manifest-if-needed"); + + var installLocalCommand = new ToolInstallLocalCommand( + parseResult, + _toolPackageInstallerMock, + _toolManifestFinder, + _toolManifestEditor, + _localToolsResolverCache, + _reporter); + + installLocalCommand.Execute().Should().Be(0); + _fileSystem.File.Exists(Path.Combine(_temporaryDirectory, ".config", "dotnet-tools.json")).Should().BeTrue(); + } + private IToolPackageInstaller GetToolToolPackageInstallerWithPreviewInFeed() { List feeds = new List diff --git a/src/Tests/dotnet.Tests/CommandTests/ToolRestoreCommandTests.cs b/src/Tests/dotnet.Tests/CommandTests/ToolRestoreCommandTests.cs index d9eb348d7d60..099f2f9dea01 100644 --- a/src/Tests/dotnet.Tests/CommandTests/ToolRestoreCommandTests.cs +++ b/src/Tests/dotnet.Tests/CommandTests/ToolRestoreCommandTests.cs @@ -416,7 +416,7 @@ public IReadOnlyCollection Find(FilePath? filePath = null) return _toReturn; } - public FilePath FindFirst() + public FilePath FindFirst(bool createManifestFileOption = false) { throw new NotImplementedException(); } @@ -434,7 +434,7 @@ public IReadOnlyCollection Find(FilePath? filePath = null) throw new ToolManifestCannotBeFoundException("In test cannot find manifest"); } - public FilePath FindFirst() + public FilePath FindFirst(bool createManifestFileOption = false) { throw new NotImplementedException(); } diff --git a/src/Tests/dotnet.Tests/CommandTests/ToolRestoreCommandWithMultipleNugetConfigTests.cs b/src/Tests/dotnet.Tests/CommandTests/ToolRestoreCommandWithMultipleNugetConfigTests.cs index 2caa85a4b628..8048ad22f357 100644 --- a/src/Tests/dotnet.Tests/CommandTests/ToolRestoreCommandWithMultipleNugetConfigTests.cs +++ b/src/Tests/dotnet.Tests/CommandTests/ToolRestoreCommandWithMultipleNugetConfigTests.cs @@ -177,7 +177,7 @@ public IReadOnlyCollection Find(FilePath? filePath = null) return _toReturn; } - public FilePath FindFirst() + public FilePath FindFirst(bool createManifestFileOption = false) { throw new NotImplementedException(); }