Skip to content

Commit

Permalink
Refactor DotNetCore args string->ArgumentBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
devlead committed Jun 3, 2016
1 parent 142516e commit d85242b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 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
10 changes: 10 additions & 0 deletions src/Cake.Core/Extensions/ProcessArgumentListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,5 +380,15 @@ 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><seealso cref="bool"/></returns>
public static bool IsNullOrEmpty(this ProcessArgumentBuilder builder)
{
return builder == null || builder.IsEmpty();
}
}
}
29 changes: 28 additions & 1 deletion src/Cake.Core/IO/ProcessArgumentBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cake.Core.IO.Arguments;
Expand Down Expand Up @@ -82,6 +83,32 @@ public string FilterUnsafe(string source)
sb => sb.ToString());
}

/// <summary>
/// Copies all the arhuments of the current <see cref="ProcessArgumentBuilder"/> to target <see cref="ProcessArgumentBuilder"/>.
/// </summary>
/// <param name="target">The copy target.</param>
public void CopyTo(ProcessArgumentBuilder target)
{
if (target == null)
{
throw new ArgumentNullException("target");
}

foreach (var token in _tokens)
{
target.Append(token);
}
}

/// <summary>
/// Indicating whether current <see cref="ProcessArgumentBuilder"/> renders empty.
/// </summary>
/// <returns>A <see cref="bool"/>.</returns>
public bool IsEmpty()
{
return _tokens.Count == 0 || string.IsNullOrEmpty(Render());
}

/// <summary>
/// Performs an implicit conversion from <see cref="System.String"/> to <see cref="ProcessArgumentBuilder"/>.
/// </summary>
Expand Down

0 comments on commit d85242b

Please sign in to comment.