From 1d6ff8af3c423ee4622185edc986ae5caad6b122 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Tue, 26 Dec 2017 15:11:40 -0800 Subject: Standardize creation of public objects in move towards interfaces --- src/WixToolset.Core/Preprocessor.cs | 136 ++++++++++++++++++++++++------------ 1 file changed, 91 insertions(+), 45 deletions(-) (limited to 'src/WixToolset.Core/Preprocessor.cs') diff --git a/src/WixToolset.Core/Preprocessor.cs b/src/WixToolset.Core/Preprocessor.cs index 53d60c87..6733f493 100644 --- a/src/WixToolset.Core/Preprocessor.cs +++ b/src/WixToolset.Core/Preprocessor.cs @@ -14,6 +14,7 @@ namespace WixToolset.Core using WixToolset.Extensibility; using WixToolset.Core.Preprocess; using WixToolset.Extensibility.Services; + using System.Linq; /// /// Preprocessor object @@ -35,6 +36,21 @@ namespace WixToolset.Core XmlResolver = null, }; + public Preprocessor(IServiceProvider serviceProvider) + { + this.ServiceProvider = serviceProvider; + } + + public IEnumerable IncludeSearchPaths { get; set; } + + public Platform Platform { get; set; } + + public string SourcePath { get; set; } + + public IDictionary Variables { get; set; } + + private IServiceProvider ServiceProvider { get; } + private IPreprocessContext Context { get; set; } private Stack CurrentFileStack { get; } = new Stack(); @@ -87,14 +103,19 @@ namespace WixToolset.Core /// /// The preprocessing context. /// XDocument with the postprocessed data. - public XDocument Process(IPreprocessContext context) + public XDocument Execute() { - this.Context = context ?? throw new ArgumentNullException(nameof(context)); + this.Context = this.CreateContext(); + + this.PreProcess(); - using (XmlReader reader = XmlReader.Create(context.SourceFile, DocumentXmlReaderSettings)) + XDocument document; + using (XmlReader reader = XmlReader.Create(this.Context.SourceFile, DocumentXmlReaderSettings)) { - return Process(context, reader); + document = this.Process(reader); } + + return PostProcess(document); } /// @@ -103,46 +124,32 @@ namespace WixToolset.Core /// The preprocessing context. /// XmlReader to processing the context. /// XDocument with the postprocessed data. - public XDocument Process(IPreprocessContext context, XmlReader reader) + public XDocument Execute(XmlReader reader) { - if (this.Context == null) - { - this.Context = context ?? throw new ArgumentNullException(nameof(context)); - } - else if (this.Context != context) - { - throw new ArgumentException(nameof(context)); - } - - if (String.IsNullOrEmpty(this.Context.SourceFile) && !String.IsNullOrEmpty(reader.BaseURI)) + if (String.IsNullOrEmpty(this.SourcePath) && !String.IsNullOrEmpty(reader.BaseURI)) { var uri = new Uri(reader.BaseURI); - this.Context.SourceFile = uri.AbsolutePath; + this.SourcePath = uri.AbsolutePath; } - this.Context.CurrentSourceLineNumber = new SourceLineNumber(this.Context.SourceFile); + this.Context = this.CreateContext(); - this.Helper = this.Context.ServiceProvider.GetService(); + this.PreProcess(); - foreach (var extension in this.Context.Extensions) - { - if (null != extension.Prefixes) - { - foreach (string prefix in extension.Prefixes) - { - if (!this.ExtensionsByPrefix.TryGetValue(prefix, out var collidingExtension)) - { - this.ExtensionsByPrefix.Add(prefix, extension); - } - else - { - this.Context.Messaging.Write(ErrorMessages.DuplicateExtensionPreprocessorType(extension.GetType().ToString(), prefix, collidingExtension.GetType().ToString())); - } - } - } + var document = this.Process(reader); - extension.PrePreprocess(context); - } + return PostProcess(document); + } + + /// + /// Preprocesses a file. + /// + /// The preprocessing context. + /// XmlReader to processing the context. + /// XDocument with the postprocessed data. + private XDocument Process(XmlReader reader) + { + this.Helper = this.ServiceProvider.GetService(); this.CurrentFileStack.Clear(); this.CurrentFileStack.Push(this.Helper.GetVariableValue(this.Context, "sys", "SOURCEFILEDIR")); @@ -161,14 +168,6 @@ namespace WixToolset.Core this.UpdateCurrentLineNumber(reader, 0); throw new WixException(ErrorMessages.InvalidXml(this.Context.CurrentSourceLineNumber, "source", e.Message)); } - finally - { - // Finalize the preprocessing. - foreach (var extension in this.Context.Extensions) - { - extension.PostPreprocess(output); - } - } return this.Context.Messaging.EncounteredError ? null : output; } @@ -1404,7 +1403,7 @@ namespace WixToolset.Core { // build a string to test the directory containing the source file first var currentFolder = this.CurrentFileStack.Peek(); - var includeTestPath = Path.Combine(Path.GetDirectoryName(currentFolder) , includePath); + var includeTestPath = Path.Combine(Path.GetDirectoryName(currentFolder), includePath); // test the source file directory if (File.Exists(includeTestPath)) @@ -1428,5 +1427,52 @@ namespace WixToolset.Core return finalIncludePath; } + + private IPreprocessContext CreateContext() + { + var context = this.ServiceProvider.GetService(); + context.Messaging = this.ServiceProvider.GetService(); + context.Extensions = this.ServiceProvider.GetService().Create(); + context.CurrentSourceLineNumber = new SourceLineNumber(this.SourcePath); + context.Platform = this.Platform; + context.IncludeSearchPaths = this.IncludeSearchPaths?.ToList() ?? new List(); + context.SourceFile = this.SourcePath; + context.Variables = new Dictionary(this.Variables); + + return context; + } + + private void PreProcess() + { + foreach (var extension in this.Context.Extensions) + { + if (extension.Prefixes != null) + { + foreach (var prefix in extension.Prefixes) + { + if (!this.ExtensionsByPrefix.TryGetValue(prefix, out var collidingExtension)) + { + this.ExtensionsByPrefix.Add(prefix, extension); + } + else + { + this.Context.Messaging.Write(ErrorMessages.DuplicateExtensionPreprocessorType(extension.GetType().ToString(), prefix, collidingExtension.GetType().ToString())); + } + } + } + + extension.PrePreprocess(this.Context); + } + } + + private XDocument PostProcess(XDocument document) + { + foreach (var extension in this.Context.Extensions) + { + extension.PostPreprocess(document); + } + + return document; + } } } -- cgit v1.2.3-55-g6feb