Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

WIP: GH18: Add support for dotnet local tools #19

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 6 additions & 2 deletions Source/Cake.DotNetTool.Module/DotNetToolContentResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public IReadOnlyCollection<IFile> GetFiles(PackageReference package, PackageType

if (type == PackageType.Tool)
{
if(package.Parameters.ContainsKey("global"))
if (package.Parameters.ContainsKey("global"))
{
if(_environment.Platform.IsUnix())
if (_environment.Platform.IsUnix())
{
return GetToolFiles(new DirectoryPath(_environment.GetEnvironmentVariable("HOME")).Combine(".dotnet/tools"), package);
}
Expand All @@ -69,6 +69,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
Copy link
Contributor Author

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.

}
else
{
return GetToolFiles(_config.GetToolPath(_environment.WorkingDirectory, _environment), package);
Expand Down
1 change: 1 addition & 0 deletions Source/Cake.DotNetTool.Module/DotNetToolPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public sealed class DotNetToolPackage
public string Id { get; set; }
public string Version { get; set; }
public string ShortCode { get; set; }
public string Manifest { get; set; }
}
}
50 changes: 35 additions & 15 deletions Source/Cake.DotNetTool.Module/DotNetToolPackageInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ public IReadOnlyCollection<IFile> Install(PackageReference package, PackageType
_log.Debug("Configured Tools Folder: {0}", toolsFolderDirectoryPath);

var toolLocation = toolsFolderDirectoryPath.FullPath;
if(package.Parameters.ContainsKey("global"))
if (package.Parameters.ContainsKey("global"))
{
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 @@ -159,24 +163,28 @@ 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);
var toolLocationDirectory = _fileSystem.GetDirectory(toolLocationDirectoryPath);

// If the requested tools path doesn't exist, then there can't be any tools
// installed there, so simply return an empty list.
if(!toolLocationDirectory.Exists)
if (!toolLocationDirectory.Exists)
{
_log.Debug("Specified installation location doesn't currently exist.");
return new List<DotNetToolPackage>();
}
}
else
{
toolLocationArgument = "--global";
}

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

string pattern = @"(?<packageName>[^\s]+)\s+(?<packageVersion>[^\s]+)\s+(?<packageShortCode>[^`s])";
Copy link
Contributor Author

@gitfool gitfool Feb 24, 2020

Choose a reason for hiding this comment

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

This regex had a bug; `s should be \s for non-whitespace match.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just noticed, it also didn't match more than one character. I guess it's not used.

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.


foreach(var installedTool in installedTools.Skip(2))
foreach (var installedTool in installedTools.Skip(2))
{
foreach (Match match in Regex.Matches(installedTool, pattern, RegexOptions.IgnoreCase))
{
Expand All @@ -201,7 +209,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 @@ -229,7 +238,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 @@ -245,16 +254,27 @@ private static ProcessArgumentBuilder GetArguments(
arguments.Append(Enum.GetName(typeof(DotNetToolOperation), operation).ToLowerInvariant());
arguments.AppendQuoted(definition.Package);

if(definition.Parameters.ContainsKey("global"))
if (definition.Parameters.ContainsKey("global"))
{
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 All @@ -271,14 +291,14 @@ private static ProcessArgumentBuilder GetArguments(
}

// Config File
if(definition.Parameters.ContainsKey("configfile"))
if (definition.Parameters.ContainsKey("configfile"))
{
arguments.Append("--configfile");
arguments.AppendQuoted(definition.Parameters["configfile"].First());
}

// Whether to ignore failed sources
if(definition.Parameters.ContainsKey("ignore-failed-sources"))
if (definition.Parameters.ContainsKey("ignore-failed-sources"))
{
arguments.Append("--ignore-failed-sources");
}
Expand All @@ -290,7 +310,7 @@ private static ProcessArgumentBuilder GetArguments(
arguments.Append(definition.Parameters["framework"].First());
}

switch(log.Verbosity)
switch (log.Verbosity)
{
case Verbosity.Quiet:
arguments.Append("--verbosity");
Expand Down