PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/ExpressInteropBinding/Microsoft.ServiceModel.Interop.ExtensionUtils/ConfigurationWizard/Service/T4Invoker.cs

#
C# | 124 lines | 58 code | 19 blank | 47 comment | 5 complexity | fcad347e80a919c1fd9a9668ab710cd1 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0
  1. // <copyright file="T4Invoker.cs" company="Microsoft Corporation">
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // </copyright>
  4. namespace Microsoft.ServiceModel.Interop.ConfigurationWizard.Services
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. using System.Text;
  10. using Microsoft.VisualStudio.TextTemplating;
  11. using Microsoft.VisualStudio.TextTemplating.VSHost;
  12. /// <summary>
  13. /// T4Invoker provides a simple mechanism to invoke the T4 engine given
  14. /// a template file, a string/string Dictionary containing substitutions and
  15. /// returns the results and/or error messages
  16. /// </summary>
  17. public class T4Invoker : ITextTemplatingCallback
  18. {
  19. /// <summary>
  20. /// List of error messages (if any) resulting from the T4 transformation
  21. /// </summary>
  22. private List<string> errorMessages = new List<string>();
  23. /// <summary>
  24. /// Errors are collected into a single string and returned in the result value
  25. /// </summary>
  26. private string errors = string.Empty;
  27. /// <summary>
  28. /// Initializes a new instance of the T4Invoker class
  29. /// </summary>
  30. /// <param name="serviceProvider">IServiceProvider for Visual Studio. You can use MEF to import this into your FeatureCommand or ValueProvider.</param>
  31. public T4Invoker(IServiceProvider serviceProvider)
  32. {
  33. this.ServiceProvider = serviceProvider;
  34. }
  35. /// <summary>
  36. /// Gets and sets the VS Service Provider from which we find the ITextTemplating service
  37. /// </summary>
  38. internal IServiceProvider ServiceProvider { get; private set; }
  39. /// <summary>
  40. /// Invokes T4 engine on provided file passing Dictionary values for substitution
  41. /// </summary>
  42. /// <param name="templateName">T4 template name</param>
  43. /// <param name="templateFileContent">T4 template content</param>
  44. /// <param name="fields">String/String Dictionary which T4 contents can use for substitutions</param>
  45. /// <returns>TextTransformationResult object with HasErrors, Content and Filename properties</returns>
  46. public TextTransformationResult TransformText(string templateName, string templateFileContent, IDictionary<string, string> fields)
  47. {
  48. if (fields == null)
  49. {
  50. throw new ArgumentNullException("fields");
  51. }
  52. // Get the T4 engine from VS
  53. ITextTemplating textTemplating = this.ServiceProvider.GetService(typeof(STextTemplating)) as ITextTemplating;
  54. this.errorMessages.Clear();
  55. textTemplating.BeginErrorSession();
  56. // Initialize the T4 host so we can transfer the Dictionary contents
  57. // into the new app domain in which the host runs
  58. ITextTemplatingSessionHost host = textTemplating as ITextTemplatingSessionHost;
  59. host.Session = host.CreateSession();
  60. foreach (string s in fields.Keys)
  61. {
  62. host.Session[s] = fields[s];
  63. }
  64. var content = textTemplating.ProcessTemplate(templateName, templateFileContent, this, null);
  65. textTemplating.EndErrorSession();
  66. return new TextTransformationResult(content, content.StartsWith("ErrorGeneratingOutput", true, CultureInfo.InvariantCulture), String.Empty);
  67. }
  68. /// <summary>
  69. /// Callback provided for T4 to report errors
  70. /// </summary>
  71. /// <param name="warning">True if error is a warning</param>
  72. /// <param name="message">Error text</param>
  73. /// <param name="line">Line number within the T4 that generated the error</param>
  74. /// <param name="column">Column number within the line that generated the error</param>
  75. public void ErrorCallback(bool warning, string message, int line, int column)
  76. {
  77. if (string.IsNullOrEmpty(message))
  78. {
  79. throw new ArgumentNullException("message");
  80. }
  81. if (!warning && message.Contains(typeof(TextTransformationException).FullName))
  82. {
  83. throw new TextTransformationException(message);
  84. }
  85. string s = string.Format(CultureInfo.InvariantCulture, "{0}: {1}. Line: {2} Column: {3}", warning ? "warning" : "error", message, line, column);
  86. this.errors += "\r\n" + s;
  87. this.errorMessages.Add(s);
  88. }
  89. /// <summary>
  90. /// Required callback for T4. Since we do not output a file, we do nothing here
  91. /// </summary>
  92. /// <param name="extension">Extension for output file</param>
  93. public void SetFileExtension(string extension)
  94. {
  95. }
  96. /// <summary>
  97. /// Required callback for T4. We do nothing with this.
  98. /// </summary>
  99. /// <param name="encoding">Encoding for output file</param>
  100. /// <param name="fromOutputDirective">True if this is from an Output directive</param>
  101. public void SetOutputEncoding(Encoding encoding, bool fromOutputDirective)
  102. {
  103. }
  104. }
  105. }