/// <summary> /// Expand and compile the script to check if the syntax is OK. /// /// This is a "standalone" method. Use this when no Session object is available. /// </summary> /// <param name="scriptFullFileName">The full script file name.</param> /// <param name="includeAndCompileErrors">String containing include and compile errors if existing.</param> /// <returns>Boolean indicating if the script is correct.</returns> public static bool ExpandAndCompile(String scriptFullFileName, out String includeAndCompileErrors) { includeAndCompileErrors = ""; String includeErrors; String compileErrors; // Construct the following objects because this is a "standalone" method. DvtkSession.ScriptSession scriptSession = new Dvtk.Sessions.ScriptSession(); VisualBasicScript visualBasicScript = new VisualBasicScript(scriptSession, Path.GetDirectoryName(scriptFullFileName), Path.GetFileName(scriptFullFileName)); // Get the script host. DvtkScriptSupport.DvtkScriptHost dvtkScriptHost = visualBasicScript.GetDvtkScriptHost(); // Set the source code. dvtkScriptHost.SourceCode = visualBasicScript.GetExpandedContent(out includeErrors); // Add expand results. includeAndCompileErrors += includeErrors; // Compile. visualBasicScript.Compile(dvtkScriptHost, out compileErrors); // Add compile results. includeAndCompileErrors += compileErrors; return(includeAndCompileErrors.Length == 0); }
/// <summary> /// Execute the Visual Basic Script if no include errors and no compile erors exist. /// If present, include errors and compile errors will be logged in the Results file(s). /// </summary> public void Execute() { String includeErrors = ""; String compileErrors = ""; // Start the results gathering the the Results file(s). scriptSession.StartResultsGatheringWithExpandedFileNaming(this.scriptFileName); // Get the script host that will do the actual execution of the Visual Basic Script. DvtkScriptSupport.DvtkScriptHost dvtkScriptHost = GetDvtkScriptHost(); // Set the source code. dvtkScriptHost.SourceCode = GetExpandedContent(out includeErrors); // If include errors exist, log in the Results file(s) and stop execution. if (includeErrors.Length > 0) { scriptSession.WriteError(includeErrors); } else // If no include errors exist... { Compile(dvtkScriptHost, out compileErrors); // If compile errors exist, log in the Results file(s) and stop execution. if (compileErrors.Length > 0) { scriptSession.WriteError(compileErrors); } // If no compile error exist, execute the script. else { try { // Run the script using the script host. dvtkScriptHost.Invoke("DvtkScript", "Main"); } catch (Exception theException) { String invokeError = string.Format("An exception occured while executing Visual Basic Script \"{0}\":\r\n{1}", scriptFullFileName, theException.Message); scriptSession.WriteError(invokeError); } } } // End the results gathering the the Results file(s). scriptSession.EndResultsGathering(); }
/// <summary> /// Compile the script to check if the syntax is OK. /// </summary> /// <param name="dvtkScriptHost">The dvtk script host.</param> /// <param name="compileErrors">If existing, the compile errors.</param> private void Compile(DvtkScriptSupport.DvtkScriptHost dvtkScriptHost, out String compileErrors) { this.compileErrors = ""; // Add the compiler error event handler. DvtkScriptSupport.CompilerErrorEventHandler compilerErrorEventHandler = new DvtkScriptSupport.CompilerErrorEventHandler(OnCompilerError); dvtkScriptHost.CompilerErrorEvent += compilerErrorEventHandler; dvtkScriptHost.Compile(); // Remove the compiler error event handler. dvtkScriptHost.CompilerErrorEvent -= compilerErrorEventHandler; // When a compile error exists, this has been stored in this.compileErrors by the callback mathod. compileErrors = this.compileErrors; }
/// <summary> /// Get a new DvtkScriptHost object and set all relevant properties. /// </summary> /// <returns>The new DvtkScriptHost object.</returns> private DvtkScriptSupport.DvtkScriptHost GetDvtkScriptHost() { DvtkScriptSupport.DvtkScriptHost dvtkScriptHost = null; Random randomValue = new Random(); // Create a new script host in which the script will run. dvtkScriptHost = new DvtkScriptSupport.DvtkScriptHost( DvtkScriptSupport.DvtkScriptLanguage.VB, "dvtk://session/" + this.scriptFileName + randomValue.Next().ToString(), Application.StartupPath); // Set the current active session in the script host dvtkScriptHost.Session = scriptSession; return(dvtkScriptHost); }
/// <summary> /// Execute the Visual Basic Script if no include errors and no compile erors exist. /// /// If present, include errors and compile errors will be logged in the Results file(s). /// </summary> /// <param name="includeError">If an include errors exists, the string will contain an error description.</param> /// <param name="compileError">If a compile errors exists, the string will contain an error description.</param> /// <returns> /// true if no include errors and no compile errors are present. /// false otherwise. /// </returns> public void Execute(object [] theArguments) { String includeErrors = ""; String compileErrors = ""; // Start the results gathering the the Results file(s). scriptSession.StartResultsGatheringWithExpandedFileNaming(this.scriptFileName); // Get the script host that will do the actual execution of the Visual Basic Script. DvtkScriptSupport.DvtkScriptHost dvtkScriptHost = GetDvtkScriptHost(); // Set the source code. dvtkScriptHost.SourceCode = GetExpandedContent(out includeErrors); // If include errors exist, log in the Results file(s) and stop execution. if (includeErrors.Length > 0) { scriptSession.WriteError(includeErrors); } else // If no include errors exist... { Compile(dvtkScriptHost, out compileErrors); // If compile errors exist, log in the Results file(s) and stop execution. if (compileErrors.Length > 0) { scriptSession.WriteError(compileErrors); } // If no compile error exist, execute the script. else { bool invoked = false; try { // Run the script using the script host. dvtkScriptHost.Invoke("DvtkScript", "Main", theArguments); invoked = true; } catch (System.Exception theException) { // String invokeError = string.Format("Unable to invoke Visual Basic Script.\n- Script: {0}\n- Needed entry point: Main\n- Needed module: DvtkScript\n\nException text:\n{1}", scriptFullFileName , theException.Message); // scriptSession.WriteError(invokeError); } catch { // String invokeError = string.Format("Unable to invoke Visual Basic Script.\n- Script: {0}\n- Needed entry point: Main\n- Needed module: DvtkScript", scriptFullFileName); // scriptSession.WriteError(invokeError); } if (!invoked) { try { // Run the script using the script host. dvtkScriptHost.Invoke("DvtkScript", "Main", null); invoked = true; } catch (System.Exception theException) { // String invokeError = string.Format("Unable to invoke Visual Basic Script.\n- Script: {0}\n- Needed entry point: Main\n- Needed module: DvtkScript\n\nException text:\n{1}", scriptFullFileName , theException.Message); // scriptSession.WriteError(invokeError); } catch { // String invokeError = string.Format("Unable to invoke Visual Basic Script.\n- Script: {0}\n- Needed entry point: Main\n- Needed module: DvtkScript", scriptFullFileName); // scriptSession.WriteError(invokeError); } } if (!invoked) { String invokeError = string.Format("Unable to invoke Visual Basic Script."); scriptSession.WriteError(invokeError); } } } // End the results gathering the the Results file(s). scriptSession.EndResultsGathering(); }