From 5812c6c4b9d40e9ae2b5234a778ecf5aeb8423ff Mon Sep 17 00:00:00 2001
From: Rob Mensching <rob@firegiant.com>
Date: Thu, 23 May 2019 15:35:48 -0700
Subject: Integrate latest Data changes for FileTuple and AssemblyTuple

---
 .../ConvertTuplesCommand.cs                        | 82 ++++++++++---------
 .../ConvertTuplesFixture.cs                        | 94 ++++++++++++++++------
 2 files changed, 113 insertions(+), 63 deletions(-)

(limited to 'src')

diff --git a/src/WixToolset.Converters.Tupleizer/ConvertTuplesCommand.cs b/src/WixToolset.Converters.Tupleizer/ConvertTuplesCommand.cs
index 007b9c62..b878f656 100644
--- a/src/WixToolset.Converters.Tupleizer/ConvertTuplesCommand.cs
+++ b/src/WixToolset.Converters.Tupleizer/ConvertTuplesCommand.cs
@@ -23,16 +23,18 @@ namespace WixToolset.Converters.Tupleizer
             var section = new IntermediateSection(String.Empty, OutputType3ToSectionType4(output.Type), output.Codepage);
 
             var wixMediaByDiskId = IndexWixMediaTableByDiskId(output);
+            var componentsById = IndexById<Wix3.Row>(output, "Component");
             var bindPathsById = IndexById<Wix3.Row>(output, "BindPath");
             var fontsById = IndexById<Wix3.Row>(output, "Font");
             var selfRegById = IndexById<Wix3.Row>(output, "SelfReg");
             var wixDirectoryById = IndexById<Wix3.Row>(output, "WixDirectory");
