コード例 #1
0
        private static string CalculateTagXml(IntermediateSection section, WixBundleTagSymbol tagSymbol)
        {
            var bundleSymbol = section.Symbols.OfType<WixBundleSymbol>().Single();
            var uniqueId = Guid.Parse(bundleSymbol.BundleId).ToString("D");
            var bundleVersion = TagWriter.CreateFourPartVersion(bundleSymbol.Version);

            var packageTags = CollectPackageTags(section);

            var licensed = (tagSymbol.Attributes == 1);
            if (!Enum.TryParse(tagSymbol.Type, out TagType type))
            {
                type = TagType.Application;
            }

            var containedTags = CalculateContainedTagsAndType(packageTags, ref type);

            using (var ms = new MemoryStream())
            {
                TagWriter.CreateTagFile(ms, tagSymbol.Regid, uniqueId.ToUpperInvariant(), bundleSymbol.Name, bundleVersion, bundleSymbol.Manufacturer, licensed, type, containedTags);

                // Use StreamReader to "eat" the BOM if present.
                ms.Position = 0;
                using (var streamReader = new StreamReader(ms, Encoding.UTF8))
                {
                    return streamReader.ReadToEnd();
                }
            }
        }
        private IEnumerable <SoftwareIdentificationTagSymbol> CreateProductTagFiles(IntermediateSection section)
        {
            var productTags = section.Symbols.OfType <WixProductTagSymbol>().ToList();

            if (!productTags.Any())
            {
                return(null);
            }

            string productCode    = null;
            string productName    = null;
            string productVersion = null;
            string manufacturer   = null;

            foreach (var property in section.Symbols.OfType <PropertySymbol>())
            {
                switch (property.Id.Id)
                {
                case "ProductCode":
                    productCode = property.Value;
                    break;

                case "ProductName":
                    productName = property.Value;
                    break;

                case "ProductVersion":
                    productVersion = TagWriter.CreateFourPartVersion(property.Value);
                    break;

                case "Manufacturer":
                    manufacturer = property.Value;
                    break;
                }
            }

            // If the ProductCode is available, only keep it if it is a GUID.
            if (!String.IsNullOrEmpty(productCode))
            {
                if (productCode.Equals("*"))
                {
                    productCode = null;
                }
                else if (Guid.TryParse(productCode, out var guid))
                {
                    productCode = guid.ToString("D").ToUpperInvariant();
                }
                else // not a GUID, erase it.
                {
                    productCode = null;
                }
            }

            Directory.CreateDirectory(this.workingFolder);

            var fileSymbols = section.Symbols.OfType <FileSymbol>().ToList();
            var swidSymbols = new Dictionary <string, SoftwareIdentificationTagSymbol>();

            foreach (var tagSymbol in productTags)
            {
                var tagFileRef = tagSymbol.FileRef;
                var licensed   = (tagSymbol.Attributes == 1);
                var uniqueId   = String.IsNullOrEmpty(productCode) ? tagSymbol.Name.Replace(" ", "-") : productCode;

                if (!Enum.TryParse(tagSymbol.Type, out TagType type))
                {
                    type = TagType.Application;
                }

                // Ensure all tag symbols in this product share a regid.
                var firstTagSymbol = swidSymbols.Values.FirstOrDefault();
                if (firstTagSymbol != null && firstTagSymbol.Regid != tagSymbol.Regid)
                {
                    this.Messaging.Write(TagErrors.SingleRegIdPerProduct(tagSymbol.SourceLineNumbers, tagSymbol.Regid, firstTagSymbol.Regid, firstTagSymbol.SourceLineNumbers));
                    continue;
                }

                // Find the FileSymbol that is referenced by this WixProductTag.
                var fileSymbol = fileSymbols.FirstOrDefault(f => f.Id.Id == tagFileRef);
                if (fileSymbol != null)
                {
                    var fileName = String.IsNullOrEmpty(fileSymbol.Name) ? Path.GetFileName(fileSymbol.Source.Path) : fileSymbol.Name;

                    // Write the tag file.
                    fileSymbol.Source = new IntermediateFieldPathValue
                    {
                        Path = Path.Combine(this.workingFolder, fileName)
                    };

                    this.backendHelper.TrackFile(fileSymbol.Source.Path, TrackedFileType.Temporary, fileSymbol.SourceLineNumbers);

                    using (var fs = new FileStream(fileSymbol.Source.Path, FileMode.Create))
                    {
                        TagWriter.CreateTagFile(fs, tagSymbol.Regid, uniqueId, productName, productVersion, manufacturer, licensed, type, null);
                    }

                    // Ensure the matching "SoftwareIdentificationTag" symbol exists and is populated correctly.
                    if (!swidSymbols.TryGetValue(tagFileRef, out var swidRow))
                    {
                        swidRow = new SoftwareIdentificationTagSymbol(fileSymbol.SourceLineNumbers)
                        {
                            FileRef = tagFileRef,
                            Regid   = tagSymbol.Regid
                        };

                        swidSymbols.Add(tagFileRef, swidRow);
                    }

                    // Always rewrite.
                    swidRow.TagId = uniqueId;
                    swidRow.Type  = type.ToString();
                }
            }

            return(swidSymbols.Values);
        }