@joebigelow / wix-1 / commits / 770bf982

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.

Rob Mensching committed Jan 2, 2022 at 08:00 UTC 770bf982da4a81e56ba4be2b6a7a3afa3868a535
5 files changed +108 -78
src/api/wix/WixToolset.Extensibility/Services/IBackendHelper.cs
+1 -18
@@ -12,7 +12,7 @@ namespace WixToolset.Extensibility.Services
12 /// <summary>
13 /// Interface provided to help backend extensions.
14 /// </summary>
15 - public interface IBackendHelper
15 + public interface IBackendHelper : ILayoutServices
16 {
17 /// <summary>
18 /// Creates a file facade from a <c>FileSymbol</c> and possible <c>AssemblySymbol</c>.
@@ -36,15 +36,6 @@ namespace WixToolset.Extensibility.Services
36 /// <returns>New <c>IFileFacade</c>.</returns>
37 IFileFacade CreateFileFacadeFromMergeModule(FileSymbol fileSymbol);
38
39 - /// <summary>
40 - /// Creates a file transfer and marks it redundant if the source and destination are identical.
41 - /// </summary>
42 - /// <param name="source">Source for the file transfer.</param>
43 - /// <param name="destination">Destination for the file transfer.</param>
44 - /// <param name="move">Indicates whether to move or copy the source file.</param>
45 - /// <param name="sourceLineNumbers">Optional source line numbers that requested the file transfer.</param>
46 - IFileTransfer CreateFileTransfer(string source, string destination, bool move, SourceLineNumber sourceLineNumbers = null);
47 -
39 /// <summary>
40 /// Creates a MSI compatible GUID.
41 /// </summary>
@@ -171,13 +162,5 @@ namespace WixToolset.Extensibility.Services
162 /// Thus the returned array will always be of length 4.
163 /// </remarks>
164 string[] SplitMsiFileName(string value);
174 -
175 - /// <summary>
176 - /// Creates a tracked file.
177 - /// </summary>
178 - /// <param name="path">Destination path for the build output.</param>
179 - /// <param name="type">Type of tracked file to create.</param>
180 - /// <param name="sourceLineNumbers">Optional source line numbers that requested the tracked file.</param>
181 - ITrackedFile TrackFile(string path, TrackedFileType type, SourceLineNumber sourceLineNumbers = null);
165 }
166 }
src/api/wix/WixToolset.Extensibility/Services/ILayoutServices.cs new
+30
@@ -0,0 +1,30 @@
1 +// 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.
2 +
3 +namespace WixToolset.Extensibility.Services
4 +{
5 + using WixToolset.Data;
6 + using WixToolset.Extensibility.Data;
7 +
8 + /// <summary>
9 + /// Interface provided to track files for use by layout later.
10 + /// </summary>
11 + public interface ILayoutServices
12 + {
13 + /// <summary>
14 + /// Creates a file transfer and marks it redundant if the source and destination are identical.
15 + /// </summary>
16 + /// <param name="source">Source for the file transfer.</param>
17 + /// <param name="destination">Destination for the file transfer.</param>
18 + /// <param name="move">Indicates whether to move or copy the source file.</param>
19 + /// <param name="sourceLineNumbers">Optional source line numbers that requested the file transfer.</param>
20 + IFileTransfer CreateFileTransfer(string source, string destination, bool move, SourceLineNumber sourceLineNumbers = null);
21 +
22 + /// <summary>
23 + /// Creates a tracked file.
24 + /// </summary>
25 + /// <param name="path">Destination path for the build output.</param>
26 + /// <param name="type">Type of tracked file to create.</param>
27 + /// <param name="sourceLineNumbers">Optional source line numbers that requested the tracked file.</param>
28 + ITrackedFile TrackFile(string path, TrackedFileType type, SourceLineNumber sourceLineNumbers = null);
29 + }
30 +}
src/wix/WixToolset.Core/ExtensibilityServices/BackendHelper.cs
+2 -60
@@ -4,7 +4,6 @@ namespace WixToolset.Core.ExtensibilityServices
4 {
5 using System;
6 using System.Collections.Generic;
7 - using System.IO;
7 using WixToolset.Core.Bind;
8 using WixToolset.Data;
9 using WixToolset.Data.Symbols;
@@ -12,17 +11,12 @@ namespace WixToolset.Core.ExtensibilityServices
11 using WixToolset.Extensibility.Data;
12 using WixToolset.Extensibility.Services;
13
15 - internal class BackendHelper : IBackendHelper
14 + internal class BackendHelper : LayoutServices, IBackendHelper
15 {
17 - 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" };
18 -
19 - public BackendHelper(IServiceProvider serviceProvider)
16 + public BackendHelper(IServiceProvider serviceProvider) : base(serviceProvider)
17 {
21 - this.Messaging = serviceProvider.GetService<IMessaging>();
18 }
19
24 - private IMessaging Messaging { get; }
25 -
20 public IFileFacade CreateFileFacade(FileSymbol file, AssemblySymbol assembly)
21 {
22 return new FileFacade(file, assembly);
@@ -38,22 +32,6 @@ namespace WixToolset.Core.ExtensibilityServices
32 return new FileFacade(true, fileSymbol);
33 }
34
41 - public IFileTransfer CreateFileTransfer(string source, string destination, bool move, SourceLineNumber sourceLineNumbers = null)
42 - {
43 - var sourceFullPath = this.GetValidatedFullPath(sourceLineNumbers, source);
44 -
45 - var destinationFullPath = this.GetValidatedFullPath(sourceLineNumbers, destination);
46 -
47 - return (String.IsNullOrEmpty(sourceFullPath) || String.IsNullOrEmpty(destinationFullPath)) ? null : new FileTransfer
48 - {
49 - Source = sourceFullPath,
50 - Destination = destinationFullPath,
51 - Move = move,
52 - SourceLineNumbers = sourceLineNumbers,
53 - Redundant = String.Equals(sourceFullPath, destinationFullPath, StringComparison.OrdinalIgnoreCase)
54 - };
55 - }
56 -
35 public string CreateGuid()
36 {
37 return Common.GenerateGuid();
@@ -112,11 +90,6 @@ namespace WixToolset.Core.ExtensibilityServices
90 return Common.GetNames(value);
91 }
92
115 - public ITrackedFile TrackFile(string path, TrackedFileType type, SourceLineNumber sourceLineNumbers = null)
116 - {
117 - return new TrackedFile(path, type, sourceLineNumbers);
118 - }
119 -
93 public bool IsValidBinderVariable(string variable)
94 {
95 return Common.IsValidBinderVariable(variable);
@@ -141,36 +114,5 @@ namespace WixToolset.Core.ExtensibilityServices
114 {
115 return Common.IsValidShortFilename(filename, allowWildcards);
116 }
144 -
145 - private string GetValidatedFullPath(SourceLineNumber sourceLineNumbers, string path)
146 - {
147 - try
148 - {
149 - var result = Path.GetFullPath(path);
150 -
151 - var filename = Path.GetFileName(result);
152 -
153 - foreach (var reservedName in ReservedFileNames)
154 - {
155 - if (reservedName.Equals(filename, StringComparison.OrdinalIgnoreCase))
156 - {
157 - this.Messaging.Write(ErrorMessages.InvalidFileName(sourceLineNumbers, path));
158 - return null;
159 - }
160 - }
161 -
162 - return result;
163 - }
164 - catch (ArgumentException)
165 - {
166 - this.Messaging.Write(ErrorMessages.InvalidFileName(sourceLineNumbers, path));
167 - }
168 - catch (PathTooLongException)
169 - {
170 - this.Messaging.Write(ErrorMessages.PathTooLong(sourceLineNumbers, path));
171 - }
172 -
173 - return null;
174 - }
117 }
118 }
src/wix/WixToolset.Core/ExtensibilityServices/LayoutServices.cs new
+74
@@ -0,0 +1,74 @@
1 +// 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.
2 +
3 +namespace WixToolset.Core.ExtensibilityServices
4 +{
5 + using System;
6 + using System.IO;
7 + using WixToolset.Data;
8 + using WixToolset.Extensibility.Data;
9 + using WixToolset.Extensibility.Services;
10 +
11 + internal class LayoutServices : ILayoutServices
12 + {
13 + 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" };
14 +
15 + public LayoutServices(IServiceProvider serviceProvider)
16 + {
17 + this.Messaging = serviceProvider.GetService<IMessaging>();
18 + }
19 +
20 + protected IMessaging Messaging { get; }
21 +
22 + public IFileTransfer CreateFileTransfer(string source, string destination, bool move, SourceLineNumber sourceLineNumbers = null)
23 + {
24 + var sourceFullPath = this.GetValidatedFullPath(sourceLineNumbers, source);
25 +
26 + var destinationFullPath = this.GetValidatedFullPath(sourceLineNumbers, destination);
27 +
28 + return (String.IsNullOrEmpty(sourceFullPath) || String.IsNullOrEmpty(destinationFullPath)) ? null : new FileTransfer
29 + {
30 + Source = sourceFullPath,
31 + Destination = destinationFullPath,
32 + Move = move,
33 + SourceLineNumbers = sourceLineNumbers,
34 + Redundant = String.Equals(sourceFullPath, destinationFullPath, StringComparison.OrdinalIgnoreCase)
35 + };
36 + }
37 +
38 + public ITrackedFile TrackFile(string path, TrackedFileType type, SourceLineNumber sourceLineNumbers = null)
39 + {
40 + return new TrackedFile(path, type, sourceLineNumbers);
41 + }
42 +
43 + protected string GetValidatedFullPath(SourceLineNumber sourceLineNumbers, string path)
44 + {
45 + try
46 + {
47 + var result = Path.GetFullPath(path);
48 +
49 + var filename = Path.GetFileName(result);
50 +
51 + foreach (var reservedName in ReservedFileNames)
52 + {
53 + if (reservedName.Equals(filename, StringComparison.OrdinalIgnoreCase))
54 + {
55 + this.Messaging.Write(ErrorMessages.InvalidFileName(sourceLineNumbers, path));
56 + return null;
57 + }
58 + }
59 +
60 + return result;
61 + }
62 + catch (ArgumentException)
63 + {
64 + this.Messaging.Write(ErrorMessages.InvalidFileName(sourceLineNumbers, path));
65 + }
66 + catch (PathTooLongException)
67 + {
68 + this.Messaging.Write(ErrorMessages.PathTooLong(sourceLineNumbers, path));
69 + }
70 +
71 + return null;
72 + }
73 + }
74 +}
src/wix/WixToolset.Core/WixToolsetServiceProvider.cs
+1
@@ -23,6 +23,7 @@ namespace WixToolset.Core
23 this.AddService((provider, singletons) => AddSingleton<ISymbolDefinitionCreator>(singletons, new SymbolDefinitionCreator(provider)));
24 this.AddService((provider, singletons) => AddSingleton<IParseHelper>(singletons, new ParseHelper(provider)));
25 this.AddService((provider, singletons) => AddSingleton<IPreprocessHelper>(singletons, new PreprocessHelper(provider)));
26 + this.AddService((provider, singletons) => AddSingleton<ILayoutServices>(singletons, new LayoutServices(provider)));
27 this.AddService((provider, singletons) => AddSingleton<IBackendHelper>(singletons, new BackendHelper(provider)));
28 this.AddService((provider, singletons) => AddSingleton<IPathResolver>(singletons, new PathResolver()));
29 this.AddService((provider, singletons) => AddSingleton<IWixBranding>(singletons, new WixBranding()));