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
@ -16,12 +16,14 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// MSH paths that match the glob strings.
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Split, "Path", DefaultParameterSetName = "ParentSet", SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113404")]
|
||||
[OutputType(typeof(string), ParameterSetName = new string[] { SplitPathCommand.leafSet,
|
||||
SplitPathCommand.noQualifierSet,
|
||||
SplitPathCommand.parentSet,
|
||||
SplitPathCommand.qualifierSet,
|
||||
SplitPathCommand.literalPathSet})]
|
||||
[OutputType(typeof(bool), ParameterSetName = new string[] { SplitPathCommand.isAbsoluteSet })]
|
||||
[OutputType(typeof(string), ParameterSetName = new[] { leafSet,
|
||||
leafBaseSet,
|
||||
extensionSet,
|
||||
noQualifierSet,
|
||||
parentSet,
|
||||
qualifierSet,
|
||||
literalPathSet})]
|
||||
[OutputType(typeof(bool), ParameterSetName = new[] { isAbsoluteSet })]
|
||||
public class SplitPathCommand : CoreCommandWithCredentialsBase
|
||||
{
|
||||
#region Parameters
|
||||
@ -36,6 +38,17 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </summary>
|
||||
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>
|
||||
/// The parameter set name to get the qualifier set.
|
||||
/// </summary>
|
||||
@ -61,21 +74,12 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </summary>
|
||||
[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 = 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 = noQualifierSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
||||
[Parameter(Position = 0, ParameterSetName = isAbsoluteSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
||||
public string[] Path
|
||||
{
|
||||
get
|
||||
{
|
||||
return _paths;
|
||||
} // get
|
||||
|
||||
set
|
||||
{
|
||||
_paths = value;
|
||||
} // set
|
||||
} // Path
|
||||
public string[] Path { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the literal path parameter to the command
|
||||
@ -86,13 +90,13 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
get
|
||||
{
|
||||
return _paths;
|
||||
return Path;
|
||||
} // get
|
||||
|
||||
set
|
||||
{
|
||||
base.SuppressWildcardExpansion = true;
|
||||
_paths = value;
|
||||
Path = value;
|
||||
} // set
|
||||
} // LiteralPath
|
||||
|
||||
@ -107,18 +111,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </value>
|
||||
///
|
||||
[Parameter(Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = qualifierSet, Mandatory = false)]
|
||||
public SwitchParameter Qualifier
|
||||
{
|
||||
get
|
||||
{
|
||||
return _qualifier;
|
||||
} // get
|
||||
|
||||
set
|
||||
{
|
||||
_qualifier = value;
|
||||
} //set
|
||||
} // Qualifier
|
||||
public SwitchParameter Qualifier { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the qualifier should be returned
|
||||
@ -131,19 +124,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </value>
|
||||
///
|
||||
[Parameter(ParameterSetName = noQualifierSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
|
||||
public SwitchParameter NoQualifier
|
||||
{
|
||||
get
|
||||
{
|
||||
return _noqualifier;
|
||||
} // get
|
||||
|
||||
set
|
||||
{
|
||||
_noqualifier = value;
|
||||
} //set
|
||||
} // NoQualifier
|
||||
|
||||
public SwitchParameter NoQualifier { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the parent path should be returned
|
||||
@ -154,18 +135,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </value>
|
||||
///
|
||||
[Parameter(ParameterSetName = parentSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
|
||||
public SwitchParameter Parent
|
||||
{
|
||||
get
|
||||
{
|
||||
return _parent;
|
||||
} // get
|
||||
|
||||
set
|
||||
{
|
||||
_parent = value;
|
||||
} //set
|
||||
} // Parent
|
||||
public SwitchParameter Parent { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the leaf name should be returned
|
||||
@ -176,98 +146,47 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </value>
|
||||
///
|
||||
[Parameter(ParameterSetName = leafSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
|
||||
public SwitchParameter Leaf
|
||||
{
|
||||
get
|
||||
{
|
||||
return _leaf;
|
||||
} // get
|
||||
public SwitchParameter Leaf { get; set; }
|
||||
|
||||
set
|
||||
{
|
||||
_leaf = value;
|
||||
} //set
|
||||
} // Leaf
|
||||
/// <summary>
|
||||
/// Determines if the leaf base name (name without extension) should be returned
|
||||
/// </summary>
|
||||
///
|
||||
/// <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>
|
||||
/// Determines if the path should be resolved before being parsed.
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
[Parameter]
|
||||
public SwitchParameter Resolve
|
||||
{
|
||||
get
|
||||
{
|
||||
return _resolve;
|
||||
} // get
|
||||
|
||||
set
|
||||
{
|
||||
_resolve = value;
|
||||
} //set
|
||||
} // Resolve
|
||||
public SwitchParameter Resolve { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the path is an absolute path.
|
||||
/// </summary>
|
||||
///
|
||||
[Parameter(ParameterSetName = isAbsoluteSet)]
|
||||
public SwitchParameter IsAbsolute
|
||||
{
|
||||
get
|
||||
{
|
||||
return _isAbsolute;
|
||||
} // get
|
||||
public SwitchParameter IsAbsolute { get; set; }
|
||||
|
||||
set
|
||||
{
|
||||
_isAbsolute = value;
|
||||
} //set
|
||||
}
|
||||
#endregion Parameters
|
||||
|
||||
#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
|
||||
|
||||
@ -281,15 +200,15 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
StringCollection pathsToParse = new StringCollection();
|
||||
|
||||
if (_resolve)
|
||||
if (Resolve)
|
||||
{
|
||||
CmdletProviderContext currentContext = CmdletProviderContext;
|
||||
|
||||
foreach (string path in _paths)
|
||||
foreach (string path in Path)
|
||||
{
|
||||
// resolve the paths and then parse each one.
|
||||
|
||||
Collection<PathInfo> resolvedPaths = null;
|
||||
Collection<PathInfo> resolvedPaths;
|
||||
|
||||
try
|
||||
{
|
||||
@ -387,7 +306,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
switch (ParameterSetName)
|
||||
{
|
||||
case isAbsoluteSet:
|
||||
string ignored = null;
|
||||
string ignored;
|
||||
bool isPathAbsolute =
|
||||
SessionState.Path.IsPSAbsolute(pathsToParse[index], out ignored);
|
||||
|
||||
@ -436,10 +355,6 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
case parentSet:
|
||||
case literalPathSet:
|
||||
bool pathStartsWithRoot =
|
||||
pathsToParse[index].StartsWith("\\", StringComparison.CurrentCulture) ||
|
||||
pathsToParse[index].StartsWith("/", StringComparison.CurrentCulture);
|
||||
|
||||
try
|
||||
{
|
||||
result =
|
||||
@ -462,13 +377,24 @@ namespace Microsoft.PowerShell.Commands
|
||||
break;
|
||||
|
||||
case leafSet:
|
||||
case leafBaseSet:
|
||||
case extensionSet:
|
||||
try
|
||||
{
|
||||
// default handles leafSet
|
||||
result =
|
||||
SessionState.Path.ParseChildName(
|
||||
pathsToParse[index],
|
||||
CmdletProviderContext,
|
||||
true);
|
||||
if (LeafBase)
|
||||
{
|
||||
result = System.IO.Path.GetFileNameWithoutExtension(result);
|
||||
}
|
||||
else if (Extension)
|
||||
{
|
||||
result = System.IO.Path.GetExtension(result);
|
||||
}
|
||||
}
|
||||
catch (PSNotSupportedException)
|
||||
{
|
||||
|
@ -569,6 +569,7 @@ Describe "Extended FileSystem Path/Location Cmdlet Provider Tests" -Tags "Featur
|
||||
$level1_0 = "Level1_0"
|
||||
$level2_0 = "Level2_0"
|
||||
$level2_1 = "Level2_1"
|
||||
$fileExt = ".ext"
|
||||
$root = Join-Path "TestDrive:" "" #adds correct / or \
|
||||
$level1_0Full = Join-Path $root $level1_0
|
||||
$level2_0Full = Join-Path $level1_0Full $level2_0
|
||||
@ -628,6 +629,22 @@ Describe "Extended FileSystem Path/Location Cmdlet Provider Tests" -Tags "Featur
|
||||
$result = Split-Path -Path $level1_0Full -Leaf
|
||||
$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" {
|
||||
$result = Split-Path -Path $level1_0Full -NoQualifier
|
||||
|
Loading…
Reference in New Issue
Block a user