public _DotNetScriptTemplate(IZeusContext context) { this.context = context; this.input = context.Input; this.objects = context.Objects; this.output = context.Output; }
public void EngineExecuteGuiCode(IZeusCodeSegment segment, IZeusContext context) { bool assemblyLoaded = false; try { this.Cleanup(); this._codeSegment = segment; assemblyLoaded = this.LoadAssembly(context); if (assemblyLoaded && !HasErrors) { this._currentObject = InstantiateClass( CurrentAssembly, typeof(_DotNetScriptGui), context ); if (this._currentObject is _DotNetScriptGui) { this._interfaceObject = this._currentObject as _DotNetScriptGui; this._interfaceObject.Setup(); } if (context.Gui.ShowGui || context.Gui.ForceDisplay) { OnShowGUI(context.Gui); } this._assemblyStack.Pop(); assemblyLoaded = false; } } catch (Exception ex) { this.Cleanup( assemblyLoaded ); throw ex; } }
public MicrosoftScriptError(MSScriptControl.Error error, IZeusContext context) { if (context != null) this._fileName = context.ExecutingTemplate.FilePath + context.ExecutingTemplate.FileName; this._source = error.Source; this._message = error.Description; this._description = string.Empty; this._stackTrace = error.Text; this._number = error.Number.ToString(); this._line = error.Line; this._column = error.Column; }
public IZeusSavedInput ExecuteTemplateAndCollectInput(IZeusContext context, string templatePath) { ZeusTemplate template = new ZeusTemplate(templatePath); ZeusSavedInput collectedInput = new ZeusSavedInput(); DefaultSettings settings = DefaultSettings.Instance; settings.PopulateZeusContext(context); template.ExecuteAndCollect(context, settings.ScriptTimeout, collectedInput.InputData.InputItems); collectedInput.Save(); return collectedInput; }
public DotNetScriptError(CompilerError error, IZeusContext context) { if (context != null) this._fileName = context.ExecutingTemplate.FilePath + context.ExecutingTemplate.FileName; this._source = error.FileName; this._message = error.ErrorText; this._description = string.Empty; this._number = error.ErrorNumber; this._line = error.Line; this._column = error.Column; this._stackTrace = string.Empty; this._isWarning = error.IsWarning; }
public bool Collect(ZeusTemplate template, IZeusContext context, int timeout, InputItemCollection collectedinput, ShowGUIEventHandler eventhandler) { IZeusExecutionHelper execHelper = null; bool exceptionOccurred = false; bool result = false; try { //Initialize Context for collection collectedinput.CopyTo(context.Gui.Defaults); context.Gui.ForceDisplay = true; collectedinput.Clear(); execHelper = template.GuiSegment.ZeusScriptingEngine.ExecutionHelper; execHelper.Timeout = timeout; if (eventhandler == null) { execHelper.SetShowGuiHandler(new ShowGUIEventHandler(DynamicGUI_Display)); } else { execHelper.SetShowGuiHandler(new ShowGUIEventHandler(eventhandler)); } result = template.GuiSegment.Execute(context); execHelper.Cleanup(); if (collectedinput != null) { collectedinput.Add(context.Input); } } catch (Exception ex) { context.Log.Write(ex); exceptionOccurred = true; } if (!exceptionOccurred && result) { context.Log.Write("Successfully collected input for Template: " + template.Title); } else { context.Log.Write("Canceled Template execution: " + template.Title); } return result; }
private void _ProcessMyMeta(IZeusContext context) { IZeusInput input = context.Input; dbRoot myMeta = null; if (context.Objects.Contains("MyMeta")) myMeta = context.Objects["MyMeta"] as dbRoot; if (myMeta != null) { if (!myMeta.IsConnected) { string driver = null, connectionString = null; if (input.Contains("__dbDriver")) driver = input["__dbDriver"].ToString(); if (input.Contains("__dbConnectionString")) connectionString = input["__dbConnectionString"].ToString(); if ( (driver != null) && (connectionString != null) ) { myMeta.Connect(driver, connectionString); if (input.Contains("__showDefaultDatabaseOnly")) myMeta.ShowDefaultDatabaseOnly = ((string)input["__showDefaultDatabaseOnly"] == bool.TrueString); if (input.Contains("__dbTargetMappingFileName")) myMeta.DbTargetMappingFileName = (string)input["__dbTargetMappingFileName"]; if (input.Contains("__dbLanguageMappingFileName")) myMeta.LanguageMappingFileName = (string)input["__dbLanguageMappingFileName"]; if (input.Contains("__userMetaDataFileName")) myMeta.UserMetaDataFileName = (string)input["__userMetaDataFileName"]; if (input.Contains("__dbTarget")) myMeta.DbTarget = (string)input["__dbTarget"]; if (input.Contains("__language")) myMeta.Language = (string)input["__language"]; if (input.Contains("__domainOverride")) myMeta.DomainOverride = (bool)input["__domainOverride"]; } } } }
public void ExecuteTemplate(IZeusContext context, string templatePath, string inputFilePath) { ZeusTemplate template = new ZeusTemplate(templatePath); ZeusSavedInput savedInput = null; DefaultSettings settings = DefaultSettings.Instance; if (!string.IsNullOrEmpty(inputFilePath)) { savedInput = new ZeusSavedInput(inputFilePath); } context.Log.Write("Executing: " + template.Title); if (savedInput != null) { context.Input.AddItems(savedInput.InputData.InputItems); template.Execute(context, settings.ScriptTimeout, true); } else { settings.PopulateZeusContext(context); template.Execute(context, settings.ScriptTimeout, false); } }
private object InstantiateClass(Assembly assembly, System.Type matchingType, IZeusContext context) { Object obj = null; //cant call the entry method if the assembly is null if (assembly != null) { bool classFound = false; //Use reflection to call the static Run function Module[] mods = assembly.GetModules(false); Type[] types = mods[0].GetTypes(); //loop through each class that was defined and look for the first occurrance of the entry point method foreach (Type type in types) { if (type.IsSubclassOf(matchingType) && !type.IsAbstract) { ConstructorInfo[] constructors = type.GetConstructors(); ConstructorInfo constructor = constructors[0]; obj = constructor.Invoke(BindingFlags.CreateInstance | BindingFlags.OptionalParamBinding, null, new object[] { context }, null); classFound = true; break; } } //if it got here, then there was no entry point method defined. Tell user about it if (!classFound) { throw new Exception("Entry creating \"" + matchingType.Name + "\" was either not found on any classes or had an invalid signature."); } } return(obj); }
/// <summary> /// Populate the context object with any deafult settings. /// Here we are setting the default connection information /// and the output/template paths. /// </summary> /// <param name="context"></param> public void Process(IZeusContext context) { this._ProcessMyMeta(context); }
public void EngineExecuteCode(IZeusCodeSegment segment, IZeusContext context) { // If the segment isn't empty, execute it if (!segment.IsEmpty) { if (!HasErrors) { int step = 0; try { if (context.ExecutionDepth > 1) { ScriptStack.Push(new MSScriptControl.ScriptControl()); } MicrosoftScriptControl.Timeout = (_timeout == -1 ? MSScriptControl.ScriptControlConstants.NoTimeout : (_timeout * 1000)); MicrosoftScriptControl.Language = segment.Language; MicrosoftScriptControl.AllowUI = true; MicrosoftScriptControl.UseSafeSubset = false; MicrosoftScriptControl.AddObject("output", context.Output, true); MicrosoftScriptControl.AddObject("input", context.Input, true); MicrosoftScriptControl.AddObject("context", context, true); foreach (string key in context.Objects.Keys) { if ((key != "output") && (key != "input")) { MicrosoftScriptControl.AddObject(key, context.Objects[key], true); } } string entryCode = this.ScriptingEntryCall(segment.Language) as String; string entryCall = BODY_ENTRY_NAME; step = 1; MicrosoftScriptControl.AddCode(segment.Code); MicrosoftScriptControl.AddCode(entryCode); step = 2; object[] paramList = new object[0]; object tmpReturn = MicrosoftScriptControl.Run(entryCall, ref paramList); } catch (System.Runtime.InteropServices.COMException) { MicrosoftScriptError error = new MicrosoftScriptError(MicrosoftScriptControl.Error, context); if (step < 2) { error.IsRuntime = false; } this.AddError(error); } catch (Exception ex) { if ((MicrosoftScriptControl.Error == null) || (MicrosoftScriptControl.Error.Description == null)) { throw ex; } else { MicrosoftScriptError error = new MicrosoftScriptError(MicrosoftScriptControl.Error, context); if (step < 2) { error.IsRuntime = false; } this.AddError(error); } } finally { MicrosoftScriptControl.Reset(); if (context.ExecutionDepth > 1) { ScriptStack.Pop(); } } } } }
public IZeusContext Execute(ZeusTemplate template, IZeusContext context, int timeout, bool skipGui) { return(this.Execute(template, context, timeout, null, null, skipGui)); }
public IZeusContext ExecuteAndCollect(ZeusTemplate template, IZeusContext context, int timeout, InputItemCollection inputitems) { return this.Execute(template, context, timeout, inputitems, null, false); }
public DotNetScriptGui(IZeusContext context) : base(context) { this.ui = context.Objects["ui"] as Zeus.UserInterface.GuiController; this.MyMeta = context.Objects["MyMeta"] as MyMeta.dbRoot; this.DnpUtils = context.Objects["DnpUtils"] as Dnp.Utils.Utils; }
public void EngineExecuteCode(IZeusCodeSegment segment, IZeusContext context) { // If the segment isn't empty, execute it if (!segment.IsEmpty) { if (!HasErrors) { int step = 0; try { if (context.ExecutionDepth > 1) { ScriptStack.Push( new MSScriptControl.ScriptControl() ); } MicrosoftScriptControl.Timeout = (_timeout == -1 ? MSScriptControl.ScriptControlConstants.NoTimeout : (_timeout * 1000) ); MicrosoftScriptControl.Language = segment.Language; MicrosoftScriptControl.AllowUI = true; MicrosoftScriptControl.UseSafeSubset = false; MicrosoftScriptControl.AddObject("output", context.Output, true); MicrosoftScriptControl.AddObject("input", context.Input, true); MicrosoftScriptControl.AddObject("context", context, true); foreach (string key in context.Objects.Keys) { if ((key != "output") && (key != "input")) MicrosoftScriptControl.AddObject(key, context.Objects[key], true); } string entryCode = this.ScriptingEntryCall(segment.Language) as String; string entryCall = BODY_ENTRY_NAME; step = 1; MicrosoftScriptControl.AddCode(segment.Code); MicrosoftScriptControl.AddCode(entryCode); step = 2; object[] paramList = new object[0]; object tmpReturn = MicrosoftScriptControl.Run(entryCall, ref paramList); } catch (System.Runtime.InteropServices.COMException) { MicrosoftScriptError error = new MicrosoftScriptError(MicrosoftScriptControl.Error, context); if (step < 2) error.IsRuntime = false; this.AddError(error); } catch (Exception ex) { if ((MicrosoftScriptControl.Error == null) || (MicrosoftScriptControl.Error.Description == null)) { throw ex; } else { MicrosoftScriptError error = new MicrosoftScriptError(MicrosoftScriptControl.Error, context); if (step < 2) error.IsRuntime = false; this.AddError(error); } } finally { MicrosoftScriptControl.Reset(); if (context.ExecutionDepth > 1) ScriptStack.Pop(); } } } }
public IZeusContext Execute(ZeusTemplate template, IZeusContext context, int timeout, InputItemCollection collectedinput, ShowGUIEventHandler eventhandler, bool skipGui) { IZeusExecutionHelper execHelper = null; bool exceptionOccurred = false; bool result = false; try { if (skipGui) { PopulateContextObjects(context); foreach (IZeusContextProcessor processor in ZeusFactory.Preprocessors) { processor.Process(context); } result = true; } else { execHelper = template.GuiSegment.ZeusScriptingEngine.ExecutionHelper; execHelper.Timeout = timeout; if (eventhandler == null) { execHelper.SetShowGuiHandler(new ShowGUIEventHandler(DynamicGUI_Display)); } else { execHelper.SetShowGuiHandler(new ShowGUIEventHandler(eventhandler)); } result = template.GuiSegment.Execute(context); execHelper.Cleanup(); if (collectedinput != null) { collectedinput.Add(context.Input); } } if (result) { execHelper = template.BodySegment.ZeusScriptingEngine.ExecutionHelper; execHelper.Timeout = timeout; result = template.BodySegment.Execute(context); } } catch (Exception ex) { context.Log.Write(ex); exceptionOccurred = true; } if (!exceptionOccurred && result) { context.Log.Write("Successfully rendered Template: " + template.Title); } else { context.Log.Write("Canceled Template execution: " + template.Title); } return(context); }
public _DotNetScriptGui(IZeusContext context) { this.context = context; this.input = context.Input; this.objects = context.Objects; }
public bool Collect(ZeusTemplate template, IZeusContext context, int timeout, InputItemCollection inputitems) { return(this.Collect(template, context, timeout, inputitems, null)); }
protected static void PopulateContextObjects(IZeusContext icontext) { ZeusContext context = icontext as ZeusContext; if (icontext != null) { ZeusConfig config = ZeusConfig.Current; context.SetIntrinsicObjects(ZeusFactory.IntrinsicObjectsArray); foreach (ZeusIntrinsicObject obj in config.IntrinsicObjects) { Type intrinsicObjectType = null; Assembly newassembly = null; object[] objs = null; if (!context.Objects.Contains(obj.VariableName) && !obj.Disabled) { newassembly = obj.Assembly; // First thing, try to create the type without knowing the assembly. /*try * { * intrinsicObjectType = Type.GetType(obj.ClassPath); * } * catch * { * intrinsicObjectType = null; * }*/ if (intrinsicObjectType == null) { try { if (obj.AssemblyPath != string.Empty) { string assemblyPath = obj.AssemblyPath; if (newassembly == null) { assemblyPath = FileTools.ResolvePath(assemblyPath, true); FileInfo finf = new FileInfo(assemblyPath); FileInfo callingfinf = new FileInfo(Assembly.GetCallingAssembly().Location); if (callingfinf.FullName == finf.FullName) { newassembly = Assembly.GetCallingAssembly(); } } if (newassembly == null) { throw new ZeusDynamicException(ZeusDynamicExceptionType.IntrinsicObjectPluginInvalid, "Invalid Assembly: " + assemblyPath); } else { intrinsicObjectType = newassembly.GetType(obj.ClassPath); } } else { intrinsicObjectType = Type.GetType(obj.ClassPath); } } catch (ZeusDynamicException zex) { context.Objects[obj.VariableName] = zex.Message; } } if (intrinsicObjectType != null) { try { if (intrinsicObjectType != null) { if (newassembly != null) { objs = DynamicAssemblyTools.InstantiateClassesByType(newassembly, newassembly.GetType(obj.ClassPath)); } else { objs = new object[1]; objs[0] = DynamicAssemblyTools.InstantiateClassByType(intrinsicObjectType); } } else { throw new ZeusDynamicException(ZeusDynamicExceptionType.IntrinsicObjectPluginInvalid, "Invalid Type: " + obj.ClassPath); } if (objs != null) { if (objs.Length > 0) { context.Objects[obj.VariableName] = objs[0]; } } } catch (ZeusDynamicException zex) { context.Objects[obj.VariableName] = zex.Message; } } } } } }
public IZeusContext ExecuteAndCollect(ZeusTemplate template, IZeusContext context, int timeout, InputItemCollection inputitems) { return(this.Execute(template, context, timeout, inputitems, null, false)); }
static protected void ExecuteCode(IZeusCodeSegment segment, IZeusContext context) { IZeusExecutionHelper helper = segment.ZeusScriptingEngine.ExecutionHelper; ExecuteCode(helper, segment.ITemplate, context, new ArrayList()); }
//public IZeusContext Execute(IZeusContext context, int timeout, ILog log, bool skipGui) public IZeusContext Execute(IZeusContext context, int timeout, bool skipGui) { ZeusExecutioner exec = new ZeusExecutioner(context.Log); return exec.Execute(this, context, timeout, null, null, skipGui); }
public void ExecuteTemplate(IZeusContext context, string templateFilePath) { this.ExecuteTemplate(context, templateFilePath, null); }
//public bool Collect(IZeusContext context, int timeout, InputItemCollection inputitems, ILog log) public bool Collect(IZeusContext context, int timeout, InputItemCollection inputitems) { ZeusExecutioner exec = new ZeusExecutioner(context.Log); return exec.Collect(this, context, timeout, inputitems); }
public bool Execute(IZeusContext context) { return(ZeusExecutioner.ExecuteCodeSegment(this, context)); }
private void _ProcessMyMeta(IZeusContext context) { IZeusInput input = context.Input; dbRoot myMeta = null; if (context.Objects.Contains("MyMeta")) { myMeta = context.Objects["MyMeta"] as dbRoot; } if (myMeta != null) { if (!myMeta.IsConnected) { string driver = null, connectionString = null; if (input.Contains("__dbDriver")) { driver = input["__dbDriver"].ToString(); } if (input.Contains("__dbConnectionString")) { connectionString = input["__dbConnectionString"].ToString(); } if ((driver != null) && (connectionString != null)) { myMeta.Connect(driver, connectionString); if (input.Contains("__showDefaultDatabaseOnly")) { myMeta.ShowDefaultDatabaseOnly = ((string)input["__showDefaultDatabaseOnly"] == bool.TrueString); } if (input.Contains("__dbTargetMappingFileName")) { myMeta.DbTargetMappingFileName = (string)input["__dbTargetMappingFileName"]; } if (input.Contains("__dbLanguageMappingFileName")) { myMeta.LanguageMappingFileName = (string)input["__dbLanguageMappingFileName"]; } if (input.Contains("__userMetaDataFileName")) { myMeta.UserMetaDataFileName = (string)input["__userMetaDataFileName"]; } if (input.Contains("__dbTarget")) { myMeta.DbTarget = (string)input["__dbTarget"]; } if (input.Contains("__language")) { myMeta.Language = (string)input["__language"]; } if (input.Contains("__domainOverride")) { myMeta.DomainOverride = (bool)input["__domainOverride"]; } } } } }
/// <summary> /// Creates a new GuiController object. /// </summary> public GuiController(IZeusContext context) { this.Top = Int32.MinValue; this.Left = Int32.MinValue; this._context = context; }
static protected void ExecuteCode(IZeusExecutionHelper helper, IZeusTemplate template, IZeusContext context, ArrayList templateGroupIds) { if (!template.BodySegment.IsEmpty) { try { // Execute Template Body helper.EngineExecuteCode(template.BodySegment, context); if (helper.HasErrors) { IZeusExecutionError[] errors = helper.Errors; helper.ClearErrors(); throw new ZeusExecutionException(template, errors, true); } } catch (Exception ex) { throw new ZeusRuntimeException(template, ex, true); } } if (template.Type == ZeusConstants.Types.GROUP) { if (template.IncludedTemplates.Count > 0) { // Execute Template Body if (templateGroupIds.Contains(template.UniqueID)) { return; } templateGroupIds.Add(template.UniqueID); foreach (ZeusTemplate childTemplate in template.IncludedTemplates) { if (childTemplate.UniqueID != template.UniqueID) { //clear the output buffer before executing the next template! context.Output.clear(); //Push the current template onto the Execution stack if (context is ZeusContext) { ((ZeusContext)context).TemplateStack.Push(childTemplate); } try { IZeusScriptingEngine engine = ZeusFactory.GetEngine(childTemplate.BodySegment.Engine); ExecuteCode(engine.ExecutionHelper, childTemplate, context, templateGroupIds); } finally { //Pop the current template off of the Execution stack if (context is ZeusContext) { ((ZeusContext)context).TemplateStack.Pop(); } } } } } } }
/// <summary> /// This caches template input to be retrieved with <see cref="ReadInputFromCache"/>. /// Thanks to Mike Griffin for contributing these. /// </summary> /// <example> /// At the top of your ui code you add this: /// <code> /// Sub setup() /// DnpUtils.ReadInputFromCache(context); /// .... /// End Sub /// </code> /// Then at the top of your template body you add: /// <code> /// DnpUtils.SaveInputToCache(context); /// </code> /// After you do that try running your template, /// it will come up with the last settings you had (no project files required) /// as long as you haven't closed down MyGeneration. /// You can even alternate between two templates and it will remember /// the input for each. /// </example> public void SaveInputToCache(IZeusContext theContext) { Hashtable h; if(cache.ContainsKey(theContext.ExecutingTemplate.UniqueID.ToString())) { h = cache[theContext.ExecutingTemplate.UniqueID.ToString()] as Hashtable; h.Clear(); } else { h = new Hashtable(); cache[theContext.ExecutingTemplate.UniqueID.ToString()] = h; } foreach(string key in theContext.Input.Keys) { switch(key) { // Eliminate the default input variables case "__dbDriver": case "dbDriver": case "__language": case "__domainOverride": case "__dbConnectionString": case "defaultTemplatePath": case "__defaultOutputPath": case "__dbTarget": case "__dbTargetMappingFileName": case "__dbLanguageMappingFileName": case "defaultOutputPath": case "__userMetaDataFileName": case "__defaultTemplatePath": case "__version": case "dbConnectionString": break; default: // This is truly a user setting // theContext.Output.writeln(key); h[key] = theContext.Input[key]; break; } } }
protected bool LoadAssembly(IZeusContext context) { bool assemblyLoaded = false; bool cacheAssembly = (_codeSegment.ITemplate.SourceType == ZeusConstants.SourceTypes.COMPILED); Assembly newAssembly = null; List <DotNetScriptError> errors = null; if ((cacheAssembly) && (_codeSegment.CachedAssembly != null)) { newAssembly = this._codeSegment.CachedAssembly; } else { this._compiledInMemory = true; List <string> references = new List <string>(); List <string> namespaces = new List <string>(); CompilerVersion = Zeus.Configuration.ZeusConfig.Current.CompilerVersion; string[] array; foreach (object obj in _codeSegment.ExtraData) { if (obj is String[]) { array = (string[])obj; if ((array.Length == 2) && (array[0] == DotNetScriptEngine.DLLREF)) { if (!references.Contains(array[1])) { references.Add(array[1]); } } if ((array.Length == 2) && (array[0] == DotNetScriptEngine.DEBUG)) { this._compiledInMemory = false; } if ((array.Length == 2) && (array[0] == DotNetScriptEngine.VERSION)) { this.CompilerVersion = array[1]; } } } foreach (string reference in _engine.BuildDLLNames(context)) { if (!references.Contains(reference)) { references.Add(reference); } } if (cacheAssembly) { _compiledInMemory = false; } DotNetLanguage lang = (_codeSegment.Language == ZeusConstants.Languages.CSHARP) ? DotNetLanguage.CSharp : DotNetLanguage.VBNet; newAssembly = CreateAssembly(_codeSegment.Code, CompilerVersion, lang, _compiledInMemory, references, out errors, context); if (cacheAssembly && (newAssembly != null)) { _codeSegment.CachedAssembly = newAssembly; } } if (errors != null) { foreach (DotNetScriptError error in errors) { this.AddError(error); } } else { this._assemblyStack.Push(newAssembly); assemblyLoaded = true; } return(assemblyLoaded); }
/// <summary> /// This retrieves cached template input. See <see cref="SaveInputToCache"/>. /// Thanks to Mike Griffin for contributing these. /// </summary> /// <example> /// At the top of your ui code you add this: /// <code> /// Sub setup() /// DnpUtils.ReadInputFromCache(context); /// .... /// End Sub /// </code> /// Then at the top of your template body you add: /// <code> /// DnpUtils.SaveInputToCache(context); /// </code> /// After you do that try running your template, /// it will come up with the last settings you had (no project files required) /// as long as you haven't closed down MyGeneration. /// You can even alternate between two templates and it will remember /// the input for each. /// </example> public void ReadInputFromCache(IZeusContext theContext) { if(cache.ContainsKey(theContext.ExecutingTemplate.UniqueID.ToString())) { Hashtable h = cache[theContext.ExecutingTemplate.UniqueID.ToString()] as Hashtable; foreach(string key in h.Keys) { theContext.Gui.Defaults[key] = h[key]; } } }
public static Assembly CreateAssembly(string code, string compilerVersion, DotNetLanguage language, bool compileInMemory, List <string> references, out List <DotNetScriptError> errors, IZeusContext context) { string tmpDirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = RootFolder; Assembly generatedAssembly = null; string ext = ".cs", lang = "C#"; errors = null; //Create an instance whichever code provider that is needed CodeDomProvider codeProvider = null; // string extraParams; if (language == DotNetLanguage.VBNet) { lang = "VB"; ext = ".vb"; codeProvider = new VBCodeProvider(new Dictionary <string, string>() { { "CompilerVersion", compilerVersion } }); } else { codeProvider = new CSharpCodeProvider(new Dictionary <string, string>() { { "CompilerVersion", compilerVersion } }); } //add compiler parameters CompilerParameters compilerParams = new CompilerParameters(); compilerParams.CompilerOptions = "/target:library /optimize"; // + extraParams; CompilerResults results = null; // Add References if (!references.Contains("mscorlib.dll")) { references.Add("mscorlib.dll"); } if (!references.Contains("System.dll")) { references.Add("System.dll"); } if (!references.Contains("System.EnterpriseServices.dll")) { references.Add("System.EnterpriseServices.dll"); } if (!references.Contains("Zeus.dll")) { references.Add("Zeus.dll"); } if (!references.Contains("PluginInterfaces.dll")) { references.Add("PluginInterfaces.dll"); } foreach (string reference in Zeus.Configuration.ZeusConfig.Current.TemplateReferences) { if (!references.Contains(reference)) { references.Add(reference); } } foreach (string reference in references) { compilerParams.ReferencedAssemblies.Add(reference); } if (compileInMemory) { compilerParams.GenerateExecutable = false; compilerParams.IncludeDebugInformation = false; compilerParams.GenerateInMemory = true; results = codeProvider.CompileAssemblyFromSource(compilerParams, code); } else { string guid = Guid.NewGuid().ToString(); string assemblyname = DotNetScriptEngine.DEBUG_FILE_PREFIX + guid + ".dll";; string codefilename = DotNetScriptEngine.DEBUG_FILE_PREFIX + guid + ext; StreamWriter writer = File.CreateText(codefilename); writer.Write(code); writer.Flush(); writer.Close(); compilerParams.GenerateExecutable = false; compilerParams.IncludeDebugInformation = true; compilerParams.GenerateInMemory = false; compilerParams.OutputAssembly = assemblyname; results = codeProvider.CompileAssemblyFromFile(compilerParams, codefilename); } //Do we have any compiler errors if (results.Errors.HasErrors) { errors = new List <DotNetScriptError>(); foreach (CompilerError compileError in results.Errors) { errors.Add(new DotNetScriptError(compileError, context)); } } else { //get a hold of the actual assembly that was generated generatedAssembly = results.CompiledAssembly; } System.Environment.CurrentDirectory = tmpDirectory; //return the assembly return(generatedAssembly); }
public void PopulateZeusContext(IZeusContext context) { DefaultSettings settings = new DefaultSettings(); IZeusInput input = context.Input; if (!input.Contains("__version")) { Assembly ver = System.Reflection.Assembly.GetEntryAssembly(); input["__version"] = ver.GetName().Version.ToString(); } //-- BEGIN LEGACY VARIABLE SUPPORT ----- if (!input.Contains("defaultTemplatePath")) { input["defaultTemplatePath"] = settings.DefaultTemplateDirectory; } if (!input.Contains("defaultOutputPath")) { input["defaultOutputPath"] = settings.DefaultOutputDirectory; } //-- END LEGACY VARIABLE SUPPORT ------- if (!input.Contains("__defaultTemplatePath")) { input["__defaultTemplatePath"] = settings.DefaultTemplateDirectory; } if (!input.Contains("__defaultOutputPath")) { input["__defaultOutputPath"] = settings.DefaultOutputDirectory; } if (settings.DbDriver != string.Empty) { //-- BEGIN LEGACY VARIABLE SUPPORT ----- if (!input.Contains("dbDriver")) { input["dbDriver"] = settings.DbDriver; } if (!input.Contains("dbConnectionString")) { input["dbConnectionString"] = settings.DomainOverride; } //-- END LEGACY VARIABLE SUPPORT ------- if (!input.Contains("__dbDriver")) { input["__dbDriver"] = settings.DbDriver; } if (!input.Contains("__dbConnectionString")) { input["__dbConnectionString"] = settings.ConnectionString; } if (!input.Contains("__domainOverride")) { input["__domainOverride"] = settings.DomainOverride; } if ((settings.DbTarget != string.Empty) && (!input.Contains("__dbTarget"))) { input["__dbTarget"] = settings.DbTarget; } if ((settings.DbTargetMappingFile != string.Empty) && (!input.Contains("__dbTargetMappingFileName"))) { input["__dbTargetMappingFileName"] = settings.DbTargetMappingFile; } if ((settings.LanguageMappingFile != string.Empty) && (!input.Contains("__dbLanguageMappingFileName"))) { input["__dbLanguageMappingFileName"] = settings.LanguageMappingFile; } if ((settings.Language != string.Empty) && (!input.Contains("__language"))) { input["__language"] = settings.Language; } if ((settings.UserMetaDataFileName != string.Empty) && (!input.Contains("__userMetaDataFileName"))) { input["__userMetaDataFileName"] = settings.UserMetaDataFileName; } } }
public void EngineExecuteGuiCode(IZeusCodeSegment segment, IZeusContext context) { // If the template has an interface block, execute it if (!segment.IsEmpty) { if (!HasErrors) { int step = 0; try { MicrosoftScriptControl.Timeout = (_timeout == -1 ? MSScriptControl.ScriptControlConstants.NoTimeout : (_timeout * 1000)); MicrosoftScriptControl.Language = segment.Language; MicrosoftScriptControl.AllowUI = true; MicrosoftScriptControl.UseSafeSubset = false; MicrosoftScriptControl.Reset(); MicrosoftScriptControl.AddObject("input", context.Input, true); MicrosoftScriptControl.AddObject("context", context, true); foreach (string key in context.Objects.Keys) { if (key != "input") { MicrosoftScriptControl.AddObject(key, context.Objects[key], true); } } step = 1; MicrosoftScriptControl.AddCode(segment.Code); step = 2; object[] paramList = new object[0]; object tmpReturn = MicrosoftScriptControl.Run(GUI_ENTRY_NAME, ref paramList); if (context.Gui.ShowGui || context.Gui.ForceDisplay) { OnShowGUI(context.Gui); } } catch (System.Runtime.InteropServices.COMException) { MicrosoftScriptError error = new MicrosoftScriptError(MicrosoftScriptControl.Error, context); if (step < 2) { error.IsRuntime = false; } this.AddError(error); } catch (Exception ex) { if ((MicrosoftScriptControl.Error == null) || (MicrosoftScriptControl.Error.Description == null)) { throw ex; } else { MicrosoftScriptError error = new MicrosoftScriptError(MicrosoftScriptControl.Error, context); if (step < 2) { error.IsRuntime = false; } this.AddError(error); } } MicrosoftScriptControl.Reset(); } } }
private object InstantiateClass(Assembly assembly, System.Type matchingType, IZeusContext context) { Object obj = null; //cant call the entry method if the assembly is null if (assembly != null) { bool classFound = false; //Use reflection to call the static Run function Module[] mods = assembly.GetModules(false); Type[] types = mods[0].GetTypes(); //loop through each class that was defined and look for the first occurrance of the entry point method foreach (Type type in types) { if (type.IsSubclassOf(matchingType) && !type.IsAbstract) { ConstructorInfo[] constructors = type.GetConstructors(); ConstructorInfo constructor = constructors[0]; obj = constructor.Invoke(BindingFlags.CreateInstance | BindingFlags.OptionalParamBinding, null, new object[] {context}, null); classFound = true; break; } } //if it got here, then there was no entry point method defined. Tell user about it if (!classFound) { throw new Exception("Entry creating \"" + matchingType.Name + "\" was either not found on any classes or had an invalid signature."); } } return obj; }
public bool Collect(ZeusTemplate template, IZeusContext context, int timeout, InputItemCollection inputitems) { return this.Collect(template, context, timeout, inputitems, null); }
public IZeusContext Execute(ZeusTemplate template, IZeusContext context, int timeout, InputItemCollection collectedinput, ShowGUIEventHandler eventhandler, bool skipGui) { IZeusExecutionHelper execHelper = null; bool exceptionOccurred = false; bool result = false; try { if (skipGui) { PopulateContextObjects(context); foreach (IZeusContextProcessor processor in ZeusFactory.Preprocessors) { processor.Process(context); } result = true; } else { execHelper = template.GuiSegment.ZeusScriptingEngine.ExecutionHelper; execHelper.Timeout = timeout; if (eventhandler == null) { execHelper.SetShowGuiHandler(new ShowGUIEventHandler(DynamicGUI_Display)); } else { execHelper.SetShowGuiHandler(new ShowGUIEventHandler(eventhandler)); } result = template.GuiSegment.Execute(context); execHelper.Cleanup(); if (collectedinput != null) { collectedinput.Add(context.Input); } } if (result) { execHelper = template.BodySegment.ZeusScriptingEngine.ExecutionHelper; execHelper.Timeout = timeout; result = template.BodySegment.Execute(context); } } catch (Exception ex) { context.Log.Write(ex); exceptionOccurred = true; } if (!exceptionOccurred && result) { context.Log.Write("Successfully rendered Template: " + template.Title); } else { context.Log.Write("Canceled Template execution: " + template.Title); } return context; }
protected bool LoadAssembly(IZeusContext context) { bool assemblyLoaded = false; bool cacheAssembly = (_codeSegment.ITemplate.SourceType == ZeusConstants.SourceTypes.COMPILED); Assembly newAssembly = null; List<DotNetScriptError> errors = null; if ((cacheAssembly) && (_codeSegment.CachedAssembly != null)) { newAssembly = this._codeSegment.CachedAssembly; } else { this._compiledInMemory = true; List<string> references = new List<string>(); List<string> namespaces = new List<string>(); CompilerVersion = Zeus.Configuration.ZeusConfig.Current.CompilerVersion; string[] array; foreach (object obj in _codeSegment.ExtraData) { if (obj is String[]) { array = (string[])obj; if ((array.Length == 2) && (array[0] == DotNetScriptEngine.DLLREF)) { if (!references.Contains(array[1])) references.Add(array[1]); } if ((array.Length == 2) && (array[0] == DotNetScriptEngine.DEBUG)) { this._compiledInMemory = false; } if ((array.Length == 2) && (array[0] == DotNetScriptEngine.VERSION)) { this.CompilerVersion = array[1]; } } } foreach (string reference in _engine.BuildDLLNames(context)) { if (!references.Contains(reference)) references.Add(reference); } if (cacheAssembly) { _compiledInMemory = false; } DotNetLanguage lang = (_codeSegment.Language == ZeusConstants.Languages.CSHARP) ? DotNetLanguage.CSharp : DotNetLanguage.VBNet; newAssembly = CreateAssembly(_codeSegment.Code, CompilerVersion, lang, _compiledInMemory, references, out errors, context); if (cacheAssembly && (newAssembly != null)) { _codeSegment.CachedAssembly = newAssembly; } } if (errors != null) { foreach (DotNetScriptError error in errors) this.AddError(error); } else { this._assemblyStack.Push(newAssembly); assemblyLoaded = true; } return assemblyLoaded; }
//public IZeusContext Execute(IZeusContext context, int timeout, ILog log, bool skipGui) public IZeusContext Execute(IZeusContext context, int timeout, bool skipGui) { ZeusExecutioner exec = new ZeusExecutioner(context.Log); return(exec.Execute(this, context, timeout, null, null, skipGui)); }
public static Assembly CreateAssembly(string code, string compilerVersion, DotNetLanguage language, bool compileInMemory, List<string> references, out List<DotNetScriptError> errors, IZeusContext context) { string tmpDirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = RootFolder; Assembly generatedAssembly = null; string ext = ".cs", lang = "C#"; errors = null; //Create an instance whichever code provider that is needed CodeDomProvider codeProvider = null; // string extraParams; if (language == DotNetLanguage.VBNet) { lang = "VB"; ext = ".vb"; codeProvider = new VBCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", compilerVersion } }); } else { codeProvider = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", compilerVersion } }); } //add compiler parameters CompilerParameters compilerParams = new CompilerParameters(); compilerParams.CompilerOptions = "/target:library /optimize"; // + extraParams; CompilerResults results = null; // Add References if (!references.Contains("mscorlib.dll")) references.Add("mscorlib.dll"); if (!references.Contains("System.dll")) references.Add("System.dll"); if (!references.Contains("System.EnterpriseServices.dll")) references.Add("System.EnterpriseServices.dll"); if (!references.Contains("Zeus.dll")) references.Add("Zeus.dll"); if (!references.Contains("PluginInterfaces.dll")) references.Add("PluginInterfaces.dll"); foreach (string reference in Zeus.Configuration.ZeusConfig.Current.TemplateReferences) { if (!references.Contains(reference)) references.Add(reference); } foreach (string reference in references) { compilerParams.ReferencedAssemblies.Add(reference); } if (compileInMemory) { compilerParams.GenerateExecutable = false; compilerParams.IncludeDebugInformation = false; compilerParams.GenerateInMemory = true; results = codeProvider.CompileAssemblyFromSource(compilerParams, code); } else { string guid = Guid.NewGuid().ToString(); string assemblyname = DotNetScriptEngine.DEBUG_FILE_PREFIX + guid + ".dll";; string codefilename = DotNetScriptEngine.DEBUG_FILE_PREFIX + guid + ext; StreamWriter writer = File.CreateText(codefilename); writer.Write(code); writer.Flush(); writer.Close(); compilerParams.GenerateExecutable = false; compilerParams.IncludeDebugInformation = true; compilerParams.GenerateInMemory = false; compilerParams.OutputAssembly = assemblyname; results = codeProvider.CompileAssemblyFromFile(compilerParams, codefilename); } //Do we have any compiler errors if (results.Errors.HasErrors) { errors = new List<DotNetScriptError>(); foreach (CompilerError compileError in results.Errors) { errors.Add(new DotNetScriptError(compileError, context)); } } else { //get a hold of the actual assembly that was generated generatedAssembly = results.CompiledAssembly; } System.Environment.CurrentDirectory = tmpDirectory; //return the assembly return generatedAssembly; }
//public bool Collect(IZeusContext context, int timeout, InputItemCollection inputitems, ILog log) public bool Collect(IZeusContext context, int timeout, InputItemCollection inputitems) { ZeusExecutioner exec = new ZeusExecutioner(context.Log); return(exec.Collect(this, context, timeout, inputitems)); }
public void EngineExecuteCode(IZeusCodeSegment segment, IZeusContext context) { bool assemblyLoaded = false; try { this.Cleanup(); this._codeSegment = segment; assemblyLoaded = this.LoadAssembly(context); if (assemblyLoaded && !HasErrors) { this._currentObject = InstantiateClass( CurrentAssembly, typeof(_DotNetScriptTemplate), context ); if (this._currentObject is _DotNetScriptTemplate) { this._primaryObject = this._currentObject as _DotNetScriptTemplate; this._primaryObject.Render(); } this._assemblyStack.Pop(); assemblyLoaded = false; } } catch (Exception ex) { this.Cleanup( assemblyLoaded ); throw ex; } }
public void ExecuteProject(IZeusContext context, string projectFilePath) { ExecuteProjectModule(context, projectFilePath); }
public void ExecuteProjectModule(IZeusContext context, string projectFilePath, params string[] modules) { ZeusProject proj = new ZeusProject(projectFilePath); DefaultSettings settings = DefaultSettings.Instance; if (modules.Length == 0) { context.Log.Write("Executing: " + proj.Name); proj.Execute(settings.ScriptTimeout, context.Log); } else { foreach (string mod in modules) { context.Log.Write("Executing: " + mod); ExecuteModules(context, proj, new List<string>(modules), settings.ScriptTimeout); } } }
public void EngineExecuteGuiCode(IZeusCodeSegment segment, IZeusContext context) { // If the template has an interface block, execute it if (!segment.IsEmpty) { if (!HasErrors) { int step = 0; try { MicrosoftScriptControl.Timeout = (_timeout == -1 ? MSScriptControl.ScriptControlConstants.NoTimeout : (_timeout * 1000)); MicrosoftScriptControl.Language = segment.Language; MicrosoftScriptControl.AllowUI = true; MicrosoftScriptControl.UseSafeSubset = false; MicrosoftScriptControl.Reset(); MicrosoftScriptControl.AddObject("input", context.Input, true); MicrosoftScriptControl.AddObject("context", context, true); foreach (string key in context.Objects.Keys) { if (key != "input") MicrosoftScriptControl.AddObject(key, context.Objects[key], true); } step = 1; MicrosoftScriptControl.AddCode(segment.Code); step = 2; object[] paramList = new object[0]; object tmpReturn = MicrosoftScriptControl.Run(GUI_ENTRY_NAME, ref paramList); if (context.Gui.ShowGui || context.Gui.ForceDisplay) { OnShowGUI(context.Gui); } } catch (System.Runtime.InteropServices.COMException) { MicrosoftScriptError error = new MicrosoftScriptError(MicrosoftScriptControl.Error, context); if (step < 2) error.IsRuntime = false; this.AddError(error); } catch (Exception ex) { if ((MicrosoftScriptControl.Error == null) || (MicrosoftScriptControl.Error.Description == null)) { throw ex; } else { MicrosoftScriptError error = new MicrosoftScriptError(MicrosoftScriptControl.Error, context); if (step < 2) error.IsRuntime = false; this.AddError(error); } } MicrosoftScriptControl.Reset(); } } }
private void ExecuteModules(IZeusContext context, ZeusModule parent, List<string> names, int timeout) { foreach (ZeusModule module in parent.ChildModules) { if (names.Contains(module.Name)) { module.Execute(timeout, context.Log); } else { ExecuteModules(context, module, names, timeout); } } }
public bool Execute(IZeusContext context) { return ZeusExecutioner.ExecuteCodeSegment(this, context); }