Example #1
0
        protected ScriptBase(ScriptContext scriptContext, ScriptLanguageType scriptLanguageType, string scriptText)
        {
            ArgumentUtility.CheckNotNull("scriptContext", scriptContext);
            ArgumentUtility.CheckNotNullOrEmpty("scriptText", scriptText);

            _scriptContext      = scriptContext;
            _scriptLanguageType = scriptLanguageType;
            _scriptText         = scriptText;
        }
        private ScriptEngine GetEngine(ScriptLanguageType languageType)
        {
            ScriptEngine scriptEngine;
            bool         engineAvailable = GetScriptEngines().TryGetValue(languageType, out scriptEngine);

            if (!engineAvailable)
            {
                throw new NotSupportedException(String.Format("ScriptEngine for ScriptLanguageType {0} cannot be supplied. Check App.config <microsoft.scripting>-section.", languageType));
            }
            return(scriptEngine);
        }
        public static TResult ExecuteScriptExpression <TResult> (string expressionScriptSourceCode, params object[] scriptParameter)
        {
            const ScriptLanguageType scriptLanguageType = ScriptLanguageType.Python;
            var engine         = ScriptingHost.GetScriptEngine(scriptLanguageType);
            var scriptSource   = engine.CreateScriptSourceFromString(expressionScriptSourceCode, SourceCodeKind.Expression);
            var compiledScript = scriptSource.Compile();
            var scriptScope    = ScriptingHost.GetScriptEngine(scriptLanguageType).CreateScope();

            for (int i = 0; i < scriptParameter.Length; i++)
            {
                scriptScope.SetVariable("p" + i, scriptParameter[i]);
            }
            return(compiledScript.Execute <TResult> (scriptScope));
        }
        public void ImportFromAssemblyIntoScriptScope(ScriptScope scriptScope, string assembly, string nameSpace, string symbol)
        {
            string scriptText = @"
import clr
clr.AddReferenceByPartialName('" + assembly + "')" +
                                @"
from " + nameSpace + " import " + symbol;

            const ScriptLanguageType scriptLanguageType = ScriptLanguageType.Python;
            var engine       = ScriptingHost.GetScriptEngine(scriptLanguageType);
            var scriptSource = engine.CreateScriptSourceFromString(scriptText, SourceCodeKind.Statements);

            scriptSource.Execute(scriptScope);
        }
        public void Ctor()
        {
            ScriptContext            scriptContext      = ScriptContextObjectMother.CreateTestScriptContext();
            const ScriptLanguageType scriptLanguageType = ScriptLanguageType.Python;

            const string scriptText = "'ExpressionScriptCtorTest'";

            var scriptEnvironment = ScriptEnvironment.Create();
            var script            = new ExpressionScript <string> (scriptContext, scriptLanguageType, scriptText, scriptEnvironment);

            Assert.That(script.ScriptContext, Is.EqualTo(scriptContext));
            Assert.That(script.ScriptLanguageType, Is.EqualTo(scriptLanguageType));
            Assert.That(script.ScriptText, Is.EqualTo(scriptText));
            Assert.That(script.Execute(), Is.EqualTo("ExpressionScriptCtorTest"));
        }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ExpressionScript{TResult}"/> class.
        /// </summary>
        /// <param name="scriptContext">
        ///   The <see cref="ScriptContext"/> to use when executing the script. The script context is used to isolate re-motion modules from each other.
        /// </param>
        /// <param name="scriptLanguageType">The script language to use for the script.</param>
        /// <param name="scriptText">
        ///   The source code of the script. This must be an expression in the language defined by <paramref name="scriptLanguageType"/>.
        /// </param>
        /// <param name="scriptEnvironment">
        ///   The <see cref="ScriptEnvironment"/> defining the variables and imported symbols the script has access to.
        /// </param>
        public ExpressionScript(
            ScriptContext scriptContext,
            ScriptLanguageType scriptLanguageType,
            string scriptText,
            ScriptEnvironment scriptEnvironment)
            : base(
                ArgumentUtility.CheckNotNull("scriptContext", scriptContext),
                scriptLanguageType,
                ArgumentUtility.CheckNotNullOrEmpty("scriptText", scriptText))
        {
            ArgumentUtility.CheckNotNull("scriptEnvironment", scriptEnvironment);

            _scriptEnvironment = scriptEnvironment;

            var engine = ScriptingHost.GetScriptEngine(scriptLanguageType);

            _scriptSource = engine.CreateScriptSourceFromString(scriptText, SourceCodeKind.Expression);
        }
        /// <summary>
        /// Imports the passed symbols from the given namespace in the given assembly into the <see cref="ScriptEnvironment"/>.
        /// </summary>
        /// <param name="assembly">Partial name of the assembly to import from (e.g. "Remotion")</param>
        /// <param name="nameSpace">Namespace name in assembly to import from (e.g. "Remotion.Diagnostics.ToText")</param>
        /// <param name="symbols">array of symbol names to import (e.g. "To", "ToTextBuilder", ...)</param>
        public void Import(string assembly, string nameSpace, params string[] symbols)
        {
            ArgumentUtility.CheckNotNullOrEmpty("assembly", assembly);
            ArgumentUtility.CheckNotNullOrEmpty("nameSpace", nameSpace);
            ArgumentUtility.CheckNotNullOrEmpty("symbols", symbols);

            string scriptText = @"
import clr
clr.AddReferenceByPartialName('" + assembly + "')" +
                                @"
from " + nameSpace + " import " + string.Join(",", symbols);

            const ScriptLanguageType scriptLanguageType = ScriptLanguageType.Python;
            var engine       = ScriptingHost.GetScriptEngine(scriptLanguageType);
            var scriptSource = engine.CreateScriptSourceFromString(scriptText, SourceCodeKind.Statements);

            scriptSource.Execute(_scriptScope);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MySqlHybridScriptEditor"/> class.
        /// </summary>
        /// <param name="sp">The service provider.</param>
        /// <param name="pane">The pane.</param>
        /// <param name="scriptType">Indicates the script type.</param>
        internal MySqlHybridScriptEditor(ServiceProvider sp, MySqlHybridScriptEditorPane pane, ScriptLanguageType scriptType = ScriptLanguageType.JavaScript)
            : this()
        {
            ScriptLanguageType = scriptType;
            ConnectionInfoToolStripDropDownButton.Image = scriptType == ScriptLanguageType.JavaScript
        ? Resources.js_id
        : Resources.py_id;
            SetBaseShellConsoleEditorPromptString();
            Pane            = pane;
            ServiceProvider = sp;
            CodeEditor.Init(sp, this);
            if (Package == null)
            {
                return;
            }

            SetConnection(Package.SelectedMySqlConnection, Package.SelectedMySqlConnectionName);
        }
        public void Ctor()
        {
            ScriptContext            scriptContext      = ScriptContextObjectMother.CreateTestScriptContext();
            const ScriptLanguageType scriptLanguageType = ScriptLanguageType.Python;
            const string             scriptFunctionName = "Test";

            const string scriptText =
                @"def Test() :
  return 'CtorTest'";

            var scriptEnvironment = ScriptEnvironment.Create();

            var script = new ScriptFunction <string> (scriptContext, scriptLanguageType, scriptText, scriptEnvironment, scriptFunctionName);

            Assert.That(script.ScriptContext, Is.EqualTo(scriptContext));
            Assert.That(script.ScriptLanguageType, Is.EqualTo(scriptLanguageType));
            Assert.That(script.ScriptText, Is.EqualTo(scriptText));
            Assert.That(script.Execute(), Is.EqualTo("CtorTest"));
        }
Example #10
0
        int IVsEditorFactory.CreateEditorInstance(uint grfCreateDoc, string pszMkDocument,
                                                  string pszPhysicalView, IVsHierarchy pvHier, uint itemid,
                                                  IntPtr punkDocDataExisting, out IntPtr ppunkDocView, out IntPtr ppunkDocData,
                                                  out string pbstrEditorCaption, out Guid pguidCmdUi, out int pgrfCdw)
        {
            string s;

            pvHier.GetCanonicalName(itemid, out s);
            pgrfCdw          = 0;
            LastDocumentPath = pszMkDocument;
            pguidCmdUi       = VSConstants.GUID_TextEditorFactory;

            ppunkDocData = IntPtr.Zero;
            ppunkDocView = IntPtr.Zero;
            WindowPane editor;

            FileInfo fileInfo = new FileInfo(LastDocumentPath);

            if (fileInfo.Extension.ToLower().Equals(".mysql"))
            {
                editor = new SqlEditorPane(_serviceProvider, this);
            }
            else
            {
                ScriptLanguageType scriptType = ScriptLanguageType.JavaScript;
                if (fileInfo.Extension.ToLower().Equals(".mypy"))
                {
                    scriptType = ScriptLanguageType.Python;
                }


                editor = new MySqlHybridScriptEditorPane(_serviceProvider, this, scriptType);
            }
            if (editor.Window != null)
            {
                ppunkDocData = Marshal.GetIUnknownForObject(editor.Window);
                ppunkDocView = Marshal.GetIUnknownForObject(editor);
            }

            pbstrEditorCaption = "";
            return(VSConstants.S_OK);
        }
Example #11
0
        /// <summary>
        /// Initializes a new script instance. This immediately executes the given
        /// <paramref name="scriptText"/>, and stores a delegate to the <paramref name="scriptFunctionName" />, which is run when <see cref="Execute"/>
        /// is called.
        /// </summary>
        /// <param name="scriptContext">
        ///   The <see cref="ScriptContext"/> to use when executing the script. The script context is used to isolate re-motion modules from each other.
        /// </param>
        /// <param name="scriptLanguageType">The script language to use for the script.</param>
        /// <param name="scriptText">
        ///   The source code of the script. This must be source code matching the language defined by <paramref name="scriptLanguageType"/>. The script
        ///   is immediately executed, i.e., all global statements are immediately run. After that, a delegate defining the main function (defined by
        ///   <paramref name="scriptFunctionName"/>) is stored for later use. When <see cref="Execute"/> is invoked, that function is run and its result
        ///   returned.
        /// </param>
        /// <param name="scriptEnvironment">
        ///   The <see cref="ScriptEnvironment"/> defining the variables and imported symbols the script has access to.
        /// </param>
        /// <param name="scriptFunctionName">
        ///   The name of the script's main function. This function must be defined by <paramref name="scriptText"/>, and it is invoked when the
        ///   script is executed.
        /// </param>
        public ScriptFunction(
            ScriptContext scriptContext,
            ScriptLanguageType scriptLanguageType,
            string scriptText,
            ScriptEnvironment scriptEnvironment,
            string scriptFunctionName)
            : base(
                ArgumentUtility.CheckNotNull("scriptContext", scriptContext),
                scriptLanguageType,
                ArgumentUtility.CheckNotNullOrEmpty("scriptText", scriptText))
        {
            ArgumentUtility.CheckNotNull("scriptEnvironment", scriptEnvironment);
            ArgumentUtility.CheckNotNullOrEmpty("scriptFunctionName", scriptFunctionName);

            var engine       = ScriptingHost.GetScriptEngine(scriptLanguageType);
            var scriptSource = engine.CreateScriptSourceFromString(scriptText, SourceCodeKind.Statements);

            // Immediately execute the script. This will cause all function definitions to be stored as variables. We can then extract the script function
            // and store it as a delegate.
            scriptSource.Execute(scriptEnvironment.ScriptScope);

            _func = scriptEnvironment.ScriptScope.GetVariable <Func <TResult> > (scriptFunctionName);
        }
 /// <summary>
 /// Retrieves the ScriptEngine given by the <paramref name="languageType"/> parameter. Throws if requested engine is not available on system.
 /// </summary>
 /// <remarks>
 /// Note: Executing scripts directly through a <see cref="ScriptEngine"/> will bypass re-motion's Extension Module separation
 /// (see <see cref="ScriptContext"/>). For guaranteed re-motion mixin stable binding use re-motion
 /// <see cref="ScriptFunction{TFixedArg1,TResult}"/>.<see cref="ScriptFunction{TFixedArg1,TResult}.Execute"/> etc instead.
 /// </remarks>
 public static ScriptEngine GetScriptEngine(ScriptLanguageType languageType)
 {
     return(Current.GetEngine(languageType));
 }
 public MySqlHybridScriptEditorPane(ServiceProvider sp, SqlEditorFactory factory, ScriptLanguageType scriptType = ScriptLanguageType.JavaScript)
     : base(sp)
 {
     Factory      = factory;
     DocumentPath = factory.LastDocumentPath;
     _editor      = new MySqlHybridScriptEditor(sp, this, scriptType);
 }
 public static ScriptEngine GetEngine(this ScriptingHost scriptingHost, ScriptLanguageType languageType)
 {
     return((ScriptEngine)PrivateInvoke.InvokeNonPublicMethod(scriptingHost, "GetEngine", languageType));
 }