Exemplo n.º 1
0
        internal override IEnumerable <PSTypeName> GetInferredType(CompletionContext context)
        {
            PseudoBindingInfo iteratorVariable0 = new PseudoParameterBinder().DoPseudoParameterBinding(this, null, null, false);

            if (iteratorVariable0.CommandInfo != null)
            {
                AstParameterArgumentPair iteratorVariable1;
                string key = "Path";
                if (!iteratorVariable0.BoundArguments.TryGetValue(key, out iteratorVariable1))
                {
                    key = "LiteralPath";
                    iteratorVariable0.BoundArguments.TryGetValue(key, out iteratorVariable1);
                }
                CommandInfo commandInfo       = iteratorVariable0.CommandInfo;
                AstPair     iteratorVariable4 = iteratorVariable1 as AstPair;
                if ((iteratorVariable4 != null) && (iteratorVariable4.Argument is StringConstantExpressionAst))
                {
                    string str = ((StringConstantExpressionAst)iteratorVariable4.Argument).Value;
                    try
                    {
                        commandInfo = commandInfo.CreateGetCommandCopy(new string[] { "-" + key, str });
                    }
                    catch (InvalidOperationException)
                    {
                    }
                }
                CmdletInfo iteratorVariable5 = commandInfo as CmdletInfo;
                if (iteratorVariable5 != null)
                {
                    if (iteratorVariable5.ImplementingType.FullName.Equals("Microsoft.PowerShell.Commands.NewObjectCommand", StringComparison.Ordinal))
                    {
                        AstParameterArgumentPair iteratorVariable6;
                        if (iteratorVariable0.BoundArguments.TryGetValue("TypeName", out iteratorVariable6))
                        {
                            AstPair iteratorVariable7 = iteratorVariable6 as AstPair;
                            if ((iteratorVariable7 != null) && (iteratorVariable7.Argument is StringConstantExpressionAst))
                            {
                                yield return(new PSTypeName(((StringConstantExpressionAst)iteratorVariable7.Argument).Value));
                            }
                        }
                        goto Label_0579;
                    }
                    if (iteratorVariable5.ImplementingType.Equals(typeof(WhereObjectCommand)) || iteratorVariable5.ImplementingType.FullName.Equals("Microsoft.PowerShell.Commands.SortObjectCommand", StringComparison.Ordinal))
                    {
                        PipelineAst parent = this.Parent as PipelineAst;
                        if (parent != null)
                        {
                            int iteratorVariable9 = 0;
                            while (iteratorVariable9 < parent.PipelineElements.Count)
                            {
                                if (parent.PipelineElements[iteratorVariable9] == this)
                                {
                                    break;
                                }
                                iteratorVariable9++;
                            }
                            if (iteratorVariable9 > 0)
                            {
                                foreach (PSTypeName iteratorVariable10 in parent.PipelineElements[iteratorVariable9 - 1].GetInferredType(context))
                                {
                                    yield return(iteratorVariable10);
                                }
                            }
                        }
                        goto Label_0579;
                    }
                    if (iteratorVariable5.ImplementingType.Equals(typeof(ForEachObjectCommand)))
                    {
                        AstParameterArgumentPair iteratorVariable11;
                        if (iteratorVariable0.BoundArguments.TryGetValue("Begin", out iteratorVariable11))
                        {
                            foreach (PSTypeName iteratorVariable12 in this.GetInferredTypeFromScriptBlockParameter(iteratorVariable11, context))
                            {
                                yield return(iteratorVariable12);
                            }
                        }
                        if (iteratorVariable0.BoundArguments.TryGetValue("Process", out iteratorVariable11))
                        {
                            foreach (PSTypeName iteratorVariable13 in this.GetInferredTypeFromScriptBlockParameter(iteratorVariable11, context))
                            {
                                yield return(iteratorVariable13);
                            }
                        }
                        if (iteratorVariable0.BoundArguments.TryGetValue("End", out iteratorVariable11))
                        {
                            foreach (PSTypeName iteratorVariable14 in this.GetInferredTypeFromScriptBlockParameter(iteratorVariable11, context))
                            {
                                yield return(iteratorVariable14);
                            }
                        }
                    }
                }
                foreach (PSTypeName iteratorVariable15 in commandInfo.OutputType)
                {
                    yield return(iteratorVariable15);
                }
            }
Label_0579:
            yield break;
        }
