1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
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 readonly 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");
85 new StringLiteralExpression(
86 Path
.GetDirectoryName(module
.LexicalInfo
.FileName
))));
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
);
108 new ParameterDeclaration("viewEngine",
109 new SimpleTypeReference("Castle.MonoRail.Views.Brail.BooViewEngine")));
112 new ParameterDeclaration("output",
113 new SimpleTypeReference("System.IO.TextWriter")));
115 new ParameterDeclaration("context",
116 new SimpleTypeReference("Castle.MonoRail.Framework.IEngineContext")));
119 new ParameterDeclaration("__controller",
120 new SimpleTypeReference("Castle.MonoRail.Framework.IController")));
123 new ParameterDeclaration("__controllerContext",
124 new SimpleTypeReference("Castle.MonoRail.Framework.IControllerContext")));
127 MethodInvocationExpression mie
= new MethodInvocationExpression(new SuperLiteralExpression());
128 mie
.Arguments
.Add(AstUtil
.CreateReferenceExpression("viewEngine"));
129 mie
.Arguments
.Add(AstUtil
.CreateReferenceExpression("output"));
130 mie
.Arguments
.Add(AstUtil
.CreateReferenceExpression("context"));
131 mie
.Arguments
.Add(AstUtil
.CreateReferenceExpression("__controller"));
132 mie
.Arguments
.Add(AstUtil
.CreateReferenceExpression("__controllerContext"));
136 macro
.Members
.Add(ctor
);