Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new --strip option to build bundle #276

Merged
merged 5 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ git init myapp
vim myapp/main.lua
# Run the app
luvi myapp
# Build the binary when done
# Build the binary with compiled Lua code when done
luvi myapp -o mybinary
# Build the binary with compiled, striped Lua code when done
luvi myapp -o mybinary -s
# Build the binary just copy Lua code when done
luvi myapp -o mybinary --copy
# Run the new self-contained binary
./mybinary
# Deploy / Publish / Profit!
Expand Down Expand Up @@ -235,10 +239,13 @@ Usage: luvi bundle+ [options] [-- extra args]

bundle Path to directory or zip file containing bundle source.
`bundle` can be specified multiple times to layer bundles
on top of eachother.
on top of each other.
--version Show luvi version and compiled in options.
--output target Build a luvi app by zipping the bundle and inserting luvi.
--main path Specify a custom main bundle path (normally main.lua)
--copy Do not compile, just copy Lua code and compress.
--strip Compile Lua code and strip debug info.
--force Force build then bundle, ignore Lua compile error.
--help Show this help file.
-- All args after this go to the luvi app itself.

Expand Down
9 changes: 8 additions & 1 deletion src/lua/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ local commands = {
["--version"] = "version",
["-h"] = "help",
["--help"] = "help",
["--copy"] = "copy",
["--force"] = "force",
["-s"] = "strip",
["--strip"] = "strip"
}

local function version(args)
Expand All @@ -64,6 +68,9 @@ Usage: $(LUVI) bundle+ [options] [-- extra args]
--version Show luvi version and compiled in options.
--output target Build a luvi app by zipping the bundle and inserting luvi.
--main path Specify a custom main bundle path (normally main.lua)
--copy Do not compile, just copy Lua code and compress.
--strip Compile Lua code and strip debug info.
--force Force build then bundle, ignore Lua compile error.
--help Show this help file.
-- All args after this go to the luvi app itself.

Expand Down Expand Up @@ -159,7 +166,7 @@ return function(args)

-- Build the app if output is given
if options.output then
return buildBundle(options.output, makeBundle(bundles))
return buildBundle(options, makeBundle(bundles))
end

-- Run the luvi app with the extra args
Expand Down
19 changes: 17 additions & 2 deletions src/lua/luvibundle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ local function zipBundle(base, zip)
return bundle
end

local function buildBundle(target, bundle)
local function buildBundle(options, bundle)
assert(type(options)=='table')
local target = assert(options.output, "missing output target")
target = pathJoin(uv.cwd(), target)
print("Creating new binary: " .. target)
local fd = assert(uv.fs_open(target, "w", 511)) -- 0777
Expand All @@ -173,6 +175,7 @@ local function buildBundle(target, bundle)
uv.fs_close(fd2)
end

local load = loadstring or load
local writer = miniz.new_writer()
local function copyFolder(path)
local files = bundle.readdir(path)
Expand All @@ -187,7 +190,19 @@ local function buildBundle(target, bundle)
copyFolder(child)
elseif stat.type == "file" then
print(" " .. child)
writer:add(child, bundle.readfile(child), 9)
local ctx = bundle.readfile(child)
local isLua = name:sub(-4, -1) == ".lua"
zhaozg marked this conversation as resolved.
Show resolved Hide resolved

-- compile, strip lua code, but skip package.lua
if isLua and name ~= 'package.lua' and not options.copy then
zhaozg marked this conversation as resolved.
Show resolved Hide resolved
local fn, err = load(ctx, child)
if not fn and not options.force then
error(err)
else
ctx = string.dump(fn, options.strip)
end
end
writer:add(child, ctx, 9)
end
end
end
Expand Down