From 770bf982da4a81e56ba4be2b6a7a3afa3868a535 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sun, 2 Jan 2022 08:00:17 -0800 Subject: Extract tracking and creating file transfers to ILayoutServices This refactoring will allow more parts of the pipeline to add files to be transferred during layout and tracked to participate in MSBuild up to date checks and cleaning. --- .../Services/IBackendHelper.cs | 19 +----- .../Services/ILayoutServices.cs | 30 +++++++++ .../ExtensibilityServices/BackendHelper.cs | 62 +----------------- .../ExtensibilityServices/LayoutServices.cs | 74 ++++++++++++++++++++++ .../WixToolset.Core/WixToolsetServiceProvider.cs | 1 + 5 files changed, 108 insertions(+), 78 deletions(-) create mode 100644 src/api/wix/WixToolset.Extensibility/Services/ILayoutServices.cs create mode 100644 src/wix/WixToolset.Core/ExtensibilityServices/LayoutServices.cs diff --git a/src/api/wix/WixToolset.Extensibility/Services/IBackendHelper.cs b/src/api/wix/WixToolset.Extensibility/Services/IBackendHelper.cs index 5c4d9a68..29b8f8e6 100644 --- a/src/api/wix/WixToolset.Extensibility/Services/IBackendHelper.cs +++ b/src/api/wix/WixToolset.Extensibility/Services/IBackendHelper.cs @@ -12,7 +12,7 @@ namespace WixToolset.Extensibility.Services /// /// Interface provided to help backend extensions. /// - public interface IBackendHelper + public interface IBackendHelper : ILayoutServices { /// /// Creates a file facade from a FileSymbol and possible AssemblySymbol. @@ -36,15 +36,6 @@ namespace WixToolset.Extensibility.Services /// New IFileFacade. IFileFacade CreateFileFacadeFromMergeModule(FileSymbol fileSymbol); - /// - /// Creates a file transfer and marks it redundant if the source and destination are identical. - /// - /// Source for the file transfer. - /// Destination for the file transfer. - /// Indicates whether to move or copy the source file. - /// Optional source line numbers that requested the file transfer. - IFileTransfer CreateFileTransfer(string source, string destination, bool move, SourceLineNumber sourceLineNumbers = null); - /// /// Creates a MSI compatible GUID. /// @@ -171,13 +162,5 @@ namespace WixToolset.Extensibility.Services /// Thus the returned array will always be of length 4. /// string[] SplitMsiFileName(string value); - - /// - /// Creates a tracked file. - /// - /// Destination path for the build output. - /// Type of tracked file to create. - /// Optional source line numbers that requested the tracked file. - ITrackedFile TrackFile(string path, TrackedFileType type, SourceLineNumber sourceLineNumbers = null); } } diff --git a/src/api/wix/WixToolset.Extensibility/Services/ILayoutServices.cs b/src/api/wix/WixToolset.Extensibility/Services/ILayoutServices.cs new file mode 100644 index 00000000..79f89d29 --- /dev/null +++ b/src/api/wix/WixToolset.Extensibility/Services/ILayoutServices.cs @@ -0,0 +1,30 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolset.Extensibility.Services +{ + using WixToolset.Data; + using WixToolset.Extensibility.Data; + + /// + /// Interface provided to track files for use by layout later. + /// + public interface ILayoutServices + { + /// + /// Creates a file transfer and marks it redundant if the source and destination are identical. + /// + /// Source for the file transfer. + /// Destination for the file transfer. + /// Indicates whether to move or copy the source file. + /// Optional source line numbers that requested the file transfer. + IFileTransfer CreateFileTransfer(string source, string destination, bool move, SourceLineNumber sourceLineNumbers = null); + + /// + /// Creates a tracked file. + /// + /// Destination path for the build output. + /// Type of tracked file to create. + /// Optional source line numbers that requested the tracked file. + ITrackedFile TrackFile(string path, TrackedFileType type, SourceLineNumber sourceLineNumbers = null); + } +} diff --git a/src/wix/WixToolset.Core/ExtensibilityServices/BackendHelper.cs b/src/wix/WixToolset.Core/ExtensibilityServices/BackendHelper.cs index cfa78623..9d657e1a 100644 --- a/src/wix/WixToolset.Core/ExtensibilityServices/BackendHelper.cs +++ b/src/wix/WixToolset.Core/ExtensibilityServices/BackendHelper.cs @@ -4,7 +4,6 @@ namespace WixToolset.Core.ExtensibilityServices { using System; using System.Collections.Generic; - using System.IO; using WixToolset.Core.Bind; using WixToolset.Data; using WixToolset.Data.Symbols; @@ -12,17 +11,12 @@ namespace WixToolset.Core.ExtensibilityServices using WixToolset.Extensibility.Data; using WixToolset.Extensibility.Services; - internal class BackendHelper : IBackendHelper + internal class BackendHelper : LayoutServices, IBackendHelper { - private static readonly string[] ReservedFileNames = { "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" }; - - public BackendHelper(IServiceProvider serviceProvider) + public BackendHelper(IServiceProvider serviceProvider) : base(serviceProvider) { - this.Messaging = serviceProvider.GetService(); } - private IMessaging Messaging { get; } - public IFileFacade CreateFileFacade(FileSymbol file, AssemblySymbol assembly) { return new FileFacade(file, assembly); @@ -38,22 +32,6 @@ namespace WixToolset.Core.ExtensibilityServices return new FileFacade(true, fileSymbol); } - public IFileTransfer CreateFileTransfer(string source, string destination, bool move, SourceLineNumber sourceLineNumbers = null) - { - var sourceFullPath = this.GetValidatedFullPath(sourceLineNumbers, source); - - var destinationFullPath = this.GetValidatedFullPath(sourceLineNumbers, destination); - - return (String.IsNullOrEmpty(sourceFullPath) || String.IsNullOrEmpty(destinationFullPath)) ? null : new FileTransfer - { - Source = sourceFullPath, - Destination = destinationFullPath, - Move = move, - SourceLineNumbers = sourceLineNumbers, - Redundant = String.Equals(sourceFullPath, destinationFullPath, StringComparison.OrdinalIgnoreCase) - }; - } - public string CreateGuid() { return Common.GenerateGuid(); @@ -112,11 +90,6 @@ namespace WixToolset.Core.ExtensibilityServices return Common.GetNames(value); } - public ITrackedFile TrackFile(string path, TrackedFileType type, SourceLineNumber sourceLineNumbers = null) - { - return new TrackedFile(path, type, sourceLineNumbers); - } - public bool IsValidBinderVariable(string variable) { return Common.IsValidBinderVariable(variable); @@ -141,36 +114,5 @@ namespace WixToolset.Core.ExtensibilityServices { return Common.IsValidShortFilename(filename, allowWildcards); } - - private string GetValidatedFullPath(SourceLineNumber sourceLineNumbers, string path) - { - try - { - var result = Path.GetFullPath(path); - - var filename = Path.GetFileName(result); - - foreach (var reservedName in ReservedFileNames) - { - if (reservedName.Equals(filename, StringComparison.OrdinalIgnoreCase)) - { - this.Messaging.Write(ErrorMessages.InvalidFileName(sourceLineNumbers, path)); - return null; - } - } - - return result; - } - catch (ArgumentException) - { - this.Messaging.Write(ErrorMessages.InvalidFileName(sourceLineNumbers, path)); - } - catch (PathTooLongException) - { - this.Messaging.Write(ErrorMessages.PathTooLong(sourceLineNumbers, path)); - } - - return null; - } } } diff --git a/src/wix/WixToolset.Core/ExtensibilityServices/LayoutServices.cs b/src/wix/WixToolset.Core/ExtensibilityServices/LayoutServices.cs new file mode 100644 index 00000000..84b43892 --- /dev/null +++ b/src/wix/WixToolset.Core/ExtensibilityServices/LayoutServices.cs @@ -0,0 +1,74 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolset.Core.ExtensibilityServices +{ + using System; + using System.IO; + using WixToolset.Data; + using WixToolset.Extensibility.Data; + using WixToolset.Extensibility.Services; + + internal class LayoutServices : ILayoutServices + { + private static readonly string[] ReservedFileNames = { "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" }; + + public LayoutServices(IServiceProvider serviceProvider) + { + this.Messaging = serviceProvider.GetService(); + } + + protected IMessaging Messaging { get; } + + public IFileTransfer CreateFileTransfer(string source, string destination, bool move, SourceLineNumber sourceLineNumbers = null) + { + var sourceFullPath = this.GetValidatedFullPath(sourceLineNumbers, source); + + var destinationFullPath = this.GetValidatedFullPath(sourceLineNumbers, destination); + + return (String.IsNullOrEmpty(sourceFullPath) || String.IsNullOrEmpty(destinationFullPath)) ? null : new FileTransfer + { + Source = sourceFullPath, + Destination = destinationFullPath, + Move = move, + SourceLineNumbers = sourceLineNumbers, + Redundant = String.Equals(sourceFullPath, destinationFullPath, StringComparison.OrdinalIgnoreCase) + }; + } + + public ITrackedFile TrackFile(string path, TrackedFileType type, SourceLineNumber sourceLineNumbers = null) + { + return new TrackedFile(path, type, sourceLineNumbers); + } + + protected string GetValidatedFullPath(SourceLineNumber sourceLineNumbers, string path) + { + try + { + var result = Path.GetFullPath(path); + + var filename = Path.GetFileName(result); + + foreach (var reservedName in ReservedFileNames) + { + if (reservedName.Equals(filename, StringComparison.OrdinalIgnoreCase)) + { + this.Messaging.Write(ErrorMessages.InvalidFileName(sourceLineNumbers, path)); + return null; + } + } + + return result; + } + catch (ArgumentException) + { + this.Messaging.Write(ErrorMessages.InvalidFileName(sourceLineNumbers, path)); + } + catch (PathTooLongException) + { + this.Messaging.Write(ErrorMessages.PathTooLong(sourceLineNumbers, path)); + } + + return null; + } + } +} diff --git a/src/wix/WixToolset.Core/WixToolsetServiceProvider.cs b/src/wix/WixToolset.Core/WixToolsetServiceProvider.cs index 5d700ba0..de89bb73 100644 --- a/src/wix/WixToolset.Core/WixToolsetServiceProvider.cs +++ b/src/wix/WixToolset.Core/WixToolsetServiceProvider.cs @@ -23,6 +23,7 @@ namespace WixToolset.Core this.AddService((provider, singletons) => AddSingleton(singletons, new SymbolDefinitionCreator(provider))); this.AddService((provider, singletons) => AddSingleton(singletons, new ParseHelper(provider))); this.AddService((provider, singletons) => AddSingleton(singletons, new PreprocessHelper(provider))); + this.AddService((provider, singletons) => AddSingleton(singletons, new LayoutServices(provider))); this.AddService((provider, singletons) => AddSingleton(singletons, new BackendHelper(provider))); this.AddService((provider, singletons) => AddSingleton(singletons, new PathResolver())); this.AddService((provider, singletons) => AddSingleton(singletons, new WixBranding())); -- cgit v1.2.3-55-g6feb