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

Commit

Permalink
Merge branch 'release/0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
gep13 committed Nov 25, 2019
2 parents 5f94612 + 96b62a6 commit f879b32
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Cake.DotNetTool.Module is a Module for Cake, which extends it with a new IPackag

## Chat Room

Come join in the conversation about Cake.DotNetTool.Module in our Gitter Chat Room
Come join in the conversation about Cake.DotNetTool.Module in our Gitter Chat Room.

[![Join the chat at https://gitter.im/cake-contrib/Lobby](https://badges.gitter.im/cake-contrib/Lobby.svg)](https://gitter.im/cake-contrib/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Expand Down
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; }
}
}
163 changes: 103 additions & 60 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,61 +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("--config-file");
arguments.AppendQuoted(definition.Parameters["configfile"].First());
}
// Config File
if(definition.Parameters.ContainsKey("configfile"))
{
arguments.Append("--configfile");
arguments.AppendQuoted(definition.Parameters["configfile"].First());
}

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

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:
// 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;
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
10 changes: 10 additions & 0 deletions docs/input/docs/usage/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ If you need to specify a NuGet config file, for example you need to authenticate
#tool dotnet:?package=Octopus.DotNet.Cli&version=4.41.0&configfile="../../NuGet.config"
```

# Ignore failed sources

There might be cases where you want to ignore failed NuGet sources as long as the package could be restored.

## Example

```
#tool dotnet:?package=Octopus.DotNet.Cli&version=4.41.0&ignore-failed-sources"
```

# Framework

Specifies the [target framework](https://docs.microsoft.com/en-us/dotnet/standard/frameworks) to install the tool for. By default, the .NET Core SDK tries to choose the most appropriate target framework.
Expand Down

0 comments on commit f879b32

Please sign in to comment.