コード例 #1
0
 private void LoadScripts()
 {
     foreach (ScriptConfigurationEntry entry in _runspace.ExecutionContext.RunspaceConfiguration.Scripts)
     {
         try
         {
             var scriptBlock = new ScriptBlock(PowerShellGrammar.ParseInteractiveInput(entry.Definition));
             _scripts.Add(entry.Name, new ScriptInfo(entry.Name, scriptBlock,
                                                     ScopeUsages.NewScriptScope));
             continue;
         }
         catch
         {
             throw new Exception("DuplicateScriptName: " + entry.Name);
         }
     }
 }
コード例 #2
0
ファイル: CommandManager.cs プロジェクト: soulhakr/Pash
 public static ScriptBlockAst ParseInput(string input)
 {
     try
     {
         return(PowerShellGrammar.ParseInteractiveInput(input));
     }
     catch (PowerShellGrammar.ParseException exception)
     {
         // nicer error message
         var msgfmt   = "Parse error at {0}: {1}";
         var location = exception.LogMessage.Location.ToUiString();
         var reason   = exception.LogMessage.Message;
         if (reason.Length > 100)
         {
             reason = reason.Substring(0, 97) + "...";
         }
         throw new ParseException(String.Format(msgfmt, location, reason), exception);
     }
 }
コード例 #3
0
        public CommandProcessorBase CreateCommandProcessor(Command command)
        {
            string cmdText = command.CommandText;

            CommandInfo commandInfo   = null;
            bool        useLocalScope = command.UseLocalScope;

            if (command.IsScript) //CommandText contains a script block. Parse it
            {
                command.ScriptBlockAst = PowerShellGrammar.ParseInteractiveInput(cmdText);
            }

            //either we parsed something like "& { foo; bar }", or the CommandText was a script and was just parsed
            if (command.ScriptBlockAst != null)
            {
                commandInfo = new ScriptInfo("", command.ScriptBlockAst.GetScriptBlock(),
                                             useLocalScope ? ScopeUsages.NewScope : ScopeUsages.CurrentScope);
            }
            else //otherwise it's a real command (cmdlet, script, function, application)
            {
                commandInfo = FindCommand(cmdText, useLocalScope);
            }
            // make sure we only create a valid command processor if it's a valid command
            commandInfo.Validate();

            switch (commandInfo.CommandType)
            {
            case CommandTypes.Application:
                return(new ApplicationProcessor((ApplicationInfo)commandInfo));

            case CommandTypes.Cmdlet:
                return(new CommandProcessor(commandInfo as CmdletInfo));

            case CommandTypes.Script:
            case CommandTypes.ExternalScript:
            case CommandTypes.Function:
                return(new ScriptBlockProcessor(commandInfo as IScriptBlockInfo, commandInfo));

            default:
                throw new NotImplementedException("Invalid command type");
            }
        }
