1 // Copyright 2004-2007 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
.Generator
.Generators
19 using System
.Collections
;
23 /// Generates a project skeleton.
25 public class ProjectGenerator
: AbstractGenerator
, IGenerator
27 private DirectoryInfo rootDir
;
28 private DirectoryInfo frameworkDir
;
29 private DirectoryInfo projectDir
;
30 private DirectoryInfo projectTestDir
;
31 private DirectoryInfo controllersDir
;
32 private DirectoryInfo modelstDir
;
33 private DirectoryInfo viewsDir
;
34 private DirectoryInfo libDir
;
35 private bool useWindsorIntegration
;
36 private bool isNVelocity
;
38 public ProjectGenerator()
42 #region IGenerator Members
44 public bool Accept(String name
, IDictionary options
, TextWriter writer
)
46 if (!"project".Equals(name
))
50 else if (options
.Count
== 1)
52 writer
.WriteLine("Creates a new VS.Net 2003 project structure");
54 writer
.WriteLine("name : Project name");
55 writer
.WriteLine("outdir : Target directory (must exists)");
56 writer
.WriteLine("windsor : [Optional] Enable WindsorContainer Integration");
57 writer
.WriteLine("view : [Optional] aspnet|nvelocity (defaults to nvelocity)");
58 writer
.WriteLine("lang : [Optional] c#|vb.net (defaults to c#)");
60 writer
.WriteLine("Example:");
62 writer
.WriteLine(@"> generator project name:My.CoR.Project windsor outdir:c:\temp");
67 else if (!options
.Contains("outdir"))
69 writer
.WriteLine("outdir must be specified");
73 else if (!options
.Contains("name"))
75 writer
.WriteLine("name must be specified");
81 DirectoryInfo info
= new DirectoryInfo(options
["outdir"] as String
);
85 // info.Create(); // Is it safe to use it?
86 writer
.WriteLine("Error: The specified outdir does not exists.");
95 public void Execute(IDictionary options
, TextWriter writer
)
97 writer
.WriteLine("Generating Project...");
101 useWindsorIntegration
= options
.Contains("windsor");
102 isNVelocity
= !(options
.Contains("view") && options
["view"].Equals("aspnet"));
103 String name
= options
["name"] as String
;
105 DirectoryInfo sysDir
= new DirectoryInfo(Environment
.GetFolderPath(Environment
.SpecialFolder
.System
));
106 frameworkDir
= new DirectoryInfo( Path
.Combine(sysDir
.Parent
.FullName
, @"Microsoft.NET\Framework\v1.1.4322\") );
108 // Steps to create the project:
110 // 1. Create a controllers and views Directory
112 writer
.WriteLine("Creating directories...");
113 CreateDirectories(name
, options
, writer
);
115 writer
.WriteLine("Copying files...");
116 CopyFiles(name
, options
, writer
);
118 // 2. Create a proper web.config
120 writer
.WriteLine("Creating web.config...");
121 CreateWebConfig(options
, writer
);
123 // 3. Create a build file?
125 CreateNAntBuildFile(options
, writer
);
127 // 4. Create the sln and the proper csproj (references to assemblies)
129 writer
.WriteLine("Creating solution...");
130 CreateSolution(name
, options
, writer
);
133 writer
.WriteLine("Done!");
138 private void CreateDirectories(string name
, IDictionary options
, TextWriter writer
)
140 DirectoryInfo outdir
= new DirectoryInfo(options
["outdir"] as String
);
144 rootDir
= outdir
.CreateSubdirectory(name
);
145 projectDir
= rootDir
.CreateSubdirectory(name
);
146 projectTestDir
= rootDir
.CreateSubdirectory(name
+ ".Tests");
147 libDir
= rootDir
.CreateSubdirectory("lib");
149 controllersDir
= projectDir
.CreateSubdirectory("Controllers");
150 modelstDir
= projectDir
.CreateSubdirectory("Models");
151 viewsDir
= projectDir
.CreateSubdirectory("Views");
155 writer
.WriteLine(ex
.Message
);
160 private void CreateWebConfig(IDictionary options
, TextWriter writer
)
162 String templateName
= "webconfig.vm";
164 Hashtable ctx
= new Hashtable();
165 ctx
.Add("name", options
["name"]);
166 ctx
.Add("viewpath", viewsDir
.FullName
);
167 ctx
.Add("useWindsorIntegration", useWindsorIntegration
);
168 ctx
.Add("webapppath", projectDir
.FullName
);
172 ctx
["viewenginetypename"] =
173 "Castle.MonoRail.Framework.Views.NVelocity.NVelocityViewEngine, Castle.MonoRail.Framework.Views.NVelocity";
177 ctx
["viewenginetypename"] =
178 "Castle.MonoRail.Framework.Views.Aspx.AspNetViewEngine, Castle.MonoRail.Framework";
181 WriteTemplateFile(Path
.Combine(projectDir
.FullName
, "web.config"), ctx
, templateName
);
183 templateName
= "testconfig.vm";
185 WriteTemplateFile(Path
.Combine(projectTestDir
.FullName
, "App.config"), ctx
, templateName
);
188 private void CreateNAntBuildFile(IDictionary options
, TextWriter writer
)
190 // TODO: ?? Is it worthwhile?
193 private void CreateSolution(string name
, IDictionary options
, TextWriter writer
)
195 Hashtable ctx
= new Hashtable();
197 ctx
.Add("projid", Guid
.NewGuid().ToString().ToUpper());
198 ctx
.Add("projtestid", Guid
.NewGuid().ToString().ToUpper());
199 ctx
.Add("basename", name
);
200 ctx
.Add("basenameproj", name
+ ".csproj");
201 ctx
.Add("basenameprojtests", name
+ ".Tests.csproj");
202 ctx
.Add("projectDir", projectDir
.FullName
);
203 ctx
.Add("projectTestDir", projectTestDir
.FullName
);
204 ctx
.Add("frameworkpath", frameworkDir
.FullName
);
205 ctx
.Add("isNVelocity", isNVelocity
);
206 ctx
.Add("useWindsorIntegration", useWindsorIntegration
);
208 WriteTemplateFile(Path
.Combine(rootDir
.FullName
, name
+ ".sln"), ctx
, "solution.vm");
209 WriteTemplateFile(Path
.Combine(projectDir
.FullName
, name
+ ".csproj"), ctx
, "csproj.vm");
210 WriteTemplateFile(Path
.Combine(projectTestDir
.FullName
, name
+ ".Tests.csproj"), ctx
, "csprojtest.vm");
212 if (useWindsorIntegration
)
214 WriteTemplateFile(Path
.Combine(projectDir
.FullName
, "global.asax"), ctx
, "global.vm");
215 WriteTemplateFile(Path
.Combine(projectDir
.FullName
, "MyHttpApplication.cs"), ctx
, "httpapp.vm");
216 WriteTemplateFile(Path
.Combine(projectDir
.FullName
, "MyContainer.cs"), ctx
, "container.vm");
220 private void CopyFiles(string name
, IDictionary options
, TextWriter writer
)
222 String sourcedir
= AppDomain
.CurrentDomain
.BaseDirectory
;
224 CopyFileToLib(sourcedir
, "Castle.Core.dll");
225 CopyFileToLib(sourcedir
, "Castle.MonoRail.Framework.dll");
226 CopyFileToLib(sourcedir
, "Castle.MonoRail.TestSupport.dll");
227 CopyFileToLib(sourcedir
, "Castle.Components.Validator.dll");
228 CopyFileToLib(sourcedir
, "Castle.Components.Common.EmailSender.dll");
232 CopyFileToLib(sourcedir
, "Castle.MonoRail.Framework.Views.NVelocity.dll");
233 CopyFileToLib(sourcedir
, "NVelocity.dll");
236 if (useWindsorIntegration
)
238 CopyFileToLib(sourcedir
, "Castle.MonoRail.WindsorExtension.dll");
239 CopyFileToLib(sourcedir
, "Castle.DynamicProxy.dll");
240 CopyFileToLib(sourcedir
, "Castle.MicroKernel.dll");
241 CopyFileToLib(sourcedir
, "Castle.Core.dll");
242 CopyFileToLib(sourcedir
, "Castle.Windsor.dll");
246 private void CopyFileToLib(string sourcedir
, String filename
)
249 Path
.Combine(sourcedir
, filename
),
250 Path
.Combine(libDir
.FullName
, filename
),