+            var wixFileById = IndexById<Wix3.Row>(output, "WixFile");
 
             foreach (Wix3.Table table in output.Tables)
             {
                 foreach (Wix3.Row row in table.Rows)
                 {
-                    var tuple = GenerateTupleFromRow(row, wixMediaByDiskId, fontsById, bindPathsById, selfRegById, wixDirectoryById);
+                    var tuple = GenerateTupleFromRow(row, wixMediaByDiskId, componentsById, fontsById, bindPathsById, selfRegById, wixFileById, wixDirectoryById);
                     if (tuple != null)
                     {
                         section.Tuples.Add(tuple);
@@ -75,7 +77,7 @@ namespace WixToolset.Converters.Tupleizer
             return byId;
         }
 
-        private static IntermediateTuple GenerateTupleFromRow(Wix3.Row row, Dictionary<int, Wix3.WixMediaRow> wixMediaByDiskId, Dictionary<string, Wix3.Row> fontsById, Dictionary<string, Wix3.Row> bindPathsById, Dictionary<string, Wix3.Row> selfRegById, Dictionary<string, Wix3.Row> wixDirectoryById)
+        private static IntermediateTuple GenerateTupleFromRow(Wix3.Row row, Dictionary<int, Wix3.WixMediaRow> wixMediaByDiskId, Dictionary<string, Wix3.Row> componentsById, Dictionary<string, Wix3.Row> fontsById, Dictionary<string, Wix3.Row> bindPathsById, Dictionary<string, Wix3.Row> selfRegById, Dictionary<string, Wix3.Row> wixFileById, Dictionary<string, Wix3.Row> wixDirectoryById)
         {
             var name = row.Table.Name;
             switch (name)
@@ -234,20 +236,15 @@ namespace WixToolset.Converters.Tupleizer
             case "File":
             {
                 var attributes = FieldAsNullableInt(row, 6);
-                var readOnly = (attributes & WindowsInstallerConstants.MsidbFileAttributesReadOnly) == WindowsInstallerConstants.MsidbFileAttributesReadOnly;
-                var hidden = (attributes & WindowsInstallerConstants.MsidbFileAttributesHidden) == WindowsInstallerConstants.MsidbFileAttributesHidden;
-                var system = (attributes & WindowsInstallerConstants.MsidbFileAttributesSystem) == WindowsInstallerConstants.MsidbFileAttributesSystem;
-                var vital = (attributes & WindowsInstallerConstants.MsidbFileAttributesVital) == WindowsInstallerConstants.MsidbFileAttributesVital;
-                var checksum = (attributes & WindowsInstallerConstants.MsidbFileAttributesChecksum) == WindowsInstallerConstants.MsidbFileAttributesChecksum;
-                bool? compressed = null;
-                if ((attributes & WindowsInstallerConstants.MsidbFileAttributesNoncompressed) == WindowsInstallerConstants.MsidbFileAttributesNoncompressed)
-                {
-                    compressed = false;
-                }
-                else if ((attributes & WindowsInstallerConstants.MsidbFileAttributesCompressed) == WindowsInstallerConstants.MsidbFileAttributesCompressed)
-                {
-                    compressed = true;
-                }
+
+                FileTupleAttributes tupleAttributes = 0;
+                tupleAttributes |= (attributes & WindowsInstallerConstants.MsidbFileAttributesReadOnly) == WindowsInstallerConstants.MsidbFileAttributesReadOnly ? FileTupleAttributes.ReadOnly : 0;
+                tupleAttributes |= (attributes & WindowsInstallerConstants.MsidbFileAttributesHidden) == WindowsInstallerConstants.MsidbFileAttributesHidden ? FileTupleAttributes.Hidden : 0;
+                tupleAttributes |= (attributes & WindowsInstallerConstants.MsidbFileAttributesSystem) == WindowsInstallerConstants.MsidbFileAttributesSystem ? FileTupleAttributes.System : 0;
+                tupleAttributes |= (attributes & WindowsInstallerConstants.MsidbFileAttributesVital) == WindowsInstallerConstants.MsidbFileAttributesVital ? FileTupleAttributes.Vital : 0;
+                tupleAttributes |= (attributes & WindowsInstallerConstants.MsidbFileAttributesChecksum) == WindowsInstallerConstants.MsidbFileAttributesChecksum ? FileTupleAttributes.Checksum : 0;
+                tupleAttributes |= (attributes & WindowsInstallerConstants.MsidbFileAttributesNoncompressed) == WindowsInstallerConstants.MsidbFileAttributesNoncompressed ? FileTupleAttributes.Uncompressed : 0;
+                tupleAttributes |= (attributes & WindowsInstallerConstants.MsidbFileAttributesCompressed) == WindowsInstallerConstants.MsidbFileAttributesCompressed ? FileTupleAttributes.Compressed : 0;
 
                 var id = FieldAsString(row, 0);
 
@@ -258,12 +255,7 @@ namespace WixToolset.Converters.Tupleizer
                     FileSize = FieldAsInt(row, 3),
                     Version = FieldAsString(row, 4),
                     Language = FieldAsString(row, 5),
-                    ReadOnly = readOnly,
-                    Hidden = hidden,
-                    System = system,
-                    Vital = vital,
-                    Checksum = checksum,
-                    Compressed = compressed,
+                    Attributes = tupleAttributes
                 };
 
                 if (bindPathsById.TryGetValue(id, out var bindPathRow))
@@ -281,6 +273,16 @@ namespace WixToolset.Converters.Tupleizer
                     tuple.SelfRegCost = FieldAsNullableInt(selfRegRow, 1) ?? 0;
                 }
 
+                if (wixFileById.TryGetValue(id, out var wixFileRow))
+                {
+                    tuple.DirectoryRef = FieldAsString(wixFileRow, 4);
+                    tuple.DiskId = FieldAsNullableInt(wixFileRow, 5) ?? 0;
+                    tuple.Source = new IntermediateFieldPathValue() { Path = FieldAsString(wixFileRow, 6) };
+                    tuple.PatchGroup = FieldAsInt(wixFileRow, 8);
+                    tuple.Attributes |= FieldAsInt(wixFileRow, 9) != 0 ? FileTupleAttributes.GeneratedShortFileName : 0;
+                    tuple.PatchAttributes = (PatchAttributeType)FieldAsInt(wixFileRow, 10);
+                }
+
                 return tuple;
             }
             case "Font":
@@ -321,7 +323,22 @@ namespace WixToolset.Converters.Tupleizer
             case "MoveFile":
                 return DefaultTupleFromRow(typeof(MoveFileTuple), row, columnZeroIsId: true);
             case "MsiAssembly":
-                return DefaultTupleFromRow(typeof(MsiAssemblyTuple), row, columnZeroIsId: false);
+            {
+                var componentId = FieldAsString(row, 0);
+                if (componentsById.TryGetValue(componentId, out var componentRow))
+                {
+                    return new AssemblyTuple(SourceLineNumber4(row.SourceLineNumbers), new Identifier(AccessModifier.Public, FieldAsString(componentRow, 5)))
+                    {
+                        ComponentRef = componentId,
+                        FeatureRef = FieldAsString(row, 1),
+                        ManifestFileRef = FieldAsString(row, 2),
+                        ApplicationFileRef = FieldAsString(row, 3),
+                        Type = FieldAsNullableInt(row, 4) == 1 ? AssemblyType.Win32Assembly : AssemblyType.DotNetAssembly,
+                    };
+                }
+
+                return null;
+            }
             case "MsiLockPermissionsEx":
                 return DefaultTupleFromRow(typeof(MsiLockPermissionsExTuple), row, columnZeroIsId: true);
             case "MsiShortcutProperty":
@@ -504,6 +521,7 @@ namespace WixToolset.Converters.Tupleizer
             case "Verb":
                 return DefaultTupleFromRow(typeof(VerbTuple), row, columnZeroIsId: false);
             case "WixAction":
+            {
                 var sequenceTable = FieldAsString(row, 0);
                 return new WixActionTuple(SourceLineNumber4(row.SourceLineNumbers))
                 {
@@ -515,6 +533,7 @@ namespace WixToolset.Converters.Tupleizer
                     After = FieldAsString(row, 5),
                     Overridable = FieldAsNullableInt(row, 6) != 0,
                 };
+            }
             case "WixBootstrapperApplication":
                 return DefaultTupleFromRow(typeof(WixBootstrapperApplicationTuple), row, columnZeroIsId: true);
             case "WixBundleContainer":
@@ -525,25 +544,10 @@ namespace WixToolset.Converters.Tupleizer
                 return DefaultTupleFromRow(typeof(WixChainItemTuple), row, columnZeroIsId: true);
             case "WixCustomTable":
                 return DefaultTupleFromRow(typeof(WixCustomTableTuple), row, columnZeroIsId: true);
-            case "WixDeltaPatchFile":
-                return DefaultTupleFromRow(typeof(WixDeltaPatchFileTuple), row, columnZeroIsId: true);
             case "WixDirectory":
                 return null;
             case "WixFile":
-                var assemblyAttributes3 = FieldAsNullableInt(row, 1);
-                return new WixFileTuple(SourceLineNumber4(row.SourceLineNumbers), new Identifier(AccessModifier.Public, FieldAsString(row, 0)))
-                {
-                    AssemblyType = assemblyAttributes3 == 0 ? FileAssemblyType.DotNetAssembly : assemblyAttributes3 == 1 ? FileAssemblyType.Win32Assembly : FileAssemblyType.NotAnAssembly,
-                    AssemblyManifestFileRef = FieldAsString(row, 2),
-                    AssemblyApplicationFileRef = FieldAsString(row, 3),
-                    DirectoryRef = FieldAsString(row, 4),
-                    DiskId = FieldAsNullableInt(row, 5) ?? 0,
-                    Source = new IntermediateFieldPathValue() { Path = FieldAsString(row, 6) },
-                    ProcessorArchitecture = FieldAsString(row, 7),
-                    PatchGroup = FieldAsInt(row, 8),
-                    Attributes = FieldAsInt(row, 9),
-                    PatchAttributes = (PatchAttributeType)FieldAsInt(row, 10),
-                };
+                return null;
             case "WixInstanceTransforms":
                 return DefaultTupleFromRow(typeof(WixInstanceTransformsTuple), row, columnZeroIsId: true);
             case "WixMedia":
diff --git a/src/test/WixToolsetTest.Converters.Tupleizer/ConvertTuplesFixture.cs b/src/test/WixToolsetTest.Converters.Tupleizer/ConvertTuplesFixture.cs
index 9b1fa4cf..14ebd70a 100644
--- a/src/test/WixToolsetTest.Converters.Tupleizer/ConvertTuplesFixture.cs
+++ b/src/test/WixToolsetTest.Converters.Tupleizer/ConvertTuplesFixture.cs
@@ -56,8 +56,11 @@ namespace WixToolsetTest.Converters.Tupleizer
                     .ToArray();
 
                 var tuples = intermediate.Sections.SelectMany(s => s.Tuples);
+
+                var assemblyTuplesByFileId = tuples.OfType<AssemblyTuple>().ToDictionary(a => a.Id.Id);
+
                 var wix4Dump = tuples
-                    .SelectMany(tuple => TupleToStrings(tuple))
+                    .SelectMany(tuple => TupleToStrings(tuple, assemblyTuplesByFileId))
                     .OrderBy(s => s)
                     .ToArray();
 
@@ -67,8 +70,11 @@ namespace WixToolsetTest.Converters.Tupleizer
                 var wix3TextDump = String.Join(Environment.NewLine, wix3Dump);
                 var wix4TextDump = String.Join(Environment.NewLine, wix4Dump);
 
-                File.WriteAllText(Path.Combine(Path.GetTempPath(), "~3.txt"), wix3TextDump);
-                File.WriteAllText(Path.Combine(Path.GetTempPath(), "~4.txt"), wix4TextDump);
+                var path3 = Path.Combine(Path.GetTempPath(), "~3.txt");
+                var path4 = Path.Combine(Path.GetTempPath(), "~4.txt");
+
+                File.WriteAllText(path3, wix3TextDump);
+                File.WriteAllText(path4, wix4TextDump);
 
                 Assert.Equal(wix3TextDump, wix4TextDump);
 #endif
@@ -143,13 +149,19 @@ namespace WixToolsetTest.Converters.Tupleizer
                 break;
             case "WixFile":
             {
-                var fieldValues = row.Fields.Take(10).Select(SafeConvertField).ToArray();
+                var fieldValues = row.Fields.Select(SafeConvertField).ToArray();
                 if (fieldValues[8] == null)
                 {
                     // "Somebody" sometimes writes out a null field even when the column definition says
                     // it's non-nullable. Not naming names or anything. (SWID tags.)
                     fieldValues[8] = "0";
                 }
+                if (fieldValues[10] == null)
+                {
+                    // WixFile rows that come from merge modules will not have the attributes column set
+                    // so initilaize with 0.
+                    fieldValues[10] = "0";
+                }
                 fields = String.Join(",", fieldValues);
                 break;
             }
@@ -166,8 +178,11 @@ namespace WixToolsetTest.Converters.Tupleizer
             }
         }
 
