mirror of
https://github.com/kepler155c/opus
synced 2025-05-03 16:04:13 +00:00
update shell with require
This commit is contained in:
parent
0aa0841b76
commit
7d64b0c6db
@ -1,6 +1,6 @@
|
|||||||
local PASTEBIN_URL = 'http://pastebin.com/raw'
|
local PASTEBIN_URL = 'http://pastebin.com/raw'
|
||||||
local GIT_URL = 'https://raw.githubusercontent.com'
|
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_BRANCH = _ENV.OPUS_BRANCH or _G.OPUS_BRANCH or 'master'
|
||||||
local DEFAULT_UPATH = GIT_URL .. '/kepler155c/opus/' .. DEFAULT_BRANCH .. '/sys/apis'
|
local DEFAULT_UPATH = GIT_URL .. '/kepler155c/opus/' .. DEFAULT_BRANCH .. '/sys/apis'
|
||||||
|
|
||||||
@ -80,14 +80,25 @@ local function requireWrapper(env)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function pathSearcher(modname)
|
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
|
for dir in string.gmatch(env.package.path, "[^:]+") do
|
||||||
local path = fs.combine(dir, fname)
|
local path = fs.combine(dir, fname)
|
||||||
if fs.exists(path) and not fs.isDir(path) then
|
if fs.exists(path) and not fs.isDir(path) then
|
||||||
return loadfile(path, env)
|
return loadfile(path, env)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
]]
|
||||||
end
|
end
|
||||||
|
|
||||||
-- require('BniCQPVf')
|
-- require('BniCQPVf')
|
||||||
|
107
sys/apps/shell
107
sys/apps/shell
@ -77,6 +77,107 @@ local function run(env, ...)
|
|||||||
return table.unpack(r)
|
return table.unpack(r)
|
||||||
end
|
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
|
-- Install shell API
|
||||||
function shell.run(...)
|
function shell.run(...)
|
||||||
local oldTitle
|
local oldTitle
|
||||||
@ -85,7 +186,8 @@ function shell.run(...)
|
|||||||
oldTitle = _ENV.multishell.getTitle(_ENV.multishell.getCurrent())
|
oldTitle = _ENV.multishell.getTitle(_ENV.multishell.getCurrent())
|
||||||
end
|
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, ...) }
|
local r = { pcall(run, env, ...) }
|
||||||
|
|
||||||
if _ENV.multishell then
|
if _ENV.multishell then
|
||||||
@ -349,7 +451,8 @@ end
|
|||||||
|
|
||||||
local tArgs = { ... }
|
local tArgs = { ... }
|
||||||
if #tArgs > 0 then
|
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, ...)
|
return run(env, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -16,11 +16,21 @@ end
|
|||||||
-- 'usr gitfs kepler155c/opus-apps/' .. _G.OPUS_BRANCH)
|
-- 'usr gitfs kepler155c/opus-apps/' .. _G.OPUS_BRANCH)
|
||||||
--end
|
--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
|
if not fs.exists('usr/config/shell') then
|
||||||
Util.writeTable('usr/config/shell', {
|
Util.writeTable('usr/config/shell', {
|
||||||
aliases = shell.aliases(),
|
aliases = shell.aliases(),
|
||||||
path = 'usr/apps:sys/apps:' .. shell.path(),
|
path = 'usr/apps:sys/apps:' .. shell.path(),
|
||||||
lua_path = 'sys/apis:usr/apis',
|
lua_path = lua_path,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -45,6 +55,7 @@ if config.aliases then
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
shell.setPath(config.path)
|
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')
|
fs.loadTab('usr/config/fstab')
|
||||||
|
@ -6,20 +6,21 @@ local Util = require('util')
|
|||||||
local shell = _ENV.shell
|
local shell = _ENV.shell
|
||||||
local fs = _G.fs
|
local fs = _G.fs
|
||||||
|
|
||||||
local appPaths = Util.split(shell.path(), '(.-):')
|
local appPaths = Util.split(shell.path(), '(.-);')
|
||||||
local luaPaths = Util.split(_G.LUA_PATH, '(.-):')
|
local luaPaths = Util.split(_G.LUA_PATH, '(.-);')
|
||||||
|
|
||||||
local function addPath(t, e)
|
local function addPath(t, path)
|
||||||
local function hasEntry()
|
local function addEntry(e)
|
||||||
for _,v in ipairs(t) do
|
for _,v in ipairs(t) do
|
||||||
if v == e then
|
if v == e then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
if not hasEntry() then
|
|
||||||
table.insert(t, 1, e)
|
table.insert(t, 1, e)
|
||||||
end
|
end
|
||||||
|
addEntry(string.format('/%s/?', path))
|
||||||
|
addEntry(string.format('/%s/?.lua', path))
|
||||||
|
addEntry(string.format('/%s/?/init.lua', path))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- dependency graph
|
-- dependency graph
|
||||||
@ -42,4 +43,4 @@ for name in pairs(Packages:installed()) do
|
|||||||
end
|
end
|
||||||
|
|
||||||
shell.setPath(table.concat(appPaths, ':'))
|
shell.setPath(table.concat(appPaths, ':'))
|
||||||
_G.LUA_PATH = table.concat(luaPaths, ':')
|
_G.LUA_PATH = table.concat(luaPaths, ';')
|
||||||
|
@ -115,6 +115,10 @@ function kernel.getCurrent()
|
|||||||
return kernel.running
|
return kernel.running
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function kernel.getShell()
|
||||||
|
return shell
|
||||||
|
end
|
||||||
|
|
||||||
function kernel.newRoutine(args)
|
function kernel.newRoutine(args)
|
||||||
kernel.UID = kernel.UID + 1
|
kernel.UID = kernel.UID + 1
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user