Exemplo n.º 2
0
 internal override IEnumerable<PSTypeName> GetInferredType(CompletionContext context)
 {
     PseudoBindingInfo iteratorVariable0 = new PseudoParameterBinder().DoPseudoParameterBinding(this, null, null, false);
     if (iteratorVariable0.CommandInfo != null)
     {
         AstParameterArgumentPair iteratorVariable1;
         string key = "Path";
         if (!iteratorVariable0.BoundArguments.TryGetValue(key, out iteratorVariable1))
         {
             key = "LiteralPath";
             iteratorVariable0.BoundArguments.TryGetValue(key, out iteratorVariable1);
         }
         CommandInfo commandInfo = iteratorVariable0.CommandInfo;
         AstPair iteratorVariable4 = iteratorVariable1 as AstPair;
         if ((iteratorVariable4 != null) && (iteratorVariable4.Argument is StringConstantExpressionAst))
         {
             string str = ((StringConstantExpressionAst) iteratorVariable4.Argument).Value;
             try
             {
                 commandInfo = commandInfo.CreateGetCommandCopy(new string[] { "-" + key, str });
             }
             catch (InvalidOperationException)
             {
             }
         }
         CmdletInfo iteratorVariable5 = commandInfo as CmdletInfo;
         if (iteratorVariable5 != null)
         {
             if (iteratorVariable5.ImplementingType.FullName.Equals("Microsoft.PowerShell.Commands.NewObjectCommand", StringComparison.Ordinal))
             {
                 AstParameterArgumentPair iteratorVariable6;
                 if (iteratorVariable0.BoundArguments.TryGetValue("TypeName", out iteratorVariable6))
                 {
                     AstPair iteratorVariable7 = iteratorVariable6 as AstPair;
                     if ((iteratorVariable7 != null) && (iteratorVariable7.Argument is StringConstantExpressionAst))
                     {
                         yield return new PSTypeName(((StringConstantExpressionAst) iteratorVariable7.Argument).Value);
                     }
                 }
                 goto Label_0579;
             }
             if (iteratorVariable5.ImplementingType.Equals(typeof(WhereObjectCommand)) || iteratorVariable5.ImplementingType.FullName.Equals("Microsoft.PowerShell.Commands.SortObjectCommand", StringComparison.Ordinal))
             {
                 PipelineAst parent = this.Parent as PipelineAst;
                 if (parent != null)
                 {
                     int iteratorVariable9 = 0;
                     while (iteratorVariable9 < parent.PipelineElements.Count)
                     {
                         if (parent.PipelineElements[iteratorVariable9] == this)
                         {
                             break;
                         }
                         iteratorVariable9++;
                     }
                     if (iteratorVariable9 > 0)
                     {
                         foreach (PSTypeName iteratorVariable10 in parent.PipelineElements[iteratorVariable9 - 1].GetInferredType(context))
                         {
                             yield return iteratorVariable10;
                         }
                     }
                 }
                 goto Label_0579;
             }
             if (iteratorVariable5.ImplementingType.Equals(typeof(ForEachObjectCommand)))
             {
                 AstParameterArgumentPair iteratorVariable11;
                 if (iteratorVariable0.BoundArguments.TryGetValue("Begin", out iteratorVariable11))
                 {
                     foreach (PSTypeName iteratorVariable12 in this.GetInferredTypeFromScriptBlockParameter(iteratorVariable11, context))
                     {
                         yield return iteratorVariable12;
                     }
                 }
                 if (iteratorVariable0.BoundArguments.TryGetValue("Process", out iteratorVariable11))
                 {
                     foreach (PSTypeName iteratorVariable13 in this.GetInferredTypeFromScriptBlockParameter(iteratorVariable11, context))
                     {
                         yield return iteratorVariable13;
                     }
                 }
                 if (iteratorVariable0.BoundArguments.TryGetValue("End", out iteratorVariable11))
                 {
                     foreach (PSTypeName iteratorVariable14 in this.GetInferredTypeFromScriptBlockParameter(iteratorVariable11, context))
                     {
                         yield return iteratorVariable14;
                     }
                 }
             }
         }
         foreach (PSTypeName iteratorVariable15 in commandInfo.OutputType)
         {
             yield return iteratorVariable15;
         }
     }
 Label_0579:
     yield break;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Bind a CommandAst to the specified command
        /// </summary>
        /// <param name="commandAst">The CommandAst that represents the command invocation.</param>
        /// <param name="resolve">Boolean to determine whether binding should be syntactic, or should attempt
        /// to resolve against an existing command.
        /// </param>
        /// <param name="desiredParameters">
        ///     A string array that represents parameter names of interest. If any of these are specified,
        ///     then full binding is done.
        /// </param>
        /// <returns>The StaticBindingResult that represents the binding.</returns>
        public static StaticBindingResult BindCommand(CommandAst commandAst, bool resolve, string[] desiredParameters)
        {
            // If they specified any desired parameters, first quickly check if they are found
            if ((desiredParameters != null) && (desiredParameters.Length > 0))
            {
                bool possiblyHadDesiredParameter = false;
                foreach (CommandParameterAst commandParameter in commandAst.CommandElements.OfType<CommandParameterAst>())
                {
                    string actualParameterName = commandParameter.ParameterName;

                    foreach (string actualParameter in desiredParameters)
                    {
                        if (actualParameter.StartsWith(actualParameterName, StringComparison.OrdinalIgnoreCase))
                        {
                            possiblyHadDesiredParameter = true;
                            break;
                        }
                    }

                    if (possiblyHadDesiredParameter)
                    {
                        break;
                    }
                }

                // Quick exit if the desired parameter was not present
                if (!possiblyHadDesiredParameter)
                {
                    return null;
                }
            }

            if (!resolve)
            {
                return new StaticBindingResult(commandAst, null);
            }

            PseudoBindingInfo pseudoBinding = null;
            if (Runspace.DefaultRunspace == null)
            {
                lock (s_bindCommandlLock)
                {
                    if (s_bindCommandPowerShell == null)
                    {
                        // Create a mini runspace by remove the types and formats
                        InitialSessionState minimalState = InitialSessionState.CreateDefault2();
                        minimalState.Types.Clear();
                        minimalState.Formats.Clear();

                        s_bindCommandPowerShell = PowerShell.Create(minimalState);
                    }

                    // Handle static binding from a non-PowerShell / C# application
                    Runspace.DefaultRunspace = s_bindCommandPowerShell.Runspace;
                    // Static binding always does argument binding (not argument or parameter completion).
                    pseudoBinding = new PseudoParameterBinder().DoPseudoParameterBinding(commandAst, null, null, PseudoParameterBinder.BindingType.ArgumentBinding);
                    Runspace.DefaultRunspace = null;
                }
            }
            else
            {
                // Static binding always does argument binding (not argument or parameter completion).
                pseudoBinding = new PseudoParameterBinder().DoPseudoParameterBinding(commandAst, null, null, PseudoParameterBinder.BindingType.ArgumentBinding);
            }

            return new StaticBindingResult(commandAst, pseudoBinding);
        }