Advertisement
theinsekt

dah_installer

Sep 17th, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.25 KB | None | 0 0
  1. --theinsektAPIs/installerAPI
  2. --a program used to install/download computercraft programs
  3.  
  4. --incomplete erroneous example
  5. --os.loadAPI("dah_installer")
  6. --doInstallFile(shell, {}, "")
  7.  
  8.  
  9. --experimental
  10. --only tested a little
  11. --may stop working when testing new stuff
  12.  
  13.  
  14.  
  15. function loadLines(path)
  16.  if  not fs.exists(path) then
  17.   return nil
  18.  end
  19.  local h=fs.open(path, "r")
  20.  if h==nil then
  21.   return nil
  22.  end
  23.  local res={}
  24.  while(true) do
  25.   local line=h.readLine()
  26.   if line==nil then break end
  27.   res[#res+1]=line
  28.  end
  29.  h.close()
  30.  return res
  31. end
  32.  
  33.  
  34.  
  35. function getWords(line)
  36.  local words={}
  37.  --"%S+"
  38.  --"%a+"
  39.  for word in string.gmatch(line, "%S+") do
  40.   words[#words+1]=word
  41.  end
  42.  return words
  43. end
  44.  
  45.  
  46.  
  47. --do the commands and add to the installed
  48. --table if something was installed
  49. --installed[path]=code
  50. --should have the same code if it's the same file
  51. --that way the same install file can be referenced twice
  52. commands={
  53.  
  54.  
  55.  installfile=function(shellObj,installed,words)
  56.   --get parameters
  57.   local command=words[1]
  58.   local code=words[2]
  59.   local path="installfiles/"..code
  60.   --local path=words[3]
  61.   --download the install file, and mark it as installed
  62.   local res=pastebin2(shellObj,installed, command, code, path)
  63.   if res~=2 then --if res==1 or res==-1
  64.    return res
  65.   end
  66.   --do installs in install file
  67.   return doInstallFile(shellObj,installed,path)
  68.  end,
  69.  
  70.  
  71.  pastebin=function(shellObj,installed,words)
  72.   --get parameters
  73.   local command=words[1]
  74.   local code=words[2]
  75.   local path=words[3]
  76.   --download the file and mark it as installed
  77.   return pastebin2(shellObj,installed, command,code,path)
  78.  end,
  79.  
  80.  
  81.  ["comment"]=function(shellObj,installed,words)
  82.   return 0
  83.  end,
  84.  
  85.  
  86. }
  87.  
  88. --returns 0 if the line is empty or should be ignored
  89. --returns -2 if unrecognised command
  90. --returns 1 if already installed
  91. --returns 2 if installed
  92. --returns -1 if two different files have the same path
  93. --positive number are good, negative are bad
  94.  
  95. resCodeMeanings={
  96. [-2]="unrecognised command",
  97. [-1]="two different files have the same path",
  98. [0]="line was empty or ignored",
  99. [1]="skipped because already installed",
  100. [2]="performed installation",
  101. }
  102.  
  103. function getRescodeMeaning(resCode)
  104.  local res=resCodeMeanings[resCode]
  105.  if res==nil then
  106.   return "unrecognized resCode"
  107.  end
  108.  return res
  109. end
  110.  
  111.  
  112.  
  113. --returns 1 if already installed
  114. --returns 2 if installed
  115. --returns -1 if two different files have the same path
  116. --positive number are good, negative are bad
  117. function pastebin2(shellObj, installed, command,code,path)
  118.   --check parameters
  119.   if type(installed)~="table" then error("Error: the parameter called installed, should be a table.",2) end
  120.   if type(command)~="string" then error("Error: the parameter called command, should be a string",2) end
  121.   if type(code)~="string" then error("Error: the parameter called code, should be a string",2) end
  122.   if type(path)~="string" then error("Error: the parameter called path, should be a string",2) end
  123.   --check if already installed
  124.   if installed[path]~=nil then
  125.    if installed[path]==code then
  126.     --return have already installed code
  127.     return 1
  128.    else
  129.     --have already installed a different file
  130.     --with the same path
  131.     return -1
  132.    end
  133.   end
  134.   --download file (install)
  135.   pastebin(shellObj,path,code,true)
  136.   --update installed
  137.   installed[path]=code
  138.   --return did install code
  139.   return 2
  140. end
  141.  
  142.  
  143.  
  144. --returns 0 if the line is empty or should be ignored
  145. --returns -2 if unrecognised command
  146. --returns 1 if already installed
  147. --returns 2 if installed
  148. --returns -1 if two different files have the same path
  149. --positive number are good, negative are bad
  150. function doInstallLine(shellObj,installed,line)
  151.  --get the words on the line
  152.   local words=getWords(line)
  153.   --
  154.   return doInstallWords(shellObj,installed,words)
  155. end
  156.  
  157.  
  158.  
  159. --install using words from command line
  160. function doInstallWords(shellObj,installed,words)
  161.   --DEBUG
  162.   --for k,v in ipairs(words) do
  163.   -- print(k,": ",v)
  164.   --end
  165.   --if line has no commands return 0
  166.   if words==nil or #words==0 then
  167.    return 0
  168.   end
  169.   --get the command on the line
  170.   local command=words[1]
  171.   --get the command function
  172.   local f=commands[command]
  173.   --if that command function does not exist return -2
  174.   if f==nil then
  175.    return -2
  176.   end
  177.   --run the command function
  178.   return f(shellObj, installed, words)
  179. end
  180.  
  181.  
  182.  
  183. function doInstallFile(shellObj, installed, path)
  184.  --check arguments
  185.  if installed==nil then installed={} end
  186.  if type(path)~="string" then error("Error: path should be a string",2) end
  187.  if type(installed)~="table" then error("Error: installed should be a table or nil",2) end
  188.  --load lines from install file
  189.  local lines=loadLines(path)
  190.  --for each line
  191.  for lineNr,line in ipairs(lines) do
  192.   local resCode = doInstallLine(shellObj, installed, line)
  193.   --throw errors here depending on the if the resCode is an error
  194.   if resCode < 0 then
  195.    local resCodeString="resCode "..tostring(resCode).." ("..installerAPI.getRescodeMeaning(resCode)..")"
  196.    error("Error: "..resCodeString.." in file "..path.." on line "..tostring(lineNr))
  197.   end
  198.  end
  199.  return 2
  200. end
  201.  
  202.  
  203.  
  204. function pastebin(shellObj,path,code,overwrite)
  205.   if overwrite then
  206.    fs.delete(path)
  207.   end
  208.   shellObj.run("pastebin","get", code, path)
  209. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement