Scorchio.VisualStudio.Services.SimpleTextTemplatingEngine.ProcessTemplate C# (CSharp) Méthode

ProcessTemplate() public méthode

Processes the template.
public ProcessTemplate ( string sourceText, string>.IDictionary parameters, bool removeFileHeaders, bool removeXmlComments, bool removeThisPointer ) : TextTransformation
sourceText string The source text.
parameters string>.IDictionary The parameters.
removeFileHeaders bool if set to true [remove file headers].
removeXmlComments bool if set to true [remove XML comments].
removeThisPointer bool if set to true [remove this pointer].
Résultat TextTransformation
        public TextTransformation ProcessTemplate(
            string sourceText,
            IDictionary<string, string> parameters,
            bool removeFileHeaders,
            bool removeXmlComments,
            bool removeThisPointer)
        {
            TraceService.WriteLine("SimpleTextTemplatingEngine::ProcessTemplate");

            TextTransformation textTransformation = new TextTransformation();

            //// first remove the this pointer if required

            if (removeThisPointer)
            {
                sourceText = sourceText.Replace("this.", string.Empty);
            }

            //// now remove all the parameter definitions!

            string[] lines = sourceText.Split('\n');

            //// by default make it a standard csharp source file!

            textTransformation.FileExtension = "cs";

            string extensionLine = lines.FirstOrDefault(x => x.StartsWith("<#@ Output Extension="));

            if (string.IsNullOrEmpty(extensionLine) == false)
            {
                ///// <#@ Output Extension="cs" #>

                string[] parts = extensionLine.Split('"');

                if (parts.Length > 1)
                {
                    textTransformation.FileExtension = parts[1];
                }
            }

            IEnumerable<string> newLines = lines.Where(x => x.StartsWith("<#@ ") == false);

            if (removeFileHeaders)
            {
                newLines = newLines.Where(x => x.TrimStart().StartsWith("//") == false);
            }

            if (removeXmlComments)
            {
                newLines = newLines.Where(x => x.TrimStart().StartsWith("///") == false);
            }

            string output = string.Empty;

            foreach (string newLine in newLines)
            {
                output += newLine + "\n";
            }

            if (parameters != null)
            {
                foreach (KeyValuePair<string, string> parameter in parameters)
                {
                    string t4Parameter = $"<#= {parameter.Key} #>";

                    output = output.Replace(t4Parameter, parameter.Value);
                }
            }

            //// sort out single LF and replace with CR LF!
            textTransformation.Output = Regex.Replace(output, @"\r\n|\r|\n", "\r\n");

            return textTransformation;
        }

Usage Example

Exemple #1
0
        /// <summary>
        /// Transforms the specified source file.
        /// </summary>
        /// <param name="textTransformationRequest">The text transformation request.</param>
        /// <returns>The Text Transformation.</returns>
        public TextTransformation Transform(TextTransformationRequest textTransformationRequest)
        {
            TraceService.WriteLine("TextTransformationService::Transform sourceFile=" + textTransformationRequest.SourceFile);

            string sourceText = this.GetText(textTransformationRequest.SourceFile);

            TraceService.WriteDebugLine("sourceText=" + sourceText);

            SimpleTextTemplatingEngine engine = new SimpleTextTemplatingEngine();

            TraceService.WriteLine("Before processing template via SimpleTextTemplatingEngine");

            TextTransformation textTransformation = engine.ProcessTemplate(
                sourceText,
                textTransformationRequest.Parameters,
                textTransformationRequest.RemoveFileHeaders,
                textTransformationRequest.RemoveXmlComments,
                textTransformationRequest.RemoveThisPointer);

            TraceService.WriteLine("After processing template via SimpleTextTemplatingEngine = SUCCESS!");

            TraceService.WriteDebugLine("output=" + textTransformation.Output);

            return(textTransformation);
        }
All Usage Examples Of Scorchio.VisualStudio.Services.SimpleTextTemplatingEngine::ProcessTemplate
SimpleTextTemplatingEngine