Uh oh!
There was an error while loading. Please reload this page.
refactor(compiler): utf8 module compression - #38
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors how the compiler embeds local module text into generated scripts by introducing a configurable compression mode (defaulting to GZip), and by carrying explicit Compression metadata alongside the existing embedded content Type. This fits the compiler’s responsibility of producing self-contained, portable compiled PowerShell scripts while keeping payloads ASCII-safe when needed.
Changes:
- Add
CompilerSettings+ CLI flag--embedded-compression (none|gzip)to control embedded local text payload compression. - Change local compiled module embedding to
UTF8Stringwith optional GZip-compressed base64 payloads, and includeCompressionmetadata in emitted PowerShell objects. - Update script template runtime and tests to support decompression + end-to-end validation for both gzip and none modes.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Compiler/Program.cs | Adds a unit test for the default compression mode and accepting none. |
| tests/Compiler/Module/Compiled/Remote.cs | Adds coverage asserting remote payload metadata uses Compression = 'None'. |
| tests/Compiler/Module/Compiled/Local.cs | Adds tests for gzip roundtrip, metadata, none-mode plain text emission, and size savings reporting. |
| tests/Compiler/Integration/ScriptTemplateRuntimeTests.cs | Adds end-to-end runtime coverage for gzip/none local text payloads and unicode behavior. |
| src/Compiler/Resources/ScriptTemplate.ps1 | Updates runtime extraction logic to handle UTF8String + Compression and to decompress gzip payloads. |
| src/Compiler/Program.cs | Adds --embedded-compression option and logs a compression summary per compiled script. |
| src/Compiler/Module/Compiled/Remote.cs | Sets remote module Compression metadata and removes the previous identity-hash field/method. |
| src/Compiler/Module/Compiled/Local.cs | Implements local text embedding as plain UTF8 (none) or gzipped base64 (gzip), and exposes embedded payload byte sizing for logging/tests. |
| src/Compiler/Module/Compiled/Compiled.cs | Introduces ContentCompression enum and emits Compression in serialized PowerShell hashtables; shifts naming/hash usage to ComputedHash(). |
| src/Compiler/CompilerSettings.cs | Adds global settings for embedded local text compression mode/level and a configuration helper. |
Comments suppressed due to low confidence (1)
src/Compiler/Module/Compiled/Remote.cs:79
CompiledRemoteModuleused to keep a stable identity hash for naming. With the base class now usingComputedHash()forGetNameHash()/metadata, the remote module hash can change afterUpdateArchiveContents()sets updated bytes, butMoveModuleManifest()renames the PSD1 usingGetNameHash()before that update. This can result in a zip whose manifest name doesn’t match the embedded module folder/hash used by the runtime template, potentially breaking imports. Restoring a stable identity hash for remote modules (and using it for naming/PSD1 renames) would avoid the circular dependency between “hash” and “archive bytes containing the hash”.
public override ContentType Type => ContentType.Zip;
public override ContentCompression Compression => ContentCompression.None;
public override Version Version { get; }
public CompiledRemoteModule(
ModuleSpec moduleSpec,
RequirementGroup requirements,
byte[] bytes
) : base(moduleSpec, requirements, new Lazy<Fin<byte[]>>(() => bytes)) {
var manifest = this.GetPowerShellManifest();
this.Version = manifest["ModuleVersion"] switch {
string version => Version.Parse(version),
null => new Version(0, 0, 1),
var other => throw new InvalidDataException($"ModuleVersion must be a string, but was {other.GetType()}")
};
this.ThisExtraModuleInfo = new(() => {
var info = Assembly.GetExecutingAssembly().GetName();
var extraModuleInfoResource = $"{info.Name}.Resources.ExtraModuleInfo.{this.ModuleSpec.Name}.json";
using var templateStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(extraModuleInfoResource)
?? Assembly.GetExecutingAssembly().GetManifestResourceStream($"{extraModuleInfoResource}c");
if (templateStream == null) return ExtraModuleInfo.Empty;
using var streamReader = new StreamReader(templateStream, Encoding.UTF8);
return JsonSerializer.Deserialize<ExtraModuleInfo>(streamReader.ReadToEnd(), JsonSerializerOptions)
?? ExtraModuleInfo.Empty;
});
}
public override void CompleteCompileAfterResolution() => this.UpdateArchiveContents();
public override Fin<string> StringifyContent() {
this.UpdateArchiveContents();
var base64 = Convert.ToBase64String(this.UpdatedContentBytes.Unwrap());
return $"'{base64}'";
}
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
No description provided.