mirror of
https://github.com/Ed94/pikuma_ps1.git
synced 2026-08-06 15:48:49 +00:00
eliminated most lag (runs in ms)
This commit is contained in:
+47
-13
@@ -19,24 +19,58 @@
|
|||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
-- Cache key for the repo root. Stored in `package.loaded` (process-global) so all 8 entry scripts + passes scripts share one git call.
|
-- Cache key for the repo root. Stored in `package.loaded` (process-global) so all 8 entry scripts + passes scripts share one resolution.
|
||||||
-- Without this cache, `git rev-parse --show-toplevel` runs once per script load.
|
|
||||||
local CACHE_KEY = "__duffle_repo_root__"
|
local CACHE_KEY = "__duffle_repo_root__"
|
||||||
|
|
||||||
--- Resolve the repo root via git (cached after first call).
|
--- Resolve the repo root from this script's own path. Zero shell spawn.
|
||||||
--- Returns a normalized path with a trailing forward-slash, or nil if not in a git repo.
|
--- `duffle_paths.lua` always lives at `<repo>/scripts/duffle_paths.lua`, so the repo root is the
|
||||||
|
--- parent of the directory containing this script. We derive it directly from `debug.getinfo(1, "S").source`
|
||||||
|
--- (returns `@<path>` for the currently-running chunk).
|
||||||
|
---
|
||||||
|
--- Replaces the prior `io.popen("git rev-parse --show-toplevel")` approach, which cost ~100-180ms per
|
||||||
|
--- LuaJIT process on Windows due to git's CLI startup. The path-derive approach costs <1ms.
|
||||||
|
---
|
||||||
|
--- If this script's path can't be parsed (shouldn't happen — dofile/debug.getinfo always populates source),
|
||||||
|
--- fall back to a defensive walk: starting from this script's directory, walk UP until we find a parent that
|
||||||
|
--- contains a `scripts/` directory. The first match is the repo root.
|
||||||
--- @return string|nil
|
--- @return string|nil
|
||||||
local function find_repo_root()
|
local function find_repo_root()
|
||||||
if package.loaded[CACHE_KEY] then return package.loaded[CACHE_KEY] end
|
if package.loaded[CACHE_KEY] then return package.loaded[CACHE_KEY] end
|
||||||
local p = io.popen("git rev-parse --show-toplevel 2>nul")
|
|
||||||
local root
|
local source = debug.getinfo(1, "S").source
|
||||||
if p then root = p:read("*l"); p:close() end
|
-- Strip the leading `@` (Lua's dofile marker) and the trailing `/duffle_paths.lua` filename.
|
||||||
if not root or root == "" then return nil end
|
-- What remains is the directory containing this script, i.e. `<repo>/scripts/` (with trailing slash or not).
|
||||||
-- Normalize to forward slashes (Windows accepts both, but mixed `\` + `/` confuses LuaJIT's file APIs).
|
local scripts_dir = source and source:match("^@?(.*)[/\\]duffle_paths%.lua$")
|
||||||
root = root:gsub("\\", "/")
|
if scripts_dir then
|
||||||
if not root:match("/$") then root = root .. "/" end
|
-- The repo root is the parent of `scripts/`. Strip the trailing `scripts/` (with or without trailing slash).
|
||||||
package.loaded[CACHE_KEY] = root
|
local root = scripts_dir:gsub("scripts[\\/]?$", "")
|
||||||
return root
|
root = root:gsub("\\", "/")
|
||||||
|
if root == "" then root = "./" end
|
||||||
|
if not root:match("/$") then root = root .. "/" end
|
||||||
|
package.loaded[CACHE_KEY] = root
|
||||||
|
return root
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Defensive fallback: walk UP from this script's directory until we find a parent that contains `scripts/`.
|
||||||
|
-- In practice this branch never fires — debug.getinfo always returns a source for dofile()'d chunks.
|
||||||
|
local lfs = pcall(require, "lfs") and require("lfs") or nil
|
||||||
|
if lfs then
|
||||||
|
local dir = source and source:match("^@?(.*[/\\])") or "./"
|
||||||
|
dir = dir:gsub("\\", "/")
|
||||||
|
while dir and dir ~= "" do
|
||||||
|
local candidate_scripts = dir .. "scripts"
|
||||||
|
if lfs.attributes(candidate_scripts, "mode") == "directory" then
|
||||||
|
dir = dir:gsub("/$", "")
|
||||||
|
package.loaded[CACHE_KEY] = dir .. "/"
|
||||||
|
return dir .. "/"
|
||||||
|
end
|
||||||
|
local parent = dir:match("^(.*)/[^/]+/$")
|
||||||
|
if not parent then break end
|
||||||
|
dir = parent .. "/"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Set `package.path` (for `require("duffle")` + `require("passes.X")`) and
|
--- Set `package.path` (for `require("duffle")` + `require("passes.X")`) and
|
||||||
|
|||||||
Reference in New Issue
Block a user