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 62c5807
Show file tree
Hide file tree
Showing 3 changed files with 55 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
43 changes: 40 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 @@ -60,6 +60,24 @@ public static WixOutput Create(string path)
return WixOutput.Create(uri, stream);
}

/// <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>
public 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);

return WixOutput.CreateNew(uri, stream);
}

/// <summary>
/// Creates a new file structure.
/// </summary>
Expand All @@ -73,6 +91,19 @@ public static WixOutput Create(Uri uri, Stream stream)
return new WixOutput(uri, archive, stream);
}

/// <summary>
/// Creates a new file structure that can only be written to.
/// </summary>
/// <param name="uri"></param>
/// <param name="stream">Stream to write the file structure to.</param>
/// <returns>Newly created <c>WixOutput</c>.</returns>
public static WixOutput CreateNew(Uri uri, Stream stream)
{
var archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true);

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

/// <summary>
/// Loads a wixout from a path on disk.
/// </summary>
Expand Down Expand Up @@ -189,7 +220,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 +237,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
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 62c5807

Please sign in to comment.