Advertisement
Guest User

pkgmake

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