DaikiKaminari

NoteBlockCheckUpdate

Jul 31st, 2020 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. local repoOwner = "DaikiKaminari"
  2. local repoName = "NoteBlock"
  3. local workingTree = "master"
  4. local downloadDir = "downloads/" .. repoName
  5. local repoLink = "https://github.com/" .. repoOwner .."/" .. repoName .. "/blob/dev"
  6. local versionFileName = "version.json"
  7.  
  8. if not fs.exists("github") then
  9.     error("[github] does not exist.")
  10. end
  11.  
  12. if not fs.exists("lib/objectJSON") or not fs.exists("lib/json") then
  13.     error("objectJSON or json does not exist.")
  14. end
  15. os.loadAPI("lib/objectJSON")
  16. objectJSON.init()
  17.  
  18. local function split(inputstr, sep)
  19.     if sep == nil then sep = "%s" end
  20.     local t={}
  21.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  22.             table.insert(t, str)
  23.     end
  24.     return t
  25. end
  26.  
  27. local function rename(path)
  28.     for _,f in pairs(fs.list(path)) do
  29.         if fs.isDir(f) then
  30.             rename(path .. "/" .. f)
  31.         else
  32.             local prog = split(f, ".")[1]
  33.             local ext = split(f, ".")[2]
  34.             if ext ~= nil then
  35.                 shell.run("mv " .. path .. "/" .. f .. " " .. path .. "/" .. prog)
  36.             end
  37.         end
  38.     end
  39. end
  40.  
  41. local function delete(where_to_delete, what_to_delete)
  42.     for _,f in pairs(fs.list(what_to_delete)) do
  43.         if fs.isDir(f) then
  44.             delete(where_to_delete .. "/" .. f, what_to_delete .. "/" .. f)
  45.         else
  46.             shell.run("rm " .. where_to_delete .. "/" .. f)
  47.         end
  48.     end
  49. end
  50.  
  51. local function copy(what_to_copy, where_to_copy)
  52.     for _,f in pairs(fs.list(what_to_copy)) do
  53.         if fs.isDir(f) then
  54.             copy(what_to_copy .. "/" .. f, where_to_copy .. "/" .. f)
  55.         else
  56.             if fs.exists(where_to_copy .. "/" .. f) then
  57.                 print("[" ..where_to_copy .. "/" .. f .. "] already exist.")
  58.             else
  59.                 shell.run("cp "  .. what_to_copy .. "/" .. f .. " ".. where_to_copy .. "/" .. f)
  60.             end
  61.         end
  62.     end
  63. end
  64.  
  65. local version = fs.exists("version") and objectJSON.decodeFromFile("version").version or -1
  66. --local version = objectJSON.decodeFromFile("version").version
  67. print("Actual version : " .. tostring(version))
  68. local newVersion
  69. while true do
  70.     newVersion = objectJSON.decodeHTTP("https://raw.githubusercontent.com/" .. repoOwner .. "/"  .. repoName .. "/"
  71.     .. workingTree .."/" .. versionFileName).version
  72.     -- wait for a new version
  73.     if not fs.exists("version") or newVersion ~= nil and version ~= newVersion then
  74.         print("New version : " .. tostring(newVersion))
  75.         -- delete old filed and then old download files
  76.         if fs.exists(downloadDir) then
  77.             delete(".", downloadDir)
  78.             shell.run("rm " .. downloadDir)
  79.         end
  80.         sleep(1)
  81.         shell.run("github " .. repoOwner .. " " .. repoName)
  82.         -- remove extensions
  83.         rename(downloadDir)
  84.         copy(downloadDir, ".")
  85.         return
  86.     end
  87.     sleep(1)
  88. end
  89.  
Add Comment
Please, Sign In to add comment