@@ -399,7 +399,7 @@ public HashSet<string> GetExportedFunction(Ast ast)
399
399
IEnumerable < Ast > cmdAsts = ast . FindAll ( item => item is CommandAst
400
400
&& exportFunctionsCmdlet . Contains ( ( item as CommandAst ) . GetCommandName ( ) , StringComparer . OrdinalIgnoreCase ) , true ) ;
401
401
402
- CommandInfo exportMM = Helper . Instance . GetCommandInfoLegacy ( "export-modulemember" , CommandTypes . Cmdlet ) ;
402
+ CommandInfo exportMM = Helper . Instance . GetCommandInfo ( "export-modulemember" , CommandTypes . Cmdlet ) ;
403
403
404
404
// switch parameters
405
405
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
614
614
return false ;
615
615
}
616
616
617
- var commandInfo = GetCommandInfoLegacy ( cmdAst . GetCommandName ( ) ) ;
617
+ var commandInfo = GetCommandInfo ( cmdAst . GetCommandName ( ) ) ;
618
618
if ( commandInfo == null || ( commandInfo . CommandType != System . Management . Automation . CommandTypes . Cmdlet ) )
619
619
{
620
620
return false ;
@@ -694,18 +694,19 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanThreePositio
694
694
/// Get a CommandInfo object of the given command name
695
695
/// </summary>
696
696
/// <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 )
698
698
{
699
+ if ( string . IsNullOrWhiteSpace ( cmdName ) )
700
+ {
701
+ return null ;
702
+ }
703
+
699
704
using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
700
705
{
701
706
var psCommand = ps . AddCommand ( "Get-Command" )
702
707
. 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 ) ;
709
710
710
711
var commandInfo = psCommand . Invoke < CommandInfo > ( )
711
712
. FirstOrDefault ( ) ;
@@ -715,67 +716,24 @@ private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? command
715
716
}
716
717
717
718
/// <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>
723
722
/// </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 )
729
725
{
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 ) ;
753
727
}
754
728
755
729
/// <summary>
756
- /// Given a command's name, checks whether it exists.
730
+ /// Given a command's name, retrieve its CommandInfo if it exists
757
731
/// </summary>
758
732
/// <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 )
762
735
{
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 ) ;
779
737
}
780
738
781
739
/// <summary>
0 commit comments