Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH952: Refactor DotNetCore args string->ArgumentBuilder #953

Merged
merged 1 commit into from
Jun 6, 2016
Merged
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
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.
Copy link
Member

Choose a reason for hiding this comment

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

Should be indicates.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unsure what you mean :)
image

Copy link
Member

Choose a reason for hiding this comment

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

Did I just have a stroke? I could have sworn it said indiates when I read it.

/// </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);
}
}
}
}
24 changes: 22 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,15 @@ public static ProcessArgumentBuilder FromString(string value)
builder.Append(new TextArgument(value));
return builder;
}

IEnumerator<IProcessArgument> IEnumerable<IProcessArgument>.GetEnumerator()
Copy link
Member

Choose a reason for hiding this comment

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

Should be Returns an enumerator that iterates through the collection.

{
return _tokens.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
Copy link
Member

Choose a reason for hiding this comment

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

No comment necessary for explicit implementation.

{
return ((IEnumerable)_tokens).GetEnumerator();
}
}
}