Skip to content

Commit

Permalink
Refactor DotNetCore args string->ArgumentBuilder
Browse files Browse the repository at this point in the history
Fixes #952
  • Loading branch information
devlead committed Jun 6, 2016
1 parent 4ae1484 commit bd318b4
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public void Should_Add_Path_Arguments()
// Given
var fixture = new DotNetCoreRunnerFixture();
fixture.Project = "./tools/tool/";
fixture.Arguments = "--args";
fixture.Arguments = "--args=\"value\"";
// When
var result = fixture.Run();

// Then
Assert.Equal("run --project \"./tools/tool/\" -- \"--args\"", result.Args);
Assert.Equal("run --project \"./tools/tool/\" -- --args=\"value\"", result.Args);
}

[Fact]
Expand Down
8 changes: 4 additions & 4 deletions src/Cake.Common/Tools/DotNetCore/DotNetCoreAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static void DotNetCoreExecute(this ICakeContext context, FilePath assembl
[CakeMethodAlias]
[CakeAliasCategory("Execute")]
[CakeNamespaceImport("Cake.Common.Tools.DotNetCore.Execute")]
public static void DotNetCoreExecute(this ICakeContext context, FilePath assemblyPath, string arguments)
public static void DotNetCoreExecute(this ICakeContext context, FilePath assemblyPath, ProcessArgumentBuilder arguments)
{
context.DotNetCoreExecute(assemblyPath, arguments, null);
}
Expand All @@ -80,7 +80,7 @@ public static void DotNetCoreExecute(this ICakeContext context, FilePath assembl
[CakeMethodAlias]
[CakeAliasCategory("Execute")]
[CakeNamespaceImport("Cake.Common.Tools.DotNetCore.Execute")]
public static void DotNetCoreExecute(this ICakeContext context, FilePath assemblyPath, string arguments, DotNetCoreSettings settings)
public static void DotNetCoreExecute(this ICakeContext context, FilePath assemblyPath, ProcessArgumentBuilder arguments, DotNetCoreSettings settings)
{
if (context == null)
{
Expand Down Expand Up @@ -363,7 +363,7 @@ public static void DotNetCoreRun(this ICakeContext context, string project)
[CakeMethodAlias]
[CakeAliasCategory("Run")]
[CakeNamespaceImport("Cake.Common.Tools.DotNetCore.Run")]
public static void DotNetCoreRun(this ICakeContext context, string project, string arguments)
public static void DotNetCoreRun(this ICakeContext context, string project, ProcessArgumentBuilder arguments)
{
context.DotNetCoreRun(project, arguments, null);
}
Expand All @@ -389,7 +389,7 @@ public static void DotNetCoreRun(this ICakeContext context, string project, stri
[CakeMethodAlias]
[CakeAliasCategory("Run")]
[CakeNamespaceImport("Cake.Common.Tools.DotNetCore.Run")]
public static void DotNetCoreRun(this ICakeContext context, string project, string arguments, DotNetCoreRunSettings settings)
public static void DotNetCoreRun(this ICakeContext context, string project, ProcessArgumentBuilder arguments, DotNetCoreRunSettings settings)
{
if (context == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public DotNetCoreExecutor(
/// <param name="assemblyPath">The assembly path.</param>
/// <param name="arguments">The arguments.</param>
/// <param name="settings">The settings.</param>
public void Execute(FilePath assemblyPath, string arguments, DotNetCoreSettings settings)
public void Execute(FilePath assemblyPath, ProcessArgumentBuilder arguments, DotNetCoreSettings settings)
{
if (assemblyPath == null)
{
Expand All @@ -48,16 +48,16 @@ public void Execute(FilePath assemblyPath, string arguments, DotNetCoreSettings
Run(settings, GetArguments(assemblyPath, arguments, settings));
}

private ProcessArgumentBuilder GetArguments(FilePath assemblyPath, string arguments, DotNetCoreSettings settings)
private ProcessArgumentBuilder GetArguments(FilePath assemblyPath, ProcessArgumentBuilder arguments, DotNetCoreSettings settings)
{
var builder = CreateArgumentBuilder(settings);

assemblyPath = assemblyPath.IsRelative ? assemblyPath.MakeAbsolute(_environment) : assemblyPath;
builder.Append(assemblyPath.FullPath);

if (!string.IsNullOrEmpty(arguments))
if (!arguments.IsNullOrEmpty())
{
builder.Append(arguments);
arguments.CopyTo(builder);
}

return builder;
Expand Down
9 changes: 5 additions & 4 deletions src/Cake.Common/Tools/DotNetCore/Run/DotNetCoreRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public DotNetCoreRunner(
/// <param name="project">The target project path.</param>
/// <param name="arguments">The arguments.</param>
/// <param name="settings">The settings.</param>
public void Run(string project, string arguments, DotNetCoreRunSettings settings)
public void Run(string project, ProcessArgumentBuilder arguments, DotNetCoreRunSettings settings)
{
if (settings == null)
{
Expand All @@ -41,7 +41,7 @@ public void Run(string project, string arguments, DotNetCoreRunSettings settings
Run(settings, GetArguments(project, arguments, settings));
}

private ProcessArgumentBuilder GetArguments(string project, string arguments, DotNetCoreRunSettings settings)
private ProcessArgumentBuilder GetArguments(string project, ProcessArgumentBuilder arguments, DotNetCoreRunSettings settings)
{
var builder = CreateArgumentBuilder(settings);

Expand All @@ -68,10 +68,11 @@ private ProcessArgumentBuilder GetArguments(string project, string arguments, Do
builder.Append(settings.Configuration);
}

if (!string.IsNullOrEmpty(arguments))
// Arguments
if (!arguments.IsNullOrEmpty())
{
builder.Append("--");
builder.AppendQuoted(arguments);
arguments.CopyTo(builder);
}

return builder;
Expand Down
34 changes: 34 additions & 0 deletions src/Cake.Core/Extensions/ProcessArgumentListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,5 +380,39 @@ public static ProcessArgumentBuilder AppendQuotedSecret(this ProcessArgumentBuil
}
return builder;
}

/// <summary>
/// Indicates whether a <see cref="ProcessArgumentBuilder"/> is null or renders empty.
/// </summary>
/// <param name="builder">The builder.</param>
/// <returns><c>true</c> if <paramref name="builder"/> refers to a null or empty <see cref="ProcessArgumentBuilder"/>;
/// <c>false</c> if the <paramref name="builder"/>refers to non null or empty <see cref="ProcessArgumentBuilder"/></returns>
public static bool IsNullOrEmpty(this ProcessArgumentBuilder builder)
{
return builder == null || builder.Count == 0 || string.IsNullOrEmpty(builder.Render());
}

/// <summary>
/// Copies all the arguments of the source <see cref="ProcessArgumentBuilder"/> to target <see cref="ProcessArgumentBuilder"/>.
/// </summary>
/// <param name="source">The argument builder to copy from..</param>
/// <param name="target">The argument builder to copy to.</param>
public static void CopyTo(this ProcessArgumentBuilder source, ProcessArgumentBuilder target)
{
if (source == null)
{
throw new ArgumentNullException("source");
}

if (target == null)
{
throw new ArgumentNullException("target");
}

foreach (var token in source)
{
target.Append(token);
}
}
}
}
32 changes: 30 additions & 2 deletions src/Cake.Core/IO/ProcessArgumentBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cake.Core.IO.Arguments;
Expand All @@ -8,10 +10,18 @@ namespace Cake.Core.IO
/// <summary>
/// Utility for building process arguments.
/// </summary>
public sealed class ProcessArgumentBuilder
public sealed class ProcessArgumentBuilder : IReadOnlyCollection<IProcessArgument>
{
private readonly List<IProcessArgument> _tokens;

/// <summary>
/// Gets the number of arguments contained in the <see cref="ProcessArgumentBuilder"/>.
/// </summary>
public int Count
{
get { return _tokens.Count; }
}

/// <summary>
/// Initializes a new instance of the <see cref="ProcessArgumentBuilder"/> class.
/// </summary>
Expand Down Expand Up @@ -103,5 +113,23 @@ public static ProcessArgumentBuilder FromString(string value)
builder.Append(new TextArgument(value));
return builder;
}

/// <summary>
/// Returns an typed enumerator that iterates thru the collection.
/// </summary>
/// <returns><see cref="IEnumerator{IProcessArgument}"/></returns>
IEnumerator<IProcessArgument> IEnumerable<IProcessArgument>.GetEnumerator()
{
return _tokens.GetEnumerator();
}

/// <summary>
/// Returns an enumerator that iterates thru the collection.
/// </summary>
/// <returns><see cref="IEnumerator"/></returns>
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_tokens).GetEnumerator();
}
}
}

0 comments on commit bd318b4

Please sign in to comment.