diff options
author | Rob Mensching <rob@firegiant.com> | 2022-08-08 10:43:49 -0700 |
---|---|---|
committer | Rob Mensching <rob@firegiant.com> | 2022-08-08 19:44:05 -0700 |
commit | 2f1a8d6caa50640925bf634062121391c450a5ed (patch) | |
tree | 95268f2ba1e6f2f11569922bce0b5d649076e97b /src/setup/MetadataTask/GenerateMetadata.cs | |
parent | d0882c24534f9bcdba63641ff9ef2cfc7780d879 (diff) | |
download | wix-2f1a8d6caa50640925bf634062121391c450a5ed.tar.gz wix-2f1a8d6caa50640925bf634062121391c450a5ed.tar.bz2 wix-2f1a8d6caa50640925bf634062121391c450a5ed.zip |
Generate metadata needed to populate update feed
Step towards completing 5367
Diffstat (limited to 'src/setup/MetadataTask/GenerateMetadata.cs')
-rw-r--r-- | src/setup/MetadataTask/GenerateMetadata.cs | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/setup/MetadataTask/GenerateMetadata.cs b/src/setup/MetadataTask/GenerateMetadata.cs new file mode 100644 index 00000000..4071c271 --- /dev/null +++ b/src/setup/MetadataTask/GenerateMetadata.cs | |||
@@ -0,0 +1,113 @@ | |||
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.Tasks | ||
4 | { | ||
5 | using System; | ||
6 | using System.Collections.Generic; | ||
7 | using System.IO; | ||
8 | using System.Linq; | ||
9 | using System.Security.Cryptography; | ||
10 | using System.Text.Json; | ||
11 | using System.Text.Json.Serialization; | ||
12 | using Microsoft.Build.Framework; | ||
13 | using Microsoft.Build.Utilities; | ||
14 | using WixToolset.Data; | ||
15 | using WixToolset.Data.Symbols; | ||
16 | |||
17 | public class GenerateMetadata : Task | ||
18 | { | ||
19 | private static readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions | ||
20 | { | ||
21 | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
22 | IgnoreNullValues = true, | ||
23 | WriteIndented = true, | ||
24 | Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }, | ||
25 | }; | ||
26 | |||
27 | [Required] | ||
28 | public string TargetFile { get; set; } | ||
29 | |||
30 | [Required] | ||
31 | public string WixpdbFile { get; set; } | ||
32 | |||
33 | public override bool Execute() | ||
34 | { | ||
35 | var intermediate = Intermediate.Load(this.WixpdbFile); | ||
36 | |||
37 | var section = intermediate.Sections.Single(); | ||
38 | |||
39 | if (section.Type != SectionType.Bundle) | ||
40 | { | ||
41 | return false; | ||
42 | } | ||
43 | |||
44 | (var metadata, var sourceLineNumber) = this.GetBundleMetadata(section); | ||
45 | |||
46 | if (metadata != null) | ||
47 | { | ||
48 | this.PopulateFileInfo(metadata); | ||
49 | |||
50 | this.SaveMetadata(metadata, sourceLineNumber); | ||
51 | } | ||
52 | |||
53 | return true; | ||
54 | } | ||
55 | |||
56 | private (Metadata, SourceLineNumber) GetBundleMetadata(IntermediateSection section) | ||
57 | { | ||
58 | var bundleSymbol = section.Symbols.OfType<WixBundleSymbol>().Single(); | ||
59 | |||
60 | var metadata = new Metadata | ||
61 | { | ||
62 | Type = MetadataType.Burn, | ||
63 | Name = bundleSymbol.Name, | ||
64 | Version = bundleSymbol.Version, | ||
65 | Publisher = bundleSymbol.Manufacturer, | ||
66 | Description = "Installation for " + bundleSymbol.Name, | ||
67 | SupportUrl = bundleSymbol.HelpUrl, | ||
68 | BundleCode = bundleSymbol.BundleId, | ||
69 | UpgradeCode = bundleSymbol.UpgradeCode, | ||
70 | AboutUrl = bundleSymbol.AboutUrl, | ||
71 | Architecture = PlatformToArchitecture(bundleSymbol.Platform), | ||
72 | }; | ||
73 | |||
74 | return (metadata, bundleSymbol.SourceLineNumbers); | ||
75 | } | ||
76 | |||
77 | private void PopulateFileInfo(Metadata metadata) | ||
78 | { | ||
79 | var fi = new FileInfo(this.TargetFile); | ||
80 | |||
81 | using (var sha256 = SHA256.Create()) | ||
82 | using (var stream = fi.OpenRead()) | ||
83 | { | ||
84 | var hash = sha256.ComputeHash(stream); | ||
85 | |||
86 | metadata.File = fi.Name; | ||
87 | metadata.Created = fi.CreationTimeUtc.ToString("O"); | ||
88 | metadata.Size = fi.Length; | ||
89 | metadata.Sha256 = BitConverter.ToString(hash).Replace("-", String.Empty); | ||
90 | } | ||
91 | } | ||
92 | |||
93 | private void SaveMetadata(Metadata metadata, SourceLineNumber sourceLineNumber) | ||
94 | { | ||
95 | var metadataFilePath = Path.ChangeExtension(this.TargetFile, "metadata.json"); | ||
96 | |||
97 | var json = JsonSerializer.Serialize(metadata, SerializerOptions); | ||
98 | |||
99 | File.WriteAllText(metadataFilePath, json); | ||
100 | } | ||
101 | |||
102 | private static ArchitectureType PlatformToArchitecture(Platform platform) | ||
103 | { | ||
104 | switch (platform) | ||
105 | { | ||
106 | case Platform.X86: return ArchitectureType.X86; | ||
107 | case Platform.X64: return ArchitectureType.X86; | ||
108 | case Platform.ARM64: return ArchitectureType.X86; | ||
109 | default: throw new ArgumentException($"Unknown platform {platform}"); | ||
110 | } | ||
111 | } | ||
112 | } | ||
113 | } | ||