Skip to content

refactor(compiler): utf8 module compression#38

Open
DaRacci wants to merge 5 commits into
masterfrom
embedded_compression
Open

refactor(compiler): utf8 module compression#38
DaRacci wants to merge 5 commits into
masterfrom
embedded_compression

Conversation

@DaRacci

@DaRacci DaRacci commented Jun 12, 2026

Copy link
Copy Markdown
Member

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 UTF8String with optional GZip-compressed base64 payloads, and include Compression metadata 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

  • CompiledRemoteModule used to keep a stable identity hash for naming. With the base class now using ComputedHash() for GetNameHash()/metadata, the remote module hash can change after UpdateArchiveContents() sets updated bytes, but MoveModuleManifest() renames the PSD1 using GetNameHash() 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}'";
    }

Comment thread src/Compiler/Resources/ScriptTemplate.ps1
Comment thread src/Compiler/Resources/ScriptTemplate.ps1
Comment thread src/Compiler/Program.cs Outdated
Comment thread tests/Compiler/Module/Compiled/Local.cs Outdated
Comment thread tests/Compiler/Module/Compiled/Local.cs Outdated
Comment thread tests/Compiler/Integration/ScriptTemplateRuntimeTests.cs Outdated
Comment thread tests/Compiler/Program.cs Outdated
Comment thread src/Compiler/Module/Compiled/Compiled.cs
Comment thread src/Compiler/Resources/ScriptTemplate.ps1
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants