Skip to content

Commit

Permalink
Include extracted manifest files in manifest MSI payload nupkg
Browse files Browse the repository at this point in the history
  • Loading branch information
dsplaisted committed Jun 24, 2022
1 parent 9176d0a commit 84a1359
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,8 @@ public override bool Execute()
WorkloadPackMsi msi = new(data.Package, platform, BuildEngine, WixToolsetPath, BaseIntermediateOutputPath);
ITaskItem msiOutputItem = msi.Build(MsiOutputPath, IceSuppressions);
// Create the JSON manifest for CLI based installations.
string msiJsonPath = MsiProperties.Create(msiOutputItem.ItemSpec);
// Generate a .csproj to package the MSI and its manifest for CLI installs.
MsiPayloadPackageProject csproj = new(msi.Metadata, msiOutputItem, BaseIntermediateOutputPath, BaseOutputPath, Path.GetFullPath(msiJsonPath));
MsiPayloadPackageProject csproj = new(msi.Metadata, msiOutputItem, BaseIntermediateOutputPath, BaseOutputPath, msi.NuGetPackageFiles);
msiOutputItem.SetMetadata(Metadata.PackageProject, csproj.Create());
lock (msiItems)
Expand Down Expand Up @@ -404,11 +401,8 @@ public override bool Execute()
WorkloadPackGroupMsi msi = new(packGroup, platform, BuildEngine, WixToolsetPath, BaseIntermediateOutputPath);
ITaskItem msiOutputItem = msi.Build(MsiOutputPath, IceSuppressions);
// Create the JSON manifest for CLI based installations.
string msiJsonPath = MsiProperties.Create(msiOutputItem.ItemSpec);
// Generate a .csproj to package the MSI and its manifest for CLI installs.
MsiPayloadPackageProject csproj = new(msi.Metadata, msiOutputItem, BaseIntermediateOutputPath, BaseOutputPath, Path.GetFullPath(msiJsonPath));
MsiPayloadPackageProject csproj = new(msi.Metadata, msiOutputItem, BaseIntermediateOutputPath, BaseOutputPath, msi.NuGetPackageFiles);
msiOutputItem.SetMetadata(Metadata.PackageProject, csproj.Create());
lock (msiItems)
Expand Down Expand Up @@ -442,9 +436,6 @@ public override bool Execute()
{
ITaskItem msiOutputItem = msi.Build(MsiOutputPath, IceSuppressions);
// Create the JSON manifest for CLI based installations.
string msiJsonPath = MsiProperties.Create(msiOutputItem.ItemSpec);
// Generate SWIX authoring for the MSI package.
MsiSwixProject swixProject = new(msiOutputItem, BaseIntermediateOutputPath, BaseOutputPath);
ITaskItem swixProjectItem = new TaskItem(swixProject.Create());
Expand All @@ -456,7 +447,7 @@ public override bool Execute()
}
// Generate a .csproj to package the MSI and its manifest for CLI installs.
MsiPayloadPackageProject csproj = new(msi.Metadata, msiOutputItem, BaseIntermediateOutputPath, BaseOutputPath, Path.GetFullPath(msiJsonPath));
MsiPayloadPackageProject csproj = new(msi.Metadata, msiOutputItem, BaseIntermediateOutputPath, BaseOutputPath, msi.NuGetPackageFiles);
msiOutputItem.SetMetadata(Metadata.PackageProject, csproj.Create());
lock (msiItems)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
</PropertyGroup>

<ItemGroup>
<None Include="__MSI__" Pack="true" PackagePath="\data" />
<None Include="__MSI_JSON__" Pack="true" PackagePath="\data\msi.json" />
<None Include="__LICENSE_FILENAME__" Pack="true" PackagePath="\" />
</ItemGroup>

<Target Name="AddPackageIcon"
Expand Down
14 changes: 14 additions & 0 deletions src/Microsoft.DotNet.Build.Tasks.Workloads/src/Msi/MsiBase.wix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
Expand Down Expand Up @@ -97,6 +98,8 @@ protected string WixToolsetPath
get;
}

public Dictionary<string, string> NuGetPackageFiles { get; set; } = new();

