Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH3209: Add support for dotnet local tools #3212

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

@gitfool gitfool Feb 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what, if anything, can be done here, since dotnet local tools are not put in the user subdirectory like dotnet global tools.

Only idea is they must be in the NuGet cache, but how to get the correct path?

Failing that, would need to revisit how tools work and allow them to be managed behind the dotnet CLI.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good question, getting and executing tools from global NuGet cache is possible quick PoC gist here
https://gist.github.com/devlead/559d9b082c9cd81c5c5d52f94d68380c

Don't know if that helps, but potentially one could return entry points 🤔

}
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]+))?";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optionally match the package manifest, when available, for local tools.

This regex had a bug; `s should be \s for non-whitespace match and it also didn't match more than one character.


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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done no testing so far, but expecting this to be empty and not throw when not matched.

});
}
}
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was another minor bug here. Changed to use Environment.NewLine when composing output message.

}
}

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