Advertisement
Oeed

Package Maker

Mar 17th, 2013
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.29 KB | None | 0 0
  1. sPackage = "local pkg = %@1 local function makeFile(_path, _content) local file = fs.open(_path, \"w\") _content = _content:gsub(\"\!@\"..\"#&\", \"%\\n\") _content = textutils.unserialize(_content) file.write(_content) file.close() end local function makeFolder(_path, _content) fs.makeDir(_path) for k,v in pairs(_content) do if type(v) == \"table\" then makeFolder(_path .. \"/\" .. k, v) else makeFile(_path .. \"/\" .. k, v) end end end local sDest = shell.resolve( \"%@2\" ) or \"/\" if sDest == \"root\" then sDest = \"/\" end local tPackage = pkg makeFolder(sDest, tPackage) print(\"Package Extracted to '\" .. sDest .. \"'!\")"
  2.  
  3. function addFile(_package, _path)
  4.     if fs.getName(_path) == ".DS_Store" then
  5.         return _package
  6.     end
  7.     local file, err = fs.open(_path, "r")
  8.     local content = file.readAll()
  9.     content = content:gsub("%\n", "\!@".."#&")
  10.     content = content:gsub("%%", "%%%%")
  11.     _package[fs.getName(_path)] = content
  12.     file.close()
  13.     print("Added file '".._path.."'")
  14.     return _package
  15. end
  16.  
  17. function addFolder(_package, _path)
  18.     if string.sub(_path,1,string.len("rom"))=="rom" or string.sub(_path,1,string.len("/rom"))=="/rom" then
  19.         print("Ignored 'rom' folder. (".._path..")")
  20.         return
  21.     end
  22.     _package = _package or {}
  23.     for _,f in ipairs(fs.list(_path)) do
  24.         local path = _path.."/"..f
  25.         if fs.isDir(path) then
  26.             _package[fs.getName(f)] = addFolder(_package[fs.getName(f)], path)
  27.         else
  28.             _package =  addFile(_package, path)
  29.         end
  30.     end
  31.     return _package
  32. end
  33.  
  34. local tArgs = { ... }
  35. if #tArgs < 2 then
  36.     print( "Usage: PkgMake <source> <destination>" )
  37.     return
  38. end
  39.  
  40. local sSource = shell.resolve( tArgs[1] )
  41. local sDest = shell.resolve( tArgs[2] )
  42.  
  43. if fs.isDir( sDest ) then
  44.     error("Destination must not be a folder.")
  45. end
  46.  
  47. if sSource == sDest then
  48.     error("Source can not be equal to destination.")
  49. end
  50.  
  51. if fs.exists( sSource ) and fs.isDir( sSource ) then
  52.     tPackage = {}
  53.     tPackage = addFolder(tPackage, sSource)
  54.     fPackage = fs.open(sDest,"w")
  55.  
  56.     sPackage = string.gsub(sPackage, "%%@2", fs.getName(sSource))
  57.     sPackage = string.gsub(sPackage, "%%@1", textutils.serialize(tPackage))
  58.     fPackage.write(sPackage)
  59.     fPackage.close()
  60.     print("Package Done! ('" .. sDest .. "')")
  61.     print("Type '" .. sDest .. "' to run it.")
  62. else
  63.     error("Source does not exist or is not a folder.")
  64. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement