Skip to content

Commit 7a46212

Browse files
committed
Remove obsolete attribute from GetCommandInfo
Add overload for GetCommandInfo without CommandType to match general usage Remove GetCommandInfoLegacy method
1 parent 00c4787 commit 7a46212

File tree

4 files changed

+23
-65
lines changed

4 files changed

+23
-65
lines changed

Engine/Helper.cs

Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ public HashSet<string> GetExportedFunction(Ast ast)
399399
IEnumerable<Ast> cmdAsts = ast.FindAll(item => item is CommandAst
400400
&& exportFunctionsCmdlet.Contains((item as CommandAst).GetCommandName(), StringComparer.OrdinalIgnoreCase), true);
401401

402-
CommandInfo exportMM = Helper.Instance.GetCommandInfoLegacy("export-modulemember", CommandTypes.Cmdlet);
402+
CommandInfo exportMM = Helper.Instance.GetCommandInfo("export-modulemember", CommandTypes.Cmdlet);
403403

404404
// switch parameters
405405
IEnumerable<ParameterMetadata> switchParams = (exportMM != null) ? exportMM.Parameters.Values.Where<ParameterMetadata>(pm => pm.SwitchParameter) : Enumerable.Empty<ParameterMetadata>();
@@ -614,7 +614,7 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanThreePositio
614614
return false;
615615
}
616616

617-
var commandInfo = GetCommandInfoLegacy(cmdAst.GetCommandName());
617+
var commandInfo = GetCommandInfo(cmdAst.GetCommandName());
618618
if (commandInfo == null || (commandInfo.CommandType != System.Management.Automation.CommandTypes.Cmdlet))
619619
{
620620
return false;
@@ -694,18 +694,19 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanThreePositio
694694
/// Get a CommandInfo object of the given command name
695695
/// </summary>
696696
/// <returns>Returns null if command does not exists</returns>
697-
private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? commandType)
697+
private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes commandType)
698698
{
699+
if (string.IsNullOrWhiteSpace(cmdName))
700+
{
701+
return null;
702+
}
703+
699704
using (var ps = System.Management.Automation.PowerShell.Create())
700705
{
701706
var psCommand = ps.AddCommand("Get-Command")
702707
.AddParameter("Name", cmdName)
703-
.AddParameter("ErrorAction", "SilentlyContinue");
704-
705-
if(commandType!=null)
706-
{
707-
psCommand.AddParameter("CommandType", commandType);
708-
}
708+
.AddParameter("ErrorAction", "SilentlyContinue")
709+
.AddParameter("CommandType", commandType);
709710

710711
var commandInfo = psCommand.Invoke<CommandInfo>()
711712
.FirstOrDefault();
@@ -715,67 +716,24 @@ private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? command
715716
}
716717

717718
/// <summary>
718-
719-
/// Legacy method, new callers should use <see cref="GetCommandInfo"/> instead.
720-
/// Given a command's name, checks whether it exists. It does not use the passed in CommandTypes parameter, which is a bug.
721-
/// But existing method callers are already depending on this behaviour and therefore this could not be simply fixed.
722-
/// It also populates the commandInfoCache which can have side effects in some cases.
719+
/// Get a CommandInfo object of the given command name and type
720+
/// <param name="name"></param>
721+
/// <param name="commandType"></param>
723722
/// </summary>
724-
/// <param name="name">Command Name.</param>
725-
/// <param name="commandType">Not being used.</param>
726-
/// <returns></returns>
727-
[Obsolete]
728-
public CommandInfo GetCommandInfoLegacy(string name, CommandTypes? commandType = null)
723+
/// <returns>The first CommandInfo found based on the name and type or null if nothing was found</returns>
724+
public CommandInfo GetCommandInfo(string name, CommandTypes commandType)
729725
{
730-
if (string.IsNullOrWhiteSpace(name))
731-
{
732-
return null;
733-
}
734-
735-
// check if it is an alias
736-
string cmdletName = Helper.Instance.GetCmdletNameFromAlias(name);
737-
if (string.IsNullOrWhiteSpace(cmdletName))
738-
{
739-
cmdletName = name;
740-
}
741-
742-
lock (getCommandLock)
743-
{
744-
if (commandInfoCache.ContainsKey(cmdletName))
745-
{
746-
return commandInfoCache[cmdletName];
747-
}
748-
749-
var commandInfo = GetCommandInfoInternal(cmdletName, commandType);
750-
commandInfoCache.Add(cmdletName, commandInfo);
751-
return commandInfo;
752-
}
726+
return GetCommandInfoInternal(name, commandType);
753727
}
754728

755729
/// <summary>
756-
/// Given a command's name, checks whether it exists.
730+
/// Given a command's name, retrieve its CommandInfo if it exists
757731
/// </summary>
758732
/// <param name="name"></param>
759-
/// <param name="commandType"></param>
760-
/// <returns></returns>
761-
public CommandInfo GetCommandInfo(string name, CommandTypes? commandType = null)
733+
/// <returns>The first CommandInfo found based on the name of any command type or null if nothing was found</returns>
734+
public CommandInfo GetCommandInfo(string name)
762735
{
763-
if (string.IsNullOrWhiteSpace(name))
764-
{
765-
return null;
766-
}
767-
768-
lock (getCommandLock)
769-
{
770-
if (commandInfoCache.ContainsKey(name))
771-
{
772-
return commandInfoCache[name];
773-
}
774-
775-
var commandInfo = GetCommandInfoInternal(name, commandType);
776-
777-
return commandInfo;
778-
}
736+
return GetCommandInfo(name, CommandTypes.All);
779737
}
780738

781739
/// <summary>

Rules/AvoidPositionalParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3939
// MSDN: CommandAst.GetCommandName Method
4040
if (cmdAst.GetCommandName() == null) continue;
4141

42-
if (Helper.Instance.GetCommandInfoLegacy(cmdAst.GetCommandName()) != null
42+
if (Helper.Instance.GetCommandInfo(cmdAst.GetCommandName()) != null
4343
&& Helper.Instance.PositionalParameterUsed(cmdAst, true))
4444
{
4545
PipelineAst parent = cmdAst.Parent as PipelineAst;

Rules/UseCmdletCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private bool MandatoryParameterExists(CommandAst cmdAst)
7979

8080
#region Compares parameter list and mandatory parameter list.
8181

82-
cmdInfo = Helper.Instance.GetCommandInfoLegacy(cmdAst.GetCommandName());
82+
cmdInfo = Helper.Instance.GetCommandInfo(cmdAst.GetCommandName());
8383
if (cmdInfo == null || (cmdInfo.CommandType != System.Management.Automation.CommandTypes.Cmdlet))
8484
{
8585
return true;

Rules/UseShouldProcessCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private bool SupportsShouldProcess(string cmdName)
299299
return false;
300300
}
301301

302-
var cmdInfo = Helper.Instance.GetCommandInfoLegacy(cmdName);
302+
var cmdInfo = Helper.Instance.GetCommandInfo(cmdName);
303303
if (cmdInfo == null)
304304
{
305305
return false;

0 commit comments

Comments
 (0)