mirror of https://github.com/Kiritow/cnpm
format code
parent
1a20b30fa7
commit
f812ef2048
227
cnpm.lua
227
cnpm.lua
|
@ -3,7 +3,7 @@ print("Author: Github/Kiritow")
|
|||
|
||||
local component=require("component")
|
||||
local shell=require("shell")
|
||||
local serialize
|
||||
local serialization=require("serialization")
|
||||
|
||||
local function cmd(cmdstr,infostr)
|
||||
local old=component.gpu.setForeground(0xFFFF00)
|
||||
|
@ -21,7 +21,7 @@ end
|
|||
local args,ops=shell.parse(...)
|
||||
local argc=#args
|
||||
if(argc<1) then
|
||||
print("Usage:")
|
||||
print("Usage:")
|
||||
cmd("cnpm install <package>","Install package")
|
||||
cmd("cnpm add [-gbmkno,--url=<url>] <repo>",
|
||||
"Add an external repository to cnpm\n" ..
|
||||
|
@ -42,7 +42,9 @@ end
|
|||
|
||||
-- Hardware check
|
||||
local network=component.internet
|
||||
if(network==nil) then err("No network device found.") end
|
||||
if(network==nil) then
|
||||
err("No network device found.")
|
||||
end
|
||||
|
||||
local function getdb()
|
||||
local f=io.open("/etc/cnpm/packages.cache","r")
|
||||
|
@ -59,111 +61,114 @@ local function savedb(db)
|
|||
end
|
||||
|
||||
if(args[1]=="add") then
|
||||
local restb=dofile("urlresolver.lua")
|
||||
|
||||
local git=false
|
||||
local oppm=false
|
||||
local resolver
|
||||
local url=nil
|
||||
|
||||
if(ops.github or ops.g) then
|
||||
git=true
|
||||
resolver=restb.github
|
||||
elseif(ops.bitbucket or ops.b) then
|
||||
git=true
|
||||
resolver=restb.bitbucket
|
||||
elseif(ops.gitee or ops.m) then
|
||||
git=true
|
||||
resolver=restb.gitee
|
||||
elseif(ops.cngit or ops.k) then
|
||||
git=true
|
||||
resolver=restb.cngit
|
||||
elseif(ops.codingnet or ops.n) then
|
||||
git=true
|
||||
resolver=restb.codingnet
|
||||
elseif(ops.oppm or ops.o) then -- Follow OPPM
|
||||
git=true
|
||||
oppm=true
|
||||
elseif(ops.url) then
|
||||
url=ops.url
|
||||
else
|
||||
err("Cannot resolve repository url.")
|
||||
return
|
||||
end
|
||||
|
||||
if(oppm) then
|
||||
-- TODO: Fill the gap between oppm and cnpm
|
||||
end
|
||||
|
||||
local realcfgurl=""
|
||||
|
||||
if(git) then
|
||||
realcfgurl=resolver(args[2],"mainfest.txt")
|
||||
else
|
||||
if(string.sub(url,-1)~="/") then url=url .. "/" end
|
||||
realcfgurl=url .. "mainfest.txt"
|
||||
end
|
||||
|
||||
local hand=network.request(realcfgurl)
|
||||
local res=""
|
||||
while true do
|
||||
local tmp=hand.read()
|
||||
if(tmp~=nil) then
|
||||
res=res .. tmp
|
||||
else break end
|
||||
end
|
||||
hand.close()
|
||||
|
||||
local fn=load(res,"Mainfest","t",{})
|
||||
local err,mft=pcall(fn)
|
||||
if(not err) then
|
||||
err("Failed to load mainfest.")
|
||||
return
|
||||
end
|
||||
|
||||
local function checkval(param,tp)
|
||||
if(tp==nil) then tp="string" end
|
||||
if(type(tp)~="table") then
|
||||
tp={tp}
|
||||
end
|
||||
for k,v in pairs(tp) do
|
||||
if(type(param)==v) then return param end
|
||||
end
|
||||
error("invalid param type")
|
||||
end
|
||||
|
||||
local pkg={}
|
||||
if(not pcall(function()
|
||||
-- Copy info
|
||||
pkg.name=checkval(mft.name)
|
||||
pkg.info=checkval(mft.info,{"string","nil"})
|
||||
pkg.author=checkval(mft.author,{"string","nil"})
|
||||
pkg.version=checkval(mft.version)
|
||||
pkg.files=checkval(mft.files,"table")
|
||||
pkg.depends=checkval(mft.depends,{"table","string","nil"})
|
||||
pkg.precheck=checkval(mft.precheck,{"string","nil"})
|
||||
pkg.setup=checkval(mft.setup,{"string","nil"})
|
||||
pkg.uninst=checkval(mft.uninst,{"string","nil"})
|
||||
|
||||
-- Package setup check
|
||||
for k,v in pairs(pkg.files) do
|
||||
if(type(k)~="string" or type(v)~="string") then
|
||||
error("file list type exception.")
|
||||
end
|
||||
end
|
||||
|
||||
if(
|
||||
(pkg.precheck and pkg.files[pkg.precheck]==nil) or
|
||||
(pkg.setup and pkg.files[pkg.setup]==nil) or
|
||||
(pkg.uninst and pkg.files[pkg.uninst]==nil)
|
||||
) then
|
||||
error("special files not found in files list.")
|
||||
end
|
||||
|
||||
)) then
|
||||
err("Invalid package mainfest.")
|
||||
end
|
||||
|
||||
|
||||
|
||||
end
|
||||
local restb=dofile("urlresolver.lua")
|
||||
|
||||
local git=false
|
||||
local oppm=false
|
||||
local resolver
|
||||
local url=nil
|
||||
|
||||
if(ops.github or ops.g) then
|
||||
git=true
|
||||
resolver=restb.github
|
||||
elseif(ops.bitbucket or ops.b) then
|
||||
git=true
|
||||
resolver=restb.bitbucket
|
||||
elseif(ops.gitee or ops.m) then
|
||||
git=true
|
||||
resolver=restb.gitee
|
||||
elseif(ops.cngit or ops.k) then
|
||||
git=true
|
||||
resolver=restb.cngit
|
||||
elseif(ops.codingnet or ops.n) then
|
||||
git=true
|
||||
resolver=restb.codingnet
|
||||
elseif(ops.oppm or ops.o) then -- Follow OPPM
|
||||
git=true
|
||||
oppm=true
|
||||
elseif(ops.url) then
|
||||
url=ops.url
|
||||
else
|
||||
err("Cannot resolve repository url.")
|
||||
return
|
||||
end
|
||||
|
||||
if(oppm) then
|
||||
-- TODO: Fill the gap between oppm and cnpm
|
||||
print("Warning: Oppm repository may not be completely supported.")
|
||||
end
|
||||
|
||||
local realcfgurl=""
|
||||
|
||||
if(git) then
|
||||
realcfgurl=resolver(args[2],"mainfest.txt")
|
||||
else
|
||||
if(string.sub(url,-1)~="/") then
|
||||
url=url .. "/"
|
||||
end
|
||||
realcfgurl=url .. "mainfest.txt"
|
||||
end
|
||||
|
||||
local hand=network.request(realcfgurl)
|
||||
local res=""
|
||||
while true do
|
||||
local tmp=hand.read()
|
||||
if(tmp~=nil) then
|
||||
res=res .. tmp
|
||||
else break end
|
||||
end
|
||||
hand.close()
|
||||
|
||||
local fn=load(res,"Mainfest","t",{})
|
||||
local err,mft=pcall(fn)
|
||||
if(not err) then
|
||||
err("Failed to load mainfest.")
|
||||
return
|
||||
end
|
||||
|
||||
local function checkval(param,tp)
|
||||
if(tp==nil) then tp="string" end
|
||||
if(type(tp)~="table") then
|
||||
tp={tp}
|
||||
end
|
||||
for k,v in pairs(tp) do
|
||||
if(type(param)==v) then return param end
|
||||
end
|
||||
error("invalid param type")
|
||||
end
|
||||
|
||||
local pkg={}
|
||||
local ret=pcall(
|
||||
function()
|
||||
-- Copy info
|
||||
pkg.name=checkval(mft.name)
|
||||
pkg.info=checkval(mft.info,{"string","nil"})
|
||||
pkg.author=checkval(mft.author,{"string","nil"})
|
||||
pkg.version=checkval(mft.version)
|
||||
pkg.files=checkval(mft.files,"table")
|
||||
pkg.depends=checkval(mft.depends,{"table","string","nil"})
|
||||
pkg.precheck=checkval(mft.precheck,{"string","nil"})
|
||||
pkg.setup=checkval(mft.setup,{"string","nil"})
|
||||
pkg.uninst=checkval(mft.uninst,{"string","nil"})
|
||||
|
||||
-- Package setup check
|
||||
for k,v in pairs(pkg.files) do
|
||||
if(type(k)~="string" or type(v)~="string") then
|
||||
error("file list type exception.")
|
||||
end
|
||||
end
|
||||
|
||||
if(
|
||||
(pkg.precheck and pkg.files[pkg.precheck]==nil) or
|
||||
(pkg.setup and pkg.files[pkg.setup]==nil) or
|
||||
(pkg.uninst and pkg.files[pkg.uninst]==nil)
|
||||
) then
|
||||
error("special files not found in files list.")
|
||||
end
|
||||
end
|
||||
)
|
||||
|
||||
if(ret==false) then
|
||||
err("Invalid package mainfest.")
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue