From fbcdc7253c1b5a560692bce95ed3179a51c28c87 Mon Sep 17 00:00:00 2001 From: "Eric Labashosky(d)" Date: Sun, 9 Feb 2014 09:38:59 -0500 Subject: [PATCH] [UPDATE] set file extension base on the value provided in the template [UPDATE] clear output window when starting --- .../CrmCodeGenerator2011.cs | 43 ++++++++++++++----- CrmCodeGenerator.VSPackage/T4/Processor.cs | 23 ++++------ 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/CrmCodeGenerator.VSPackage/CrmCodeGenerator2011.cs b/CrmCodeGenerator.VSPackage/CrmCodeGenerator2011.cs index 4be4da1..708e37d 100644 --- a/CrmCodeGenerator.VSPackage/CrmCodeGenerator2011.cs +++ b/CrmCodeGenerator.VSPackage/CrmCodeGenerator2011.cs @@ -45,6 +45,7 @@ public class CrmCodeGenerator2011 : IVsSingleFileGenerator, IObjectWithSite, IDi private CodeDomProvider codeDomProvider = null; private ServiceProvider serviceProvider = null; private Settings settings = Configuration.Instance.Settings; + private String extension = null; private CodeDomProvider CodeProvider { @@ -72,6 +73,26 @@ private ServiceProvider SiteServiceProvider return serviceProvider; } } + public CrmCodeGenerator2011() + { + //Configuration.Instance.DTE.ExecuteCommand("View.Output"); + var dte = Package.GetGlobalService(typeof(SDTE)) as EnvDTE.DTE; + var win = dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput); + win.Visible = true; + + + //System.Windows.Forms.Application.DoEvents(); + + IVsOutputWindow outputWindow = Package.GetGlobalService(typeof(SVsOutputWindow)) as IVsOutputWindow; + Guid guidGeneral = Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.GeneralPane_guid; + IVsOutputWindowPane pane; + int hr = outputWindow.CreatePane(guidGeneral, "General", 1, 0); + hr = outputWindow.GetPane(guidGeneral, out pane); + pane.Activate(); + pane.Clear(); + pane.FlushToTaskList(); + System.Windows.Forms.Application.DoEvents(); + } public void Dispose() { @@ -92,6 +113,8 @@ public void Dispose() public int DefaultExtension(out string pbstrDefaultExtension) { pbstrDefaultExtension = "." + CodeProvider.FileExtension; + if (extension != null) + pbstrDefaultExtension = extension; return VSConstants.S_OK; } @@ -102,8 +125,8 @@ public int Generate(string wszInputFilePath, string bstrInputFileContents, strin UpdateStatus("In order to generate code from this template, you need to provide login credentials for your CRM system"); - UpdateStatus("The Discovery URL is the URL to your Discovery Service, you can find this URL in CRM -> Settings -> Customizations -> Developer Resources. \r \n " + @"https://dsc.yourdomain.com/XRMServices/2011/Discovery.svc"); - + UpdateStatus("The Discovery URL is the URL to your Discovery Service, you can find this URL in CRM -> Settings -> Customizations -> Developer Resources. \n eg " + @"https://dsc.yourdomain.com/XRMServices/2011/Discovery.svc"); + int exit = 0; try { @@ -141,16 +164,16 @@ public int Generate(string wszInputFilePath, string bstrInputFileContents, strin settings.CrmConnection = QuickConnection.Connect(settings.CrmSdkUrl, settings.Domain, settings.Username, settings.Password, settings.CrmOrg); } - UpdateStatus("Mapping entities, this might take a while depending on CRM server/connection speed... "); - settings.Context = new CrmCodeGenerator.VSPackage.Model.Context { Namespace = settings.Namespace }; // TODO review namespace, it might be better to use the settings??? - var mapper = new CrmCodeGenerator.VSPackage.Mapper(settings); + UpdateStatus("Mapping entities, this might take a while depending on CRM server/connection speed... "); + settings.Context = new Context { Namespace = wszDefaultNamespace }; + var mapper = new Mapper(settings); var context = mapper.MapContext(); - UpdateStatus("Generating code from template... "); + UpdateStatus("Generating code from template... "); pGenerateProgress.Progress(40, 100); - string content = Processor.ProcessTemplateCore(wszInputFilePath, context); // TODO convert to just send the bstrInputFileContents + string content = Processor.ProcessTemplateCore(wszInputFilePath, bstrInputFileContents, context, out extension); - UpdateStatus("Writing code to disk... "); + UpdateStatus("Writing code to disk... "); pGenerateProgress.Progress(50, 100); byte[] bytes = Encoding.UTF8.GetBytes(content); @@ -166,8 +189,8 @@ public int Generate(string wszInputFilePath, string bstrInputFileContents, strin pcbOutput = (uint)bytes.Length; } - UpdateStatus("Done!"); - + UpdateStatus("Done!"); + return VSConstants.S_OK; } diff --git a/CrmCodeGenerator.VSPackage/T4/Processor.cs b/CrmCodeGenerator.VSPackage/T4/Processor.cs index 4fc60c3..1d05695 100644 --- a/CrmCodeGenerator.VSPackage/T4/Processor.cs +++ b/CrmCodeGenerator.VSPackage/T4/Processor.cs @@ -13,8 +13,10 @@ namespace CrmCodeGenerator.VSPackage.T4 class Processor { - public static string ProcessTemplateCore(string templatePath, Context context) + public static string ProcessTemplateCore(string templatePath, string templateContent, Context context, out string extension) { + extension = null; + // Get the text template service: ITextTemplating t4 = Package.GetGlobalService(typeof(STextTemplating)) as ITextTemplating; ITextTemplatingSessionHost sessionHost = t4 as ITextTemplatingSessionHost; @@ -23,24 +25,17 @@ public static string ProcessTemplateCore(string templatePath, Context context) sessionHost.Session = sessionHost.CreateSession(); sessionHost.Session["Context"] = context; - string templateContent = System.IO.File.ReadAllText(templatePath); + // string templateContent = System.IO.File.ReadAllText(templatePath); Callback cb = new Callback(); // Process a text template: string result = t4.ProcessTemplate(templatePath, templateContent, cb); - - // TODO need to change the file output based on the extenstion defined in the TT template - //string OutputFullPath; - //if (!string.IsNullOrWhiteSpace(cb.fileExtension)) - //{ - // // If there was an output directive in the TemplateFile, then cb.SetFileExtension() will have been called. - // OutputFullPath = System.IO.Path.ChangeExtension(templatePath, cb.fileExtension); - //} - //else - //{ - // OutputFullPath = System.IO.Path.ChangeExtension(templatePath, ".cs"); - //} + // If there was an output directive in the TemplateFile, then cb.SetFileExtension() will have been called. + if (!string.IsNullOrWhiteSpace(cb.fileExtension)) + { + extension = cb.fileExtension; + } // Append any error messages: