Skip to content

Commit

Permalink
[UPDATE] set file extension base on the value provided in the template
Browse files Browse the repository at this point in the history
[UPDATE] clear output window when starting
  • Loading branch information
xairrick committed Feb 9, 2014
1 parent bb5417d commit fbcdc72
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
43 changes: 33 additions & 10 deletions CrmCodeGenerator.VSPackage/CrmCodeGenerator2011.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()
{
Expand All @@ -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;
}

Expand All @@ -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
{
Expand Down Expand Up @@ -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);

Expand All @@ -166,8 +189,8 @@ public int Generate(string wszInputFilePath, string bstrInputFileContents, strin
pcbOutput = (uint)bytes.Length;
}

UpdateStatus("Done!");
UpdateStatus("Done!");

return VSConstants.S_OK;
}

Expand Down
23 changes: 9 additions & 14 deletions CrmCodeGenerator.VSPackage/T4/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
Expand Down

0 comments on commit fbcdc72

Please sign in to comment.