Permalink
Browse files

Simplified IronConsole even more, added tests

  • Loading branch information...
kmees committed Mar 21, 2013
1 parent 40c38eb commit b79ce240fed55b6ba097cd127e7188d2dd346387
Showing with 231 additions and 550 deletions.
  1. +0 −25 src/IronSharePoint.Framework/Console/Hooks/HookBase.cs
  2. +0 −12 src/IronSharePoint.Framework/Console/Hooks/IHook.cs
  3. +0 −21 src/IronSharePoint.Framework/Console/Hooks/RubyHook.cs
  4. +62 −0 src/IronSharePoint.Framework/Console/IOCapture.cs
  5. +1 −3 src/IronSharePoint.Framework/Console/ScriptResult.cs
  6. +0 −1 src/IronSharePoint.Framework/IronCompositeControl.cs
  7. +12 −100 src/IronSharePoint.Framework/IronConsole.cs
  8. +0 −2 src/IronSharePoint.Framework/IronConstant.cs
  9. +0 −1 src/IronSharePoint.Framework/IronControl.cs
  10. +0 −106 src/IronSharePoint.Framework/IronEngine.cs
  11. +0 −53 src/IronSharePoint.Framework/IronHttpHandler.cs
  12. +2 −2 src/IronSharePoint.Framework/IronJob.cs
  13. +21 −48 src/IronSharePoint.Framework/IronRuntime.cs
  14. +2 −5 src/IronSharePoint.Framework/IronSharePoint.Framework.csproj
  15. +8 −12 src/IronSharePoint.Framework/Util/ScriptEngineExtensions.cs
  16. +12 −0 src/IronSharePoint.Framework/Util/ScriptRuntimeExtensions.cs
  17. +0 −1 src/IronSharePoint/Features/IronSP_Hive_Site/IronSP_Hive_Site.feature
  18. +15 −38 src/IronSharePoint/Features/IronSP_WebApp/IronSP_WebApp.EventReceiver.cs
  19. +1 −7 src/IronSharePoint/IronPart/IronPart.cs
  20. +0 −4 src/IronSharePoint/IronSharePoint.csproj
  21. +3 −0 src/IronSharePoint/IronSharePoint.csproj.DotSettings
  22. +1 −1 src/IronSharePoint/Layouts/IronSP/IronConsole.coffee
  23. +1 −1 src/IronSharePoint/Layouts/IronSP/IronConsole.js
  24. +16 −23 src/IronSharePoint/Layouts/IronSP/IronConsoleService.ashx.cs
  25. +0 −10 src/IronSharePoint/Lists/IronLogs/Elements.xml
  26. +0 −9 src/IronSharePoint/Lists/IronLogs/SharePointProjectItem.spdata
  27. +0 −47 test/IronSharePoint.Framework.Test/Console/Hooks/RubyHook_Fixture.cs
  28. +2 −1 test/IronSharePoint.Framework.Test/{ → Console}/IOCapture_Fixture.cs
  29. +9 −6 test/IronSharePoint.Framework.Test/Console/ScriptResult_Fixture.cs
  30. +14 −5 test/IronSharePoint.Framework.Test/IronConsole_Fixture.cs
  31. +5 −2 test/IronSharePoint.Framework.Test/IronSharePoint.Framework.Test.csproj
  32. +17 −1 test/IronSharePoint.Framework.Test/TestHelper.cs
  33. +2 −3 test/IronSharePoint.Framework.Test/Util/ScriptEngineExtensions_Fixture.cs
  34. +24 −0 test/IronSharePoint.Framework.Test/Util/ScriptRuntimeExtensions_Fixture.cs
  35. +1 −0 test/IronSharePoint.Framework.Test/_assets/script_result.json
