From a2340be96d3c2a71a0420a8f4816ddc03de5ebfd Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Sat, 17 Nov 2018 17:26:07 +0800 Subject: [PATCH] New script installer Replace update.lua with Grab --- grab.lua | 74 +++++++++++++++++++++++++++++++++++ update.lua | 110 ----------------------------------------------------- 2 files changed, 74 insertions(+), 110 deletions(-) create mode 100644 grab.lua delete mode 100644 update.lua diff --git a/grab.lua b/grab.lua new file mode 100644 index 0000000..4d62ac7 --- /dev/null +++ b/grab.lua @@ -0,0 +1,74 @@ +-- Grab : OpenComputerScripts Installer +-- Created By Kiritow +local component=require('component') +local shell=require('shell') +local args,options=shell.parse(...) +if( (#args<1 and (not options["setup"])) or options["help"]) then + print("Grab : OpenComputerScripts Installer") + print("Usage:\n\tgrab [options] [files]") + print("Options:" + .. "\n\t--cn Use mirror site in China. By default grab will download from Github." + .. "\n\t--help Display this help page." + .. "\n\t--setup Download some basic files for development" + ) + return +end +local function download(url) + if(component.internet==nil) then + error("This program requires an Internet card.") + end + local handle=component.internet.request(url) + while true do + local ret,err=handle.finishConnect() + if(ret==nil) then + return false,err + elseif(ret==true) then + break + end + end + local code=handle.response() + local result='' + while true do + local temp=handle.read() + if(temp==nil) then break end + result=result .. temp + end + handle.close() + return true,result,code +end +local UrlGenerator +if(not options["cn"]) then + UrlGenerator=function(RepoName,Branch,FileAddress) + return "https://raw.githubusercontent.com/" .. RepoName .. "/" .. Branch .. "/" .. FileAddress + end +else + UrlGenerator=function(RepoName,Branch,FileAddress) + return "http://kiritow.com:3000/" .. RepoName .. "/raw/" .. Branch .. "/" .. FileAddress + end +end +local files +if(options["setup"]) then + files={"LICENSE","checkarg.lua","libevent.lua","class.lua","util.lua"} +else + files=args +end +print('Downloading...') +for idx,filename in ipairs(files) do + io.write("[" .. idx .. "/" .. #files .. "] " .. filename) + local ok,err=pcall(function() + local flag,result,code=download(UrlGenerator("Kiritow/OpenComputerScripts","master",filename)) + if(not flag) then + print(" [Download Failed] " .. result) + elseif(code~=200) then + print(" [Download Failed] response code " .. code .. " is not 200.") + else + local f=io.open(filename,"w") + f:write(result) + f:close() + print(" [OK]") + end + end) + if(not ok) then + print(" [Error] " .. err) + end +end \ No newline at end of file diff --git a/update.lua b/update.lua deleted file mode 100644 index 39418c9..0000000 --- a/update.lua +++ /dev/null @@ -1,110 +0,0 @@ ----------------------------- Begin of From Downloader - -local component=require("component") - -local function doRealDownload(url) - if(component.internet==nil) then - error("The downloader requires an Internet card.") - end - - local handle=component.internet.request(url) - - while true do - local ret,err=handle.finishConnect() - if(ret==nil) then - return false,err - elseif(ret==true) then - break - end - --os.sleep(0.1) - end - - local response_code=handle.response() - - local ans="" - while true do - local tmp=handle.read() - if(tmp==nil) then break end - ans=ans .. tmp - end - handle.close() - - return true,ans,response_code -end - -function DownloadFromGitHub(RepoName,Branch,FileAddress) - local url="https://raw.githubusercontent.com/" .. RepoName .. "/" .. Branch .. "/" .. FileAddress - return doRealDownload(url) -end - -function DownloadFromOCS(FileAddress) - return DownloadFromGitHub("Kiritow/OpenComputerScripts","master",FileAddress) -end - -function WriteStringToFile(StringValue,FileName,IsAppend) - if(IsAppend==nil) then IsAppend=false end - local handle,err - if(IsAppend) then - handle,err=io.open(FileName,"a") - else - handle,err=io.open(FileName,"w") - end - if(handle==nil) then return false,err end - - handle:write(StringValue) - handle:close() - - return true,"Success" -end - ------------------------------ End of From Downloader -local shell=require("shell") -local args=shell.parse(...) -local argc=#args - -local file_lst={} - -if(argc<1) then -file_lst= -{ - "SignReader.lua", - "checkarg.lua", - "class.lua", - "downloader.lua", - "libevent.lua", - "queue.lua", - "util.lua", - "vector.lua", - "LICENSE" -} -else - for i=1,argc,1 do - table.insert(file_lst,args[i]) - end -end - -local cnt_all=0 -for k,v in pairs(file_lst) do - cnt_all=cnt_all+1 -end - -local cnt_now=1 - --- Download from the list -for k,v in pairs(file_lst) do - io.write("Updating (" .. cnt_now .. "/" .. cnt_all .. "): " .. v .. " ") - local flag,x,y=DownloadFromOCS(v) - if((not flag) or (y~=200) ) then - print("[Download Failed]") - -- Stop if download failed. - break - else - local ret=WriteStringToFile(x,v) - if(not ret) then - print("[Write Failed]") - else - print("[OK]") - end - cnt_now = cnt_now + 1 - end -end \ No newline at end of file