Removed initial binder support which was replaced with more general approach.
[castle.git] / MonoRail / NewGenerator / Castle.NewGenerator.Core / MR / NewProject.cs
blob096668f343f5173ac95d8b85e5c6402f658abb3d
1 namespace Castle.NewGenerator.Core.MR
3 using System.Collections.Specialized;
4 using System.IO;
5 using System.Reflection;
6 using Mono.GetOptions;
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;
15 public NewProject()
17 viewEngine = "nvelocity";
20 [Param(Required = true)]
21 public string Name
23 get { return name; }
24 set { name = value; }
27 [Param]
28 public string SolutionName
30 get { return solutionName; }
31 set { solutionName = value; }
34 [Param]
35 public bool EnableWindsor
37 get { return enableWindsor; }
38 set { enableWindsor = value; }
41 [Param]
42 public string FileExtension
44 get { return fileExtension; }
45 set { fileExtension = value; }
48 [Param]
49 public string ViewEngine
51 get { return viewEngine; }
52 set { viewEngine = value.ToLower(); }
55 [Param]
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"); }
68 /// <summary>
69 /// Creates the following structure:
70 ///
71 /// <![CDATA[
72 /// <pre>
73 /// -- \Contents
74 /// -- \css
75 /// -- \images
76 /// -- \javascripts
77 /// -- \Filters
78 /// -- \Controllers
79 /// -- BaseController.cs
80 /// -- \Helpers
81 /// -- \ViewComponents
82 /// -- \Models
83 /// -- \Views
84 /// -- \components
85 /// -- \layouts
86 /// -- \rescues
87 /// -- default.aspx
88 /// -- web.config
89 /// </pre>
90 /// ]]>
91 /// </summary>
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))
104 solutionName = Name;
105 Name = Name + ".Web";
108 // Create folders
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");
139 // Add files
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");
184 webProject.Save();
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);
194 testProject.Save();
196 VSSolution solution = generator.CreateSolution(solutionName, solutionFolder, webProject, testProject);
197 solution.Save();
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')]
227 public string name;
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)
243 ProcessArgs(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);