From 3fc39be019419b4363afe605de03976d16fb80ac Mon Sep 17 00:00:00 2001 From: Sean Fausett Date: Sun, 28 Feb 2021 12:48:59 +1300 Subject: [PATCH] Add support for dotnet local tools --- .../DotNetToolContentResolver.cs | 4 +++ .../DotNetToolPackage.cs | 6 ++++ .../DotNetToolPackageInstaller.cs | 36 ++++++++++++++----- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Cake.DotNetTool.Module/DotNetToolContentResolver.cs b/src/Cake.DotNetTool.Module/DotNetToolContentResolver.cs index df8b0b8537..a6c995b752 100644 --- a/src/Cake.DotNetTool.Module/DotNetToolContentResolver.cs +++ b/src/Cake.DotNetTool.Module/DotNetToolContentResolver.cs @@ -73,6 +73,10 @@ public IReadOnlyCollection GetFiles(PackageReference package, PackageType return GetToolFiles(new DirectoryPath(_environment.GetEnvironmentVariable("USERPROFILE")).Combine(".dotnet/tools"), package); } } + else if (package.Parameters.ContainsKey("local")) + { + return new List(); // TODO: implementation + } else { return GetToolFiles(_config.GetToolPath(_environment.WorkingDirectory, _environment), package); diff --git a/src/Cake.DotNetTool.Module/DotNetToolPackage.cs b/src/Cake.DotNetTool.Module/DotNetToolPackage.cs index dcaf4ac75c..45f13c99a8 100644 --- a/src/Cake.DotNetTool.Module/DotNetToolPackage.cs +++ b/src/Cake.DotNetTool.Module/DotNetToolPackage.cs @@ -26,5 +26,11 @@ public sealed class DotNetToolPackage /// /// The tool package short code. public string ShortCode { get; set; } + + /// + /// Gets or sets the tool package manifest. + /// + /// The tool package manifest. + public string Manifest { get; set; } } } \ No newline at end of file diff --git a/src/Cake.DotNetTool.Module/DotNetToolPackageInstaller.cs b/src/Cake.DotNetTool.Module/DotNetToolPackageInstaller.cs index f07a586ec0..e126c91ce1 100644 --- a/src/Cake.DotNetTool.Module/DotNetToolPackageInstaller.cs +++ b/src/Cake.DotNetTool.Module/DotNetToolPackageInstaller.cs @@ -110,6 +110,10 @@ public IReadOnlyCollection Install(PackageReference package, PackageType { toolLocation = "global"; } + else if (package.Parameters.ContainsKey("local")) + { + toolLocation = "local"; + } // First we need to check if the Tool is already installed var installedTools = GetInstalledTools(toolLocation); @@ -163,7 +167,15 @@ public IReadOnlyCollection Install(PackageReference package, PackageType private List GetInstalledTools(string toolLocation) { var toolLocationArgument = string.Empty; - if (toolLocation != "global") + if (toolLocation == "global") + { + toolLocationArgument = "--global"; + } + else if (toolLocation == "local") + { + toolLocationArgument = "--local"; + } + else { toolLocationArgument = string.Format("--tool-path \"{0}\"", toolLocation); var toolLocationDirectoryPath = new DirectoryPath(toolLocation).MakeAbsolute(_environment); @@ -177,10 +189,6 @@ private List GetInstalledTools(string toolLocation) return new List(); } } - else - { - toolLocationArgument = "--global"; - } var isInstalledProcess = _processRunner.Start( "dotnet", @@ -196,7 +204,7 @@ private List GetInstalledTools(string toolLocation) var installedTools = isInstalledProcess.GetStandardOutput().ToList(); var installedToolNames = new List(); - const string pattern = @"(?[^\s]+)\s+(?[^\s]+)\s+(?[^`s])"; + const string pattern = @"(?[^\s]+)\s+(?[^\s]+)\s+(?[^\s]+)(?:\s+(?[^\s]+))?"; foreach (var installedTool in installedTools.Skip(2)) { @@ -207,7 +215,8 @@ private List GetInstalledTools(string toolLocation) { Id = match.Groups["packageName"].Value, Version = match.Groups["packageVersion"].Value, - ShortCode = match.Groups["packageShortCode"].Value + ShortCode = match.Groups["packageShortCode"].Value, + Manifest = match.Groups["packageManifest"].Value }); } } @@ -237,7 +246,7 @@ private void RunDotNetTool(PackageReference package, DirectoryPath toolsFolderDi { _log.Warning("dotnet exited with {0}", exitCode); var output = string.Join(Environment.NewLine, process.GetStandardError()); - _log.Verbose(Verbosity.Diagnostic, "Output:\r\n{0}", output); + _log.Verbose(Verbosity.Diagnostic, "Output:{0}{1}", Environment.NewLine, output); } } @@ -257,12 +266,23 @@ private static ProcessArgumentBuilder GetArguments( { arguments.Append("--global"); } + else if (definition.Parameters.ContainsKey("local")) + { + arguments.Append("--local"); + } else { arguments.Append("--tool-path"); arguments.AppendQuoted(toolDirectoryPath.FullPath); } + // Tool manifest + if (definition.Parameters.ContainsKey("tool-manifest")) + { + arguments.Append("--tool-manifest"); + arguments.AppendQuoted(definition.Parameters["tool-manifest"].First()); + } + if (operation != DotNetToolOperation.Uninstall) { if (definition.Address != null)