| 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.Collections.Generic; |
| 7 | using System.IO; |
| 8 | using WixToolset.Data; |
| 9 | using WixToolset.Data.WindowsInstaller; |
| 10 | using WixToolset.Extensibility.Data; |
| 11 | using WixToolset.Extensibility.Services; |
| 12 | |
| 13 | internal class PathResolver : IPathResolver |
| 14 | { |
| 15 | public string GetCanonicalDirectoryPath(Dictionary<string, IResolvedDirectory> directories, Dictionary<string, string> componentIdGenSeeds, string directory, Platform platform) |
| 16 | { |
| 17 | if (!directories.TryGetValue(directory, out var resolvedDirectory)) |
| 18 | { |
| 19 | throw new WixException(ErrorMessages.ExpectedDirectory(directory)); |
| 20 | } |
| 21 | |
| 22 | if (null == resolvedDirectory.Path) |
| 23 | { |
| 24 | if (null != componentIdGenSeeds && componentIdGenSeeds.ContainsKey(directory)) |
| 25 | { |
| 26 | resolvedDirectory.Path = componentIdGenSeeds[directory]; |
| 27 | } |
| 28 | else if (WindowsInstallerStandard.IsStandardDirectory(directory)) |
| 29 | { |
| 30 | resolvedDirectory.Path = WindowsInstallerStandard.GetPlatformSpecificDirectoryId(directory, platform); |
| 31 | } |
| 32 | else |
| 33 | { |
| 34 | var name = resolvedDirectory.Name?.ToLowerInvariant(); |
| 35 | |
| 36 | if (String.IsNullOrEmpty(resolvedDirectory.DirectoryParent)) |
| 37 | { |
| 38 | resolvedDirectory.Path = name; |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | var parentPath = this.GetCanonicalDirectoryPath(directories, componentIdGenSeeds, resolvedDirectory.DirectoryParent, platform); |
| 43 | |
| 44 | if (null != resolvedDirectory.Name) |
| 45 | { |
| 46 | resolvedDirectory.Path = Path.Combine(parentPath, name); |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | resolvedDirectory.Path = parentPath; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return resolvedDirectory.Path; |
| 57 | } |
| 58 | |
| 59 | public string GetDirectoryPath(Dictionary<string, IResolvedDirectory> directories, string directory) |
| 60 | { |
| 61 | if (!directories.TryGetValue(directory, out var resolvedDirectory)) |
| 62 | { |
| 63 | throw new WixException(ErrorMessages.ExpectedDirectory(directory)); |
| 64 | } |
| 65 | |
| 66 | if (null == resolvedDirectory.Path) |
| 67 | { |
| 68 | var name = resolvedDirectory.Name; |
| 69 | |
| 70 | if (String.IsNullOrEmpty(resolvedDirectory.DirectoryParent)) |
| 71 | { |
| 72 | resolvedDirectory.Path = name; |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | var parentPath = this.GetDirectoryPath(directories, resolvedDirectory.DirectoryParent); |
| 77 | |
| 78 | if (null != resolvedDirectory.Name) |
| 79 | { |
| 80 | resolvedDirectory.Path = Path.Combine(parentPath, name); |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | resolvedDirectory.Path = parentPath; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return resolvedDirectory.Path; |
| 90 | } |
| 91 | |
| 92 | public string GetFileSourcePath(Dictionary<string, IResolvedDirectory> directories, string directoryId, string fileName, bool compressed, bool useLongName) |
| 93 | { |
| 94 | var fileSourcePath = Common.GetName(fileName, true, useLongName); |
| 95 | |
| 96 | if (compressed) |
| 97 | { |
| 98 | // Use just the file name of the file since all uncompressed files must appear |
| 99 | // in the root of the image in a compressed package. |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | // Get the relative path of where we want the file to be layed out as specified |
| 104 | // in the Directory table. |
| 105 | var directoryPath = this.GetDirectoryPath(directories, directoryId); |
| 106 | fileSourcePath = Path.Combine(directoryPath, fileSourcePath); |
| 107 | } |
| 108 | |
| 109 | // Strip off "SourceDir" if it's still on there. |
| 110 | if (fileSourcePath.StartsWith("SourceDir\\", StringComparison.Ordinal)) |
| 111 | { |
| 112 | fileSourcePath = fileSourcePath.Substring(10); |
| 113 | } |
| 114 | |
| 115 | return fileSourcePath; |
| 116 | } |
| 117 | } |
| 118 | } |