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

Commit

Permalink
(GH-2) Allow changing of requested tool version
Browse files Browse the repository at this point in the history
Previously, the Module wouldn't check to see what version of a Tool was
installed.  As a result, when a tool was already installed, even if a
newer version was requested, it wouldn't be installed.  This is really
down to the underlying tool and how it works, but smarts have now been
put into the module, to check installed version, against what is
requested, so that an uninstall is first executed, and then the requested
version is installed.
  • Loading branch information
gep13 committed Nov 25, 2019
1 parent 728e0da commit 96b62a6
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 65 deletions.
9 changes: 9 additions & 0 deletions Source/Cake.DotNetTool.Module/DotNetToolOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Cake.DotNetTool.Module
{
public enum DotNetToolOperation
{
Install,
Uninstall,
Update
}
}
9 changes: 9 additions & 0 deletions Source/Cake.DotNetTool.Module/DotNetToolPackage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Cake.DotNetTool.Module
{
public sealed class DotNetToolPackage
{
public string Id { get; set; }
public string Version { get; set; }
public string ShortCode { get; set; }
}
}
167 changes: 102 additions & 65 deletions Source/Cake.DotNetTool.Module/DotNetToolPackageInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,42 @@ public IReadOnlyCollection<IFile> Install(PackageReference package, PackageType
}

// First we need to check if the Tool is already installed
var installedToolNames = GetInstalledTools(toolLocation);
var installedTools = GetInstalledTools(toolLocation);

_log.Debug("Checking for tool: {0}", package.Package.ToLowerInvariant());
if(installedToolNames.Contains(package.Package.ToLowerInvariant()))

var installedTool = installedTools.FirstOrDefault(t => t.Id.ToLowerInvariant() == package.Package.ToLowerInvariant());

if (installedTool != null)
{
_log.Information("Tool {0} is already installed, so nothing to do here.", package.Package);
// The tool is already installed, so need to check if requested version is the same as
// what is already installed
string requestedVersion = null;

if (package.Parameters.ContainsKey("version"))
{
requestedVersion = package.Parameters["version"].First();
}

if (requestedVersion == null)
{
_log.Warning("Tool {0} is already installed, and no specific version has been requested via pre-processor directive, so leaving current version installed.", package.Package);
}
else if (requestedVersion.ToLowerInvariant() != installedTool.Version.ToLowerInvariant())
{
_log.Warning("Tool {0} is already installed, but a different version has been requested. Uninstall/install will now be performed...", package.Package);
RunDotNetTool(package, toolsFolderDirectoryPath, DotNetToolOperation.Uninstall);
RunDotNetTool(package, toolsFolderDirectoryPath, DotNetToolOperation.Install);
}
else
{
_log.Information("Tool {0} is already installed, with required version.", package.Package);
}
}
else
{
InstallTool(package, toolsFolderDirectoryPath);
// The tool isn't already installed, go ahead and install it
RunDotNetTool(package, toolsFolderDirectoryPath, DotNetToolOperation.Install);
}

var result = _contentResolver.GetFiles(package, type);
Expand All @@ -130,7 +156,7 @@ public IReadOnlyCollection<IFile> Install(PackageReference package, PackageType
return result;
}

private List<string> GetInstalledTools(string toolLocation)
private List<DotNetToolPackage> GetInstalledTools(string toolLocation)
{
var toolLocationArgument = string.Empty;
if(toolLocation != "global")
Expand All @@ -144,7 +170,7 @@ private List<string> GetInstalledTools(string toolLocation)
if(!toolLocationDirectory.Exists)
{
_log.Debug("Specified installation location doesn't currently exist.");
return new List<string>();
return new List<DotNetToolPackage>();
}
}
else
Expand All @@ -162,7 +188,7 @@ private List<string> GetInstalledTools(string toolLocation)
isInstalledProcess.WaitForExit();

var installedTools = isInstalledProcess.GetStandardOutput().ToList();
var installedToolNames = new List<string>();
var installedToolNames = new List<DotNetToolPackage>();

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

Expand All @@ -171,22 +197,27 @@ private List<string> GetInstalledTools(string toolLocation)
foreach (Match match in Regex.Matches(installedTool, pattern, RegexOptions.IgnoreCase))
{
_log.Debug("Adding tool {0}", match.Groups["packageName"].Value);
installedToolNames.Add(match.Groups["packageName"].Value);
installedToolNames.Add(new DotNetToolPackage
{
Id = match.Groups["packageName"].Value,
Version = match.Groups["packageVersion"].Value,
ShortCode = match.Groups["packageShortCode"].Value
});
}
}

