Adding -Extension and -LeafBase switches to Split-Path (#2721)
* Refactoring ParsePathCommand.cs (SplitPathCommand) for readability - Using auto properties when no when there is no logic in getter/setter - Removing unused code - Removing redundant qualifiers - Removing Redundant initializers * Add -Extension and -Leafbase switches to Split-Path cmdlet - Extension and LeafBase are specializations of Leaf, and uses System.IO.Path.GetExtension and System.IO.Path.GetFilenameWithoutExtension to extract parts from the Leaf * Adding tests for Split-Path -LeafBase and Split-Path -Extension
This commit is contained in:
parent
63ba98fd25
commit
220eaa1ba7
2 changed files with 83 additions and 140 deletions
|
@ -16,12 +16,14 @@ namespace Microsoft.PowerShell.Commands
|
||||||
/// MSH paths that match the glob strings.
|
/// MSH paths that match the glob strings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Cmdlet(VerbsCommon.Split, "Path", DefaultParameterSetName = "ParentSet", SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113404")]
|
[Cmdlet(VerbsCommon.Split, "Path", DefaultParameterSetName = "ParentSet", SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113404")]
|
||||||
[OutputType(typeof(string), ParameterSetName = new string[] { SplitPathCommand.leafSet,
|
[OutputType(typeof(string), ParameterSetName = new[] { leafSet,
|
||||||
SplitPathCommand.noQualifierSet,
|
leafBaseSet,
|
||||||
SplitPathCommand.parentSet,
|
extensionSet,
|
||||||
SplitPathCommand.qualifierSet,
|
noQualifierSet,
|
||||||
SplitPathCommand.literalPathSet})]
|
parentSet,
|
||||||
[OutputType(typeof(bool), ParameterSetName = new string[] { SplitPathCommand.isAbsoluteSet })]
|
qualifierSet,
|
||||||
|
literalPathSet})]
|
||||||
|
[OutputType(typeof(bool), ParameterSetName = new[] { isAbsoluteSet })]
|
||||||
public class SplitPathCommand : CoreCommandWithCredentialsBase
|
public class SplitPathCommand : CoreCommandWithCredentialsBase
|
||||||
{
|
{
|
||||||
#region Parameters
|
#region Parameters
|
||||||
|
@ -36,6 +38,17 @@ namespace Microsoft.PowerShell.Commands
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private const string leafSet = "LeafSet";
|
private const string leafSet = "LeafSet";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The parameter set name to get the leaf base name
|
||||||
|
/// </summary>
|
||||||
|
private const string leafBaseSet = "LeafBaseSet";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The parameter set name to get the extension
|
||||||
|
/// </summary>
|
||||||
|
private const string extensionSet = "ExtensionSet";
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The parameter set name to get the qualifier set.
|
/// The parameter set name to get the qualifier set.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -61,21 +74,12 @@ namespace Microsoft.PowerShell.Commands
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Parameter(Position = 0, ParameterSetName = parentSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
[Parameter(Position = 0, ParameterSetName = parentSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
||||||
[Parameter(Position = 0, ParameterSetName = leafSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
[Parameter(Position = 0, ParameterSetName = leafSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
||||||
|
[Parameter(Position = 0, ParameterSetName = leafBaseSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
||||||
|
[Parameter(Position = 0, ParameterSetName = extensionSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
||||||
[Parameter(Position = 0, ParameterSetName = qualifierSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
[Parameter(Position = 0, ParameterSetName = qualifierSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
||||||
[Parameter(Position = 0, ParameterSetName = noQualifierSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
[Parameter(Position = 0, ParameterSetName = noQualifierSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
||||||
[Parameter(Position = 0, ParameterSetName = isAbsoluteSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
[Parameter(Position = 0, ParameterSetName = isAbsoluteSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
||||||
public string[] Path
|
public string[] Path { get; set; }
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return _paths;
|
|
||||||
} // get
|
|
||||||
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_paths = value;
|
|
||||||
} // set
|
|
||||||
} // Path
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the literal path parameter to the command
|
/// Gets or sets the literal path parameter to the command
|
||||||
|
@ -86,13 +90,13 @@ namespace Microsoft.PowerShell.Commands
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return _paths;
|
return Path;
|
||||||
} // get
|
} // get
|
||||||
|
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
base.SuppressWildcardExpansion = true;
|
base.SuppressWildcardExpansion = true;
|
||||||
_paths = value;
|
Path = value;
|
||||||
} // set
|
} // set
|
||||||
} // LiteralPath
|
} // LiteralPath
|
||||||
|
|
||||||
|
@ -107,18 +111,7 @@ namespace Microsoft.PowerShell.Commands
|
||||||
/// </value>
|
/// </value>
|
||||||
///
|
///
|
||||||
[Parameter(Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = qualifierSet, Mandatory = false)]
|
[Parameter(Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = qualifierSet, Mandatory = false)]
|
||||||
public SwitchParameter Qualifier
|
public SwitchParameter Qualifier { get; set; }
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return _qualifier;
|
|
||||||
} // get
|
|
||||||
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_qualifier = value;
|
|
||||||
} //set
|
|
||||||
} // Qualifier
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines if the qualifier should be returned
|
/// Determines if the qualifier should be returned
|
||||||
|
@ -131,19 +124,7 @@ namespace Microsoft.PowerShell.Commands
|
||||||
/// </value>
|
/// </value>
|
||||||
///
|
///
|
||||||
[Parameter(ParameterSetName = noQualifierSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
|
[Parameter(ParameterSetName = noQualifierSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
|
||||||
public SwitchParameter NoQualifier
|
public SwitchParameter NoQualifier { get; set; }
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return _noqualifier;
|
|
||||||
} // get
|
|
||||||
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_noqualifier = value;
|
|
||||||
} //set
|
|
||||||
} // NoQualifier
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines if the parent path should be returned
|
/// Determines if the parent path should be returned
|
||||||
|
@ -154,18 +135,7 @@ namespace Microsoft.PowerShell.Commands
|
||||||
/// </value>
|
/// </value>
|
||||||
///
|
///
|
||||||
[Parameter(ParameterSetName = parentSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
|
[Parameter(ParameterSetName = parentSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
|
||||||
public SwitchParameter Parent
|
public SwitchParameter Parent { get; set; } = true;
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return _parent;
|
|
||||||
} // get
|
|
||||||
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_parent = value;
|
|
||||||
} //set
|
|
||||||
} // Parent
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines if the leaf name should be returned
|
/// Determines if the leaf name should be returned
|
||||||
|
@ -176,98 +146,47 @@ namespace Microsoft.PowerShell.Commands
|
||||||
/// </value>
|
/// </value>
|
||||||
///
|
///
|
||||||
[Parameter(ParameterSetName = leafSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
|
[Parameter(ParameterSetName = leafSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
|
||||||
public SwitchParameter Leaf
|
public SwitchParameter Leaf { get; set; }
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return _leaf;
|
|
||||||
} // get
|
|
||||||
|
|
||||||
set
|
/// <summary>
|
||||||
{
|
/// Determines if the leaf base name (name without extension) should be returned
|
||||||
_leaf = value;
|
/// </summary>
|
||||||
} //set
|
///
|
||||||
} // Leaf
|
/// <value>
|
||||||
|
/// If true the leaf base name of the path will be returned.
|
||||||
|
/// </value>
|
||||||
|
///
|
||||||
|
[Parameter(ParameterSetName = leafBaseSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
|
||||||
|
public SwitchParameter LeafBase { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if the extension should be returned
|
||||||
|
/// </summary>
|
||||||
|
///
|
||||||
|
/// <value>
|
||||||
|
/// If true the extension of the path will be returned.
|
||||||
|
/// </value>
|
||||||
|
///
|
||||||
|
[Parameter(ParameterSetName = extensionSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
|
||||||
|
public SwitchParameter Extension { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines if the path should be resolved before being parsed.
|
/// Determines if the path should be resolved before being parsed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value></value>
|
/// <value></value>
|
||||||
[Parameter]
|
[Parameter]
|
||||||
public SwitchParameter Resolve
|
public SwitchParameter Resolve { get; set; }
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return _resolve;
|
|
||||||
} // get
|
|
||||||
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_resolve = value;
|
|
||||||
} //set
|
|
||||||
} // Resolve
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines if the path is an absolute path.
|
/// Determines if the path is an absolute path.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
///
|
|
||||||
[Parameter(ParameterSetName = isAbsoluteSet)]
|
[Parameter(ParameterSetName = isAbsoluteSet)]
|
||||||
public SwitchParameter IsAbsolute
|
public SwitchParameter IsAbsolute { get; set; }
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return _isAbsolute;
|
|
||||||
} // get
|
|
||||||
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_isAbsolute = value;
|
|
||||||
} //set
|
|
||||||
}
|
|
||||||
#endregion Parameters
|
#endregion Parameters
|
||||||
|
|
||||||
#region parameter data
|
#region parameter data
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The path to resolve
|
|
||||||
/// </summary>
|
|
||||||
private string[] _paths;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if the qualifier of the path should be returned.
|
|
||||||
/// The qualifier is either the drive name or provider name that
|
|
||||||
/// is qualifying the path.
|
|
||||||
/// </summary>
|
|
||||||
private bool _qualifier;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if the qualifier of the path should be returned.
|
|
||||||
/// If false, the qualifier will be returned. If true, it will
|
|
||||||
/// be stripped from the path.
|
|
||||||
/// The qualifier is either the drive name or provider name that
|
|
||||||
/// is qualifying the path.
|
|
||||||
/// </summary>
|
|
||||||
private bool _noqualifier;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if the parent path of the specified path should be returned.
|
|
||||||
/// </summary>
|
|
||||||
private bool _parent = true;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if the leaf name of the specified path should be returned.
|
|
||||||
/// </summary>
|
|
||||||
private bool _leaf;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if the path(s) should be resolved before being parsed.
|
|
||||||
/// </summary>
|
|
||||||
private bool _resolve;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if the path(s) are absolute paths.
|
|
||||||
/// </summary>
|
|
||||||
private bool _isAbsolute;
|
|
||||||
|
|
||||||
#endregion parameter data
|
#endregion parameter data
|
||||||
|
|
||||||
|
@ -281,15 +200,15 @@ namespace Microsoft.PowerShell.Commands
|
||||||
{
|
{
|
||||||
StringCollection pathsToParse = new StringCollection();
|
StringCollection pathsToParse = new StringCollection();
|
||||||
|
|
||||||
if (_resolve)
|
if (Resolve)
|
||||||
{
|
{
|
||||||
CmdletProviderContext currentContext = CmdletProviderContext;
|
CmdletProviderContext currentContext = CmdletProviderContext;
|
||||||
|
|
||||||
foreach (string path in _paths)
|
foreach (string path in Path)
|
||||||
{
|
{
|
||||||
// resolve the paths and then parse each one.
|
// resolve the paths and then parse each one.
|
||||||
|
|
||||||
Collection<PathInfo> resolvedPaths = null;
|
Collection<PathInfo> resolvedPaths;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -387,7 +306,7 @@ namespace Microsoft.PowerShell.Commands
|
||||||
switch (ParameterSetName)
|
switch (ParameterSetName)
|
||||||
{
|
{
|
||||||
case isAbsoluteSet:
|
case isAbsoluteSet:
|
||||||
string ignored = null;
|
string ignored;
|
||||||
bool isPathAbsolute =
|
bool isPathAbsolute =
|
||||||
SessionState.Path.IsPSAbsolute(pathsToParse[index], out ignored);
|
SessionState.Path.IsPSAbsolute(pathsToParse[index], out ignored);
|
||||||
|
|
||||||
|
@ -436,10 +355,6 @@ namespace Microsoft.PowerShell.Commands
|
||||||
|
|
||||||
case parentSet:
|
case parentSet:
|
||||||
case literalPathSet:
|
case literalPathSet:
|
||||||
bool pathStartsWithRoot =
|
|
||||||
pathsToParse[index].StartsWith("\\", StringComparison.CurrentCulture) ||
|
|
||||||
pathsToParse[index].StartsWith("/", StringComparison.CurrentCulture);
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
result =
|
result =
|
||||||
|
@ -462,13 +377,24 @@ namespace Microsoft.PowerShell.Commands
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case leafSet:
|
case leafSet:
|
||||||
|
case leafBaseSet:
|
||||||
|
case extensionSet:
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// default handles leafSet
|
||||||
result =
|
result =
|
||||||
SessionState.Path.ParseChildName(
|
SessionState.Path.ParseChildName(
|
||||||
pathsToParse[index],
|
pathsToParse[index],
|
||||||
CmdletProviderContext,
|
CmdletProviderContext,
|
||||||
true);
|
true);
|
||||||
|
if (LeafBase)
|
||||||
|
{
|
||||||
|
result = System.IO.Path.GetFileNameWithoutExtension(result);
|
||||||
|
}
|
||||||
|
else if (Extension)
|
||||||
|
{
|
||||||
|
result = System.IO.Path.GetExtension(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (PSNotSupportedException)
|
catch (PSNotSupportedException)
|
||||||
{
|
{
|
||||||
|
|
|
@ -569,6 +569,7 @@ Describe "Extended FileSystem Path/Location Cmdlet Provider Tests" -Tags "Featur
|
||||||
$level1_0 = "Level1_0"
|
$level1_0 = "Level1_0"
|
||||||
$level2_0 = "Level2_0"
|
$level2_0 = "Level2_0"
|
||||||
$level2_1 = "Level2_1"
|
$level2_1 = "Level2_1"
|
||||||
|
$fileExt = ".ext"
|
||||||
$root = Join-Path "TestDrive:" "" #adds correct / or \
|
$root = Join-Path "TestDrive:" "" #adds correct / or \
|
||||||
$level1_0Full = Join-Path $root $level1_0
|
$level1_0Full = Join-Path $root $level1_0
|
||||||
$level2_0Full = Join-Path $level1_0Full $level2_0
|
$level2_0Full = Join-Path $level1_0Full $level2_0
|
||||||
|
@ -629,6 +630,22 @@ Describe "Extended FileSystem Path/Location Cmdlet Provider Tests" -Tags "Featur
|
||||||
$result | Should Be $level1_0
|
$result | Should Be $level1_0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
It 'Validate LeafBase' {
|
||||||
|
$result = Split-Path -Path "$level2_1Full$fileExt" -LeafBase
|
||||||
|
$result | Should Be $level2_1
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'Validate LeafBase is not over-zealous' {
|
||||||
|
|
||||||
|
$result = Split-Path -Path "$level2_1Full$fileExt$fileExt" -LeafBase
|
||||||
|
$result | Should Be "$level2_1$fileExt"
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'Validate LeafBase' {
|
||||||
|
$result = Split-Path -Path "$level2_1Full$fileExt" -Extension
|
||||||
|
$result | Should Be $fileExt
|
||||||
|
}
|
||||||
|
|
||||||
It "Validate NoQualifier" {
|
It "Validate NoQualifier" {
|
||||||
$result = Split-Path -Path $level1_0Full -NoQualifier
|
$result = Split-Path -Path $level1_0Full -NoQualifier
|
||||||
($result.TrimStart('/\')) | Should Be $level1_0
|
($result.TrimStart('/\')) | Should Be $level1_0
|
||||||
|
|
Loading…
Reference in a new issue