Skip to content

Commit

Permalink
Add overloads to support create-only Wixouts.
Browse files Browse the repository at this point in the history
This prevents the .NET ZipArchive (and friends) from keeping the whole
thing in memory, to support updating when we don't need to update the
Wixout when building a binary Wixlib.
  • Loading branch information
barnson committed Oct 4, 2024
1 parent dd2fe20 commit f82e422
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/api/wix/WixToolset.Data/Intermediate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,20 @@ public void Save(string path)
}
}

/// <summary>
/// Saves an intermediate that can only be written to to a path on disk.
/// </summary>
/// <param name="path">Path to save intermediate file to disk.</param>
public void SaveNew(string path)
{
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path)));

using (var wixout = WixOutput.CreateNew(path))
{
this.Save(wixout);
}
}

/// <summary>
/// Saves an intermediate to a WixOutput.
/// </summary>
Expand Down
32 changes: 29 additions & 3 deletions src/api/wix/WixToolset.Data/WixOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private WixOutput(Uri uri, ZipArchive archive, Stream stream)
}

/// <summary>
///
///
/// </summary>
public Uri Uri { get; }

Expand Down Expand Up @@ -189,7 +189,10 @@ public void ExtractEmbeddedFile(string embeddedId, string outputPath)
/// <returns>Stream to the data of the file.</returns>
public Stream CreateDataStream(string name)
{
this.DeleteExistingEntry(name);
if (this.archive.Mode == ZipArchiveMode.Update)
{
this.DeleteExistingEntry(name);
}

var entry = this.archive.CreateEntry(name);

Expand All @@ -203,7 +206,10 @@ public Stream CreateDataStream(string name)
/// <param name="path">Path to file on disk to include in the output.</param>
public void ImportDataStream(string name, string path)
{
this.DeleteExistingEntry(name);
if (this.archive.Mode == ZipArchiveMode.Update)
{
this.DeleteExistingEntry(name);
}

this.archive.CreateEntryFromFile(path, name, System.IO.Compression.CompressionLevel.Optimal);
}
Expand Down Expand Up @@ -240,6 +246,26 @@ public string GetData(string name)
}
}

/// <summary>
/// Creates a new file structure on disk that can only be written to.
/// </summary>
/// <param name="path">Path to write file structure to.</param>
/// <returns>Newly created <c>WixOutput</c>.</returns>
internal static WixOutput CreateNew(string path)
{
var fullPath = Path.GetFullPath(path);

Directory.CreateDirectory(Path.GetDirectoryName(fullPath));

var uri = new Uri(fullPath);

var stream = File.Create(path);

var archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true);

return new WixOutput(uri, archive, stream);
}

/// <summary>
/// Disposes of the internal state of the file structure.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/wix/WixToolset.Core/CommandLine/BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private void LibraryPhase(IReadOnlyCollection<Intermediate> intermediates, IRead

if (!this.Messaging.EncounteredError)
{
result.Library.Save(outputPath);
result.Library.SaveNew(outputPath);

this.LayoutFiles(result.TrackedFiles, null, cancellationToken);
}
Expand Down

0 comments on commit f82e422

Please sign in to comment.