| 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.WindowsInstaller.Bind |
| 4 | { |
| 5 | using System.Collections.Generic; |
| 6 | using System.Linq; |
| 7 | using WixToolset.Data; |
| 8 | using WixToolset.Data.Symbols; |
| 9 | using WixToolset.Data.WindowsInstaller; |
| 10 | using WixToolset.Extensibility; |
| 11 | using WixToolset.Extensibility.Services; |
| 12 | |
| 13 | internal class LoadTableDefinitionsCommand |
| 14 | { |
| 15 | public LoadTableDefinitionsCommand(IMessaging messaging, IntermediateSection section, IEnumerable<IWindowsInstallerBackendBinderExtension> backendExtensions) |
| 16 | { |
| 17 | this.Messaging = messaging; |
| 18 | this.Section = section; |
| 19 | this.BackendExtensions = backendExtensions; |
| 20 | } |
| 21 | |
| 22 | public IMessaging Messaging { get; } |
| 23 | |
| 24 | private IntermediateSection Section { get; } |
| 25 | |
| 26 | private IEnumerable<IWindowsInstallerBackendBinderExtension> BackendExtensions { get; } |
| 27 | |
| 28 | public TableDefinitionCollection TableDefinitions { get; private set; } |
| 29 | |
| 30 | public TableDefinitionCollection Execute() |
| 31 | { |
| 32 | var tableDefinitions = new TableDefinitionCollection(WindowsInstallerTableDefinitions.All); |
| 33 | var customColumnsById = this.Section?.Symbols.OfType<WixCustomTableColumnSymbol>().ToDictionary(t => t.Id.Id); |
| 34 | |
| 35 | if (customColumnsById?.Any() == true) |
| 36 | { |
| 37 | foreach (var symbol in this.Section.Symbols.OfType<WixCustomTableSymbol>()) |
| 38 | { |
| 39 | var customTableDefinition = this.CreateCustomTable(symbol, customColumnsById); |
| 40 | tableDefinitions.Add(customTableDefinition); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | foreach (var backendExtension in this.BackendExtensions) |
| 45 | { |
| 46 | foreach (var tableDefinition in backendExtension.TableDefinitions) |
| 47 | { |
| 48 | if (tableDefinitions.Contains(tableDefinition.Name)) |
| 49 | { |
| 50 | this.Messaging.Write(ErrorMessages.DuplicateExtensionTable(backendExtension.GetType().Assembly.Location, tableDefinition.Name)); |
| 51 | } |
| 52 | |
| 53 | tableDefinitions.Add(tableDefinition); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | this.TableDefinitions = tableDefinitions; |
| 58 | return this.TableDefinitions; |
| 59 | } |
| 60 | |
| 61 | private TableDefinition CreateCustomTable(WixCustomTableSymbol symbol, Dictionary<string, WixCustomTableColumnSymbol> customColumnsById) |
| 62 | { |
| 63 | var columnNames = symbol.ColumnNamesSeparated; |
| 64 | var columns = new List<ColumnDefinition>(columnNames.Length); |
| 65 | |
| 66 | foreach (var name in columnNames) |
| 67 | { |
| 68 | var column = customColumnsById[symbol.Id.Id + "/" + name]; |
| 69 | |
| 70 | var type = ColumnType.Unknown; |
| 71 | |
| 72 | if (column.Type == IntermediateFieldType.String) |
| 73 | { |
| 74 | type = column.Localizable ? ColumnType.Localized : ColumnType.String; |
| 75 | } |
| 76 | else if (column.Type == IntermediateFieldType.Number) |
| 77 | { |
| 78 | type = ColumnType.Number; |
| 79 | } |
| 80 | else if (column.Type == IntermediateFieldType.Path) |
| 81 | { |
| 82 | type = ColumnType.Object; |
| 83 | } |
| 84 | |
| 85 | var category = ColumnCategory.Unknown; |
| 86 | switch (column.Category) |
| 87 | { |
| 88 | case WixCustomTableColumnCategoryType.Text: |
| 89 | category = ColumnCategory.Text; |
| 90 | break; |
| 91 | case WixCustomTableColumnCategoryType.UpperCase: |
| 92 | category = ColumnCategory.UpperCase; |
| 93 | break; |
| 94 | case WixCustomTableColumnCategoryType.LowerCase: |
| 95 | category = ColumnCategory.LowerCase; |
| 96 | break; |
| 97 | case WixCustomTableColumnCategoryType.Integer: |
| 98 | category = ColumnCategory.Integer; |
| 99 | break; |
| 100 | case WixCustomTableColumnCategoryType.DoubleInteger: |
| 101 | category = ColumnCategory.DoubleInteger; |
| 102 | break; |
| 103 | case WixCustomTableColumnCategoryType.TimeDate: |
| 104 | category = ColumnCategory.TimeDate; |
| 105 | break; |
| 106 | case WixCustomTableColumnCategoryType.Identifier: |
| 107 | category = ColumnCategory.Identifier; |
| 108 | break; |
| 109 | case WixCustomTableColumnCategoryType.Property: |
| 110 | category = ColumnCategory.Property; |
| 111 | break; |
| 112 | case WixCustomTableColumnCategoryType.Filename: |
| 113 | category = ColumnCategory.Filename; |
| 114 | break; |
| 115 | case WixCustomTableColumnCategoryType.WildCardFilename: |
| 116 | category = ColumnCategory.WildCardFilename; |
| 117 | break; |
| 118 | case WixCustomTableColumnCategoryType.Path: |
| 119 | category = ColumnCategory.Path; |
| 120 | break; |
| 121 | case WixCustomTableColumnCategoryType.Paths: |
| 122 | category = ColumnCategory.Paths; |
| 123 | break; |
| 124 | case WixCustomTableColumnCategoryType.AnyPath: |
| 125 | category = ColumnCategory.AnyPath; |
| 126 | break; |
| 127 | case WixCustomTableColumnCategoryType.DefaultDir: |
| 128 | category = ColumnCategory.DefaultDir; |
| 129 | break; |
| 130 | case WixCustomTableColumnCategoryType.RegPath: |
| 131 | category = ColumnCategory.RegPath; |
| 132 | break; |
| 133 | case WixCustomTableColumnCategoryType.Formatted: |
| 134 | category = ColumnCategory.Formatted; |
| 135 | break; |
| 136 | case WixCustomTableColumnCategoryType.FormattedSddl: |
| 137 | category = ColumnCategory.FormattedSDDLText; |
| 138 | break; |
| 139 | case WixCustomTableColumnCategoryType.Template: |
| 140 | category = ColumnCategory.Template; |
| 141 | break; |
| 142 | case WixCustomTableColumnCategoryType.Condition: |
| 143 | category = ColumnCategory.Condition; |
| 144 | break; |
| 145 | case WixCustomTableColumnCategoryType.Guid: |
| 146 | category = ColumnCategory.Guid; |
| 147 | break; |
| 148 | case WixCustomTableColumnCategoryType.Version: |
| 149 | category = ColumnCategory.Version; |
| 150 | break; |
| 151 | case WixCustomTableColumnCategoryType.Language: |
| 152 | category = ColumnCategory.Language; |
| 153 | break; |
| 154 | case WixCustomTableColumnCategoryType.Binary: |
| 155 | category = ColumnCategory.Binary; |
| 156 | break; |
| 157 | case WixCustomTableColumnCategoryType.CustomSource: |
| 158 | category = ColumnCategory.CustomSource; |
| 159 | break; |
| 160 | case WixCustomTableColumnCategoryType.Cabinet: |
| 161 | category = ColumnCategory.Cabinet; |
| 162 | break; |
| 163 | case WixCustomTableColumnCategoryType.Shortcut: |
| 164 | category = ColumnCategory.Shortcut; |
| 165 | break; |
| 166 | case null: |
| 167 | default: |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | var modularization = ColumnModularizeType.None; |
| 172 | |
| 173 | switch (column.Modularize) |
| 174 | { |
| 175 | case null: |
| 176 | case WixCustomTableColumnModularizeType.None: |
| 177 | modularization = ColumnModularizeType.None; |
| 178 | break; |
| 179 | case WixCustomTableColumnModularizeType.Column: |
| 180 | modularization = ColumnModularizeType.Column; |
| 181 | break; |
| 182 | case WixCustomTableColumnModularizeType.CompanionFile: |
| 183 | modularization = ColumnModularizeType.CompanionFile; |
| 184 | break; |
| 185 | case WixCustomTableColumnModularizeType.Condition: |
| 186 | modularization = ColumnModularizeType.Condition; |
| 187 | break; |
| 188 | case WixCustomTableColumnModularizeType.ControlEventArgument: |
| 189 | modularization = ColumnModularizeType.ControlEventArgument; |
| 190 | break; |
| 191 | case WixCustomTableColumnModularizeType.ControlText: |
| 192 | modularization = ColumnModularizeType.ControlText; |
| 193 | break; |
| 194 | case WixCustomTableColumnModularizeType.Icon: |
| 195 | modularization = ColumnModularizeType.Icon; |
| 196 | break; |
| 197 | case WixCustomTableColumnModularizeType.Property: |
| 198 | modularization = ColumnModularizeType.Property; |
| 199 | break; |
| 200 | case WixCustomTableColumnModularizeType.SemicolonDelimited: |
| 201 | modularization = ColumnModularizeType.SemicolonDelimited; |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | var columnDefinition = new ColumnDefinition(name, type, column.Width, column.PrimaryKey, column.Nullable, category, column.MinValue, column.MaxValue, column.KeyTable, column.KeyColumn, column.Set, column.Description, modularization, ColumnType.Localized == type, useCData: true, column.Unreal); |
| 206 | columns.Add(columnDefinition); |
| 207 | } |
| 208 | |
| 209 | var customTable = new TableDefinition(symbol.Id.Id, null, columns, symbol.Unreal); |
| 210 | return customTable; |
| 211 | } |
| 212 | } |
| 213 | } |