aboutsummaryrefslogtreecommitdiff
path: root/src/setup/MetadataTask
diff options
context:
space:
mode:
authorRob Mensching <rob@firegiant.com>2022-08-08 10:43:49 -0700
committerRob Mensching <rob@firegiant.com>2022-08-08 19:44:05 -0700
commit2f1a8d6caa50640925bf634062121391c450a5ed (patch)
tree95268f2ba1e6f2f11569922bce0b5d649076e97b /src/setup/MetadataTask
parentd0882c24534f9bcdba63641ff9ef2cfc7780d879 (diff)
downloadwix-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')
-rw-r--r--src/setup/MetadataTask/GenerateMetadata.cs113
-rw-r--r--src/setup/MetadataTask/Metadata.cs78
-rw-r--r--src/setup/MetadataTask/MetadataTask.csproj14
3 files changed, 205 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
3namespace 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}
diff --git a/src/setup/MetadataTask/Metadata.cs b/src/setup/MetadataTask/Metadata.cs
new file mode 100644
index 00000000..139d9240
--- /dev/null
+++ b/src/setup/MetadataTask/Metadata.cs
@@ -0,0 +1,78 @@
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
3namespace WixToolset.Tasks
4{
5 public enum MetadataType
6 {
7 Unknown,
8 Burn,
9 Msi,
10 }
11
12 public enum ArchitectureType
13 {
14 Unknown,
15 Arm64,
16 X64,
17 X86,
18 }
19
20 //{
21 // "id": [PackageSymbolId] | [BundleSymbolId],
22 // "type": "msi" | "burn"
23 // "name": [ProductName] | [BundleName]
24 // "locale": [ProductLanguage] | [BundleLanguage]
25 // "publisher": [Manufacturer]
26 // "aboutUrl": [ARPURLINFOABOUT] | [AboutUrl]
27 // "supportUrl": [ARPHELPLINK] | [SupportUrl]
28 // "description": [ARPCOMMENTS] | "Installation for" + [BundleName]
29 // "license": [ProductLicense] | [BundleLicense]
30 // "architecture": "x86" | "x64" | "arm64"
31 // "size": ####
32 // "sha256": hex,
33 // "file": <filename>
34 // "created": <ISO timestamp>
35 // "productCode": [ProductCode]
36 // "bundleCode": [BundleId]
37 // "upgradeCode": [UpgradeCode]
38 //}
39
40 public class Metadata
41 {
42 public string Id { get; set; }
43
44 public MetadataType Type { get; set; }
45
46 public string Name { get; set; }
47
48 public string Version { get; set; }
49
50 public string Locale { get; set; }
51
52 public string Publisher { get; set; }
53
54 public string AboutUrl { get; set; }
55
56 public string SupportUrl { get; set; }
57
58 public string Description { get; set; }
59
60 public string License { get; set; }
61
62 public ArchitectureType Architecture { get; set; }
63
64 public string File { get; set; }
65
66 public long Size { get; set; }
67
68 public string Sha256 { get; set; }
69
70 public string Created { get; set; }
71
72 public string ProductCode { get; set; }
73
74 public string BundleCode { get; set; }
75
76 public string UpgradeCode { get; set; }
77 }
78}
diff --git a/src/setup/MetadataTask/MetadataTask.csproj b/src/setup/MetadataTask/MetadataTask.csproj
new file mode 100644
index 00000000..2146a591
--- /dev/null
+++ b/src/setup/MetadataTask/MetadataTask.csproj
@@ -0,0 +1,14 @@
1<Project Sdk="Microsoft.NET.Sdk">
2 <PropertyGroup>
3 <TargetFramework>net472</TargetFramework>
4 <DebugType>embedded</DebugType>
5 <!-- https://github.com/Microsoft/msbuild/issues/2360 -->
6 <PlatformTarget>AnyCPU</PlatformTarget>
7 </PropertyGroup>
8
9 <ItemGroup>
10 <PackageReference Include="Microsoft.Build.Tasks.Core" />
11 <PackageReference Include="System.Text.Json" />
12 <PackageReference Include="WixToolset.Data" />
13 </ItemGroup>
14</Project>