Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Argument documentation:
- -- Note, everything automatically gets added to the list of auto updating
- -- Include a -m hook anywhere in the arguments, preferrably on the end of the original argument. This hook will make sure it only downloads it and places it in said file/folder
- -- updater git <user/repo> <branch> <folder>
- -- Example: updater git EngineerCoding/Miscellaneous-Lua-Projects master /.MiscLuaProjects
- -- updater gitfile <user/repo/file> <branch> <file>
- -- Example: updater gitfile EngineerCoding/Miscellaneous-Lua-Projects/primes.lua master /primeGenerator
- -- updater pastebin <code> <file>
- -- Example: updater pastebin ee8xhTUK updater ( the updater is added by default :) )
- --[[ The .update file structure:
- [pastebin]
- code file
- [git]
- user/repo branch folder
- [gitfile]
- user/repo/file branch file
- --]]
- local tArgs = { ... }
- if not http then
- error( "HTTP must be enabled in the config!", 0 )
- end
- -- Util functions
- local function loadJSON( sJSON )
- if type( sJSON ) ~= "string" then
- error( "String expected, got " .. type( sJSON ), 2 )
- end
- sJSON = sJSON:gsub( "\"(%a+)\":%s*", "%1 = " )
- sJSON = sJSON:gsub( "%[", "{" )
- sJSON = sJSON:gsub( "%]", "}" )
- -- Load the table
- local func, err = loadstring( "return " .. sJSON, "JSON" )
- if func then
- local tJSON = func()
- if type( tJSON ) == "table" and not tJSON.message then
- return tJSON
- end
- end
- error( "sJSON wasn't a JSON file", 2 )
- end
- local function httpWrapper( sUrl )
- if type( sUrl ) ~= "string" then
- error( "String expected, got " .. type( sUrl ), 2 )
- end
- for i = 1, 3 do
- http.request( sUrl )
- while true do
- local e = { os.pullEvent() }
- if e[ 1 ] == "http_success" then
- return e[ 3 ], e[ 2 ]
- elseif e[ 1 ] == "http_failure" then
- break
- end
- end
- end
- end
- -- All objects
- local parseObjects = {
- git = {
- params = 4,
- pattern = "^git%s(.-)/(.-)%s(.-)%s(.-)$",
- pattern_file = "%1/%2 %3 %4",
- pattern_func = function( sUser, sProject, sBranch, sFolder )
- -- Get the file tree
- local httpHandle = httpWrapper( string.format( "https://api.github.com/repos/%s/%s/git/trees/%s?recursive=1", sUser, sProject, sBranch ) )
- if httpHandle then
- local sContent = httpHandle.readAll()
- httpHandle.close()
- local parsedJSON = loadJSON( sContent )
- if parsedJSON then
- -- Download the necessary files
- local files = {}
- for _, treeObject in next, parsedJSON.tree do
- if treeObject.type == "blob" then
- local file_httpHandle = httpWrapper( string.format( "https://raw.github.com/%s/%s/%s/%s", sUser, sProject, sBranch, treeObject.path:gsub( "[%s]", "%%20" ) ) )
- if file_httpHandle then
- files[ treeObject.path ] = file_httpHandle.readAll()
- file_httpHandle.close()
- else
- print( "Couldn't download all necessary files for the repo, no files changed!" )
- return
- end
- end
- end
- if fs.exists( sFolder ) then
- fs.delete( sFolder )
- end
- -- Create all directories
- for index, treeObject in next, parsedJSON.tree do
- if treeObject.type == "tree" then
- fs.makeDir( fs.combine( sFolder, treeObject.path ) )
- end
- end
- -- Change the files
- for filePath, fileContent in next, files do
- local fileHandle = fs.open( fs.combine( sFolder, filePath ), "w" )
- fileHandle.write( fileContent )
- fileHandle.close()
- end
- else
- print( "Couldn't retrieve file list" )
- end
- else
- print( "Couldn't retrieve file list" )
- end
- end,
- },
- gitfile = {
- params = 4,
- pattern = "^gitfile%s(.-)/(.-)/(.-)%s(.-)%s(.-)$",
- pattern_file = "%1/%2/%3 %4 %5",
- pattern_func = function( sUser, sProject, sPath, sBranch, sFile )
- -- Get the file tree
- local httpHandle = httpWrapper( string.format( "https://api.github.com/repos/%s/%s/git/trees/%s?recursive=1", sUser, sProject, sBranch ) )
- if httpHandle then
- local sJSON = httpHandle.readAll()
- httpHandle.close()
- local tJSON = loadJSON( sJSON )
- if tJSON then
- -- Check if the file is in the repository
- local isFile = false
- for _, treeObject in next, tJSON.tree do
- if treeObject.path == sPath then
- isFile = ( treeObject.type == "blob" )
- break
- end
- end
- if isFile then
- -- Download the file
- local file_httpHandle = httpWrapper( string.format( "https://raw.github.com/%s/%s/%s/%s", sUser, sProject, sBranch, sPath:gsub( "[%s]", "%%20" ) ) )
- if file_httpHandle then
- if fs.exists( sFile ) then
- fs.delete( sFile )
- end
- local fileHandle = fs.open( sFile , "w" )
- fileHandle.write( file_httpHandle.readAll() )
- file_httpHandle.close()
- fileHandle.close()
- else
- print( "Couldn't get file content" )
- end
- end
- end
- else
- print( "Couldn't retrieve file list" )
- end
- end
- },
- pastebin = {
- params = 3,
- pattern = "^pastebin%s(.-)%s(.-)$",
- pattern_file = "%1 %2",
- pattern_func = function( sCode, sFile )
- local httpHandle = httpWrapper( "http://pastebin.com/raw.php?i=" .. sCode )
- if httpHandle then
- local fileHandle = fs.open( sFile, "w" )
- fileHandle.write( httpHandle.readAll() )
- httpHandle.close()
- fileHandle.close()
- else
- print( "Couldn't retrieve file contents" )
- end
- end
- },
- }
- -- Parse the arguments
- local manual = false
- -- First check for hooks like -m
- local removeIndices = {}
- local removeIndices_mt = {
- __call = function()
- for _, index in next, removeIndices do
- table.remove( tArgs, index )
- end
- removeIndices = setmetatable( {}, getmetatable( removeIndices ) )
- end
- }
- setmetatable( removeIndices, removeIndices_mt )
- if #tArgs > 0 then
- for index, argument in next, tArgs do
- if argument:lower() == "-m" then
- manual = true
- table.insert( removeIndices, index )
- end
- end
- end
- -- Remove the indices which contain -m
- removeIndices()
- -- Master table which holds all modules
- local modules = setmetatable( {}, {
- __index = function( t, k )
- local raw = rawget( t, k )
- if not raw then
- local pointer = {}
- t[ k ] = pointer
- return pointer
- end
- return raw
- end
- })
- -- Check all parseObjects ( with the arguments from the command prompt )
- local function tryParse()
- if #tArgs > 0 then
- local cmdLine = ""
- local count = 0
- for index, arg in next, tArgs do
- count = count + 1
- cmdLine = cmdLine .. arg .. " "
- table.insert( removeIndices, index )
- for objectName, parseObject in next, parseObjects do
- if string.match( cmdLine, parseObject.pattern ) and count == parseObject.params then
- return objectName, parseObject, cmdLine:sub( 1, cmdLine:len() - 1 )
- end
- end
- end
- end
- end
- for objectName, parseObject, cmdLine in tryParse do
- if not manual then
- local sFileObject = string.gsub( cmdLine, parseObject.pattern, parseObject.pattern_file )
- table.insert( modules[ objectName ], sFileObject )
- else
- string.gsub( cmdLine, parseObject.pattern, parseObject.pattern_func )
- end
- removeIndices()
- end
- -- Get modules from the .update file
- if fs.exists( "/.update" ) then
- -- Read the file contents
- local fileLines = {}
- local fileHandle = fs.open( "/.update", "r" )
- for line in fileHandle.readLine do
- table.insert( fileLines, line )
- end
- fileHandle.close()
- -- Parse the file contents
- local currentModule = nil
- for _, sLine in next, fileLines do
- -- Try to get the module
- local skip = false
- if string.match( sLine, "%[.-%]" ) then
- currentModule = string.gsub( sLine, "%[(.-)%]", "%1" )
- skip = true
- end
- if currentModule and parseObjects[ currentModule ] and sLine ~= "" and not skip then
- table.insert( modules[ currentModule ], sLine )
- end
- end
- -- Delete the file, it's getting remade anyway
- fs.delete( "/.update" )
- end
- -- Write all modules to the .update file
- local fileHandle = fs.open( "/.update", "a" )
- for moduleName, tObjects in next, modules do
- fileHandle.writeLine( string.format( "[%s]", moduleName ) )
- for _, sObject in next, tObjects do
- fileHandle.writeLine( sObject )
- end
- end
- fileHandle.close()
- -- Finally, update all files
- for moduleName, tObjects in next, modules do
- for _, sObject in next, tObjects do
- local object = parseObjects[ moduleName ]
- string.gsub( string.format( "%s %s", moduleName, sObject ), object.pattern, object.pattern_func )
- end
- end
Add Comment
Please, Sign In to add comment