Skip to content

Commit 7cebc4d

Browse files
committed
feat: add compression for proto files
This is kind of a "why not" feature.
1 parent 0a0db30 commit 7cebc4d

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/NativeCodeGen.Cli/Program.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ static async Task<int> Main(string[] args)
7575
description: "npm package version (e.g., 1.0.0)",
7676
getDefaultValue: () => "0.0.1");
7777

78+
var compressOption = new Option<bool>(
79+
aliases: new[] { "--compress" },
80+
description: "Gzip compress the output (proto format only)",
81+
getDefaultValue: () => false);
82+
7883
generateCommand.AddOption(inputOption);
7984
generateCommand.AddOption(outputOption);
8085
generateCommand.AddOption(formatOption);
@@ -86,6 +91,7 @@ static async Task<int> Main(string[] args)
8691
generateCommand.AddOption(packageOption);
8792
generateCommand.AddOption(packageNameOption);
8893
generateCommand.AddOption(packageVersionOption);
94+
generateCommand.AddOption(compressOption);
8995

9096
generateCommand.SetHandler(async (context) =>
9197
{
@@ -100,8 +106,9 @@ static async Task<int> Main(string[] args)
100106
var package_ = context.ParseResult.GetValueForOption(packageOption);
101107
var packageName = context.ParseResult.GetValueForOption(packageNameOption);
102108
var packageVersion = context.ParseResult.GetValueForOption(packageVersionOption);
109+
var compress = context.ParseResult.GetValueForOption(compressOption);
103110

104-
var exitCode = await Generate(input, output, format, namespaces, raw, singleFile, strict, exports, package_, packageName, packageVersion);
111+
var exitCode = await Generate(input, output, format, namespaces, raw, singleFile, strict, exports, package_, packageName, packageVersion, compress);
105112
context.ExitCode = exitCode;
106113
});
107114

@@ -136,7 +143,7 @@ static async Task<int> Main(string[] args)
136143
return await rootCommand.InvokeAsync(args);
137144
}
138145

139-
static async Task<int> Generate(string input, string output, string format, string[]? namespaces, bool raw, bool singleFile, bool strict, bool exports, bool package_, string? packageName, string? packageVersion)
146+
static async Task<int> Generate(string input, string output, string format, string[]? namespaces, bool raw, bool singleFile, bool strict, bool exports, bool package_, string? packageName, string? packageVersion, bool compress)
140147
{
141148
Console.WriteLine($"Generating {format} output...");
142149
Console.WriteLine($"Input: {input}");
@@ -210,7 +217,8 @@ static async Task<int> Generate(string input, string output, string format, stri
210217
PackageVersion = packageVersion ?? "0.0.1",
211218
Namespaces = namespaces?.Length > 0
212219
? new HashSet<string>(namespaces.SelectMany(n => n.Split(',')), StringComparer.OrdinalIgnoreCase)
213-
: null
220+
: null,
221+
Compress = compress
214222
};
215223

216224
exporter.Export(db, output, options);

src/NativeCodeGen.Core/Export/IExporter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ public class ExportOptions
1919
public HashSet<string>? Namespaces { get; set; }
2020
public bool IncludeEnums { get; set; } = true;
2121
public bool IncludeStructs { get; set; } = true;
22+
public bool Compress { get; set; }
2223
}

src/NativeCodeGen.Core/Export/ProtobufExporter.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics.CodeAnalysis;
2+
using System.IO.Compression;
23
using NativeCodeGen.Core.Models;
34
using NativeCodeGen.Core.Parsing;
45
using ProtoBuf;
@@ -46,6 +47,15 @@ public void Export(NativeDatabase db, string outputPath, ExportOptions options)
4647
}
4748

4849
using var file = File.Create(binaryPath);
49-
Serializer.Serialize(file, exportDb);
50+
51+
if (options.Compress)
52+
{
53+
using var gzip = new GZipStream(file, CompressionLevel.SmallestSize);
54+
Serializer.Serialize(gzip, exportDb);
55+
}
56+
else
57+
{
58+
Serializer.Serialize(file, exportDb);
59+
}
5060
}
5161
}

0 commit comments

Comments
 (0)