mirror of
https://github.com/kepler155c/opus
synced 2024-12-26 16:40:27 +00:00
require overhaul part 2
This commit is contained in:
parent
26564cbcc1
commit
1264b0149b
@ -4,9 +4,10 @@ local DEFAULT_PATH = '/sys/apis/?;/sys/apis/?.lua'
|
||||
local DEFAULT_BRANCH = _ENV.OPUS_BRANCH or _G.OPUS_BRANCH or 'master'
|
||||
local DEFAULT_UPATH = GIT_URL .. '/kepler155c/opus/' .. DEFAULT_BRANCH .. '/sys/apis'
|
||||
|
||||
local fs = _G.fs
|
||||
local http = _G.http
|
||||
local os = _G.os
|
||||
local fs = _G.fs
|
||||
local http = _G.http
|
||||
local os = _G.os
|
||||
local string = _G.string
|
||||
|
||||
if not http._patched then
|
||||
-- fix broken http get
|
||||
@ -58,7 +59,7 @@ local function loadUrl(url)
|
||||
end
|
||||
|
||||
-- Add require and package to the environment
|
||||
local function requireWrapper(env)
|
||||
return function(env)
|
||||
|
||||
local function standardSearcher(modname)
|
||||
if env.package.loaded[modname] then
|
||||
@ -71,10 +72,13 @@ local function requireWrapper(env)
|
||||
local function shellSearcher(modname)
|
||||
local fname = modname:gsub('%.', '/') .. '.lua'
|
||||
|
||||
if env.shell and type(env.shell.dir) == 'function' then
|
||||
local path = env.shell.resolve(fname)
|
||||
if fs.exists(path) and not fs.isDir(path) then
|
||||
return loadfile(path, env)
|
||||
if env.shell and type(env.shell.getRunningProgram) == 'function' then
|
||||
local running = env.shell.getRunningProgram()
|
||||
if running then
|
||||
local path = fs.combine(fs.getDir(running), fname)
|
||||
if fs.exists(path) and not fs.isDir(path) then
|
||||
return loadfile(path, env)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -145,11 +149,12 @@ local function requireWrapper(env)
|
||||
upath = env.LUA_UPATH or _G.LUA_UPATH or DEFAULT_UPATH,
|
||||
config = '/\n:\n?\n!\n-',
|
||||
loaded = {
|
||||
coroutine = coroutine,
|
||||
io = io,
|
||||
math = math,
|
||||
os = os,
|
||||
string = string,
|
||||
table = table,
|
||||
io = io,
|
||||
os = os,
|
||||
},
|
||||
loaders = {
|
||||
standardSearcher,
|
||||
@ -181,9 +186,3 @@ local function requireWrapper(env)
|
||||
|
||||
return env.require -- backwards compatible
|
||||
end
|
||||
|
||||
return function(env)
|
||||
env = env or getfenv(2)
|
||||
--setfenv(requireWrapper, env)
|
||||
return requireWrapper(env)
|
||||
end
|
||||
|
@ -1,4 +1,3 @@
|
||||
local Config = require('config')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
@ -7,13 +6,6 @@ local shell = _ENV.shell
|
||||
|
||||
UI:configure('System', ...)
|
||||
|
||||
local env = {
|
||||
path = shell.path(),
|
||||
aliases = shell.aliases(),
|
||||
lua_path = _ENV.LUA_PATH,
|
||||
}
|
||||
Config.load('shell', env)
|
||||
|
||||
local systemPage = UI.Page {
|
||||
tabs = UI.Tabs {
|
||||
settings = UI.Window {
|
||||
|
101
sys/apps/shell
101
sys/apps/shell
@ -77,97 +77,6 @@ local function run(env, ...)
|
||||
return table.unpack(r)
|
||||
end
|
||||
|
||||
local function createShellEnv(sDir)
|
||||
local tEnv = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
|
||||
|
||||
local package
|
||||
|
||||
package = {
|
||||
loaded = {
|
||||
_G = _G,
|
||||
bit32 = bit32,
|
||||
coroutine = coroutine,
|
||||
math = math,
|
||||
package = package,
|
||||
string = string,
|
||||
table = table,
|
||||
},
|
||||
path = _G.LUA_PATH,
|
||||
config = "/\n;\n?\n!\n-",
|
||||
preload = { },
|
||||
loaders = {
|
||||
function( name )
|
||||
if package.preload[name] then
|
||||
return package.preload[name]
|
||||
else
|
||||
return nil, "no field package.preload['" .. name .. "']"
|
||||
end
|
||||
end,
|
||||
function( name )
|
||||
local fname = string.gsub(name, "%.", "/")
|
||||
local sError = ""
|
||||
for pattern in string.gmatch(package.path, "[^;]+") do
|
||||
local sPath = string.gsub(pattern, "%?", fname)
|
||||
if sPath:sub(1,1) ~= "/" then
|
||||
sPath = fs.combine(sDir, sPath)
|
||||
end
|
||||
if fs.exists(sPath) and not fs.isDir(sPath) then
|
||||
local fnFile, sError = loadfile( sPath, tEnv )
|
||||
if fnFile then
|
||||
return fnFile, sPath
|
||||
else
|
||||
return nil, sError
|
||||
end
|
||||
else
|
||||
if #sError > 0 then
|
||||
sError = sError .. "\n"
|
||||
end
|
||||
sError = sError .. "no file '" .. sPath .. "'"
|
||||
end
|
||||
end
|
||||
return nil, sError
|
||||
end
|
||||
}
|
||||
}
|
||||
|
||||
local sentinel = {}
|
||||
local function require( name )
|
||||
if type( name ) ~= "string" then
|
||||
error( "bad argument #1 (expected string, got " .. type( name ) .. ")", 2 )
|
||||
end
|
||||
if package.loaded[name] == sentinel then
|
||||
error("Loop detected requiring '" .. name .. "'", 0)
|
||||
end
|
||||
if package.loaded[name] then
|
||||
return package.loaded[name]
|
||||
end
|
||||
|
||||
local sError = "Error loading module '" .. name .. "':"
|
||||
for _,searcher in ipairs(package.loaders) do
|
||||
local loader, err = searcher(name)
|
||||
if loader then
|
||||
package.loaded[name] = sentinel
|
||||
local result = loader( err )
|
||||
if result ~= nil then
|
||||
package.loaded[name] = result
|
||||
return result
|
||||
else
|
||||
package.loaded[name] = true
|
||||
return true
|
||||
end
|
||||
else
|
||||
sError = sError .. "\n" .. err
|
||||
end
|
||||
end
|
||||
error(sError, 2)
|
||||
end
|
||||
|
||||
tEnv["package"] = package
|
||||
tEnv["require"] = require
|
||||
|
||||
return tEnv
|
||||
end
|
||||
|
||||
-- Install shell API
|
||||
function shell.run(...)
|
||||
local oldTitle
|
||||
@ -176,8 +85,9 @@ function shell.run(...)
|
||||
oldTitle = _ENV.multishell.getTitle(_ENV.multishell.getCurrent())
|
||||
end
|
||||
|
||||
local env = createShellEnv(shell.dir())
|
||||
-- local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
|
||||
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
|
||||
_G.requireInjector(env)
|
||||
|
||||
local r = { pcall(run, env, ...) }
|
||||
|
||||
if _ENV.multishell then
|
||||
@ -441,8 +351,9 @@ end
|
||||
|
||||
local tArgs = { ... }
|
||||
if #tArgs > 0 then
|
||||
local env = createShellEnv(shell.dir())
|
||||
-- local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
|
||||
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
|
||||
_G.requireInjector(env)
|
||||
|
||||
return run(env, ...)
|
||||
end
|
||||
|
||||
|
@ -9,26 +9,24 @@ local shell = _ENV.shell
|
||||
|
||||
local appPaths = Util.split(shell.path(), '(.-);')
|
||||
local luaPaths = Util.split(_G.LUA_PATH, '(.-);')
|
||||
local helpPaths = Util.split(help.path(), '(.-):')
|
||||
|
||||
local function addPath(t, path)
|
||||
local function addEntry(e)
|
||||
for _,v in ipairs(t) do
|
||||
if v == e then
|
||||
return true
|
||||
end
|
||||
table.insert(helpPaths, '/sys/help')
|
||||
|
||||
local function addEntry(t, e)
|
||||
for _,v in ipairs(t) do
|
||||
if v == e then
|
||||
return true
|
||||
end
|
||||
table.insert(t, 1, e)
|
||||
end
|
||||
addEntry(string.format('/%s/?', path))
|
||||
addEntry(string.format('/%s/?.lua', path))
|
||||
addEntry(string.format('/%s/?/init.lua', path))
|
||||
table.insert(t, 1, e)
|
||||
end
|
||||
|
||||
-- dependency graph
|
||||
-- https://github.com/mpeterv/depgraph/blob/master/src/depgraph/init.lua
|
||||
|
||||
local helpPaths = Util.split(help.path(), '(.-):')
|
||||
table.insert(helpPaths, '/sys/help')
|
||||
local function addRequirePath(t, path)
|
||||
addEntry(t, string.format('/%s/?', path))
|
||||
addEntry(t, string.format('/%s/?.lua', path))
|
||||
addEntry(t, string.format('/%s/?/init.lua', path))
|
||||
end
|
||||
|
||||
for name in pairs(Packages:installed()) do
|
||||
local packageDir = fs.combine('packages', name)
|
||||
@ -39,13 +37,13 @@ for name in pairs(Packages:installed()) do
|
||||
end
|
||||
end
|
||||
|
||||
addPath(appPaths, packageDir)
|
||||
addEntry(appPaths, packageDir)
|
||||
local apiPath = fs.combine(fs.combine('packages', name), 'apis')
|
||||
if fs.exists(apiPath) then
|
||||
addPath(luaPaths, apiPath)
|
||||
addRequirePath(luaPaths, apiPath)
|
||||
end
|
||||
|
||||
local helpPath = fs.combine(fs.combine('packages', name), 'help')
|
||||
local helpPath = '/' .. fs.combine(fs.combine('packages', name), 'help')
|
||||
if fs.exists(helpPath) then
|
||||
table.insert(helpPaths, helpPath)
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user