2018-02-13 09:23:53 -08:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// Licensed under the MIT License.
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Management.Automation.Host;
|
|
|
|
|
using System.Management.Automation.Language;
|
|
|
|
|
using System.Management.Automation.Runspaces;
|
|
|
|
|
|
|
|
|
|
namespace System.Management.Automation
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This class aggregates the objects necessary for the Monad
|
|
|
|
|
/// engine to run.
|
|
|
|
|
/// </summary>
|
2016-07-29 13:02:49 -07:00
|
|
|
internal class AutomationEngine
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
// Holds the parser to use for this instance of the engine...
|
|
|
|
|
internal Language.Parser EngineParser;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the handle to the execution context
|
|
|
|
|
/// for this instance of the automation engine.
|
|
|
|
|
/// </summary>
|
2016-08-02 13:29:09 -07:00
|
|
|
internal ExecutionContext Context { get; }
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
2019-01-06 04:40:24 +05:00
|
|
|
/// Gets the CommandDiscovery instance for the current engine.
|
2016-03-30 14:20:52 -07:00
|
|
|
/// </summary>
|
2016-08-02 13:29:09 -07:00
|
|
|
internal CommandDiscovery CommandDiscovery { get; }
|
2016-07-29 13:02:49 -07:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// The principal constructor that most hosts will use when creating
|
|
|
|
|
/// an instance of the automation engine. It allows you to pass in an
|
|
|
|
|
/// instance of PSHost that provides the host-specific I/O routines, etc.
|
|
|
|
|
/// </summary>
|
2017-10-18 09:12:23 -07:00
|
|
|
internal AutomationEngine(PSHost hostInterface, InitialSessionState iss)
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
2017-10-18 09:12:23 -07:00
|
|
|
Context = new ExecutionContext(this, hostInterface, iss);
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
EngineParser = new Language.Parser();
|
2016-08-02 13:29:09 -07:00
|
|
|
CommandDiscovery = new CommandDiscovery(Context);
|
2016-03-30 14:20:52 -07:00
|
|
|
|
2017-10-18 09:12:23 -07:00
|
|
|
// Load the iss, resetting everything to it's defaults...
|
|
|
|
|
iss.Bind(Context, /*updateOnly*/ false);
|
2016-03-30 14:20:52 -07:00
|
|
|
|
2016-08-02 13:29:09 -07:00
|
|
|
InitialSessionState.SetSessionStateDrive(Context, true);
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Method to take a string and expand any metachars in it.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal string Expand(string s)
|
|
|
|
|
{
|
|
|
|
|
var ast = Parser.ScanString(s);
|
|
|
|
|
|
|
|
|
|
// ExpandString is assumed to invoke code, so passing 'IsTrustedInput'
|
2018-06-13 00:30:10 +05:00
|
|
|
return Compiler.GetExpressionValue(ast, true, Context, Context.EngineSessionState) as string ?? string.Empty;
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Compile a piece of text into a parse tree for later execution.
|
|
|
|
|
/// </summary>
|
2018-12-28 16:39:06 +05:00
|
|
|
/// <param name="script">The text to parse.</param>
|
2018-12-29 08:25:49 +05:00
|
|
|
/// <param name="addToHistory">True iff the scriptblock will be added to history.</param>
|
2016-03-30 14:20:52 -07:00
|
|
|
/// <returns>The parse text as a parsetree node.</returns>
|
2018-04-25 18:04:04 -07:00
|
|
|
internal ScriptBlock ParseScriptBlock(string script, bool addToHistory)
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
2018-04-25 18:04:04 -07:00
|
|
|
return ParseScriptBlock(script, null, addToHistory);
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-25 18:04:04 -07:00
|
|
|
internal ScriptBlock ParseScriptBlock(string script, string fileName, bool addToHistory)
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
ParseError[] errors;
|
|
|
|
|
var ast = EngineParser.Parse(fileName, script, null, out errors, ParseMode.Default);
|
|
|
|
|
|
2018-04-25 18:04:04 -07:00
|
|
|
if (addToHistory)
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
2016-08-02 13:29:09 -07:00
|
|
|
EngineParser.SetPreviousFirstLastToken(Context);
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (errors.Any())
|
|
|
|
|
{
|
|
|
|
|
if (errors[0].IncompleteInput)
|
|
|
|
|
{
|
|
|
|
|
throw new IncompleteParseException(errors[0].Message, errors[0].ErrorId);
|
|
|
|
|
}
|
2018-12-23 22:20:06 -08:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
throw new ParseException(errors);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new ScriptBlock(ast, isFilter: false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|