Skip to content

Commit

Permalink
Improve XML handling of MSIX packer
Browse files Browse the repository at this point in the history
This adds missing root UTF-8 declaration and adds slightly better error message parsing from makeappx.exe. It may potentially solve #114 and #108
  • Loading branch information
marcinotorowski committed Apr 25, 2021
1 parent e907e72 commit a02b4ac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
17 changes: 16 additions & 1 deletion src/Otor.MsixHero.Appx/Packaging/Packer/AppxPacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ public async Task Pack(string directory, string packagePath, AppxPackerOptions o

cancellationToken.ThrowIfCancellationRequested();

await File.WriteAllTextAsync(tempManifest, xmlDocument.ToString(), Encoding.UTF8, cancellationToken).ConfigureAwait(false);
var xmlContent = new StringBuilder();
await using (TextWriter textWriter = new Utf8StringWriter(xmlContent))
{
await xmlDocument.SaveAsync(textWriter, SaveOptions.None, cancellationToken).ConfigureAwait(false);
}

await File.WriteAllTextAsync(tempManifest, xmlContent.ToString(), Encoding.UTF8, cancellationToken).ConfigureAwait(false);

cancellationToken.ThrowIfCancellationRequested();

Expand Down Expand Up @@ -193,5 +199,14 @@ public Task Unpack(string packagePath, string directory, CancellationToken cance

return new MsixSdkWrapper().UnpackPackage(packagePath, directory, cancellationToken, progress);
}

private class Utf8StringWriter : StringWriter
{
public Utf8StringWriter(StringBuilder sb) : base(sb)
{
}

public override Encoding Encoding => Encoding.UTF8;
}
}
}
3 changes: 1 addition & 2 deletions src/Otor.MsixHero.Appx/Signing/SigningManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using Org.BouncyCastle.X509;
using Otor.MsixHero.Appx.Packaging;
using Otor.MsixHero.Appx.Packaging.Manifest.Entities.Summary;
using Otor.MsixHero.Appx.Signing.DeviceGuard;
using Otor.MsixHero.Appx.Signing.Entities;
using Otor.MsixHero.Infrastructure.Branding;
Expand Down Expand Up @@ -848,6 +846,7 @@ await using (TextWriter textWriter = new Utf8StringWriter(sb))

return localCopy;
}

private class Utf8StringWriter : StringWriter
{
public Utf8StringWriter(StringBuilder sb) : base(sb)
Expand Down
21 changes: 18 additions & 3 deletions src/Otor.MsixHero.Infrastructure/ThirdParty/Sdk/MsixSdkWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,18 +311,28 @@ private async Task RunMakeAppx(string arguments, CancellationToken cancellationT

throw new SdkException($"MakeAppx.exe returned exit code {e.ExitCode}. {findSimilar}", e.ExitCode);
}

findSimilar = e.StandardError.FirstOrDefault(item => item.StartsWith("MakeAppx : error: 0x", StringComparison.OrdinalIgnoreCase));
if (findSimilar != null)
{
var manifestError = e.StandardError.FirstOrDefault(item => item.StartsWith("MakeAppx : error: Manifest validation error: "));
manifestError = manifestError?.Substring("MakeAppx : error: Manifest validation error: ".Length);

findSimilar = findSimilar.Substring("MakeAppx : error: ".Length);

int exitCode;
var error = Regex.Match(findSimilar, "([0-9a-zA-Z]+) \\- ");
if (error.Success)
{
findSimilar = findSimilar.Substring(error.Length).Trim();

if (!string.IsNullOrEmpty(manifestError))
{
findSimilar = manifestError;
}
else
{
findSimilar = findSimilar.Substring(error.Length).Trim();
}

if (int.TryParse(error.Groups[1].Value, out exitCode) && exitCode > 0)
{
throw new SdkException($"MakeAppx.exe returned exit code {e.ExitCode} due to error {error.Groups[1].Value}. {findSimilar}", exitCode);
Expand All @@ -340,6 +350,11 @@ private async Task RunMakeAppx(string arguments, CancellationToken cancellationT
throw new InvalidOperationException($"MakeAppx.exe returned exit code {e.ExitCode} due to error {error.Groups[1].Value}. {findSimilar}");
}

if (!string.IsNullOrEmpty(manifestError))
{
findSimilar = manifestError;
}

if (int.TryParse(error.Groups[1].Value, out exitCode) && exitCode > 0)
{
throw new SdkException($"MakeAppx.exe returned exit code {e.ExitCode}. {findSimilar}", exitCode);
Expand Down

0 comments on commit a02b4ac

Please sign in to comment.