Skip to content

Commit

Permalink
Add support for dotnet local tools
Browse files Browse the repository at this point in the history
  • Loading branch information
gitfool committed Feb 27, 2021
1 parent 4fc6d8c commit 0214c6f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/Cake.DotNetTool.Module/DotNetToolContentResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public IReadOnlyCollection<IFile> GetFiles(PackageReference package, PackageType
return GetToolFiles(new DirectoryPath(_environment.GetEnvironmentVariable("USERPROFILE")).Combine(".dotnet/tools"), package);
}
}
else if (package.Parameters.ContainsKey("local"))
{
return new List<IFile>(); // TODO: implementation
}
else
{
return GetToolFiles(_config.GetToolPath(_environment.WorkingDirectory, _environment), package);
Expand Down
6 changes: 6 additions & 0 deletions src/Cake.DotNetTool.Module/DotNetToolPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@ public sealed class DotNetToolPackage
/// </summary>
/// <value>The tool package short code.</value>
public string ShortCode { get; set; }

/// <summary>
/// Gets or sets the tool package manifest.
/// </summary>
/// <value>The tool package manifest.</value>
public string Manifest { get; set; }
}
}
36 changes: 28 additions & 8 deletions src/Cake.DotNetTool.Module/DotNetToolPackageInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ public IReadOnlyCollection<IFile> 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);
Expand Down Expand Up @@ -163,7 +167,15 @@ public IReadOnlyCollection<IFile> Install(PackageReference package, PackageType
private List<DotNetToolPackage> 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);
Expand All @@ -177,10 +189,6 @@ private List<DotNetToolPackage> GetInstalledTools(string toolLocation)
return new List<DotNetToolPackage>();
}
}
else
{
toolLocationArgument = "--global";
}

var isInstalledProcess = _processRunner.Start(
"dotnet",
Expand All @@ -196,7 +204,7 @@ private List<DotNetToolPackage> GetInstalledTools(string toolLocation)
var installedTools = isInstalledProcess.GetStandardOutput().ToList();
var installedToolNames = new List<DotNetToolPackage>();

const string pattern = @"(?<packageName>[^\s]+)\s+(?<packageVersion>[^\s]+)\s+(?<packageShortCode>[^`s])";
const string pattern = @"(?<packageName>[^\s]+)\s+(?<packageVersion>[^\s]+)\s+(?<packageShortCode>[^\s]+)(?:\s+(?<packageManifest>[^\s]+))?";

foreach (var installedTool in installedTools.Skip(2))
{
Expand All @@ -207,7 +215,8 @@ private List<DotNetToolPackage> 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
});
}
}
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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)
Expand Down

0 comments on commit 0214c6f

Please sign in to comment.