public ScriptContext Execute(string fullPath, FileAndDate scriptEditedAndBody, ScriptContext context) { ScriptEngine engine = runtime.GetEngineByFileExtension(fullPath.Split('.').Last().ToLowerInvariant()); var paths = engine.GetSearchPaths(); try { foreach (string path in site.FeatureDefinitions[new Guid("867106ba-29b5-4301-8b78-d4597f31f22a")].Properties["Paths"] .ToString().SplitByChars(";")) { if (string.IsNullOrWhiteSpace(path) || !Directory.Exists(path)) { byte[] msg = ("Directory not found while trying to Paths from DLR feature: " + path).ToUtf8ByteArray(); engine.Runtime.IO.OutputStream.WriteAsync(msg, 0, msg.Count()); continue; } paths.Add(path); } } catch (Exception E) { byte[] exc = ("Exception while trying to load property Paths from DLR feature\n" + E.ToString()).ToUtf8ByteArray(); engine.Runtime.IO.OutputStream.WriteAsync(exc, 0, exc.Count()); } engine.SetSearchPaths(paths); byte[] importPaths = string.Format("Import paths: {0}\n", paths.JoinStrings("; ")).ToUtf8ByteArray(); engine.Runtime.IO.OutputStream.WriteAsync(importPaths, 0, importPaths.Count()); CompiledCode code = Compile(fullPath, scriptEditedAndBody, engine); context.Add("site", this.site); // TODO cache scope with preloaded libraries and namespaces ScriptScope scope = engine.CreateScope(context); code.Execute(scope); return new ScriptContext(scope.GetItems()); }
private CompiledCode Compile(string scriptLink, FileAndDate scriptItem, ScriptEngine engine) { string cacheName = "script_" + scriptItem.LastUpdate.Ticks; KeyValuePair<DateTime, CompiledCode>? compiledItem = (KeyValuePair<DateTime, CompiledCode>?) HttpRuntime.Cache.Get(cacheName); // not in cache or not matching modificaction datetime of script from repository if (compiledItem == null || compiledItem.Value.Key != scriptItem.LastUpdate) { ScriptSource source = engine.CreateScriptSourceFromFile(scriptLink, Encoding.UTF8, SourceCodeKind.File); compiledItem = new KeyValuePair<DateTime, CompiledCode>(scriptItem.LastUpdate, source.Compile()); HttpRuntime.Cache.Insert(cacheName, compiledItem, null, DateTime.Now.AddDays(1), Cache.NoSlidingExpiration); } return compiledItem.Value.Value; }
/// <summary> /// Load a dllplugin from a path in SiteScripts, execute with given context /// </summary> /// <param name="fullPath"></param> /// <param name="scriptEditedAndBody"></param> /// <param name="context">Contains (at least) SPSite site</param> /// <returns></returns> public ScriptContext Execute(string fullPath, FileAndDate scriptEditedAndBody, ScriptContext context) { ILogging log = NaverticaLog.GetInstance(); Assembly dll = null; try { dll = Assembly.Load(scriptEditedAndBody.BinaryData); } catch (Exception exc) { log.LogError(fullPath, exc); } if (dll != null) { try { object instance = dll.CreateInstance(dll.DefinedTypes.First().FullName); if (instance != null) { MethodInfo getInstanceMethod = instance.GetType().GetMethod("GetInstance"); var pluginInstance = getInstanceMethod.Invoke(instance, null); MethodInfo pluginScopeMethod = instance.GetType().GetMethod("PluginScope"); var pluginScopes = pluginScopeMethod.Invoke(instance, null); if (pluginScopes != null) { foreach (PluginHost scope in ((IEnumerable) pluginScopes)) { switch (scope.GetType().Name) { case "ExecutePagePluginHost": ExecutePagePluginHost.Add(this.site, pluginInstance as IPlugin); break; case "ItemReceiverPluginHost": ItemReceiverPluginHost.Add(this.site, pluginInstance as IPlugin); break; case "ListReceiverPluginHost": ListReceiverPluginHost.Add(this.site, pluginInstance as IPlugin); break; case "TimerJobPluginHost": TimerJobPluginHost.Add(this.site, pluginInstance as IPlugin); break; } } } } } catch (ReflectionTypeLoadException rtle) { log.LogError("DLLExecute.Execute", rtle, rtle.LoaderExceptions.Select(ee => ee.ToString()).JoinStrings("\n")); } catch (Exception e) { log.LogError("DLLExecute.Execute", e); } } return context; }