- Fixed MR-84
[castle.git] / MonoRail / NewGenerator / Castle.NewGenerator.Core / DefaultGeneratorService.cs
blobb6bcead1e378251a42fa05f60316ce212d9cb924
1 namespace Castle.NewGenerator.Core
3 using System.Collections;
4 using System.IO;
5 using System.Text;
6 using System.Xml;
7 using Castle.Components.Common.TemplateEngine;
9 public class DefaultGeneratorService : IGeneratorService
11 protected GeneratorContext context;
12 protected ITemplateEngine templateEngine;
14 public DefaultGeneratorService(GeneratorContext context, ITemplateEngine templateEngine)
16 this.context = context;
17 this.templateEngine = templateEngine;
20 public string CreateFolderOn(string baseFolder, string folderName)
22 return CreateFolderOn(baseFolder, folderName, null);
25 public string CreateFolderOn(string baseFolder, string folderName, string description)
27 EnsureExists(baseFolder);
28 string targetPath = Path.Combine(baseFolder, folderName);
30 if (EnsureExists(targetPath))
32 if (description != null)
34 context.Info(description);
38 return targetPath;
41 public void AddFromTemplate(string folder, string template, string targetFileName, IDictionary parameters)
43 string targetFile = Path.Combine(folder, targetFileName);
45 if (File.Exists(targetFile))
47 context.Info("\tSkipping " + targetFileName);
48 return;
51 context.Info("\tProcessing " + targetFileName);
53 using(TextWriter writer = new StreamWriter(new FileStream(targetFile, FileMode.CreateNew, FileAccess.Write), Encoding.UTF8))
55 templateEngine.Process(parameters, template, writer);
56 writer.Flush();
60 public void AddStaticFile(string folder, string file)
62 string source = Path.Combine(context.TemplateFolder.FullName, file);
63 string dest = Path.Combine(folder, Path.GetFileName(file));
65 if (File.Exists(dest))
67 context.Info("\tSkipping " + Path.GetFileName(dest));
68 return;
71 context.Info("\tCopying " + Path.GetFileName(file));
73 File.Copy(source, dest);
76 public VSProject CreateVSProjectFromFromFolder(string name, string folder, ProjectType type)
78 XmlDocument doc = new XmlDocument(new NameTable());
80 XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
81 nsManager.AddNamespace("", "http://schemas.microsoft.com/developer/msbuild/2003");
82 nsManager.PushScope();
84 doc.Load(Path.Combine(context.TemplateFolder.FullName, "VS/CSProject_template.xml"));
86 return VSProject.Create(name, folder, type, doc);
89 public VSSolution CreateSolution(string name, string solutionFolder, params VSProject[] projects)
91 VSSolution solution = new VSSolution(name, solutionFolder, Path.Combine(solutionFolder, name + ".sln"));
93 foreach (VSProject project in projects)
95 solution.Add(project);
98 return solution;
101 private static bool EnsureExists(string folder)
103 DirectoryInfo dir = new DirectoryInfo(folder);
105 if (!dir.Exists)
107 dir.Create();
108 return true;
111 return false;