/
RubyEngine.cs
359 lines (307 loc) · 12.8 KB
/
RubyEngine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
extern alias clr3;
#region Usings
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web.Mvc.IronRuby.Controllers;
using System.Web.Mvc.IronRuby.Extensions;
using System.Web.Mvc.IronRuby.ViewEngine;
using System.Web.Routing;
using System.Web.Security;
using IronRuby;
using IronRuby.Builtins;
using IronRuby.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Runtime;
using DLRConfigSection = Microsoft.Scripting.Hosting.Configuration.Section;
#endregion
namespace System.Web.Mvc.IronRuby.Core
{
/// <summary>
/// A facade for ScriptEngine, Runtime and Context
/// This class handles all the interaction with IronRuby
/// </summary>
public class RubyEngine : IRubyEngine
{
private readonly string _routesPath;
/// <summary>
/// Initializes a new instance of the <see cref="RubyEngine"/> class.
/// </summary>
/// <param name="runtime">The runtime.</param>
/// <param name="pathProvider">The VPP.</param>
/// <param name="routesPath">the path to the routes file</param>
public RubyEngine(ScriptRuntime runtime, IPathProvider pathProvider, string routesPath)
{
_routesPath = routesPath;
Runtime = runtime;
PathProvider = pathProvider;
Initialize();
}
/// <summary>
/// Initializes a new instance of the <see cref="RubyEngine"/> class.
/// </summary>
/// <param name="runtime">The runtime.</param>
/// <param name="pathProvider">The VPP.</param>
public RubyEngine(ScriptRuntime runtime, IPathProvider pathProvider)
: this(runtime, pathProvider, "~/routes.rb")
{
}
private ScriptRuntime Runtime { get; set; }
/// <summary>
/// Gets the context.
/// </summary>
/// <value>The context.</value>
internal RubyContext Context { get; set; }
/// <summary>
/// Gets the engine.
/// </summary>
/// <value>The engine.</value>
private ScriptEngine Engine { get; set; }
/// <summary>
/// Gets the current scope.
/// </summary>
/// <value>The current scope.</value>
private ScriptScope CurrentScope { get; set; }
/// <summary>
/// Gets the operations.
/// </summary>
/// <value>The operations.</value>
private ObjectOperations Operations { get; set; }
/// <summary>
/// Gets the path provider.
/// </summary>
/// <value>The path provider.</value>
private IPathProvider PathProvider { get; set; }
#region IRubyEngine Members
public void RemoveClassFromGlobals(string className)
{
// Remove the current controller from the classes cache so it's completely renewed.
if (Runtime.Globals.ContainsVariable(className)) Runtime.Globals.RemoveVariable(className);
}
public object CreateInstance(RubyClass rubyClass)
{
return Operations.CreateInstance(rubyClass);
}
public void ExecuteInScope(Action<ScriptScope> block)
{
var scope = Engine.CreateScope();
block(scope);
}
/// <summary>
/// Calls the method.
/// </summary>
/// <param name="receiver">The receiver.</param>
/// <param name="message">The message.</param>
/// <param name="args">The args.</param>
/// <returns></returns>
public object CallMethod(object receiver, string message, params object[] args)
{
return Operations.InvokeMember(receiver, GetMethodName(receiver, message));
}
/// <summary>
/// Gets a list of method names that are defined on the controller.
/// </summary>
/// <param name="controller">The controller.</param>
/// <returns></returns>
public IEnumerable<string> MethodNames(IController controller)
{
return Operations.GetMemberNames(controller);
}
/// <summary>
/// Methods the names.
/// </summary>
/// <param name="controllerClass">The controller class.</param>
/// <returns></returns>
public IEnumerable<string> MethodNames(RubyClass controllerClass)
{
var names = new List<string>();
using (Context.ClassHierarchyLocker())
{
controllerClass.EnumerateMethods((_, methodName, __) =>
{
names.Add(methodName);
return false;
});
}
return names;
}
/// <summary>
/// Loads the assembly.
/// </summary>
/// <param name="assembly">The assembly.</param>
public void LoadAssembly(Assembly assembly)
{
Runtime.LoadAssembly(assembly);
}
/// <summary>
/// Executes the script.
/// </summary>
/// <param name="script">The script.</param>
/// <returns></returns>
public object ExecuteScript(string script)
{
return ExecuteScript(script, CurrentScope);
}
/// <summary>
/// Executes the script.
/// </summary>
/// <param name="script">The script.</param>
/// <param name="scope">The scope.</param>
/// <returns></returns>
public object ExecuteScript(string script, ScriptScope scope)
{
return Engine.Execute(script, scope ?? CurrentScope);
}
public object ExecuteFile(string path, bool throwIfNotExist)
{
path.EnsureArgumentNotNull("path");
if (throwIfNotExist && !PathProvider.FileExists(path))
throw new FileNotFoundException("Can't find the file", path);
if (!PathProvider.FileExists(path)) return null;
var source = Engine.CreateScriptSourceFromFile(path);
return source.Execute(CurrentScope);
}
/// <summary>
/// Defines the read only global variable.
/// </summary>
/// <param name="variableName">Name of the variable.</param>
/// <param name="value">The value.</param>
public void DefineGlobalVariable(string variableName, object value)
{
Context.SetGlobalVariable(null, variableName, value);
}
/// <summary>
/// Gets the ruby class.
/// </summary>
/// <param name="className">Name of the class.</param>
/// <returns></returns>
public RubyClass GetRubyClass(string className)
{
var klass = (RubyClass) GetGlobalVariable(className);
return klass;
}
/// <summary>
/// Gets the global variable.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name">The name.</param>
/// <returns></returns>
public object GetGlobalVariable(string name)
{
return Runtime.Globals.GetVariable<object>(name);
}
/// <summary>
/// Loads the assemblies.
/// </summary>
/// <param name="assemblies">The assemblies.</param>
public void LoadAssemblies(params Type[] assemblies)
{
assemblies.ForEach(type => LoadAssembly(type.Assembly));
}
/// <summary>
/// Requires the ruby file.
/// </summary>
/// <param name="path">The path.</param>
public void RequireRubyFile(string path)
{
Engine.RequireRubyFile(path);
}
/// <summary>
/// Requires the ruby file.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="readerType">Type of the reader.</param>
public void RequireRubyFile(string path, ReaderType readerType)
{
Engine.CreateScriptSource(readerType == ReaderType.File
? (StreamContentProvider)
new VirtualPathStreamContentProvider(path, PathProvider)
: new AssemblyStreamContentProvider(path, typeof (IRubyEngine).Assembly), null,
Encoding.ASCII).Execute();
}
#endregion
public string GetMethodName(object receiver, string message)
{
var methodNames = Operations.GetMemberNames(receiver);
if (methodNames.Contains(message.Pascalize())) return message.Pascalize();
if (methodNames.Contains(message.Underscore())) return message.Underscore();
return message;
}
private void Initialize()
{
Engine = Ruby.GetEngine(Runtime);
Context = Ruby.GetExecutionContext(Engine);
CurrentScope = Engine.CreateScope();
Operations = Engine.CreateOperations();
LoadAssemblies(typeof (object), typeof (Uri), typeof (HttpResponseBase), typeof (MembershipCreateStatus),
typeof (RouteTable), typeof (Controller), typeof (RubyController));
AddLoadPaths();
DefineGlobalVariable(Constants.ScriptRuntimeVariable, Engine);
RequireControllerFile();
ProcessRubyRoutes();
}
private void RequireControllerFile()
{
// RequireRubyFile(PathProvider.MapPath("~/Controllers/controller.rb"));
Engine.CreateScriptSource(
new AssemblyStreamContentProvider("System.Web.Mvc.IronRuby.Controllers.controller.rb",
typeof (IRubyEngine).Assembly), null, Encoding.ASCII).Execute(
CurrentScope);
}
/// <summary>
/// Sets the model and controllers path.
/// </summary>
private void AddLoadPaths()
{
var controllersDir = Path.Combine(PathProvider.ApplicationPhysicalPath, Constants.Controllers);
var modelsDir = Path.Combine(PathProvider.ApplicationPhysicalPath, Constants.Models);
var filtersDir = Path.Combine(PathProvider.ApplicationPhysicalPath, Constants.Filters);
var helpersDir = Path.Combine(PathProvider.ApplicationPhysicalPath, Constants.Helpers);
Context.Loader.SetLoadPaths(new[]
{
PathProvider.ApplicationPhysicalPath, controllersDir, modelsDir, filtersDir
, helpersDir
});
}
private void ProcessRubyRoutes()
{
if (!PathProvider.FileExists(_routesPath)) return;
var routeCollection = new RubyRoutes(RouteTable.Routes);
DefineGlobalVariable("routes", routeCollection);
RequireRubyFile(_routesPath, ReaderType.File);
}
/// <summary>
/// Initializes ironruby MVC.
/// </summary>
/// <param name="pathProvider">The Path provider.</param>
/// <param name="routesPath">The routes path.</param>
public static RubyEngine InitializeIronRubyMvc(IPathProvider pathProvider, string routesPath)
{
var engine = InitializeIronRuby(pathProvider, routesPath);
IntializeMvc(pathProvider, engine);
return engine;
}
private static void IntializeMvc(IPathProvider pathProvider, IRubyEngine engine)
{
var factory = new RubyControllerFactory(pathProvider, ControllerBuilder.Current.GetControllerFactory(),
engine);
ControllerBuilder.Current.SetControllerFactory(factory);
ViewEngines.Engines.Add(new RubyViewEngine(engine));
}
private static RubyEngine InitializeIronRuby(IPathProvider vpp, string routesPath)
{
var runtimeSetup = new ScriptRuntimeSetup();
runtimeSetup.LanguageSetups.Add(Ruby.CreateRubySetup(su => su.InterpretedMode = true));
#if DEBUG
runtimeSetup.DebugMode = true;
#endif
// runtimeSetup.HostType = typeof (MvcScriptHost);
var runtime = Ruby.CreateRuntime(runtimeSetup);
return new RubyEngine(runtime, vpp, routesPath);
}
}
}