コード例 #4
0
        public CommandProcessorBase CreateCommandProcessor(Command command)
        {
            string cmdName = command.CommandText;

            CommandInfo commandInfo = null;

            if (command.CommandText == null)
            {
                commandInfo = new ScriptInfo("", command.ScriptBlockAst.GetScriptBlock());
            }

            if (command.IsScript)
            {
                // TODO: take care of the script commands
                throw new NotImplementedException(this.ToString());
            }

            if (commandInfo == null && _aliases.ContainsKey(cmdName))
            {
                commandInfo = _aliases[cmdName].ReferencedCommand;
            }

            if (commandInfo == null && _cmdLets.ContainsKey(cmdName))
            {
                commandInfo = _cmdLets[cmdName].First();
            }

            if (commandInfo == null && _scripts.ContainsKey(cmdName))
            {
                commandInfo = _scripts[cmdName];
            }

            // TODO: if the command wasn't found should we treat is as a Script?
            if (commandInfo == null)
            {
                var parseTree  = PowerShellGrammar.ParseInteractiveInput(command.CommandText);
                var statements = parseTree.EndBlock.Statements;
                if (statements.Count == 1 && statements.Single() is PipelineAst)
                {
                    var pipelineAst = statements.Single() as PipelineAst;
                    if (pipelineAst.PipelineElements.Count == 1 && pipelineAst.PipelineElements.Single() is CommandAst)
                    {
                        var commandAst = pipelineAst.PipelineElements.Single() as CommandAst;
                        if (commandAst.CommandElements.Count == 1 && commandAst.CommandElements.Single() is StringConstantExpressionAst)
                        {
                            var stringAst = commandAst.CommandElements.Single() as StringConstantExpressionAst;
                            var path      = ResolveExecutablePath(stringAst.Value);
                            if (path == null)
                            {
                                throw new Exception(string.Format("Command '{0}' not found.", cmdName));
                            }

                            if (Path.GetExtension(path) == ".ps1")
                            {
                                // I think we should be using a ScriptFile parser, but this will do for now.
                                commandInfo = new ScriptInfo(path, new ScriptBlock(PowerShellGrammar.ParseInteractiveInput(File.ReadAllText(path))));
                            }
                            else
                            {
                                var commandName = Path.GetFileName(path);
                                var extension   = Path.GetExtension(path);

                                commandInfo = new ApplicationInfo(commandName, path, extension);
                            }
                        }
                    }
                }

                if (commandInfo == null)
                {
                    commandInfo = new ScriptInfo("", new ScriptBlock(parseTree));
                }
            }

            if (commandInfo != null)
            {
                switch (commandInfo.CommandType)
                {
                case CommandTypes.Application:
                    return(new ApplicationProcessor((ApplicationInfo)commandInfo));

                case CommandTypes.Cmdlet:
                    return(new CommandProcessor(commandInfo as CmdletInfo));

                case CommandTypes.Function:
                    // TODO: teat the function as a script
                    break;

                case CommandTypes.Script:
                    return(new ScriptProcessor(commandInfo as ScriptInfo));
                }
            }

            throw new Exception(string.Format("Command '{0}' not found.", cmdName));
        }
コード例 #5
0
ファイル: Parser.cs プロジェクト: zhuyue1314/Pash
 static Parser()
 {
     Grammar     = new PowerShellGrammar();
     IronyParser = new IronyParser(Grammar);
 }
コード例 #6
0
        public CommandProcessorBase CreateCommandProcessor(Command command)
        {
            string cmdName = command.CommandText;

            CommandInfo commandInfo = null;

            if (!command.IsScript)
            {
                // check aliases
                if (_aliases.ContainsKey(cmdName))
                {
                    var aliasInfo = _aliases[cmdName];
                    commandInfo = aliasInfo.ReferencedCommand;
                }

                if (commandInfo == null)
                {
                    if (_cmdLets.ContainsKey(cmdName))
                    {
                        var cmdletInfoList = _cmdLets[cmdName];
                        if ((cmdletInfoList != null) && (cmdletInfoList.Count > 0))
                        {
                            commandInfo = cmdletInfoList[0];
                        }
                    }
                }

                if (commandInfo == null)
                {
                    if (_scripts.ContainsKey(cmdName))
                    {
                        commandInfo = _scripts[cmdName];
                    }
                }

                if (commandInfo == null)
                {
                    if (File.Exists(cmdName))
                    {
                        // I think we should be using a ScriptFile parser, but this will do for now.
                        commandInfo = new ScriptInfo(cmdName, new ScriptBlock(PowerShellGrammar.ParseInteractiveInput(File.ReadAllText(cmdName))));
                    }
                }
            }
            else
            {
                // TODO: take care of the script commands
            }

            // TODO: if the command wasn't found should we treat is as a Script?
            if (commandInfo == null)
            {
                commandInfo = new ScriptInfo("", new ScriptBlock(PowerShellGrammar.ParseInteractiveInput(command.CommandText)));
            }

            if (commandInfo != null)
            {
                switch (commandInfo.CommandType)
                {
                case CommandTypes.Cmdlet:
                    return(new CommandProcessor(commandInfo as CmdletInfo));

                case CommandTypes.Function:
                    // TODO: teat the function as a script
                    break;

                case CommandTypes.Script:
                    return(new ScriptProcessor(commandInfo as ScriptInfo));
                }
            }

            throw new Exception(string.Format("Command {0} not found.", cmdName));
        }
コード例 #7
0
 static dynamic ParseInput(string s)
 {
     return(PowerShellGrammar.ParseInteractiveInput(s));
 }
コード例 #8
0
 public ScriptBlock NewScriptBlock(string scriptText)
 {
     return(new ScriptBlock(PowerShellGrammar.ParseInteractiveInput(scriptText)));
 }