-        private static IEnumerable<string> TupleToStrings(IntermediateTuple tuple)
+        private static IEnumerable<string> TupleToStrings(IntermediateTuple tuple, Dictionary<string, AssemblyTuple> assemblyTuplesByFileId)
         {
+            var name = tuple.Definition.Type == TupleDefinitionType.SummaryInformation ? "_SummaryInformation" : tuple.Definition.Name;
+            var id = tuple.Id?.Id ?? String.Empty;
+
             string fields;
             switch (tuple.Definition.Name)
             {
@@ -280,20 +295,53 @@ namespace WixToolsetTest.Converters.Tupleizer
                     yield return $"SelfReg:{fileTuple.Id.Id},{fileTuple.SelfRegCost}";
                 }
 
+                int? assemblyAttributes = null;
+                if (assemblyTuplesByFileId.TryGetValue(fileTuple.Id.Id, out var assemblyTuple))
+                {
+                    if (assemblyTuple.Type == AssemblyType.DotNetAssembly)
+                    {
+                        assemblyAttributes = 0;
+                    }
+                    else if (assemblyTuple.Type == AssemblyType.Win32Assembly)
+                    {
+                        assemblyAttributes = 1;
+                    }
+                }
+
+                yield return "WixFile:" + String.Join(",",
+                    fileTuple.Id.Id,
+                    assemblyAttributes,
+                    assemblyTuple?.ManifestFileRef,
+                    assemblyTuple?.ApplicationFileRef,
+                    fileTuple.DirectoryRef,
+                    fileTuple.DiskId,
+                    fileTuple.Source.Path,
+                    null, // assembly processor arch
+                    fileTuple.PatchGroup,
+                    (fileTuple.Attributes & FileTupleAttributes.GeneratedShortFileName) != 0 ? 1 : 0,
+                    (int)fileTuple.PatchAttributes,
+                    fileTuple.RetainLengths,
+                    fileTuple.IgnoreOffsets,
+                    fileTuple.IgnoreLengths,
+                    fileTuple.RetainOffsets
+                    );
+
+                var fileAttributes = 0;
+                fileAttributes |= (fileTuple.Attributes & FileTupleAttributes.ReadOnly) != 0 ? WindowsInstallerConstants.MsidbFileAttributesReadOnly : 0;
+                fileAttributes |= (fileTuple.Attributes & FileTupleAttributes.Hidden) != 0 ? WindowsInstallerConstants.MsidbFileAttributesHidden : 0;
+                fileAttributes |= (fileTuple.Attributes & FileTupleAttributes.System) != 0 ? WindowsInstallerConstants.MsidbFileAttributesSystem : 0;
+                fileAttributes |= (fileTuple.Attributes & FileTupleAttributes.Vital) != 0 ? WindowsInstallerConstants.MsidbFileAttributesVital : 0;
+                fileAttributes |= (fileTuple.Attributes & FileTupleAttributes.Checksum) != 0 ? WindowsInstallerConstants.MsidbFileAttributesChecksum : 0;
+                fileAttributes |= (fileTuple.Attributes & FileTupleAttributes.Compressed) != 0 ? WindowsInstallerConstants.MsidbFileAttributesCompressed : 0;
+                fileAttributes |= (fileTuple.Attributes & FileTupleAttributes.Uncompressed) != 0 ? WindowsInstallerConstants.MsidbFileAttributesNoncompressed : 0;
+
                 fields = String.Join(",",
                 fileTuple.ComponentRef,
                 fileTuple.Name,
                 fileTuple.FileSize.ToString(),
                 fileTuple.Version,
                 fileTuple.Language,
-                ((fileTuple.ReadOnly ? WindowsInstallerConstants.MsidbFileAttributesReadOnly : 0)
-                    | (fileTuple.Hidden ? WindowsInstallerConstants.MsidbFileAttributesHidden : 0)
-                    | (fileTuple.System ? WindowsInstallerConstants.MsidbFileAttributesSystem : 0)
-                    | (fileTuple.Vital ? WindowsInstallerConstants.MsidbFileAttributesVital : 0)
-                    | (fileTuple.Checksum ? WindowsInstallerConstants.MsidbFileAttributesChecksum : 0)
-                    | ((fileTuple.Compressed.HasValue && fileTuple.Compressed.Value) ? WindowsInstallerConstants.MsidbFileAttributesCompressed : 0)
-                    | ((fileTuple.Compressed.HasValue && !fileTuple.Compressed.Value) ? WindowsInstallerConstants.MsidbFileAttributesNoncompressed : 0))
-                    .ToString());
+                fileAttributes);
                 break;
             }
 
