public override object EvaluateExpression(object target, System.Web.UI.BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { string value = String.Empty; string scriptName = entry.Expression; try { string functionName = null; if (scriptName.Contains("@")) { var tmp = scriptName.Split('@'); functionName = tmp[0].Trim(); scriptName = tmp[1].Trim(); } else { throw new ArgumentException("Invalid expression! Use <%$Iron:My.sayHello@my/functions.rb"); } engine = IronRuntime.GetDefaultIronRuntime(SPContext.Current.Site).GetEngineByExtension(Path.GetExtension(scriptName)); value = engine.InvokeDynamicFunction(functionName, scriptName, target, entry).ToString(); } catch (Exception ex) { IronRuntime.LogError("Error", ex); if (SPContext.Current.Web.UserIsSiteAdmin && engine.IronRuntime.IronHive.Web.CurrentUser.IsSiteAdmin) { var eo = engine.ScriptEngine.GetService <ExceptionOperations>(); string error = eo.FormatException(ex); IronRuntime.LogError(String.Format("Error executing script {0}: {1}", scriptName, error), ex); value = error; if (engine != null) { new IronLogger(engine.IronRuntime).Log(String.Format("Ruby Error: {0} at {1}", ex.Message, error)); } } else { value = "Error occured"; } } return(value); }
protected override void OnInit(EventArgs e) { try { if (String.IsNullOrEmpty(ScriptName)) { Exception = new InvalidEnumArgumentException("Property ScriptName is empty!"); } else if (String.IsNullOrEmpty(ScriptClass)) { Exception = new InvalidEnumArgumentException("Property ScriptClass is empty!"); } if (Exception != null) { return; } //Guid hiveId = String.IsNullOrEmpty(ScriptHiveId) ? Guid.Empty : new Guid(ScriptHiveId); //IronRuntime ironRuntime = IronRuntime.GetIronRuntime(SPContext.Current.Site, hiveId); IronRuntime ironRuntime = IronRuntime.GetDefaultIronRuntime(SPContext.Current.Site); engine = ironRuntime.GetEngineByExtension(Path.GetExtension(ScriptName)); if (engine != null) { ctrl = engine.CreateDynamicInstance(ScriptClass, ScriptName) as Control; var dynamicControl = ctrl as IIronControl; if (dynamicControl != null) { dynamicControl.WebPart = null; dynamicControl.Data = null; dynamicControl.Config = Config; } this.Controls.Add(ctrl); } } catch (Exception ex) { Exception = ex; } base.OnInit(e); }
public IronEngine GetEngineByExtension(string extension, bool initialized) { IronEngine ironEngine = null; if (!Engines.TryGetValue(extension, out ironEngine)) { string error = String.Format("Error occured while getting engine for extension {0}", extension); var ex = new ArgumentException(error, "extension"); LogError(error, ex); throw ex; } if (initialized && !ironEngine.IsInitialized) { ShowUnavailable(); } return(ironEngine); }
private void Initialize() { if (!IsInitialized && !IsInitializing) { lock (_sync) { if (!IsInitialized && !IsInitializing) { IsInitializing = true; var setup = new ScriptRuntimeSetup(); var languageSetup = new LanguageSetup( "IronRuby.Runtime.RubyContext, IronRuby, Version=1.0.0.1, Culture=neutral, PublicKeyToken=baeaf26a6e0611a7", IronConstant.IronRubyLanguageName, new[] { "IronRuby", "Ruby", "rb" }, new[] { ".rb" }); setup.LanguageSetups.Add(languageSetup); setup.HostType = typeof(IronHive); setup.DebugMode = IronConstant.IronEnv == IronEnvironment.Debug; _scriptRuntime = new ScriptRuntime(setup); (_scriptRuntime.Host as IronHive).Id = _hiveId; _scriptRuntime.LoadAssembly(typeof(IronRuntime).Assembly); // IronSharePoint _scriptRuntime.LoadAssembly(typeof(SPSite).Assembly); // Microsoft.SharePoint _scriptRuntime.LoadAssembly(typeof(IHttpHandler).Assembly); // System.Web using (new SPMonitoredScope("Creating IronEngine(s)")) { string ironRubyRoot = Path.Combine(IronHive.FeatureFolderPath, "IronSP_IronRuby10\\"); SPSecurity.RunWithElevatedPrivileges(() => PrivilegedInitialize(ironRubyRoot)); ScriptEngine rubyEngine = _scriptRuntime.GetEngineByFileExtension(".rb"); rubyEngine.SetSearchPaths(new List <String> { Path.Combine(ironRubyRoot, @"Lib\IronRuby"), Path.Combine(ironRubyRoot, @"Lib\ruby\site_ruby\1.8"), Path.Combine(ironRubyRoot, @"Lib\ruby\1.8"), IronHive.CurrentDir }); var ironRubyEngine = new IronEngine(this, rubyEngine); Engines[".rb"] = ironRubyEngine; ScriptScope scope = rubyEngine.CreateScope(); scope.SetVariable("iron_runtime", this); scope.SetVariable("ruby_engine", ironRubyEngine); scope.SetVariable("rails_root", IronHive.CurrentDir); scope.SetVariable("rails_env", IronConstant.IronEnv == IronEnvironment.Debug ? "development" : IronConstant.IronEnv.ToString().ToLower()); rubyEngine.Execute("$RUNTIME = iron_runtime; $RUBY_ENGINE = ruby_engine; RAILS_ROOT = rails_root; RAILS_ENV = rails_env", scope); IronConsole.Execute(@" Dir.chdir RAILS_ROOT require 'rubygems' begin load_assembly 'Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' require './iron_sharepoint' require 'application' rescue Exception => ex IRON_DEFAULT_LOGGER.error ex ensure $RUBY_ENGINE.is_initialized = true end", ".rb", false); IsInitializing = false; IsInitialized = true; } } } } }
protected override void OnInit(EventArgs e) { try { if (String.IsNullOrEmpty(ScriptName)) { _exception = new InvalidEnumArgumentException("Property ScripName is empty!"); } else if (String.IsNullOrEmpty(ScriptClass)) { _exception = new InvalidEnumArgumentException("Property ScripClass is empty!"); } if (_exception != null) { return; } Guid hiveId = String.IsNullOrEmpty(ScriptHiveId) ? Guid.Empty : new Guid(ScriptHiveId); //IronRuntime ironRuntime = IronRuntime.GetIronRuntime(SPContext.Current.Site, hiveId); IronRuntime ironRuntime = IronRuntime.GetDefaultIronRuntime(SPContext.Current.Site); engine = ironRuntime.GetEngineByExtension(Path.GetExtension(ScriptName)); if (engine != null) { ctrl = engine.CreateDynamicInstance(ScriptClass, ScriptName) as Control; var dynamicControl = ctrl as IIronControl; if (dynamicControl != null) { dynamicControl.WebPart = null; dynamicControl.Data = null; dynamicControl.Config = Config; } if (Template != null) { Template.InstantiateIn(ctrl); } else { if (!String.IsNullOrEmpty(TemplatePath)) { var path = TemplatePath.Replace("~site", SPContext.Current.Site.ServerRelativeUrl) .Replace("~web", SPContext.Current.Web.ServerRelativeUrl) .Replace("~hiveSite", engine.IronRuntime.IronHive.Site.ServerRelativeUrl) .Replace("~hiveWeb", engine.IronRuntime.IronHive.Web.ServerRelativeUrl) .Replace("~hiveFolder", engine.IronRuntime.IronHive.Folder.ServerRelativeUrl); Template = this.LoadTemplate(path); Template.InstantiateIn(ctrl); } } this.Controls.Add(ctrl); } } catch (Exception ex) { IronDiagnosticsService.Local.WriteTrace(1, IronDiagnosticsService.Local[IronCategoryDiagnosticsId.Controls], TraceSeverity.Unexpected, String.Format("Error: {0}; Stack: {1}", ex.Message, ex.StackTrace)); _exception = ex; } base.OnInit(e); }