1 namespace Castle
.NewGenerator
.Core
.MR
3 using System
.Collections
.Specialized
;
5 using System
.Reflection
;
8 [Generator("mrproject", "New MonoRail Project")]
9 [GeneratorOptions(typeof(NewProject
.CLIOptions
))]
10 public class NewProject
: BaseGenerator
12 private string name
, solutionName
, viewEngine
, fileExtension
, solutionFolder
;
13 private bool enableWindsor
;
17 viewEngine
= "nvelocity";
20 [Param(Required
= true)]
28 public string SolutionName
30 get { return solutionName; }
31 set { solutionName = value; }
35 public bool EnableWindsor
37 get { return enableWindsor; }
38 set { enableWindsor = value; }
42 public string FileExtension
44 get { return fileExtension; }
45 set { fileExtension = value; }
49 public string ViewEngine
51 get { return viewEngine; }
52 set { viewEngine = value.ToLower(); }
56 public string SolutionFolder
58 get { return solutionFolder; }
59 set { solutionFolder = value; }
62 //TODO: Expose the created solution and remove this property
63 public string SolutionFilePath
65 get { return Path.Combine(solutionFolder, solutionName + ".sln"); }
69 /// Creates the following structure:
79 /// -- BaseController.cs
81 /// -- \ViewComponents
92 public override void Generate(GeneratorContext context
, IGeneratorService generator
)
94 string basePath
= TargetPath
;
95 string testProjectName
= Name
+ ".Tests";
97 if (string.IsNullOrEmpty(fileExtension
))
99 fileExtension
= "castle";
102 if (string.IsNullOrEmpty(solutionName
))
105 Name
= Name
+ ".Web";
110 if (string.IsNullOrEmpty(solutionFolder
))
112 solutionFolder
= generator
.CreateFolderOn(basePath
, solutionName
, "Creating solution folder");
115 string libFolder
= generator
.CreateFolderOn(solutionFolder
, "lib");
116 string hintPath
= @"..\lib\";
118 basePath
= solutionFolder
;
120 string projectFolder
= generator
.CreateFolderOn(basePath
, name
, "Creating project folder");
122 string controllersFolder
= generator
.CreateFolderOn(projectFolder
, "Controllers");
124 string viewsFolder
= generator
.CreateFolderOn(projectFolder
, "Views");
125 string componentsFolder
= generator
.CreateFolderOn(viewsFolder
, "components");
126 string layoutsFolder
= generator
.CreateFolderOn(viewsFolder
, "layouts");
127 string rescuesFolder
= generator
.CreateFolderOn(viewsFolder
, "rescues");
129 string contentFolder
= generator
.CreateFolderOn(projectFolder
, "Content");
130 string cssFolder
= generator
.CreateFolderOn(contentFolder
, "css");
131 string imagesFolder
= generator
.CreateFolderOn(contentFolder
, "images");
132 string jsFolder
= generator
.CreateFolderOn(contentFolder
, "javascripts");
134 generator
.CreateFolderOn(projectFolder
, "Filters");
135 generator
.CreateFolderOn(projectFolder
, "Helpers");
136 generator
.CreateFolderOn(projectFolder
, "ViewComponents");
137 generator
.CreateFolderOn(projectFolder
, "Models");
141 HybridDictionary parameters
= new HybridDictionary(true);
142 parameters
["projectName"] = Name
;
143 parameters
["useWindsor"] = enableWindsor
;
144 parameters
["fileExtension"] = fileExtension
;
146 generator
.AddFromTemplate(controllersFolder
, "MR/Controllers/BaseController.vm", "BaseController.cs", parameters
);
147 generator
.AddFromTemplate(layoutsFolder
, "MR/Layouts/default.vm", "default.vm", parameters
);
148 generator
.AddFromTemplate(rescuesFolder
, "MR/Rescues/generalerror.vm", "generalerror.vm", parameters
);
150 generator
.AddStaticFile(jsFolder
, "MR/js/prototype/prototype.js");
151 generator
.AddStaticFile(jsFolder
, "MR/js/prototype/behaviour.js");
152 generator
.AddStaticFile(jsFolder
, "MR/js/prototype/scriptaculous.js");
153 generator
.AddStaticFile(jsFolder
, "MR/js/prototype/formhelper.js");
155 // Create folders for test project
157 string testProjectFolder
= generator
.CreateFolderOn(basePath
, testProjectName
, "Creating test project folder");
158 generator
.CreateFolderOn(testProjectFolder
, "Controllers");
159 generator
.CreateFolderOn(testProjectFolder
, "Filters");
160 generator
.CreateFolderOn(testProjectFolder
, "Helpers");
161 generator
.CreateFolderOn(testProjectFolder
, "ViewComponents");
163 // Create configuration files (and others)
165 generator
.AddFromTemplate(projectFolder
, "MR/webconfig.vm", "web.config", parameters
);
166 generator
.AddFromTemplate(projectFolder
, "MR/GlobalApplication.vm", "GlobalApplication.cs", parameters
);
167 generator
.AddFromTemplate(projectFolder
, "MR/globalasax.vm", "global.asax", parameters
);
169 // Create vs solution and projects
171 VSProject webProject
= generator
.CreateVSProjectFromFromFolder(name
, projectFolder
, ProjectType
.Web
);
172 webProject
.AddAssemblyReference("Castle.Components.Binder.dll", hintPath
);
173 webProject
.AddAssemblyReference("Castle.Components.Common.EmailSender.dll", hintPath
);
174 webProject
.AddAssemblyReference("Castle.Components.Validator.dll", hintPath
);
175 webProject
.AddAssemblyReference("Castle.Core.dll", hintPath
);
176 webProject
.AddAssemblyReference("Castle.MonoRail.Framework.dll", hintPath
);
177 webProject
.AddAssemblyReference("Castle.MonoRail.Framework.Views.NVelocity.dll", hintPath
);
178 webProject
.AddAssemblyReference("NVelocity.dll", hintPath
);
179 webProject
.AddAssemblyReferenceShared("System");
180 webProject
.AddAssemblyReferenceShared("System.Data");
181 webProject
.AddAssemblyReferenceShared("System.Web");
182 webProject
.AddAssemblyReferenceShared("System.Xml");
183 webProject
.AddAssemblyReferenceShared("System.Configuration");
186 VSProject testProject
= generator
.CreateVSProjectFromFromFolder(testProjectName
, testProjectFolder
, ProjectType
.Test
);
187 testProject
.AddAssemblyReference("Castle.Core.dll", hintPath
);
188 testProject
.AddAssemblyReference("Castle.MonoRail.Framework.dll", hintPath
);
189 testProject
.AddAssemblyReference("Castle.MonoRail.TestSupport.dll", hintPath
);
190 testProject
.AddAssemblyReference("nunit.framework.dll", hintPath
);
191 testProject
.AddAssemblyReference("WatiN.Core.dll", hintPath
);
192 testProject
.AddAssemblyReference("Rhino.Mocks.dll", hintPath
);
193 testProject
.AddProjectReference(webProject
);
196 VSSolution solution
= generator
.CreateSolution(solutionName
, solutionFolder
, webProject
, testProject
);
199 // Create nant script
203 // Copy required assemblies to lib folder
205 CopyReferencedAssembliesToLibFolder(libFolder
, webProject
, testProject
);
208 private static void CopyReferencedAssembliesToLibFolder(string libFolder
, params VSProject
[] projects
)
210 foreach(VSProject project
in projects
)
212 foreach(Assembly assm
in project
.AssemblyReferences
)
214 string dest
= Path
.Combine(libFolder
, Path
.GetFileName(assm
.Location
));
216 if (!File
.Exists(dest
))
218 File
.Copy(assm
.Location
, dest
);
224 public class CLIOptions
: GeneratorConfigurer
<NewProject
>
226 [Option("name", 'n')]
229 [Option("solutionname", 's')]
230 public string solutionname
;
232 [Option("viewengine")]
233 public string viewengine
;
235 [Option("handlerextension")]
236 public string handlerextension
;
238 [Option("windsor", 'w')]
239 public bool enableWindsor
;
241 public override void Configure(NewProject generator
, string[] args
)
245 generator
.Name
= name
;
246 generator
.SolutionName
= solutionname
;
247 generator
.FileExtension
= handlerextension
;
248 generator
.EnableWindsor
= enableWindsor
;
250 if (viewengine
!= null)
252 generator
.ViewEngine
= viewengine
;
255 base.Configure(generator
, args
);