update shell with require

This commit is contained in:
kepler155c@gmail.com 2018-12-16 22:17:19 -05:00
parent 0aa0841b76
commit 7d64b0c6db
5 changed files with 143 additions and 13 deletions

View File

@ -1,6 +1,6 @@
local PASTEBIN_URL = 'http://pastebin.com/raw'
local GIT_URL = 'https://raw.githubusercontent.com'
local DEFAULT_PATH = 'sys/apis'
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'
@ -80,14 +80,25 @@ local function requireWrapper(env)
end
local function pathSearcher(modname)
local fname = modname:gsub('%.', '/') .. '.lua'
local fname = modname:gsub('%.', '/')
for pattern in string.gmatch(env.package.path, "[^;]+") do
local sPath = string.gsub(pattern, "%?", fname)
if env.shell and env.shell.dir and sPath:sub(1, 1) ~= "/" then
sPath = fs.combine(env.shell.dir(), sPath)
end
if fs.exists(sPath) and not fs.isDir(sPath) then
return loadfile(sPath, env)
end
end
--[[
for dir in string.gmatch(env.package.path, "[^:]+") do
local path = fs.combine(dir, fname)
if fs.exists(path) and not fs.isDir(path) then
return loadfile(path, env)
end
end
]]
end
-- require('BniCQPVf')

View File

@ -77,6 +77,107 @@ local function run(env, ...)
return table.unpack(r)
end
local function createShellEnv(sDir)
local tEnv = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
--[[
package.path = "?;?.lua;?/init.lua;/rom/modules/main/?;/rom/modules/main/?.lua;/rom/modules/main/?/init.lua"
if turtle then
package.path = package.path..";/rom/modules/turtle/?;/rom/modules/turtle/?.lua;/rom/modules/turtle/?/init.lua"
elseif command then
package.path = package.path..";/rom/modules/command/?;/rom/modules/command/?.lua;/rom/modules/command/?/init.lua"
end
]]
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 )
_G._p = _ENV
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
@ -85,7 +186,8 @@ function shell.run(...)
oldTitle = _ENV.multishell.getTitle(_ENV.multishell.getCurrent())
end
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
local env = createShellEnv(shell.dir())
-- local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
local r = { pcall(run, env, ...) }
if _ENV.multishell then
@ -349,7 +451,8 @@ end
local tArgs = { ... }
if #tArgs > 0 then
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
local env = createShellEnv(shell.dir())
-- local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
return run(env, ...)
end

View File

@ -16,11 +16,21 @@ end
-- 'usr gitfs kepler155c/opus-apps/' .. _G.OPUS_BRANCH)
--end
local lua_path = '?;?.lua;?/init.lua'
lua_path = lua_path .. ';/usr/apis/?;/usr/apis/?.lua'
lua_path = lua_path .. ';/sys/apis/?;/sys/apis/?.lua'
lua_path = lua_path .. ';/rom/modules/main/?;/rom/modules/main/?.lua;/rom/modules/main/?/init.lua;'
if _G.turtle then
lua_path = lua_path..';/rom/modules/turtle/?;/rom/modules/turtle/?.lua;/rom/modules/turtle/?/init.lua'
elseif _G.command then
lua_path = lua_path..';/rom/modules/command/?;/rom/modules/command/?.lua;/rom/modules/command/?/init.lua'
end
if not fs.exists('usr/config/shell') then
Util.writeTable('usr/config/shell', {
aliases = shell.aliases(),
path = 'usr/apps:sys/apps:' .. shell.path(),
lua_path = 'sys/apis:usr/apis',
lua_path = lua_path,
})
end
@ -45,6 +55,7 @@ if config.aliases then
end
end
shell.setPath(config.path)
_G.LUA_PATH = config.lua_path
--_G.LUA_PATH = config.lua_path
_G.LUA_PATH = lua_path
fs.loadTab('usr/config/fstab')

View File

@ -6,20 +6,21 @@ local Util = require('util')
local shell = _ENV.shell
local fs = _G.fs
local appPaths = Util.split(shell.path(), '(.-):')
local luaPaths = Util.split(_G.LUA_PATH, '(.-):')
local appPaths = Util.split(shell.path(), '(.-);')
local luaPaths = Util.split(_G.LUA_PATH, '(.-);')
local function addPath(t, e)
local function hasEntry()
local function addPath(t, path)
local function addEntry(e)
for _,v in ipairs(t) do
if v == e then
return true
end
end
end
if not hasEntry() then
table.insert(t, 1, e)
end
addEntry(string.format('/%s/?', path))
addEntry(string.format('/%s/?.lua', path))
addEntry(string.format('/%s/?/init.lua', path))
end
-- dependency graph
@ -42,4 +43,4 @@ for name in pairs(Packages:installed()) do
end
shell.setPath(table.concat(appPaths, ':'))
_G.LUA_PATH = table.concat(luaPaths, ':')
_G.LUA_PATH = table.concat(luaPaths, ';')

View File

@ -115,6 +115,10 @@ function kernel.getCurrent()
return kernel.running
end
function kernel.getShell()
return shell
end
function kernel.newRoutine(args)
kernel.UID = kernel.UID + 1