From f812ef204855bb7fa7d857eb8b3fc926c8a48963 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Wed, 7 Mar 2018 23:20:13 +0800 Subject: [PATCH] format code --- cnpm.lua | 227 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 116 insertions(+), 111 deletions(-) diff --git a/cnpm.lua b/cnpm.lua index 43e86ce..cf9dc4c 100644 --- a/cnpm.lua +++ b/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 ","Install package") cmd("cnpm add [-gbmkno,--url=] ", "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 \ No newline at end of file + 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