Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 9 additions & 18 deletions packages/lde-core/src/package/run.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ local function runFileWithLuaCLI(package, scriptPath, args, vars, engine, cwd)
if vars then for k, v in pairs(vars) do env[k] = v end end
local code, _, stderr = process.exec(engine, { scriptPath },
{ cwd = cwd, env = env, stdout = "inherit", stderr = "inherit" })
return code == 0 or nil, stderr
return code == 0, stderr or "Script exited with a non-zero exit code"
end

---@param package lde.Package
Expand All @@ -84,7 +84,7 @@ local function runStringWithLuaCLI(package, code, args, vars, engine, cwd)
if vars then for k, v in pairs(vars) do env[k] = v end end
local exitCode, _, stderr = process.exec(engine, { "-e", code, unpack(args or {}) },
{ cwd = cwd, env = env, stdout = "inherit", stderr = "inherit" })
return exitCode == 0 or nil, stderr
return exitCode == 0, stderr or "Script exited with a non-zero exit code"
end

---@param package lde.Package
Expand Down Expand Up @@ -115,18 +115,13 @@ local function runFile(package, scriptPath, args, vars, cwd, profile, flamegraph
cwd = cwd or package:getDir()

local engine = config.engine or "lde"
local ok, err
if engine == "lde" or engine == "lpm" --[[ compat ]] then
ok, err = runFileWithLDE(package, scriptPath, args, vars, cwd, profile, flamegraph)
else
if profile or flamegraph then
return nil, "Profiling is only supported when engine is 'lde'"
end

ok, err = runFileWithLuaCLI(package, scriptPath, args, vars, engine, cwd)
return runFileWithLDE(package, scriptPath, args, vars, cwd, profile, flamegraph)
end

return ok or nil, err or "Script exited with a non-zero exit code"
if profile or flamegraph then
return nil, "Profiling is only supported when engine is 'lde'"
end
return runFileWithLuaCLI(package, scriptPath, args, vars, engine, cwd)
end

---@param package lde.Package
Expand All @@ -142,14 +137,10 @@ local function runString(package, code, args, vars, cwd)
cwd = cwd or package:getDir()

local engine = config.engine or "lde"
local ok, err
if engine == "lde" or engine == "lpm" --[[ compat ]] then
ok, err = runStringWithLDE(package, code, args, vars, cwd)
else
ok, err = runStringWithLuaCLI(package, code, args, vars, engine, cwd)
return runStringWithLDE(package, code, args, vars, cwd)
end

return ok or nil, err or "Script exited with a non-zero exit code"
return runStringWithLuaCLI(package, code, args, vars, engine, cwd)
end

return { runFile = runFile, runString = runString, getLuaPaths = getLuaPathsForPackage }
95 changes: 49 additions & 46 deletions packages/lde-core/src/runtime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -301,59 +301,62 @@ local function executeWith(compile, opts, scriptName)
local originalTmpname = os.tmpname
os.tmpname = env.tmpfile

local ok, result = pcall(function()
package.path = opts.packagePath or oldPath
package.cpath = opts.packageCPath or oldCPath

local stopProfiler, profilerErr
if opts.profile or opts.flamegraph then
stopProfiler, profilerErr = startProfiler(PROFILER_MS_PER_SAMPLE, scriptName)
if not stopProfiler then
error("Failed to start profiler: " .. tostring(profilerErr))
end
package.path = opts.packagePath or oldPath
package.cpath = opts.packageCPath or oldCPath

local stopProfiler
if opts.profile or opts.flamegraph then
local profilerErr
stopProfiler, profilerErr = startProfiler(PROFILER_MS_PER_SAMPLE, scriptName)
if not stopProfiler then
for k, v in pairs(oldEnvVars) do env.set(k, v) end
if oldCwd then env.chdir(oldCwd) end
ffi.cdef = originalCdef
os.tmpname = originalTmpname
restore(package.loaded, savedLoaded)
restore(package.preload, savedPreload)
package.loaders = oldLoaders
package.path, package.cpath = oldPath, oldCPath
return false, "Failed to start profiler: " .. tostring(profilerErr)
end
end

local a, b, c, d, e, f
local runOk, runErr = xpcall(function()
if opts.args then
arg = opts.args
arg[0] = scriptName
a, b, c, d, e, f = chunk(unpack(opts.args))
else
a, b, c, d, e, f = chunk()
end
end, function(err) return err end)

if stopProfiler then
local counts, vmstates, total, stacks = stopProfiler()
if opts.profile and lde.verbose then
printProfileReport(counts, vmstates, total, env.cwd(), PROFILER_MS_PER_SAMPLE)
end

if opts.flamegraph then
local fgTitle = scriptName and scriptName:match("[^/\\]+$")
local ok, err = lde.flamegraph.write(stacks, total, PROFILER_MS_PER_SAMPLE, opts.flamegraph, fgTitle)

if lde.verbose then
if ok then
ansi.printf("{cyan}Flamegraph written to %s\n", opts.flamegraph)
else
ansi.printf("{red}Flamegraph error: %s\n", err or "unknown error")
end
local function finishProfiler()
if not stopProfiler then return end
local counts, vmstates, total, stacks = stopProfiler()
stopProfiler = nil
if opts.profile and lde.verbose then
printProfileReport(counts, vmstates, total, env.cwd(), PROFILER_MS_PER_SAMPLE)
end
if opts.flamegraph then
local fgTitle = scriptName and scriptName:match("[^/\\]+$")
local ok, err = lde.flamegraph.write(stacks, total, PROFILER_MS_PER_SAMPLE, opts.flamegraph, fgTitle)
if lde.verbose then
if ok then
ansi.printf("{cyan}Flamegraph written to %s", opts.flamegraph)
else
ansi.printf("{red}Flamegraph error: %s", err or "unknown error")
end
end
end
end

if not runOk then
error(runErr, 0)
end
if opts.args then
arg = opts.args
arg[0] = scriptName
end

if opts.postexec then
return opts.postexec()
end
local ok, a, b, c, d, e, f = pcall(chunk, unpack(opts.args or {}))

return a, b, c, d, e, f
end)
finishProfiler()

if ok and opts.postexec then
ok, a, b, c, d, e, f = pcall(opts.postexec)
end

if stopProfiler then
stopProfiler()
end

for k, v in pairs(oldEnvVars) do env.set(k, v) end
if oldCwd then env.chdir(oldCwd) end
Expand All @@ -365,7 +368,7 @@ local function executeWith(compile, opts, scriptName)
package.loaders = oldLoaders
package.path, package.cpath = oldPath, oldCPath

return ok, result
return ok, a, b, c, d, e, f
end

---@param scriptPath string
Expand Down
23 changes: 0 additions & 23 deletions packages/lde/src/commands/eval.lua

This file was deleted.

24 changes: 13 additions & 11 deletions packages/lde/src/commands/run.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ local function run(args)
scriptArgs = args:drain()
end

local function execute()
if not watch then
if not pkg then
if name and fs.exists(name) then
local ok, err = lde.runtime.executeFile(name, {
Expand All @@ -39,44 +39,45 @@ local function run(args)
profile = profile,
flamegraph = flamegraph
})

if not ok then
error("Failed to run script: " .. (err or "Script exited with a non-zero exit code"))
end

return
end

ansi.printf("{red}%s\n", pkgErr)
ansi.printf("{red}%s", pkgErr)
return
end

pkg:build()
pkg:installDependencies()

local ok, err
local scripts = pkg:readConfig().scripts
local ok, err
if name and scripts and scripts[name] then
ok, err = pkg:runScript(name)
else
ok, err = pkg:runFile(name, scriptArgs, nil, nil, profile, flamegraph)
end

if not ok then
error("Failed to run script: " .. err)
ansi.printf("{red}Error: %s", err or "Script exited with a non-zero exit code")
os.exit(1)
end
end

if not watch then
execute()
return
end

local watchDir = pkg and pkg:getSrcDir() or env.cwd()
local watcher = fs.watch(watchDir, function() end, { recursive = true })
if not watcher then
error("Failed to watch: " .. watchDir)
ansi.printf("{red}Failed to watch: %s", watchDir)
return
end

ansi.printf("{cyan}Watching %s for changes...\n", watchDir)
ansi.printf("{cyan}Watching %s for changes...", watchDir)

local spawnArgs = { "run" }
if profile then spawnArgs[#spawnArgs + 1] = "--profile" end
Expand All @@ -90,16 +91,17 @@ local function run(args)
local function spawnChild()
local child, err = process.spawn(env.execPath(), spawnArgs, { stdout = "inherit", stderr = "inherit" })
if not child then
ansi.printf("{red}Error: %s\n", tostring(err))
ansi.printf("{red}Error: %s", tostring(err))
end

return child
end

local child = spawnChild()

while true do
watcher.wait()
ansi.printf("{cyan}Change detected, restarting...\n")
ansi.printf("{cyan}Change detected, restarting...")
if child then child:kill() end
child = spawnChild()
end
Expand Down
5 changes: 4 additions & 1 deletion packages/lde/src/commands/x.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ local function x(args)
end

local pkg, err = resolvePackage(args)
if not pkg then error(err) end
if not pkg then
ansi.printf("{red}Error: %s", err)
os.exit(1)
end

args:flag("") -- consume -- separator if present
executePackage(pkg, args:drain() or {}, userCwd)
Expand Down
Loading