NexAdn

[ComputerCraft] CPM v0.2

Sep 4th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.39 KB | None | 0 0
  1. -- Visit http://github.com/NexAdn/cpm
  2.  
  3. -- Copyright (C) 2016 Adrian Schollmeyer
  4. --
  5. -- cpm.lua is part of CPM.
  6. --
  7. --  CPM is free software: you can redistribute it and/or modify
  8. --  it under the terms of the GNU General Public License as published by
  9. --  the Free Software Foundation, either version 3 of the License, or
  10. --  (at your option) any later version.
  11. --  This program is distributed in the hope that it will be useful,
  12. --  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. --  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. --  GNU General Public License and the GNU General Public License for more details.
  15. --  You should have received a copy of the GNU General Public License
  16. --  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  
  18. -- User specific configuration
  19. local tConfig = {
  20.     -- Server URL to connect to
  21.     aPackageServer = "https://raw.githubusercontent.com",
  22.     -- Path to main repository directory
  23.     sPackageDirectory = "/nexadn/cpmcontent/master"
  24. }
  25.  
  26. -- Static configuration (DO NOT CHANGE)
  27. local tStatic = {
  28.     sVersion = "0.2",
  29.    
  30.     sPackagesFile       = "/packages",
  31.     sDependenciesFile   = "/dependencies",
  32.     sVersionFile        = "/version",
  33.     sMainLua            = "/main.lua"
  34. }
  35.  
  36. local tMsg = {
  37.     usageMessage = "Syntax: cpm install [package]\n        cpm update\n        cpm cpmupdate",
  38.     wrongURL = "Wrong URL",
  39.     generalError = "Unkown error occured!",
  40.     updateSuccess = "Successfully updated CPM"
  41. }
  42.  
  43. local tData = {
  44.     aPackageList = {},
  45.     aVersionList = {}
  46. }
  47.  
  48. local tArgs = { ... }
  49.  
  50. function fetchArgs()
  51.     if tArgs[1] == "update" then
  52.            tData.aPackageList,tData.aVersionList = cpmUpdate()
  53.     elseif tArgs[1] == "install" then
  54.         if #tArgs < 2 then
  55.             print(tMsg.usageMessage)
  56.         end
  57.         if cpmInstall(tArgs[2]) ~= 1 then
  58.             print(tMsg.generalError)
  59.         end
  60.     elseif tArgs[1] == "upgrade" then
  61.         cpmUpgrade()
  62.     elseif tArgs[1] == "cpmupdate" then
  63.         local res = downloadFile("https://raw.githubusercontent.com/NexAdn/cpm/master/cpm.lua")
  64.         if res == nil then
  65.             print(tMsg.generalError)
  66.         else
  67.             local buf = nil
  68.             local file = fs.open("cpm", "w")
  69.             while true do
  70.                 buf = res.readLine()
  71.                 if buf == nil then
  72.                     break
  73.                 end
  74.                 file.writeLine(buf)
  75.             end
  76.             file.close()
  77.             print(tMsg.updateSuccess)
  78.         end
  79.     else
  80.         print(tMsg.usageMessage)
  81.     end
  82. end
  83.  
  84. function checkURL(sURL)
  85.     if http.checkURL(sURL) == false then
  86.         print(tMsg.wrongURL)
  87.         return false
  88.     end
  89.     return true
  90. end
  91.  
  92. function cpmUpdate()
  93.     local sListURL = tConfig.aPackageServer .. tConfig.sPackageDirectory .. tStatic.sPackagesFile
  94.     if checkURL(sListURL) then
  95.         local tResPkglist = http.get(sListURL)
  96.         if tResPkglist.getResponseCode() == 200 then
  97.             local tFileList = {}
  98.             local i = 0
  99.             local cont = true
  100.             while cont do
  101.                 i = i + 1
  102.                 tFileList[i] = tResPkglist.readLine()
  103.                 if tFileList[i] == nil then
  104.                     cont = false
  105.                 end
  106.             end
  107.             local tVersionList = {}
  108.             for k,v in pairs(tFileList) do
  109.                 local res = http.get(tConfig.aPackageServer .. tConfig.sPackageDirectory .. "/" .. v .. tStatic.sVersionFile)
  110.                 if res.getResponseCode() ~= 200 then
  111.                     print(tMsg.generalError)
  112.                     return nil,nil
  113.                 end
  114.                 tVersionList[k] = res.readLine()
  115.             end
  116.             return tFileList,tVersionList
  117.         else
  118.             print(tMsg.generalError)
  119.         end
  120.     end
  121. end
  122.  
  123. function cpmUpgrade()
  124.     return 0
  125. end
  126.  
  127. function cpmInstall(sPackage)
  128.     -- Recursive dependency installation
  129.     loadDependencies(sPackage)
  130.    
  131.     local res = downloadFile(tConfig.aPackageServer .. tConfig.sPackageDirectory .. "/" .. sPackage .. tStatic.sMainLua)
  132.    
  133.     if res == nil then
  134.         print(tMsg.generalError)
  135.         return -1
  136.     end
  137.    
  138.     local file = fs.open(sPackage, "w")
  139.    
  140.     local buf = nil
  141.    
  142.     while true do
  143.         buf = res.readLine()
  144.         if buf ~= nil then
  145.             file.writeLine(buf)
  146.         else
  147.             break
  148.         end
  149.     end
  150.    
  151.     file.close()
  152.    
  153.     return 1
  154. end
  155.  
  156. function downloadFile(sURL)
  157.     if checkURL(sURL) then
  158.         local res = http.get(sURL)
  159.        
  160.         print(res.getResponseCode())
  161.        
  162.         if res.getResponseCode() ~= 200 and res.getResponseCode() ~= 304 then
  163.             return nil
  164.         end
  165.        
  166.         return res
  167.     else
  168.         return nil
  169.     end
  170. end
  171.  
  172. function loadDependencies(sPackage)
  173.     local res = downloadFile( tConfig.aPackageServer .. tConfig.sPackageDirectory .. "/" .. sPackage .. tStatic.sDependenciesFile )
  174.    
  175.     local buf = nil
  176.    
  177.     while true do
  178.         buf = res.readLine()
  179.        
  180.         if buf == nil or buf == "NULL" then
  181.             break
  182.         end
  183.        
  184.         cpmInstall(buf)
  185.     end
  186. end
  187.  
  188. function main()
  189.     textutils.slowPrint("CPM v" .. tStatic.sVersion .. "\n")
  190.     --[[for k,v in pairs(tArgs) do
  191.         print(k .. " " ..v)
  192.     end]]
  193.     fetchArgs()
  194. end
  195.  
  196. main()
Advertisement
Add Comment
Please, Sign In to add comment