From a194118179deba5bba8b605452fcee5f21e10470 Mon Sep 17 00:00:00 2001 From: Dave Wyatt Date: Mon, 22 Aug 2016 10:52:09 -0400 Subject: [PATCH 1/3] Join multiple paths in one call --- .../commands/management/CombinePathCommand.cs | 26 ++++++++++++++++--- .../Join-Path.Tests.ps1 | 5 ++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs index ef8a815fdf3..36df718c61d 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs @@ -3,6 +3,7 @@ --********************************************************************/ using System; +using System.Text; using System.Collections.ObjectModel; using System.Management.Automation; using Dbg = System.Management.Automation; @@ -29,10 +30,11 @@ public class JoinPathCommand : CoreCommandWithCredentialsBase /// /// Gets or sets the childPath parameter to the command /// - [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true)] + [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = true)] [AllowNull] [AllowEmptyString] - public string ChildPath { get; set; } = String.Empty; + [AllowEmptyCollection] + public string[] ChildPath { get; set; } = new string[0]; /// /// Determines if the path should be resolved after being joined @@ -55,6 +57,24 @@ protected override void ProcessRecord() Path != null, "Since Path is a mandatory parameter, paths should never be null"); + string combinedChildPath = String.Empty; + + // join the ChildPath elements + if (ChildPath != null) + { + foreach (string childPath in ChildPath) + { + if (String.IsNullOrEmpty(combinedChildPath)) + { + combinedChildPath = childPath; + } + else + { + combinedChildPath = SessionState.Path.Combine(combinedChildPath, childPath, CmdletProviderContext); + } + } + } + foreach (string path in Path) { // First join the path elements @@ -64,7 +84,7 @@ protected override void ProcessRecord() try { joinedPath = - SessionState.Path.Combine(path, ChildPath, CmdletProviderContext); + SessionState.Path.Combine(path, combinedChildPath, CmdletProviderContext); } catch (PSNotSupportedException notSupported) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Join-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Join-Path.Tests.ps1 index 9bd63888dff..d21e03369be 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Join-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Join-Path.Tests.ps1 @@ -43,4 +43,9 @@ Describe "Join-Path cmdlet tests" -Tags "CI" { $result.Count | Should be 1 $result | Should BeExactly ("Env:"+$SepChar+"foo") } + It "should be able to join multiple child paths passed by position with remaining arguments" { + $result = Join-Path one two three four five + $result.Count | Should Be 1 + $result | Should BeExactly "one${sepChar}two${sepChar}three${sepChar}four${sepChar}five" + } } From 203ace04c09dbbc1ac00d6b497849cb69cc919fb Mon Sep 17 00:00:00 2001 From: Dave Wyatt Date: Mon, 22 Aug 2016 11:48:50 -0400 Subject: [PATCH 2/3] Using Utils.EmptyArray for parameter default value --- .../commands/management/CombinePathCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs index 36df718c61d..97134f04376 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs @@ -34,7 +34,7 @@ public class JoinPathCommand : CoreCommandWithCredentialsBase [AllowNull] [AllowEmptyString] [AllowEmptyCollection] - public string[] ChildPath { get; set; } = new string[0]; + public string[] ChildPath { get; set; } = Utils.EmptyArray(); /// /// Determines if the path should be resolved after being joined From e24f3fa12ae193a101d06478966218ccd480d7d3 Mon Sep 17 00:00:00 2001 From: Dave Wyatt Date: Sat, 27 Aug 2016 02:26:43 -0400 Subject: [PATCH 3/3] AdditionalChildPath parameter on Join-Path --- .../commands/management/CombinePathCommand.cs | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs index 97134f04376..3ed263eaed5 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs @@ -30,11 +30,20 @@ public class JoinPathCommand : CoreCommandWithCredentialsBase /// /// Gets or sets the childPath parameter to the command /// - [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = true)] + [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true)] + [AllowNull] + [AllowEmptyString] + public string ChildPath { get; set; } + + /// + /// Gets or sets additional childPaths to the command. + /// + + [Parameter(Position = 2, Mandatory = false, ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = true)] [AllowNull] [AllowEmptyString] [AllowEmptyCollection] - public string[] ChildPath { get; set; } = Utils.EmptyArray(); + public string[] AdditionalChildPath { get; set; } = Utils.EmptyArray(); /// /// Determines if the path should be resolved after being joined @@ -57,21 +66,14 @@ protected override void ProcessRecord() Path != null, "Since Path is a mandatory parameter, paths should never be null"); - string combinedChildPath = String.Empty; + string combinedChildPath = ChildPath; // join the ChildPath elements - if (ChildPath != null) + if (AdditionalChildPath != null) { - foreach (string childPath in ChildPath) + foreach (string childPath in AdditionalChildPath) { - if (String.IsNullOrEmpty(combinedChildPath)) - { - combinedChildPath = childPath; - } - else - { - combinedChildPath = SessionState.Path.Combine(combinedChildPath, childPath, CmdletProviderContext); - } + combinedChildPath = SessionState.Path.Combine(combinedChildPath, childPath, CmdletProviderContext); } }