Add diffs in compression generation script

This commit is contained in:
Filip Tibell 2024-05-10 22:45:01 +02:00
parent 8a3f84cbd2
commit 6875353e95
No known key found for this signature in database

View file

@ -3,7 +3,67 @@ local process = require("@lune/process")
local serde = require("@lune/serde") local serde = require("@lune/serde")
local stdio = require("@lune/stdio") local stdio = require("@lune/stdio")
-- Make sure we have all of the different compression tools installed -- Make some utility functions for viewing unexpected differences in files easier
local function stringAsHex(str: string): string
local hex = {}
for i = 1, #str do
table.insert(hex, string.format("%02x", string.byte(str, i)))
end
return table.concat(hex)
end
local function hexDiff(a: string, b: string): string
local diff = {}
for i = 1, math.max(#a, #b) do
local aByte = if #a >= i then string.byte(a, i) else nil
local bByte = if #b >= i then string.byte(b, i) else nil
if aByte == nil then
table.insert(
diff,
string.format(
"%s%02x%s",
stdio.color("green"),
assert(bByte, "unreachable"),
stdio.color("reset")
)
)
elseif bByte == nil then
table.insert(
diff,
string.format(
"%s%02x%s",
stdio.color("red"),
assert(aByte, "unreachable"),
stdio.color("reset")
)
)
else
if aByte == bByte then
table.insert(diff, string.format("%02x", aByte))
else
table.insert(
diff,
string.format(
"%s%02x%s",
stdio.color("yellow"),
assert(bByte, "unreachable"),
stdio.color("reset")
)
)
end
end
end
return table.concat(diff)
end
-- Make sure we have all of the different compression tools installed,
-- note that on macos we do not use the system-installed compression
-- tools, instead preferring to use homebrew-installed (gnu) ones
local BIN_BROTLI = if process.os == "macos" then "/opt/homebrew/bin/brotli" else "brotli"
local BIN_GZIP = if process.os == "macos" then "/opt/homebrew/bin/gzip" else "gzip"
local BIN_LZ4 = if process.os == "macos" then "/opt/homebrew/bin/lz4" else "lz4"
local function checkInstalled(program: string, args: { string }?) local function checkInstalled(program: string, args: { string }?)
print("Checking if", program, "is installed") print("Checking if", program, "is installed")
@ -14,9 +74,9 @@ local function checkInstalled(program: string, args: { string }?)
end end
end end
checkInstalled("brotli", { "--version" }) checkInstalled(BIN_BROTLI, { "--version" })
checkInstalled("gzip", { "--version" }) checkInstalled(BIN_GZIP, { "--version" })
checkInstalled("lz4", { "--version" }) checkInstalled(BIN_LZ4, { "--version" })
-- checkInstalled("zlib", { "--version" }) -- checkInstalled("zlib", { "--version" })
-- Run them to generate files -- Run them to generate files
@ -48,21 +108,21 @@ local INPUT_FILE_CONTENTS = fs.readFile(INPUT_FILE)
local OUTPUT_FILES = { local OUTPUT_FILES = {
{ {
command = "brotli", command = BIN_BROTLI,
format = "brotli" :: serde.CompressDecompressFormat, format = "brotli" :: serde.CompressDecompressFormat,
args = { "--best", "-w", "22", TEMP_FILE }, args = { "--best", "-w", "22", TEMP_FILE },
output = TEMP_FILE .. ".br", output = TEMP_FILE .. ".br",
final = INPUT_FILE .. ".br", final = INPUT_FILE .. ".br",
}, },
{ {
command = "gzip", command = BIN_GZIP,
format = "gzip" :: serde.CompressDecompressFormat, format = "gzip" :: serde.CompressDecompressFormat,
args = { "--best", "--no-name", TEMP_FILE }, args = { "--best", "--no-name", "--synchronous", TEMP_FILE },
output = TEMP_FILE .. ".gz", output = TEMP_FILE .. ".gz",
final = INPUT_FILE .. ".gz", final = INPUT_FILE .. ".gz",
}, },
{ {
command = "lz4", command = BIN_LZ4,
format = "lz4" :: serde.CompressDecompressFormat, format = "lz4" :: serde.CompressDecompressFormat,
args = { "--best", TEMP_FILE }, args = { "--best", TEMP_FILE },
output = TEMP_FILE .. ".lz4", output = TEMP_FILE .. ".lz4",
@ -108,17 +168,15 @@ for _, spec in OUTPUT_FILES do
-- warn the user about this and ask if they want to overwrite the file -- warn the user about this and ask if they want to overwrite the file
local existingContents = fs.readFile(spec.final) local existingContents = fs.readFile(spec.final)
if compressedContents ~= existingContents then if compressedContents ~= existingContents then
stdio.ewrite("Output file does not match expected contents\n") stdio.ewrite("\nCompressed file does not match existing contents!")
stdio.ewrite("\nCompressed:\n") stdio.ewrite("\n\nExisting:\n")
stdio.ewrite(compressedContents) stdio.ewrite(stringAsHex(existingContents))
stdio.ewrite("\n") stdio.ewrite("\n\nCompressed:\n")
stdio.ewrite("\nExisting:\n") stdio.ewrite(hexDiff(existingContents, compressedContents))
stdio.ewrite(existingContents)
stdio.ewrite("\n\n") stdio.ewrite("\n\n")
local confirm = stdio.prompt("confirm", "Do you want to overwrite the file?") local confirm = stdio.prompt("confirm", "Do you want to continue?")
if confirm == true then if confirm == true then
print("Overwriting file!") print("Overwriting file!")
fs.writeFile(spec.output, compressedContents)
else else
stdio.ewrite("\n\nAborting...\n") stdio.ewrite("\n\nAborting...\n")
process.exit(1) process.exit(1)
@ -131,13 +189,12 @@ for _, spec in OUTPUT_FILES do
-- it because we are updating the serde library and need to update test files -- it because we are updating the serde library and need to update test files
local serdeContents = serde.compress(spec.format, INPUT_FILE_CONTENTS) local serdeContents = serde.compress(spec.format, INPUT_FILE_CONTENTS)
if compressedContents ~= serdeContents then if compressedContents ~= serdeContents then
stdio.ewrite("Temp file does not match contents compressed with serde!\n") stdio.ewrite("\nTemp file does not match contents compressed with serde!")
stdio.ewrite("This will caused the new compressed file to fail tests.\n") stdio.ewrite("\nThis will caused the new compressed file to fail tests.")
stdio.ewrite("\nCompressed:\n") stdio.ewrite("\n\nSerde:\n")
stdio.ewrite(compressedContents) stdio.ewrite(stringAsHex(serdeContents))
stdio.ewrite("\n") stdio.ewrite("\n\nCompressed:\n")
stdio.ewrite("\nSerde:\n") stdio.ewrite(hexDiff(serdeContents, compressedContents))
stdio.ewrite(serdeContents)
stdio.ewrite("\n\n") stdio.ewrite("\n\n")
local confirm = stdio.prompt("confirm", "Do you want to continue?") local confirm = stdio.prompt("confirm", "Do you want to continue?")
if confirm == true then if confirm == true then