public MsiBase(MsiMetadata metadata, IBuildEngine buildEngine, string wixToolsetPath,
string platform, string baseIntermediateOutputPath)
{
Expand Down Expand Up @@ -217,6 +220,17 @@ protected ITaskItem Link(string compilerOutputPath, string outputFile, ITaskItem

return msiItem;
}

protected void AddDefaultPackageFiles(ITaskItem msi)
{
NuGetPackageFiles[msi.GetMetadata(Workloads.Metadata.FullPath)] = @"\data";

// Create the JSON manifest for CLI based installations.
string msiJsonPath = MsiProperties.Create(msi.ItemSpec);
NuGetPackageFiles[Path.GetFullPath(msiJsonPath)] = "\\data\\msi.json";

NuGetPackageFiles["LICENSE.TXT"] = @"\";
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Linq;
using Microsoft.Build.Framework;

namespace Microsoft.DotNet.Build.Tasks.Workloads.Msi
Expand All @@ -26,22 +27,24 @@ protected override string ProjectFile
get;
}

public MsiPayloadPackageProject(MsiMetadata package, ITaskItem msi, string baseIntermediateOutputPath, string baseOutputPath, string msiJsonPath) :
// Key: path to file, value: path in package
public Dictionary<string, string> PackageContents { get; set; } = new();

public MsiPayloadPackageProject(MsiMetadata package, ITaskItem msi, string baseIntermediateOutputPath, string baseOutputPath, Dictionary<string, string> packageContents) :
base(baseIntermediateOutputPath, baseOutputPath)
{
string platform = msi.GetMetadata(Metadata.Platform);
ProjectSourceDirectory = Path.Combine(SourceDirectory, "msiPackage", platform, package.Id);
ProjectFile = "msi.csproj";

PackageContents = packageContents;

ReplacementTokens[PayloadPackageTokens.__AUTHORS__] = package.Authors;
ReplacementTokens[PayloadPackageTokens.__COPYRIGHT__] = package.Copyright;
ReplacementTokens[PayloadPackageTokens.__DESCRIPTION__] = package.Description;
ReplacementTokens[PayloadPackageTokens.__PACKAGE_ID__] = $"{package.Id}.Msi.{platform}";
ReplacementTokens[PayloadPackageTokens.__PACKAGE_PROJECT_URL__] = package.ProjectUrl;
ReplacementTokens[PayloadPackageTokens.__PACKAGE_VERSION__] = $"{package.PackageVersion}";
ReplacementTokens[PayloadPackageTokens.__MSI__] = msi.GetMetadata(Metadata.FullPath);
ReplacementTokens[PayloadPackageTokens.__MSI_JSON__] = msiJsonPath;
ReplacementTokens[PayloadPackageTokens.__LICENSE_FILENAME__] = "LICENSE.TXT";
}

/// <inheritdoc />
Expand All @@ -50,6 +53,18 @@ public override string Create()
string msiCsproj = EmbeddedTemplates.Extract("msi.csproj", ProjectSourceDirectory);

Utils.StringReplace(msiCsproj, ReplacementTokens, Encoding.UTF8);

var proj = XDocument.Load(msiCsproj);
var itemGroup = proj.Root.Element("ItemGroup");
foreach (var packageFile in PackageContents)
{
itemGroup.Add(new XElement("None",
new XAttribute("Include", packageFile.Key),
new XAttribute("Pack", "true"),
new XAttribute("PackagePath", packageFile.Value)));
}
proj.Save(msiCsproj);

EmbeddedTemplates.Extract("Icon.png", ProjectSourceDirectory);
EmbeddedTemplates.Extract("LICENSE.TXT", ProjectSourceDirectory);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Text;

Expand All @@ -12,9 +12,6 @@ internal static class PayloadPackageTokens
public static readonly string __AUTHORS__ = nameof(__AUTHORS__);
public static readonly string __COPYRIGHT__ = nameof(__COPYRIGHT__);
public static readonly string __DESCRIPTION__ = nameof(__DESCRIPTION__);
public static readonly string __LICENSE_FILENAME__ = nameof(__LICENSE_FILENAME__);
public static readonly string __MSI__ = nameof(__MSI__);
public static readonly string __MSI_JSON__ = nameof(__MSI_JSON__);
public static readonly string __PACKAGE_ID__ = nameof(__PACKAGE_ID__);
public static readonly string __PACKAGE_PROJECT_URL__ = nameof(__PACKAGE_PROJECT_URL__);
public static readonly string __PACKAGE_VERSION__ = nameof(__PACKAGE_VERSION__);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public override ITaskItem Build(string outputPath, ITaskItem[]? iceSuppressions
throw new Exception(Strings.HeatFailedToHarvest);
}

foreach (var file in Directory.GetFiles(packageDataDirectory).Select(f => Path.GetFullPath(f)))
{
NuGetPackageFiles[file] = @"\data\extractedManifest\" + Path.GetFileName(file);
}

// Add WorkloadPackGroups.json to add to workload manifest MSI
string? jsonContentWxs = null;
string? jsonDirectory = null;
Expand All @@ -67,7 +72,9 @@ public override ITaskItem Build(string outputPath, ITaskItem[]? iceSuppressions
string jsonAsString = JsonSerializer.Serialize(WorkloadPackGroups, typeof(IList<WorkloadPackGroupJson>), new JsonSerializerOptions() { WriteIndented = true });
jsonDirectory = Path.Combine(WixSourceDirectory, "json");
Directory.CreateDirectory(jsonDirectory);
File.WriteAllText(Path.Combine(jsonDirectory, "WorkloadPackGroups.json"), jsonAsString);

string jsonFullPath = Path.GetFullPath(Path.Combine(jsonDirectory, "WorkloadPackGroups.json"));
File.WriteAllText(jsonFullPath, jsonAsString);

HarvesterToolTask jsonHeat = new(BuildEngine, WixToolsetPath)
{
Expand All @@ -83,6 +90,8 @@ public override ITaskItem Build(string outputPath, ITaskItem[]? iceSuppressions
{
throw new Exception(Strings.HeatFailedToHarvest);
}

NuGetPackageFiles[jsonFullPath] = @"\data\extractedManifest\" + Path.GetFileName(jsonFullPath);
}

CompilerToolTask candle = CreateDefaultCompiler();
Expand Down Expand Up @@ -130,6 +139,8 @@ public override ITaskItem Build(string outputPath, ITaskItem[]? iceSuppressions
ITaskItem msi = Link(candle.OutputPath,
Path.Combine(outputPath, Path.GetFileNameWithoutExtension(Package.PackagePath) + $"-{Platform}.msi"),
iceSuppressions);

AddDefaultPackageFiles(msi);

return msi;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public override ITaskItem Build(string outputPath, ITaskItem[] iceSuppressions)

ITaskItem msi = Link(candle.OutputPath, msiFileName, iceSuppressions);

AddDefaultPackageFiles(msi);

return msi;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public override ITaskItem Build(string outputPath, ITaskItem[]? iceSuppressions

ITaskItem msi = Link(candle.OutputPath, msiFileName, iceSuppressions);

AddDefaultPackageFiles(msi);

return msi;
}

Expand Down

0 comments on commit 84a1359

Please sign in to comment.