예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CandleTemplateHost"/> class.
        /// </summary>
        /// <param name="currentElement">The current element.</param>
        /// <param name="properties">The properties.</param>
        public CandleTemplateHost(ICustomizableElement currentElement, TemplateProperties properties)
        {
            s_templateProperties = properties;
            if (properties == null)
            {
                s_templateProperties = new TemplateProperties();
            }
            _currentElement = currentElement;
            s_instance      = this;

            standardAssemblyReferences = new List <string>();
            standardAssemblyReferences.Add("System.dll");
            standardAssemblyReferences.Add("Microsoft.VisualStudio.Modeling.SDK, Version=" +
                                           ModelConstants.VisualStudioVersion);
            standardAssemblyReferences.Add("Microsoft.VisualStudio.TextTemplating.VSHost, Version=" +
                                           ModelConstants.VisualStudioVersion);

            //if (type != null && type.Assembly.FullName != this.GetType().Assembly.FullName)
            //    standardAssemblyReferences.Add(type.Assembly.FullName);
        }
예제 #2
0
 /// <summary>
 /// Initializes the specified host.
 /// </summary>
 /// <param name="host">The host.</param>
 public override void Initialize(ITextTemplatingEngineHost host)
 {
     base.Initialize(host);
     currentHost = (CandleTemplateHost)host;
 }
예제 #3
0
        /// <summary>
        /// Appel d'un autre template de transformation
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="T4Template">The t4 template.</param>
        /// <param name="outputFile">Nom du fichier de sortie ou null</param>
        /// <param name="properties">Dictionnaire contenant des variables de remplacement sur le code T4 avant l'exécution ou null</param>
        /// <returns></returns>
        internal static string CallT4Template(ICustomizableElement element, string T4Template, string outputFile, TemplateProperties properties)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element must not be null in CallT4Template");
            }

            if (String.IsNullOrEmpty(T4Template))
            {
                throw new ArgumentException("T4Template is required in CallT4Template");
            }


            ILogger  logger   = ServiceLocator.Instance.GetService <ILogger>();
            string   data     = null;
            Encoding encoding = Encoding.Default;

            try
            {
                RepositoryFile repFile = null;

                // Si le fichier n'est pas un chemin statique, on le récupére sur le repository
                if (!Path.IsPathRooted(T4Template))
                {
                    // On le prend dans le repository
                    repFile = new RepositoryFile(CandleSettings.GetT4TemplateFileName(T4Template));
                }

                string inputFileContent = null;
                try
                {
                    if (repFile != null)
                    {
                        inputFileContent = repFile.ReadContent(out encoding);
                    }
                    else
                    {
                        encoding         = RepositoryFile.FindEncodingFromFile(T4Template);
                        inputFileContent = File.ReadAllText(T4Template);
                    }
                }
                catch { }

                if (String.IsNullOrEmpty(inputFileContent))
                {
                    throw new ApplicationException(String.Format("Template {0} not found or incorrect.", T4Template));
                }

                // Génération du code
                CandleTemplateHost host   = new CandleTemplateHost(element, properties);
                Engine             engine = new Engine();
                data = engine.ProcessTemplate(inputFileContent, host);

                if (host.Errors.HasErrors)
                {
                    StringBuilder sb = new StringBuilder(" ERRORS {");
                    sb.Append(T4Template);
                    sb.Append("} \n");

                    foreach (CompilerError error in host.Errors)
                    {
                        if (!error.IsWarning)
                        {
                            sb.Append(error.ToString());
                            sb.AppendLine();
                        }
                    }

                    if (String.IsNullOrEmpty(data))
                    {
                        data = sb.ToString();
                    }
                    else
                    {
                        data = String.Concat(data, sb.ToString());
                    }

                    foreach (CompilerError err in host.Errors)
                    {
                        err.FileName = T4Template;
                    }
                    ServiceLocator.Instance.IDEHelper.LogErrors(host.Errors);
                    if (logger != null)
                    {
                        logger.Write("Calling T4", String.Format("Run T4 template ({0}) for element naming {1} (id={2}) - Output file = {3} - See errors list.", T4Template, element.Name, element.Id, outputFile ?? " in memory"), LogType.Error);
                    }
                }
                else if (logger != null)
                {
                    logger.Write("Calling T4", String.Format("Run T4 template ({0}) for element naming {1} (id={2}) - Output file = {3}", T4Template, element.Name, element.Id, outputFile ?? " in memory"), LogType.Info);
                }
            }
            catch (Exception ex)
            {
                data = ex.Message;
                if (logger != null)
                {
                    logger.WriteError("Calling T4", T4Template, ex);
                }
            }

            // Si le fichier de sortie est null, on sort.
            // Utilise dans le cas d'un appel de template appelant d'autres templates
            // On ignore le code de la première génération
            if (!String.IsNullOrEmpty(outputFile) && !String.IsNullOrEmpty(data) && s_context.GenerationPass != GenerationPass.MetaModelUpdate)
            {
                WriteSafeOutputFile(outputFile, data, encoding);
                return(data);
            }
            return(data);
        }