From f2abeeba0026c14a8a64fcbf6456759c02dc0269 Mon Sep 17 00:00:00 2001 From: Mattias Karlsson Date: Fri, 3 Jun 2016 22:46:47 +0200 Subject: [PATCH] Refactor DotNetCore args string->ArgumentBuilder Fixes #952 --- .../DotNetCore/Run/DotNetCoreRunnerTests.cs | 4 +-- .../Tools/DotNetCore/DotNetCoreAliases.cs | 8 ++--- .../DotNetCore/Execute/DotNetCoreExecutor.cs | 8 ++--- .../Tools/DotNetCore/Run/DotNetCoreRunner.cs | 9 ++--- .../ProcessArgumentListExtensions.cs | 34 +++++++++++++++++++ src/Cake.Core/IO/ProcessArgumentBuilder.cs | 24 +++++++++++-- 6 files changed, 71 insertions(+), 16 deletions(-) diff --git a/src/Cake.Common.Tests/Unit/Tools/DotNetCore/Run/DotNetCoreRunnerTests.cs b/src/Cake.Common.Tests/Unit/Tools/DotNetCore/Run/DotNetCoreRunnerTests.cs index be93b54247..7d9cc3da70 100644 --- a/src/Cake.Common.Tests/Unit/Tools/DotNetCore/Run/DotNetCoreRunnerTests.cs +++ b/src/Cake.Common.Tests/Unit/Tools/DotNetCore/Run/DotNetCoreRunnerTests.cs @@ -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] diff --git a/src/Cake.Common/Tools/DotNetCore/DotNetCoreAliases.cs b/src/Cake.Common/Tools/DotNetCore/DotNetCoreAliases.cs index 83418801c1..f22a7bf9eb 100644 --- a/src/Cake.Common/Tools/DotNetCore/DotNetCoreAliases.cs +++ b/src/Cake.Common/Tools/DotNetCore/DotNetCoreAliases.cs @@ -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); } @@ -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) { @@ -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); } @@ -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) { diff --git a/src/Cake.Common/Tools/DotNetCore/Execute/DotNetCoreExecutor.cs b/src/Cake.Common/Tools/DotNetCore/Execute/DotNetCoreExecutor.cs index 0f10631814..2e65b154a0 100644 --- a/src/Cake.Common/Tools/DotNetCore/Execute/DotNetCoreExecutor.cs +++ b/src/Cake.Common/Tools/DotNetCore/Execute/DotNetCoreExecutor.cs @@ -34,7 +34,7 @@ public DotNetCoreExecutor( /// The assembly path. /// The arguments. /// The settings. - public void Execute(FilePath assemblyPath, string arguments, DotNetCoreSettings settings) + public void Execute(FilePath assemblyPath, ProcessArgumentBuilder arguments, DotNetCoreSettings settings) { if (assemblyPath == null) { @@ -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; diff --git a/src/Cake.Common/Tools/DotNetCore/Run/DotNetCoreRunner.cs b/src/Cake.Common/Tools/DotNetCore/Run/DotNetCoreRunner.cs index 78718d233b..f5a5d30415 100644 --- a/src/Cake.Common/Tools/DotNetCore/Run/DotNetCoreRunner.cs +++ b/src/Cake.Common/Tools/DotNetCore/Run/DotNetCoreRunner.cs @@ -31,7 +31,7 @@ public DotNetCoreRunner( /// The target project path. /// The arguments. /// The settings. - public void Run(string project, string arguments, DotNetCoreRunSettings settings) + public void Run(string project, ProcessArgumentBuilder arguments, DotNetCoreRunSettings settings) { if (settings == null) { @@ -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); @@ -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; diff --git a/src/Cake.Core/Extensions/ProcessArgumentListExtensions.cs b/src/Cake.Core/Extensions/ProcessArgumentListExtensions.cs index 0daae556e8..e1ecf7a06b 100644 --- a/src/Cake.Core/Extensions/ProcessArgumentListExtensions.cs +++ b/src/Cake.Core/Extensions/ProcessArgumentListExtensions.cs @@ -380,5 +380,39 @@ public static ProcessArgumentBuilder AppendQuotedSecret(this ProcessArgumentBuil } return builder; } + + /// + /// Indicates whether a is null or renders empty. + /// + /// The builder. + /// true if refers to a null or empty ; + /// false if the refers to non null or empty + public static bool IsNullOrEmpty(this ProcessArgumentBuilder builder) + { + return builder == null || builder.Count == 0 || string.IsNullOrEmpty(builder.Render()); + } + + /// + /// Copies all the arguments of the source to target . + /// + /// The argument builder to copy from.. + /// The argument builder to copy to. + 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); + } + } } } diff --git a/src/Cake.Core/IO/ProcessArgumentBuilder.cs b/src/Cake.Core/IO/ProcessArgumentBuilder.cs index 0143045a89..b9b494de94 100644 --- a/src/Cake.Core/IO/ProcessArgumentBuilder.cs +++ b/src/Cake.Core/IO/ProcessArgumentBuilder.cs @@ -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; @@ -8,10 +10,18 @@ namespace Cake.Core.IO /// /// Utility for building process arguments. /// - public sealed class ProcessArgumentBuilder + public sealed class ProcessArgumentBuilder : IReadOnlyCollection { private readonly List _tokens; + /// + /// Gets the number of arguments contained in the . + /// + public int Count + { + get { return _tokens.Count; } + } + /// /// Initializes a new instance of the class. /// @@ -103,5 +113,15 @@ public static ProcessArgumentBuilder FromString(string value) builder.Append(new TextArgument(value)); return builder; } + + IEnumerator IEnumerable.GetEnumerator() + { + return _tokens.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return ((IEnumerable)_tokens).GetEnumerator(); + } } } \ No newline at end of file