Beispiel #1
0
 /// <summary>
 /// In the constructor sets the host instance into a static object.
 /// </summary>
 /// <param name="host">The host to set as current.</param>
 public void SetCurrentHost(TemplateHost host)
 {
     if (host == null)
     {
         throw new ArgumentNullException("host");
     }
     TemplateHost.CurrentHost = host;
 }
Beispiel #2
0
        /// <summary>
        /// Initialize the host.
        /// </summary>
        /// <param name="host"></param>
        public override void Initialize(ITextTemplatingEngineHost host)
        {
            base.Initialize(host);

            if (host == null)
            {
                throw new ArgumentNullException("host");
            }

            this.host = (TemplateHost)host;
        }
Beispiel #3
0
        /// <summary>
        /// Invokes the T4 rendering engine
        /// </summary>
        /// <param name="templateCode">The T4 template to render</param>
        /// <param name="templateFile">The file containing the template code</param>
        /// <returns>The rendered result</returns>
        protected string Render(string templateCode, string templateFile)
        {
            // Get the package root folder.
            string basePath = GetBasePath();

            // Create the Engine
            ITextTemplatingEngine engine = new Engine();

            // Build arguments.
            IValueInfoService mdService            = (IValueInfoService)GetService(typeof(IValueInfoService));
            Dictionary <string, PropertyData> args = new Dictionary <string, PropertyData>();

            foreach (string key in additionalArguments.Keys)
            {
                Type type = null;
                try
                {
                    type = mdService.GetInfo(key).Type;
                }
                catch (ArgumentException)
                {
                    // Type is not defined in recipe, so take it from the argument value itself.
                    // This is useful for values passed-in as configuration attributes.
                    if (additionalArguments[key] != null)
                    {
                        type = additionalArguments[key].GetType();
                    }
                    else
                    {
                        // Can't determine type, so we can't emit a property.
                        continue;
                    }
                }

                PropertyData propertyData = new PropertyData(additionalArguments[key], type);
                args.Add(key, propertyData);
            }

            // Create the Host. References will be resolved relative to the guidance
            // package installation folder.
            TemplateHost host = new TemplateHost(basePath, args);

            host.TemplateFile = templateFile;

            // Set the output
            string content = engine.ProcessTemplate(templateCode, host);

            // Looking for errors
            if (host.Errors.HasErrors)
            {
                throw new TemplateException(host.Errors);
            }
            else if (host.Errors.HasWarnings)
            {
                StringBuilder warnings = new StringBuilder();
                foreach (CompilerError warning in host.Errors)
                {
                    warnings.AppendLine(warning.ToString());
                }

                Trace.WriteLine(String.Format(
                                    CultureInfo.CurrentCulture,
                                    Properties.Resources.T4Action_CompilationWarnings,
                                    templateFile,
                                    warnings.ToString()));
            }
            return(content);
        }
Beispiel #4
0
 /// <summary>
 /// Causes the current host on the current AppDomain to be set to
 /// the received one for static access from the template.
 /// </summary>
 public TemplateHostInitializer(TemplateHost host)
 {
     this.SetCurrentHost(host);
 }