public static void AddController(AspMvcProject project, string path, string name) { var provider = project.LanguageBinding.GetCodeDomProvider(); if (provider == null) { throw new InvalidOperationException("Project language has null CodeDOM provider"); } string outputFile = null; MvcTextTemplateHost host = null; Mono.TextTemplating.TemplatingAppDomainRecycler.Handle handle = null; AddControllerDialog dialog = null; try { dialog = new AddControllerDialog(project); if (!String.IsNullOrEmpty(name)) { dialog.ControllerName = name; } bool fileGood = false; while (!fileGood) { Gtk.ResponseType resp = (Gtk.ResponseType)MessageService.RunCustomDialog(dialog); dialog.Hide(); if (resp != Gtk.ResponseType.Ok || !dialog.IsValid()) { return; } outputFile = System.IO.Path.Combine(path, dialog.ControllerName) + ".cs"; if (System.IO.File.Exists(outputFile)) { fileGood = MessageService.AskQuestion("Overwrite file?", String.Format("The file '{0}' already exists.\n", dialog.ControllerName) + "Would you like to overwrite it?", AlertButton.OverwriteFile, AlertButton.Cancel) != AlertButton.Cancel; } else { break; } } handle = MonoDevelop.TextTemplating.TextTemplatingService.GetTemplatingDomain(); handle.AddAssembly(typeof(MvcTextTemplateHost).Assembly); host = MvcTextTemplateHost.Create(handle.Domain); host.LanguageExtension = provider.FileExtension; host.ItemName = dialog.ControllerName; host.NameSpace = project.DefaultNamespace + ".Controllers"; host.ProcessTemplate(dialog.TemplateFile, outputFile); MonoDevelop.TextTemplating.TextTemplatingService.ShowTemplateHostErrors(host.Errors); } finally { if (handle != null) { handle.Dispose(); } if (dialog != null) { dialog.Destroy(); } } if (System.IO.File.Exists(outputFile)) { project.AddFile(outputFile); IdeApp.ProjectOperations.Save(project); } }
public static void AddView(AspMvcProject project, string path, string name) { var provider = project.LanguageBinding.GetCodeDomProvider(); if (provider == null) { throw new InvalidOperationException("Project language has null CodeDOM provider"); } string outputFile = null; MvcTextTemplateHost host = null; Mono.TextTemplating.TemplatingAppDomainRecycler.Handle handle = null; AddViewDialog dialog = null; try { dialog = new AddViewDialog(project); dialog.ViewName = name; bool fileGood = false; while (!fileGood) { Gtk.ResponseType resp = (Gtk.ResponseType)MessageService.RunCustomDialog(dialog); dialog.Hide(); if (resp != Gtk.ResponseType.Ok || !dialog.IsValid()) { return; } string ext = ".cshtml"; if (dialog.ActiveViewEngine == "Aspx") { ext = dialog.IsPartialView ? ".ascx" : ".aspx"; } if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } outputFile = System.IO.Path.Combine(path, dialog.ViewName) + ext; if (System.IO.File.Exists(outputFile)) { fileGood = MessageService.AskQuestion("Overwrite file?", String.Format("The file '{0}' already exists.\n", dialog.ViewName) + "Would you like to overwrite it?", AlertButton.OverwriteFile, AlertButton.Cancel) != AlertButton.Cancel; } else { break; } } handle = MonoDevelop.TextTemplating.TextTemplatingService.GetTemplatingDomain(); handle.AddAssembly(typeof(MvcTextTemplateHost).Assembly); host = MvcTextTemplateHost.Create(handle.Domain); host.LanguageExtension = provider.FileExtension; host.ItemName = dialog.ViewName; host.ViewDataTypeString = ""; if (dialog.HasMaster) { host.IsViewContentPage = true; host.ContentPlaceholder = dialog.PrimaryPlaceHolder; host.MasterPage = dialog.MasterFile; host.ContentPlaceHolders = dialog.ContentPlaceHolders; } else if (dialog.IsPartialView) { host.IsViewUserControl = true; } else { host.IsViewPage = true; } if (dialog.IsStronglyTyped) { host.ViewDataTypeString = dialog.ViewDataTypeString; } host.ProcessTemplate(dialog.TemplateFile, outputFile); MonoDevelop.TextTemplating.TextTemplatingService.ShowTemplateHostErrors(host.Errors); } finally { if (handle != null) { handle.Dispose(); } if (dialog != null) { dialog.Destroy(); } } if (System.IO.File.Exists(outputFile)) { project.AddFile(outputFile); IdeApp.ProjectOperations.Save(project); } }