@@ -1,25 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using Microsoft.Scripting.Hosting;
-
-namespace IronSharePoint.Console.Hooks
-{
- abstract class HookBase : IHook
- {
- #region IHook Members
-
- public bool SupportsLanguage(string languageName)
- {
- return SupportedLanguages().Contains(languageName, StringComparer.InvariantCultureIgnoreCase);
- }
-
- public virtual void BeforeExecute(ScriptEngine scriptEngine, ScriptScope scope, ScriptResult result) {}
-
- public virtual void AfterExecute(ScriptEngine scriptEngine, ScriptScope scope, ScriptResult result) {}
-
- #endregion
-
- protected abstract IEnumerable<string> SupportedLanguages();
- }
-}
@@ -1,12 +0,0 @@
-using Microsoft.Scripting.Hosting;
-
-namespace IronSharePoint.Console.Hooks
-{
- interface IHook
- {
- bool SupportsLanguage(string languageName);
-
- void BeforeExecute(ScriptEngine scriptEngine, ScriptScope scope, ScriptResult result);
- void AfterExecute(ScriptEngine scriptEngine, ScriptScope scope, ScriptResult result);
- }
-}
@@ -1,21 +0,0 @@
-using System;
-using System.Collections.Generic;
-using Microsoft.Scripting.Hosting;
-
-namespace IronSharePoint.Console.Hooks
-{
- class RubyHook : HookBase
- {
- public override void AfterExecute(ScriptEngine scriptEngine, ScriptScope scope, ScriptResult result)
- {
- base.AfterExecute(scriptEngine, scope, result);
- var inspected = scriptEngine.Execute("_.inspect", scope);
- result.ReturnString = Convert.ToString(inspected ?? "nil");
- }
-
- protected override IEnumerable<string> SupportedLanguages()
- {
- return new[] {IronConstant.RubyLanguageName};
- }
- }
-}
@@ -0,0 +1,62 @@
+using System;
+using System.IO;
+using System.Text;
+using Microsoft.Scripting.Hosting;
+
+namespace IronSharePoint.Console
+{
+ internal class IOCapture : IDisposable
+ {
+ private readonly MemoryStream _err;
+ private readonly Stream _errBackup;
+ private readonly Encoding _errEncoding;
+
+ private readonly MemoryStream _out;
+ private readonly Stream _outBackup;
+ private readonly Encoding _outEncoding;
+ private readonly ScriptRuntime _runtime;
+
+ public IOCapture(ScriptRuntime runtime)
+ {
+ _runtime = runtime;
+ _outBackup = runtime.IO.OutputStream;
+ _outEncoding = runtime.IO.OutputEncoding;
+ _errBackup = runtime.IO.ErrorStream;
+ _errEncoding = runtime.IO.ErrorEncoding;
+
+ _out = new MemoryStream();
+ _err = new MemoryStream();
+
+ _runtime.IO.SetOutput(_out, _outEncoding);
+ _runtime.IO.SetErrorOutput(_err, _errEncoding);
+ }
+
+ public string Out { get; private set; }
+ public string Error { get; private set; }
+
+ public void Dispose()
+ {
+ _runtime.IO.SetOutput(_outBackup, _outEncoding);
+ _runtime.IO.SetErrorOutput(_errBackup, _errEncoding);
+
+ _out.Dispose();
+ _err.Dispose();
+ }
+
+ public void Read()
+ {
+ _runtime.IO.OutputWriter.Flush();
+ _runtime.IO.ErrorWriter.Flush();
+ _out.Position = 0;
+ _err.Position = 0;
+ using (var sr = new StreamReader(_out))
+ {
+ Out = sr.ReadToEnd();
+ }
+ using (var sr = new StreamReader(_err))
+ {
+ Error = sr.ReadToEnd();
+ }
+ }
+ }
+}
@@ -6,9 +6,7 @@ namespace IronSharePoint.Console
public class ScriptResult
{
public string Output { get; set; }
- [JsonIgnore]
- public dynamic ReturnValue { get; set; }
- public string ReturnString { get; set; }
+ public string ReturnValue { get; set; }
public string Error { get; set; }
public string StackTrace { get; set; }
public long ExecutionTime { get; set; }
@@ -10,7 +10,6 @@ namespace IronSharePoint
{
public class IronCompositeControl : CompositeControl, IIronControl
{
- public IronEngine Engine { get; set; }
public WebPart WebPart { get; set; }
public IIronDataStore DataStore { get; set; }
@@ -1,13 +1,7 @@
using System;
-using System.Collections.Generic;
using System.Diagnostics;
-using System.IO;
-using System.Linq;
-using System.Text;
-using System.Threading;
using System.Threading.Tasks;
using IronSharePoint.Console;
-using IronSharePoint.Console.Hooks;
using IronSharePoint.Util;
using Microsoft.Scripting.Hosting;
@@ -16,53 +10,46 @@ namespace IronSharePoint
public class IronConsole
{
private readonly ScriptRuntime _runtime;
- private readonly IHook[] _hooks;
+ private readonly ScriptScope _scope;
internal IronConsole(ScriptRuntime scriptRuntime)
{
_runtime = scriptRuntime;
- _hooks = new IHook[]
- {
- new RubyHook()
- };
+ _scope = scriptRuntime.Globals;
}
public ScriptRuntime Runtime
{
get { return _runtime; }
}
+ public ScriptEngine Engine
+ {
+ get { return _runtime.GetRubyEngine(); }
+ }
+
public bool IsDisposed { get; private set; }
- public async Task<ScriptResult> Execute(string script, string languageName)
+ public async Task<ScriptResult> Execute(string script)
{
- return await Task.Run(() => RunScript(script, languageName));
+ return await Task.Run(() => RunScript(script));
}
- private ScriptResult RunScript(string script, string languageName)
+ private ScriptResult RunScript(string script)
{
var result = new ScriptResult();
var sw = new Stopwatch();
sw.Start();
try
{
- var engine = Runtime.GetEngine(languageName);
- var scope = engine.CreateScope();
-
- ExecuteBeforeHooks(engine, result, scope);
-
using (var io = new IOCapture(Runtime))
{
- script = string.Format("_ = ({0})", script);
- result.ReturnValue = engine.Execute(script, scope);
+ script = string.Format("_ = ({0}); _.inspect", script);
+ result.ReturnValue = Convert.ToString(Engine.Execute(script, _scope) ?? "nil");
io.Read();
result.Output = io.Out;
result.Error = io.Error;
}
-
- ExecuteAfterHooks(engine, result, scope);
-
-
}
catch (Exception ex)
{
@@ -74,80 +61,5 @@ private ScriptResult RunScript(string script, string languageName)
return result;
}
-
- private void ExecuteBeforeHooks(ScriptEngine engine, ScriptResult result, ScriptScope scope)
- {
- var languageName = engine.Setup.DisplayName;
-
- foreach (var hook in _hooks.Where(x => x.SupportsLanguage(languageName)))
- {
- hook.BeforeExecute(engine, scope, result);
- }
- }
-
- private void ExecuteAfterHooks(ScriptEngine engine, ScriptResult result, ScriptScope scope)
- {
- var languageName = engine.Setup.DisplayName;
-
- foreach (var hook in _hooks.Where(x => x.SupportsLanguage(languageName)))
- {
- hook.AfterExecute(engine, scope, result);
- }
- }
- }
-
- internal class IOCapture : IDisposable
- {
- public IOCapture(ScriptRuntime runtime)
- {
- _runtime = runtime;
- _outBackup = runtime.IO.OutputStream;
- _outEncoding = runtime.IO.OutputEncoding;
- _errBackup = runtime.IO.ErrorStream;
- _errEncoding = runtime.IO.ErrorEncoding;
-
- _out = new MemoryStream();
- _err = new MemoryStream();
-
- _runtime.IO.SetOutput(_out, _outEncoding);
- _runtime.IO.SetErrorOutput(_err, _errEncoding);
- }
-
- private readonly Encoding _errEncoding;
- private readonly Stream _errBackup;
- private readonly Encoding _outEncoding;
- private readonly Stream _outBackup;
-
- private readonly MemoryStream _out;
- private readonly MemoryStream _err;
- private readonly ScriptRuntime _runtime;
-
- public void Read()
- {
- _runtime.IO.OutputWriter.Flush();
- _runtime.IO.ErrorWriter.Flush();
- _out.Position = 0;
- _err.Position = 0;
- using (var sr = new StreamReader(_out))
- {
- Out = sr.ReadToEnd();
- }
- using (var sr = new StreamReader(_err))
- {
- Error = sr.ReadToEnd();
- }
- }
-
- public string Out { get; private set; }
- public string Error { get; private set; }
-
- public void Dispose()
- {
- _runtime.IO.SetOutput(_outBackup, _outEncoding);
- _runtime.IO.SetErrorOutput(_errBackup, _errEncoding);
-
- _out.Dispose();
- _err.Dispose();
- }
}
}
@@ -10,15 +10,13 @@ namespace IronSharePoint
public static class IronConstant
{
public const string IronHiveLibraryPath = "_catalogs/IronHive";
- public const string IronLogsListPath = "Lists/IronLogs";
public const string IronSiteFeatureId = "a1752f91-1403-40c1-a257-69eddf8976cf";
public const string IronHiveSiteFeatureId = "354ee774-7d04-4ad6-91f9-1bc433a70bee";
public const string IronRubyFeatureId = "183909b9-cfb3-477c-a48c-02e3d5e59844";
public const string IronPrefix = "IronSP_";
- public const string RubyLanguageName = "ruby";
public const string IronRuntimeKey = IronPrefix + "Runtime";
public static string IronSPRootDirectory =
@@ -13,7 +13,6 @@ namespace IronSharePoint
{
public class IronControl : Control, IIronControl
{
- public IronEngine Engine { get; set; }
public WebPart WebPart { get; set; }
public IIronDataStore DataStore { get; set; }
Oops, something went wrong.

0 comments on commit b79ce24

Please sign in to comment.