From 464ef93b8f00c01ca1a370ace0148841e36fa24e Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 25 May 2015 21:24:29 -0700 Subject: [PATCH] have some broken code --- Lua/depend.lua | 80 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/Lua/depend.lua b/Lua/depend.lua index 84022fa..efca704 100644 --- a/Lua/depend.lua +++ b/Lua/depend.lua @@ -1,9 +1,85 @@ _require = _require or require -return function(path) + +local function dumbdepend(path) if package and package.loaded and package.loaded[path] then - -- TODO: check if file is more recent using luanet hacks package.loaded[path] = nil end -- TODO: pcall? return _require(path) 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