Minor style changes
[castle.git] / MonoRail / Castle.MonoRail.Views.Brail / TransformToBrailStep.cs
blob94c7946b16012a4d4026822d24479d7f8e19951c
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Views.Brail
17 using System.IO;
18 using Boo.Lang.Compiler.Ast;
19 using Boo.Lang.Compiler.Steps;
21 //This class is responsible for taking a view script and transforming it to a legible
22 //code. It does so by doing the following transformations:
23 // * Add imports to Brail and Castle.MonoRail.Framework
24 // * Create an overriding method call Run and trasnfer all the global code in the script ot it and then
25 // empty all the code in the global scope
26 // * Create a class that inherit from BrailBase with the same name as the file
27 // * Add the Run method to this class
28 // * Add any higher level elements (classes, methods, etc) to the newly created class
29 // and then remove them from the global scope
30 // * Create a constructor that delegate to BrailBase constructor
31 public class TransformToBrailStep : AbstractCompilerStep
33 private BooViewEngineOptions options;
35 public TransformToBrailStep(BooViewEngineOptions options)
37 this.options = options;
40 public override void Run()
42 foreach(Module module in CompileUnit.Modules)
44 module.Imports.Add(new Import(module.LexicalInfo, "Castle.MonoRail.Views.Brail"));
45 module.Imports.Add(new Import(module.LexicalInfo, "Castle.MonoRail.Framework"));
47 foreach(string name in options.NamespacesToImport)
49 module.Imports.Add(new Import(module.LexicalInfo, name));
52 ClassDefinition macro = new ClassDefinition();
53 macro.Name = GetViewTypeName(module.FullName);
54 macro.BaseTypes.Add(new SimpleTypeReference("Castle.MonoRail.Views.Brail.BrailBase"));
56 AddConstructor(macro);
57 ScriptDirectoryProperty(macro, module);
58 AddRunMethod(macro, module);
60 foreach(TypeMember member in module.Members)
62 macro.Members.Add(member);
65 module.Members.Clear();
66 module.Members.Add(macro);
70 public static string GetViewTypeName(string name)
72 return "BrailView_"+ name;
75 // get the directory name where this script reside and create a property
76 // that return this value.
77 // this is used to calculate relative paths when loading subviews.
78 private void ScriptDirectoryProperty(ClassDefinition macro, Module module)
80 Property p = new Property("ScriptDirectory");
81 p.Modifiers = TypeMemberModifiers.Override;
82 p.Getter = new Method("getScriptDirectory");
83 p.Getter.Body.Add(
84 new ReturnStatement(
85 new StringLiteralExpression(
86 Path.GetDirectoryName(module.LexicalInfo.FileName))));
88 macro.Members.Add(p);
91 // create the Run method override for this class
92 // this is where all the global code from the script goes
93 private void AddRunMethod(ClassDefinition macro, Module module)
95 Method method = new Method("Run");
96 method.Modifiers = TypeMemberModifiers.Override;
97 method.Body = module.Globals;
98 module.Globals = new Block();
99 macro.Members.Add(method);
102 // create a constructor that delegate to the base class
103 private void AddConstructor(ClassDefinition macro)
105 Constructor ctor = new Constructor(macro.LexicalInfo);
106 ctor.Parameters.Add(
107 new ParameterDeclaration("viewEngine",
108 new SimpleTypeReference("Castle.MonoRail.Views.Brail.BooViewEngine")));
110 ctor.Parameters.Add(
111 new ParameterDeclaration("output",
112 new SimpleTypeReference("System.IO.TextWriter")));
113 ctor.Parameters.Add(
114 new ParameterDeclaration("context",
115 new SimpleTypeReference("Castle.MonoRail.Framework.IRailsEngineContext")));
117 ctor.Parameters.Add(
118 new ParameterDeclaration("__controller",
119 new SimpleTypeReference("Castle.MonoRail.Framework.Controller")));
122 MethodInvocationExpression mie = new MethodInvocationExpression(new SuperLiteralExpression());
123 mie.Arguments.Add(AstUtil.CreateReferenceExpression("viewEngine"));
124 mie.Arguments.Add(AstUtil.CreateReferenceExpression("output"));
125 mie.Arguments.Add(AstUtil.CreateReferenceExpression("context"));
126 mie.Arguments.Add(AstUtil.CreateReferenceExpression("__controller"));
128 ctor.Body.Add(mie);
130 macro.Members.Add(ctor);