@@ -301,6 +349,15 @@ namespace WixToolsetTest.Converters.Tupleizer
                 fields = String.Join(",", tuple.Fields.Skip(1).Select(SafeConvertField));
                 break;
 
+            case "Assembly":
+            {
+                var assemblyTuple = (AssemblyTuple)tuple;
+
+                id = null;
+                name = "MsiAssembly";
+                fields = String.Join(",", assemblyTuple.ComponentRef, assemblyTuple.FeatureRef, assemblyTuple.ManifestFileRef, assemblyTuple.ApplicationFileRef, assemblyTuple.Type == AssemblyType.Win32Assembly ? 1 : 0);
+                break;
+            }
             case "Registry":
             {
                 var registryTuple = (RegistryTuple)tuple;
@@ -468,15 +525,6 @@ namespace WixToolsetTest.Converters.Tupleizer
                 break;
             }
 
-            case "WixFile":
-            {
-                var wixFileTuple = (WixFileTuple)tuple;
-                fields = String.Concat(
-                    wixFileTuple.AssemblyType == FileAssemblyType.DotNetAssembly ? "0" : wixFileTuple.AssemblyType == FileAssemblyType.Win32Assembly ? "1" : String.Empty, ",",
-                    String.Join(",", tuple.Fields.Skip(1).Take(8).Select(field => (string)field)));
-                break;
-            }
-
             case "WixProperty":
             {
                 var wixPropertyTuple = (WixPropertyTuple)tuple;
@@ -496,8 +544,6 @@ namespace WixToolsetTest.Converters.Tupleizer
                 break;
             }
 
-            var name = tuple.Definition.Type == TupleDefinitionType.SummaryInformation ? "_SummaryInformation" : tuple.Definition.Name;
-            var id = tuple.Id?.Id ?? String.Empty;
             fields = String.IsNullOrEmpty(id) ? fields : String.IsNullOrEmpty(fields) ? id : $"{id},{fields}";
             yield return $"{name}:{fields}";
         }
-- 
cgit v1.2.3-55-g6feb