mirror of
https://github.com/notwa/mm
synced 2025-02-05 13:23:23 -08:00
have some broken code
This commit is contained in:
parent
e6240fb72c
commit
464ef93b8f
1 changed files with 78 additions and 2 deletions
|
@ -1,9 +1,85 @@
|
||||||
_require = _require or require
|
_require = _require or require
|
||||||
return function(path)
|
|
||||||
|
local function dumbdepend(path)
|
||||||
if package and package.loaded and package.loaded[path] then
|
if package and package.loaded and package.loaded[path] then
|
||||||
-- TODO: check if file is more recent using luanet hacks
|
|
||||||
package.loaded[path] = nil
|
package.loaded[path] = nil
|
||||||
end
|
end
|
||||||
-- TODO: pcall?
|
-- TODO: pcall?
|
||||||
return _require(path)
|
return _require(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if luanet == nil then _require "luanet" end
|
||||||
|
if luanet == nil then
|
||||||
|
print('depend: luanet is missing. no smart depend for you!')
|
||||||
|
return dumbdepend
|
||||||
|
end
|
||||||
|
|
||||||
|
if true then
|
||||||
|
-- gfgsdgfdfgsfgdfgdsgggsdgsddfsgdfgs luanet corrupts memory or something
|
||||||
|
return dumbdepend
|
||||||
|
end
|
||||||
|
|
||||||
|
local NET = luanet.import_type
|
||||||
|
local File = NET('System.IO.File')
|
||||||
|
local Directory = NET('System.IO.Directory')
|
||||||
|
|
||||||
|
local here = Directory.GetCurrentDirectory()
|
||||||
|
local cd = Directory.SetCurrentDirectory
|
||||||
|
|
||||||
|
local function getmodtime(path)
|
||||||
|
local dt = File.GetLastWriteTime(path)
|
||||||
|
if dt == nil or dt == 0 then return end
|
||||||
|
return dt:ToFileTimeUtc()
|
||||||
|
end
|
||||||
|
|
||||||
|
local function isdir(path)
|
||||||
|
local ok, attr = pcall(function() return File.GetAttributes(path) end)
|
||||||
|
if not ok then return false end
|
||||||
|
attr = tostring(attr)
|
||||||
|
return not not attr:find("Directory")
|
||||||
|
end
|
||||||
|
|
||||||
|
package.depended = package.depended or {}
|
||||||
|
package.times = package.times or {}
|
||||||
|
local packages = package.depended
|
||||||
|
local times = package.times
|
||||||
|
|
||||||
|
local function depend(require_path)
|
||||||
|
-- require, but with reloading based on last modified date
|
||||||
|
-- also always relative to THIS file's current directory
|
||||||
|
-- WARNING: not fully compatible with require path syntax
|
||||||
|
|
||||||
|
local path = require_path:gsub('%.', '/')
|
||||||
|
--print()
|
||||||
|
--print('DEPEND', path)
|
||||||
|
|
||||||
|
if not pcall(function() cd(here) end) then
|
||||||
|
print('depend: failed to set current directory:', here)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local t
|
||||||
|
local function try(path)
|
||||||
|
if isdir(path) then return end
|
||||||
|
t = getmodtime(path)
|
||||||
|
if t == nil or t == 0 then return end
|
||||||
|
return path
|
||||||
|
end
|
||||||
|
|
||||||
|
local id = path:gsub('[.][^.]+$', '') -- remove extension
|
||||||
|
local ext_path = try(id) or try(id..'.lua') or try(id..'.luac')
|
||||||
|
local needs_update = t == nil or times[id] == nil or t > times[id]
|
||||||
|
|
||||||
|
--if not ext_path then print('no path :c') end
|
||||||
|
--if not needs_update then print('no update required') end
|
||||||
|
|
||||||
|
if ext_path and needs_update then
|
||||||
|
--print('requiring', ext_path)
|
||||||
|
times[id] = t
|
||||||
|
packages[require_path] = dumbdepend(path)
|
||||||
|
end
|
||||||
|
|
||||||
|
return packages[require_path]
|
||||||
|
end
|
||||||
|
|
||||||
|
return depend
|
||||||
|
|
Loading…
Add table
Reference in a new issue