aboutsummaryrefslogtreecommitdiff
path: root/src/setup/MetadataTask/GenerateMetadata.cs
blob: 65e6f7a46158876ef0b11bf0ff9e0252a9758c43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// 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.

namespace WixToolset.Tasks
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text.Json;
    using System.Text.Json.Serialization;
    using Microsoft.Build.Framework;
    using Microsoft.Build.Utilities;
    using WixToolset.Data;
    using WixToolset.Data.Symbols;

    public class GenerateMetadata : Task
    {
        private static readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions
        {
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
            WriteIndented = true,
            Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) },
        };

        [Required]
        public string TargetFile { get; set; }

        [Required]
        public string WixpdbFile { get; set; }

        public override bool Execute()
        {
            var intermediate = Intermediate.Load(this.WixpdbFile);

            var section = intermediate.Sections.Single();

            if (section.Type != SectionType.Bundle)
            {
                return false;
            }

            (var metadata, var sourceLineNumber) = this.GetBundleMetadata(section);

            if (metadata != null)
            {
                this.PopulateFileInfo(metadata);

                this.SaveMetadata(metadata, sourceLineNumber);
            }

            return true;
        }

        private (Metadata, SourceLineNumber) GetBundleMetadata(IntermediateSection section)
        {
            var bundleSymbol = section.Symbols.OfType<WixBundleSymbol>().Single();

            var metadata = new Metadata
            {
                Id = "WixToolset.AdditionalTools",
                Type = MetadataType.Burn,
                Name = bundleSymbol.Name,
                Version = bundleSymbol.Version,
                Publisher = bundleSymbol.Manufacturer,
                Description = "Installation for " + bundleSymbol.Name,
                License = "MS-RL",
                SupportUrl = bundleSymbol.HelpUrl,
                BundleCode = bundleSymbol.BundleId,
                UpgradeCode = bundleSymbol.UpgradeCode,
                AboutUrl = bundleSymbol.AboutUrl,
                Architecture = PlatformToArchitecture(bundleSymbol.Platform),
            };

            return (metadata, bundleSymbol.SourceLineNumbers);
        }

        private void PopulateFileInfo(Metadata metadata)
        {
            var fi = new FileInfo(this.TargetFile);

            using (var sha256 = SHA256.Create())
            using (var stream = fi.OpenRead())
            {
                var hash = sha256.ComputeHash(stream);

                metadata.File = fi.Name;
                metadata.Created = fi.CreationTimeUtc.ToString("O");
                metadata.Size = fi.Length;
                metadata.Sha256 = BitConverter.ToString(hash).Replace("-", String.Empty);
            }
        }

        private void SaveMetadata(Metadata metadata, SourceLineNumber sourceLineNumber)
        {
            var metadataFilePath = Path.ChangeExtension(this.TargetFile, "metadata.json");

            var json = JsonSerializer.Serialize(metadata, SerializerOptions);

            File.WriteAllText(metadataFilePath, json);
        }

        private static ArchitectureType PlatformToArchitecture(Platform platform)
        {
            switch (platform)
            {
                case Platform.X86: return ArchitectureType.X86;
                case Platform.X64: return ArchitectureType.X64;
                case Platform.ARM64: return ArchitectureType.Arm64;
                default: throw new ArgumentException($"Unknown platform {platform}");
            }
        }
    }
}