AIT.Tools.VisualStudioTextTransform.TemplateProcessor.ProcessTemplate C# (CSharp) Method

ProcessTemplate() public static method

/
public static ProcessTemplate ( DTE2 dte, string templateFileName, Options options ) : CompilerErrorCollection
dte DTE2
templateFileName string
options Options
return System.CodeDom.Compiler.CompilerErrorCollection
        public static CompilerErrorCollection ProcessTemplate(DTE2 dte, string templateFileName, Options options)
        {
            if (dte == null)
            {
                throw new ArgumentNullException("dte");
            }
            if (templateFileName == null)
            {
                throw new ArgumentNullException("templateFileName");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            var templateDir = Path.GetDirectoryName(templateFileName);
            Debug.Assert(templateDir != null, "templateDir != null, don't expected templateFileName to be a root directory!");
            var defaultResolver = DefaultVariableResolver.CreateFromDte(dte, templateFileName);
            IVariableResolver resolver = defaultResolver;
            Source.TraceEvent(TraceEventType.Information, 1, "Default TargetDir {0} will be used", defaultResolver.TargetDir);
            Source.TraceEvent(TraceEventType.Information, 1, "Default SolutionDir {0} will be used", defaultResolver.SolutionDir);
            Source.TraceEvent(TraceEventType.Information, 1, "Default ProjectDir {0} will be used", defaultResolver.ProjectDir);

            if (!string.IsNullOrEmpty(options.TargetDir))
            {
                if (Directory.Exists(options.TargetDir))
                {
                    Source.TraceEvent(TraceEventType.Information, 1, "TargetDir {0} will be added ", options.TargetDir);
                    resolver = new CombiningVariableResolver(new DefaultVariableResolver(null, null, options.TargetDir), resolver);
                }
                else
                {
                    Source.TraceEvent(TraceEventType.Warning, 1, "TargetDir {0} doesn't exist and will be ignored!", options.TargetDir);
                }
            }

            var result = ProcessTemplateInMemory(dte, templateFileName, resolver);
            var host = result.Item2;
            var output = result.Item1;

            var outFileName = Path.GetFileNameWithoutExtension(templateFileName);
            var outFilePath = Path.Combine(templateDir, outFileName + host.FileExtension);
            // Because with TFS the files could be read-only!
            if (File.Exists(outFilePath))
            {
                var attr = File.GetAttributes(outFilePath);
                File.SetAttributes(outFilePath, attr & ~FileAttributes.ReadOnly);
                File.Delete(outFilePath);
            }
            File.WriteAllText(outFilePath, output, host.FileEncoding);
            return host.Errors;
        }