_log.Debug("There are {0} dotnet tools installed", installedToolNames.Count);
return installedToolNames;
}

private void InstallTool(PackageReference package, DirectoryPath toolsFolderDirectoryPath)
private void RunDotNetTool(PackageReference package, DirectoryPath toolsFolderDirectoryPath, DotNetToolOperation operation)
{
// Install the tool....
_log.Debug("Installing dotnet tool: {0}...", package.Package);
_log.Debug("Running dotnet tool with operation {0}: {1}...", operation, package.Package);
var process = _processRunner.Start(
"dotnet",
new ProcessSettings {
Arguments = GetArguments(package, _log, toolsFolderDirectoryPath),
Arguments = GetArguments(package, operation, _log, toolsFolderDirectoryPath),
RedirectStandardOutput = true,
Silent = _log.Verbosity < Verbosity.Diagnostic,
NoWorkingDirectory = true });
Expand All @@ -201,14 +232,17 @@ private void InstallTool(PackageReference package, DirectoryPath toolsFolderDire
_log.Verbose(Verbosity.Diagnostic, "Output:\r\n{0}", output);
}
}

private static ProcessArgumentBuilder GetArguments(
PackageReference definition,
DotNetToolOperation operation,
ICakeLog log,
DirectoryPath toolDirectoryPath)
{
var arguments = new ProcessArgumentBuilder();

arguments.Append("tool install");
arguments.Append("tool");
arguments.Append(Enum.GetName(typeof(DotNetToolOperation), operation).ToLowerInvariant());
arguments.AppendQuoted(definition.Package);

if(definition.Parameters.ContainsKey("global"))
Expand All @@ -221,67 +255,70 @@ private static ProcessArgumentBuilder GetArguments(
arguments.AppendQuoted(toolDirectoryPath.FullPath);
}

if (definition.Address != null)
if (operation != DotNetToolOperation.Uninstall)
{
arguments.Append("--add-source");
arguments.AppendQuoted(definition.Address.AbsoluteUri);
}
if (definition.Address != null)
{
arguments.Append("--add-source");
arguments.AppendQuoted(definition.Address.AbsoluteUri);
}

// Version
if (definition.Parameters.ContainsKey("version"))
{
arguments.Append("--version");
arguments.Append(definition.Parameters["version"].First());
}
// Version
if (definition.Parameters.ContainsKey("version"))
{
arguments.Append("--version");
arguments.Append(definition.Parameters["version"].First());
}

// Config File
if(definition.Parameters.ContainsKey("configfile"))
{
arguments.Append("--configfile");
arguments.AppendQuoted(definition.Parameters["configfile"].First());
}
// Config File
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"))
{
arguments.Append("--ignore-failed-sources");
}
// Whether to ignore failed sources
if(definition.Parameters.ContainsKey("ignore-failed-sources"))
{
arguments.Append("--ignore-failed-sources");
}

// Framework
if (definition.Parameters.ContainsKey("framework"))
{
arguments.Append("--framework");
arguments.Append(definition.Parameters["framework"].First());
}
// Framework
if (definition.Parameters.ContainsKey("framework"))
{
arguments.Append("--framework");
arguments.Append(definition.Parameters["framework"].First());
}

switch(log.Verbosity)
{
case Verbosity.Quiet:
arguments.Append("--verbosity");
arguments.Append("quiet");
break;
case Verbosity.Minimal:
arguments.Append("--verbosity");
arguments.Append("minimal");
break;
case Verbosity.Normal:
arguments.Append("--verbosity");
arguments.Append("normal");
break;
case Verbosity.Verbose:
arguments.Append("--verbosity");
arguments.Append("detailed");
break;
case Verbosity.Diagnostic:
arguments.Append("--verbosity");
arguments.Append("diagnostic");
break;
default:
switch(log.Verbosity)
{
case Verbosity.Quiet:
arguments.Append("--verbosity");
arguments.Append("quiet");
break;
case Verbosity.Minimal:
arguments.Append("--verbosity");
arguments.Append("minimal");
break;
case Verbosity.Normal:
arguments.Append("--verbosity");
arguments.Append("normal");
break;
arguments.Append("normal");
break;
case Verbosity.Verbose:
arguments.Append("--verbosity");
arguments.Append("detailed");
break;
case Verbosity.Diagnostic:
arguments.Append("--verbosity");
arguments.Append("diagnostic");
break;
default:
arguments.Append("--verbosity");
arguments.Append("normal");
break;
}
}

return arguments;
}
}
Expand Down

0 comments on commit 96b62a6

Please sign in to comment.